| 1 |
import classnames from 'classnames/dedupe'; |
| 2 |
|
| 3 |
import { |
| 4 |
InspectorControls, |
| 5 |
useBlockProps, |
| 6 |
useInnerBlocksProps, |
| 7 |
} from '@wordpress/block-editor'; |
| 8 |
import { createBlock } from '@wordpress/blocks'; |
| 9 |
import { |
| 10 |
Button, |
| 11 |
PanelBody, |
| 12 |
SelectControl, |
| 13 |
ToggleControl, |
| 14 |
} from '@wordpress/components'; |
| 15 |
import { useDispatch, useSelect } from '@wordpress/data'; |
| 16 |
import { useEffect } from '@wordpress/element'; |
| 17 |
import { applyFilters } from '@wordpress/hooks'; |
| 18 |
import { __ } from '@wordpress/i18n'; |
| 19 |
|
| 20 |
const accordionItemBlockName = 'ghostkit/accordion-item'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Block Edit Class. |
| 24 |
* |
| 25 |
* @param props |
| 26 |
*/ |
| 27 |
export default function BlockEdit(props) { |
| 28 |
const { attributes, setAttributes, clientId } = props; |
| 29 |
|
| 30 |
let { className = '' } = props; |
| 31 |
|
| 32 |
const { itemsCount, collapseOne, collapseTitleTag } = attributes; |
| 33 |
|
| 34 |
const { isSelectedBlockInRoot, count } = useSelect( |
| 35 |
(select) => { |
| 36 |
const { isBlockSelected, hasSelectedInnerBlock, getBlockCount } = |
| 37 |
select('core/block-editor'); |
| 38 |
|
| 39 |
return { |
| 40 |
isSelectedBlockInRoot: |
| 41 |
isBlockSelected(clientId) || |
| 42 |
hasSelectedInnerBlock(clientId, true), |
| 43 |
count: getBlockCount(clientId), |
| 44 |
}; |
| 45 |
}, |
| 46 |
[clientId] |
| 47 |
); |
| 48 |
|
| 49 |
useEffect(() => { |
| 50 |
if (count !== itemsCount) { |
| 51 |
setAttributes({ |
| 52 |
itemsCount: count, |
| 53 |
}); |
| 54 |
} |
| 55 |
}, [count, itemsCount, setAttributes]); |
| 56 |
|
| 57 |
const { insertBlock } = useDispatch('core/block-editor'); |
| 58 |
|
| 59 |
function insertAccordionItem() { |
| 60 |
insertBlock(createBlock(accordionItemBlockName), undefined, clientId); |
| 61 |
} |
| 62 |
|
| 63 |
className = classnames(className, 'ghostkit-accordion'); |
| 64 |
|
| 65 |
className = applyFilters('ghostkit.editor.className', className, props); |
| 66 |
|
| 67 |
const blockProps = useBlockProps(); |
| 68 |
|
| 69 |
const innerBlocksProps = useInnerBlocksProps( |
| 70 |
{ |
| 71 |
className, |
| 72 |
}, |
| 73 |
{ |
| 74 |
allowedBlocks: [accordionItemBlockName], |
| 75 |
template: [[accordionItemBlockName], [accordionItemBlockName]], |
| 76 |
} |
| 77 |
); |
| 78 |
|
| 79 |
return ( |
| 80 |
<div {...blockProps}> |
| 81 |
<InspectorControls> |
| 82 |
<PanelBody> |
| 83 |
<ToggleControl |
| 84 |
label={__('Collapse one item only', 'ghostkit')} |
| 85 |
checked={!!collapseOne} |
| 86 |
onChange={(val) => setAttributes({ collapseOne: val })} |
| 87 |
/> |
| 88 |
<SelectControl |
| 89 |
label={__('Collapse Title HTML Element', 'ghostkit')} |
| 90 |
value={collapseTitleTag} |
| 91 |
options={[ |
| 92 |
{ |
| 93 |
value: 'div', |
| 94 |
label: __('Default (<div>)', 'ghostkit'), |
| 95 |
}, |
| 96 |
{ |
| 97 |
value: 'h1', |
| 98 |
label: __('<h1>', 'ghostkit'), |
| 99 |
}, |
| 100 |
{ |
| 101 |
value: 'h2', |
| 102 |
label: __('<h2>', 'ghostkit'), |
| 103 |
}, |
| 104 |
{ |
| 105 |
value: 'h3', |
| 106 |
label: __('<h3>', 'ghostkit'), |
| 107 |
}, |
| 108 |
{ |
| 109 |
value: 'h4', |
| 110 |
label: __('<h4>', 'ghostkit'), |
| 111 |
}, |
| 112 |
{ |
| 113 |
value: 'h5', |
| 114 |
label: __('<h5>', 'ghostkit'), |
| 115 |
}, |
| 116 |
{ |
| 117 |
value: 'h6', |
| 118 |
label: __('<h6>', 'ghostkit'), |
| 119 |
}, |
| 120 |
]} |
| 121 |
onChange={(value) => |
| 122 |
setAttributes({ collapseTitleTag: value }) |
| 123 |
} |
| 124 |
/> |
| 125 |
</PanelBody> |
| 126 |
</InspectorControls> |
| 127 |
<div {...innerBlocksProps} /> |
| 128 |
{isSelectedBlockInRoot ? ( |
| 129 |
<div className="ghostkit-accordion-add-item"> |
| 130 |
<Button |
| 131 |
isSecondary |
| 132 |
icon={ |
| 133 |
<svg |
| 134 |
xmlns="http://www.w3.org/2000/svg" |
| 135 |
viewBox="0 0 24 24" |
| 136 |
width="24" |
| 137 |
height="24" |
| 138 |
role="img" |
| 139 |
focusable="false" |
| 140 |
> |
| 141 |
<path d="M18 11.2h-5.2V6h-1.6v5.2H6v1.6h5.2V18h1.6v-5.2H18z" /> |
| 142 |
</svg> |
| 143 |
} |
| 144 |
onClick={() => { |
| 145 |
insertAccordionItem(); |
| 146 |
}} |
| 147 |
> |
| 148 |
{__('Add Accordion Item', 'ghostkit')} |
| 149 |
</Button> |
| 150 |
</div> |
| 151 |
) : null} |
| 152 |
</div> |
| 153 |
); |
| 154 |
} |
| 155 |
|