| 1 |
import { blocksAttributes } from '@Attributes/getBlocksDefaultAttributes'; |
| 2 |
import { select } from '@wordpress/data'; |
| 3 |
import { Button, Tooltip, Dashicon } from '@wordpress/components'; |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { useState } from '@wordpress/element'; |
| 6 |
|
| 7 |
const UAGReset = ( props ) => { |
| 8 |
const { onReset, attributeNames, setAttributes } = props; |
| 9 |
|
| 10 |
const [ refreshPresets, toggleRefreshPresets ] = useState( false ); |
| 11 |
|
| 12 |
const { getSelectedBlock } = select( 'core/block-editor' ); |
| 13 |
|
| 14 |
const getBlockResetValue = () => { |
| 15 |
const selectedBlockName = getSelectedBlock()?.name.split( '/' ).pop(); |
| 16 |
let defaultValues = false; |
| 17 |
|
| 18 |
if ( |
| 19 |
attributeNames && |
| 20 |
'undefined' !== typeof blocksAttributes[ selectedBlockName ] |
| 21 |
) { |
| 22 |
attributeNames.map( ( attributeName ) => { |
| 23 |
if ( attributeName ) { |
| 24 |
const blockDefaultAttributeValue = |
| 25 |
'undefined' !== |
| 26 |
typeof blocksAttributes[ selectedBlockName ][ |
| 27 |
attributeName |
| 28 |
]?.default |
| 29 |
? blocksAttributes[ selectedBlockName ][ |
| 30 |
attributeName |
| 31 |
]?.default |
| 32 |
: ''; |
| 33 |
defaultValues = { |
| 34 |
...defaultValues, |
| 35 |
[ attributeName ]: blockDefaultAttributeValue, |
| 36 |
}; |
| 37 |
} |
| 38 |
|
| 39 |
return attributeName; |
| 40 |
} ); |
| 41 |
} |
| 42 |
|
| 43 |
return defaultValues; |
| 44 |
}; |
| 45 |
|
| 46 |
const getResetState = () => { |
| 47 |
const defaultValues = getBlockResetValue(); |
| 48 |
const selectedBlockAttributes = getSelectedBlock()?.attributes; |
| 49 |
let resetDisableState = true; |
| 50 |
|
| 51 |
attributeNames.map( ( attributeName ) => { |
| 52 |
if ( |
| 53 |
selectedBlockAttributes?.[ attributeName ] && |
| 54 |
selectedBlockAttributes?.[ attributeName ] !== |
| 55 |
defaultValues?.[ attributeName ] |
| 56 |
) { |
| 57 |
resetDisableState = false; |
| 58 |
} |
| 59 |
return attributeName; |
| 60 |
} ); |
| 61 |
|
| 62 |
return resetDisableState; |
| 63 |
}; |
| 64 |
|
| 65 |
const resetDisableState = getResetState(); |
| 66 |
|
| 67 |
const resetHandler = () => { |
| 68 |
const defaultValues = getBlockResetValue(); |
| 69 |
|
| 70 |
if ( attributeNames ) { |
| 71 |
attributeNames.map( ( attributeName ) => { |
| 72 |
if ( attributeName ) { |
| 73 |
if ( setAttributes ) { |
| 74 |
setAttributes( { |
| 75 |
[ attributeName ]: defaultValues?.[ attributeName ], |
| 76 |
} ); |
| 77 |
} |
| 78 |
} |
| 79 |
toggleRefreshPresets( ! refreshPresets ); |
| 80 |
return attributeName; |
| 81 |
} ); |
| 82 |
} |
| 83 |
|
| 84 |
if ( onReset ) { |
| 85 |
onReset( defaultValues ); |
| 86 |
} |
| 87 |
}; |
| 88 |
|
| 89 |
return ( |
| 90 |
<Tooltip |
| 91 |
text={ __( 'Reset', 'ultimate-addons-for-gutenberg' ) } |
| 92 |
key={ 'reset' } |
| 93 |
> |
| 94 |
<Button |
| 95 |
className="uagb-reset" |
| 96 |
isSecondary |
| 97 |
isSmall |
| 98 |
onClick={ ( e ) => { |
| 99 |
e.preventDefault(); |
| 100 |
resetHandler(); |
| 101 |
} } |
| 102 |
disabled={ resetDisableState } |
| 103 |
> |
| 104 |
<Dashicon icon="image-rotate" /> |
| 105 |
</Button> |
| 106 |
</Tooltip> |
| 107 |
); |
| 108 |
}; |
| 109 |
|
| 110 |
export default UAGReset; |
| 111 |
|