| 1 |
import classnames from 'classnames/dedupe'; |
| 2 |
|
| 3 |
import { |
| 4 |
InnerBlocks, |
| 5 |
useBlockProps, |
| 6 |
useInnerBlocksProps, |
| 7 |
} from '@wordpress/block-editor'; |
| 8 |
import { useSelect } from '@wordpress/data'; |
| 9 |
import { useEffect } from '@wordpress/element'; |
| 10 |
import { applyFilters } from '@wordpress/hooks'; |
| 11 |
|
| 12 |
/** |
| 13 |
* Block Edit Class. |
| 14 |
* |
| 15 |
* @param props |
| 16 |
*/ |
| 17 |
export default function BlockEdit(props) { |
| 18 |
const { clientId, attributes, setAttributes, context } = props; |
| 19 |
const { slug, active, anchor } = attributes; |
| 20 |
const { 'ghostkit/tabActive': tabActive } = context; |
| 21 |
|
| 22 |
let { className = '' } = props; |
| 23 |
|
| 24 |
// Update active state. |
| 25 |
useEffect(() => { |
| 26 |
if (tabActive === slug && !active) { |
| 27 |
setAttributes({ active: true }); |
| 28 |
} else if (tabActive !== slug && active) { |
| 29 |
setAttributes({ active: false }); |
| 30 |
} |
| 31 |
}, [active, setAttributes, slug, tabActive]); |
| 32 |
|
| 33 |
// Update anchor. |
| 34 |
useEffect(() => { |
| 35 |
if (anchor !== `${slug}-content`) { |
| 36 |
setAttributes({ anchor: `${slug}-content` }); |
| 37 |
} |
| 38 |
}, [slug, anchor, setAttributes]); |
| 39 |
|
| 40 |
const hasChildBlocks = useSelect( |
| 41 |
(select) => { |
| 42 |
const blockEditor = select('core/block-editor'); |
| 43 |
|
| 44 |
return blockEditor |
| 45 |
? blockEditor.getBlockOrder(clientId).length > 0 |
| 46 |
: false; |
| 47 |
}, |
| 48 |
[clientId] |
| 49 |
); |
| 50 |
|
| 51 |
className = classnames( |
| 52 |
className, |
| 53 |
'ghostkit-tab', |
| 54 |
tabActive === slug && 'ghostkit-tab-active' |
| 55 |
); |
| 56 |
|
| 57 |
className = applyFilters('ghostkit.editor.className', className, props); |
| 58 |
|
| 59 |
const blockProps = useBlockProps({ |
| 60 |
className, |
| 61 |
tabIndex: 0, |
| 62 |
role: 'tabpanel', |
| 63 |
'aria-labelledby': slug, |
| 64 |
'data-tab': slug, |
| 65 |
}); |
| 66 |
|
| 67 |
const innerBlockProps = useInnerBlocksProps(blockProps, { |
| 68 |
renderAppender: hasChildBlocks |
| 69 |
? undefined |
| 70 |
: InnerBlocks.ButtonBlockAppender, |
| 71 |
templateLock: false, |
| 72 |
}); |
| 73 |
|
| 74 |
return <div {...innerBlockProps} />; |
| 75 |
} |
| 76 |
|