| 1 |
import classnames from 'classnames/dedupe'; |
| 2 |
import { throttle } from 'throttle-debounce'; |
| 3 |
|
| 4 |
import { |
| 5 |
BlockAlignmentToolbar, |
| 6 |
BlockControls, |
| 7 |
InspectorControls, |
| 8 |
useBlockProps, |
| 9 |
useInnerBlocksProps, |
| 10 |
} from '@wordpress/block-editor'; |
| 11 |
import { BaseControl, PanelBody } from '@wordpress/components'; |
| 12 |
import { useDispatch, useSelect } from '@wordpress/data'; |
| 13 |
import { useEffect } from '@wordpress/element'; |
| 14 |
import { applyFilters } from '@wordpress/hooks'; |
| 15 |
import { __ } from '@wordpress/i18n'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Parse all button blocks. |
| 19 |
* |
| 20 |
* @param {Object} submitData submit block data. |
| 21 |
* |
| 22 |
* @return {Array} - fields list. |
| 23 |
*/ |
| 24 |
function getAllButtonBlocks(submitData) { |
| 25 |
let result = []; |
| 26 |
|
| 27 |
if (submitData.innerBlocks && submitData.innerBlocks.length) { |
| 28 |
submitData.innerBlocks.forEach((block) => { |
| 29 |
// field data. |
| 30 |
if (block.name && block.name === 'ghostkit/button-single') { |
| 31 |
result.push(block); |
| 32 |
} |
| 33 |
|
| 34 |
// inner blocks. |
| 35 |
if (block.innerBlocks && block.innerBlocks.length) { |
| 36 |
result = [...result, ...getAllButtonBlocks(block)]; |
| 37 |
} |
| 38 |
}); |
| 39 |
} |
| 40 |
|
| 41 |
return result; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Block Edit Class. |
| 46 |
* |
| 47 |
* @param props |
| 48 |
*/ |
| 49 |
export default function BlockEdit(props) { |
| 50 |
const { attributes, setAttributes, clientId } = props; |
| 51 |
|
| 52 |
let { className = '' } = props; |
| 53 |
|
| 54 |
const { align } = attributes; |
| 55 |
|
| 56 |
const { updateBlockAttributes } = useDispatch('core/block-editor'); |
| 57 |
|
| 58 |
const { blockData } = useSelect( |
| 59 |
(select) => { |
| 60 |
const { getBlock } = select('core/block-editor'); |
| 61 |
|
| 62 |
return { |
| 63 |
blockData: getBlock(clientId), |
| 64 |
}; |
| 65 |
}, |
| 66 |
[clientId] |
| 67 |
); |
| 68 |
|
| 69 |
function changeButtonTagName() { |
| 70 |
const allButtonBlocks = getAllButtonBlocks(blockData) || []; |
| 71 |
|
| 72 |
// Generate slugs for new fields. |
| 73 |
allButtonBlocks.forEach((data) => { |
| 74 |
if ( |
| 75 |
!data.attributes.tagName || |
| 76 |
data.attributes.tagName !== 'button' |
| 77 |
) { |
| 78 |
updateBlockAttributes(data.clientId, { |
| 79 |
tagName: 'button', |
| 80 |
}); |
| 81 |
} |
| 82 |
}); |
| 83 |
} |
| 84 |
|
| 85 |
const maybeChangeButtonTagName = throttle( |
| 86 |
200, |
| 87 |
changeButtonTagName.bind(this) |
| 88 |
); |
| 89 |
|
| 90 |
// Mount and update. |
| 91 |
useEffect(() => { |
| 92 |
maybeChangeButtonTagName(); |
| 93 |
}); |
| 94 |
|
| 95 |
className = classnames( |
| 96 |
'ghostkit-form-submit-button', |
| 97 |
align && align !== 'none' |
| 98 |
? `ghostkit-form-submit-button-align-${align}` |
| 99 |
: false, |
| 100 |
className |
| 101 |
); |
| 102 |
className = applyFilters('ghostkit.editor.className', className, props); |
| 103 |
|
| 104 |
const blockProps = useBlockProps({ className }); |
| 105 |
const innerBlockProps = useInnerBlocksProps(blockProps, { |
| 106 |
template: [ |
| 107 |
[ |
| 108 |
'ghostkit/button-single', |
| 109 |
{ |
| 110 |
text: __('Submit', 'ghostkit'), |
| 111 |
tagName: 'button', |
| 112 |
focusOutlineWeight: 2, |
| 113 |
}, |
| 114 |
], |
| 115 |
], |
| 116 |
allowedBlocks: ['ghostkit/button-single'], |
| 117 |
templateLock: 'all', |
| 118 |
}); |
| 119 |
|
| 120 |
return ( |
| 121 |
<> |
| 122 |
<BlockControls> |
| 123 |
<BlockAlignmentToolbar |
| 124 |
value={align} |
| 125 |
onChange={(value) => setAttributes({ align: value })} |
| 126 |
controls={['left', 'center', 'right']} |
| 127 |
/> |
| 128 |
</BlockControls> |
| 129 |
<InspectorControls> |
| 130 |
<PanelBody> |
| 131 |
<BaseControl |
| 132 |
id={__('Align', 'ghostkit')} |
| 133 |
label={__('Align', 'ghostkit')} |
| 134 |
__nextHasNoMarginBottom |
| 135 |
> |
| 136 |
<div> |
| 137 |
<BlockAlignmentToolbar |
| 138 |
value={align} |
| 139 |
onChange={(value) => |
| 140 |
setAttributes({ align: value }) |
| 141 |
} |
| 142 |
controls={['left', 'center', 'right']} |
| 143 |
isCollapsed={false} |
| 144 |
/> |
| 145 |
</div> |
| 146 |
</BaseControl> |
| 147 |
</PanelBody> |
| 148 |
</InspectorControls> |
| 149 |
<div {...innerBlockProps} /> |
| 150 |
</> |
| 151 |
); |
| 152 |
} |
| 153 |
|