| 1 |
import classnames from 'classnames/dedupe'; |
| 2 |
|
| 3 |
import { |
| 4 |
BlockAlignmentToolbar, |
| 5 |
BlockControls, |
| 6 |
InspectorControls, |
| 7 |
useBlockProps, |
| 8 |
useInnerBlocksProps, |
| 9 |
} from '@wordpress/block-editor'; |
| 10 |
import { createBlock } from '@wordpress/blocks'; |
| 11 |
import { BaseControl, Button, PanelBody, Tooltip } 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 |
import GapSettings from '../../components/gap-settings'; |
| 18 |
|
| 19 |
const buttonBlockName = 'ghostkit/button-single'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Block Edit Class. |
| 23 |
* |
| 24 |
* @param props |
| 25 |
*/ |
| 26 |
export default function BlockEdit(props) { |
| 27 |
const { attributes, setAttributes, clientId } = props; |
| 28 |
|
| 29 |
let { className = '' } = props; |
| 30 |
|
| 31 |
const { align, gap, gapCustom, gapVerticalCustom, count } = attributes; |
| 32 |
|
| 33 |
const { block, isSelectedBlockInRoot } = useSelect((select) => { |
| 34 |
const { getBlock, isBlockSelected, hasSelectedInnerBlock } = |
| 35 |
select('core/block-editor'); |
| 36 |
|
| 37 |
return { |
| 38 |
block: getBlock(clientId), |
| 39 |
isSelectedBlockInRoot: |
| 40 |
isBlockSelected(clientId) || |
| 41 |
hasSelectedInnerBlock(clientId, true), |
| 42 |
}; |
| 43 |
// eslint-disable-next-line react-hooks/exhaustive-deps |
| 44 |
}, []); |
| 45 |
|
| 46 |
const { insertBlock } = useDispatch('core/block-editor'); |
| 47 |
|
| 48 |
useEffect(() => { |
| 49 |
if (block && block.innerBlocks && count !== block.innerBlocks.length) { |
| 50 |
setAttributes({ |
| 51 |
count: block.innerBlocks.length, |
| 52 |
}); |
| 53 |
} |
| 54 |
}, [block, block?.innerBlocks?.length, count, setAttributes]); |
| 55 |
|
| 56 |
/** |
| 57 |
* Insert a single button. |
| 58 |
*/ |
| 59 |
function insertButtonSingle() { |
| 60 |
insertBlock(createBlock(buttonBlockName), undefined, clientId); |
| 61 |
} |
| 62 |
|
| 63 |
className = classnames( |
| 64 |
'ghostkit-button-wrapper', |
| 65 |
gap ? `ghostkit-button-wrapper-gap-${gap}` : false, |
| 66 |
align && align !== 'none' |
| 67 |
? `ghostkit-button-wrapper-align-${align}` |
| 68 |
: false, |
| 69 |
className |
| 70 |
); |
| 71 |
|
| 72 |
className = applyFilters('ghostkit.editor.className', className, props); |
| 73 |
|
| 74 |
const blockProps = useBlockProps({ |
| 75 |
className, |
| 76 |
}); |
| 77 |
|
| 78 |
const { children, ...innerBlocksProps } = useInnerBlocksProps( |
| 79 |
{ |
| 80 |
className: 'ghostkit-button-wrapper-inner', |
| 81 |
}, |
| 82 |
{ |
| 83 |
allowedBlocks: [buttonBlockName], |
| 84 |
template: [[buttonBlockName]], |
| 85 |
orientation: 'horizontal', |
| 86 |
directInsert: true, |
| 87 |
templateInsertUpdatesSelection: true, |
| 88 |
renderAppender: isSelectedBlockInRoot |
| 89 |
? () => ( |
| 90 |
<Tooltip text={__('Add Button', 'ghostkit')}> |
| 91 |
<Button |
| 92 |
className="block-list-appender__toggle block-editor-button-block-appender" |
| 93 |
icon={ |
| 94 |
<svg |
| 95 |
xmlns="http://www.w3.org/2000/svg" |
| 96 |
viewBox="0 0 24 24" |
| 97 |
width="24" |
| 98 |
height="24" |
| 99 |
aria-hidden="true" |
| 100 |
focusable="false" |
| 101 |
> |
| 102 |
<path d="M18 11.2h-5.2V6h-1.6v5.2H6v1.6h5.2V18h1.6v-5.2H18z" /> |
| 103 |
</svg> |
| 104 |
} |
| 105 |
onClick={() => { |
| 106 |
insertButtonSingle(); |
| 107 |
}} |
| 108 |
/> |
| 109 |
</Tooltip> |
| 110 |
) |
| 111 |
: undefined, |
| 112 |
} |
| 113 |
); |
| 114 |
|
| 115 |
return ( |
| 116 |
<div {...blockProps}> |
| 117 |
<BlockControls> |
| 118 |
<BlockAlignmentToolbar |
| 119 |
value={align} |
| 120 |
onChange={(value) => setAttributes({ align: value })} |
| 121 |
controls={['left', 'center', 'right']} |
| 122 |
/> |
| 123 |
</BlockControls> |
| 124 |
<InspectorControls> |
| 125 |
<PanelBody> |
| 126 |
<GapSettings |
| 127 |
gap={gap} |
| 128 |
gapCustom={gapCustom} |
| 129 |
gapVerticalCustom={gapVerticalCustom} |
| 130 |
onChange={(data) => { |
| 131 |
setAttributes(data); |
| 132 |
}} |
| 133 |
allowVerticalGap |
| 134 |
/> |
| 135 |
<BaseControl |
| 136 |
id={__('Align', 'ghostkit')} |
| 137 |
label={__('Align', 'ghostkit')} |
| 138 |
__nextHasNoMarginBottom |
| 139 |
> |
| 140 |
<div> |
| 141 |
<BlockAlignmentToolbar |
| 142 |
value={align} |
| 143 |
onChange={(value) => |
| 144 |
setAttributes({ align: value }) |
| 145 |
} |
| 146 |
controls={['left', 'center', 'right']} |
| 147 |
isCollapsed={false} |
| 148 |
/> |
| 149 |
</div> |
| 150 |
</BaseControl> |
| 151 |
</PanelBody> |
| 152 |
</InspectorControls> |
| 153 |
<div {...innerBlocksProps}>{children}</div> |
| 154 |
</div> |
| 155 |
); |
| 156 |
} |
| 157 |
|