| 1 |
import { InnerBlocks, useBlockProps } from "@wordpress/block-editor"; |
| 2 |
import { useDispatch, useSelect } from "@wordpress/data"; |
| 3 |
import { useEffect, useMemo } from "@wordpress/element"; |
| 4 |
|
| 5 |
const blockNameList = [ |
| 6 |
"post-carousel", |
| 7 |
"post-carousel-two", |
| 8 |
"post-slider", |
| 9 |
"post-slider-two", |
| 10 |
"news-ticker", |
| 11 |
"post-grid-one", |
| 12 |
"post-grid-two", |
| 13 |
"post-grid-three", |
| 14 |
"post-grid-four", |
| 15 |
"post-grid-five", |
| 16 |
"post-grid-six", |
| 17 |
"post-list-one", |
| 18 |
"post-list-two", |
| 19 |
"post-list-three", |
| 20 |
"post-timeline-one", |
| 21 |
"post-timeline-two", |
| 22 |
"post-timeline-three", |
| 23 |
]; |
| 24 |
|
| 25 |
const ALLOWED_BLOCKS = [ |
| 26 |
"sp-smart-post-show/post-carousel", |
| 27 |
"sp-smart-post-show/post-carousel-two", |
| 28 |
"sp-smart-post-show/post-slider", |
| 29 |
"sp-smart-post-show/post-slider-two", |
| 30 |
"sp-smart-post-show/news-ticker", |
| 31 |
"sp-smart-post-show/post-grid-one", |
| 32 |
"sp-smart-post-show/post-grid-two", |
| 33 |
"sp-smart-post-show/post-grid-three", |
| 34 |
"sp-smart-post-show/post-grid-four", |
| 35 |
"sp-smart-post-show/post-grid-five", |
| 36 |
"sp-smart-post-show/post-grid-six", |
| 37 |
"sp-smart-post-show/post-list-one", |
| 38 |
"sp-smart-post-show/post-list-two", |
| 39 |
"sp-smart-post-show/post-list-three", |
| 40 |
"sp-smart-post-show/post-timeline-one", |
| 41 |
"sp-smart-post-show/post-timeline-two", |
| 42 |
"sp-smart-post-show/post-timeline-three", |
| 43 |
]; |
| 44 |
|
| 45 |
const Edit = ({ clientId }) => { |
| 46 |
const blockProps = useBlockProps(); |
| 47 |
const { updateBlockAttributes } = useDispatch("core/block-editor"); |
| 48 |
|
| 49 |
// All child blocks |
| 50 |
const innerBlocks = useSelect((select) => select("core/block-editor").getBlocks(clientId), [clientId]); |
| 51 |
|
| 52 |
// Find main post block |
| 53 |
const findBlock = useMemo( |
| 54 |
() => innerBlocks.find((block) => blockNameList.includes(block.name.replace("sp-smart-post-show/", ""))), |
| 55 |
[innerBlocks] |
| 56 |
); |
| 57 |
|
| 58 |
// Find live-filter block |
| 59 |
const filterBlock = useMemo( |
| 60 |
() => innerBlocks.find((block) => block.name === "sp-smart-post-show/live-filter"), |
| 61 |
[innerBlocks] |
| 62 |
); |
| 63 |
|
| 64 |
// Get inner blocks of live-filter |
| 65 |
const filterInnerBlocks = useSelect( |
| 66 |
(select) => (filterBlock?.clientId ? select("core/block-editor").getBlocks(filterBlock.clientId) : []), |
| 67 |
[filterBlock?.clientId] |
| 68 |
); |
| 69 |
|
| 70 |
// Sync postType between blocks |
| 71 |
useEffect(() => { |
| 72 |
if (!findBlock || !filterBlock) return; |
| 73 |
|
| 74 |
const multiplePostType = findBlock.attributes?.multiplePostType; |
| 75 |
const postQuery = findBlock.attributes?.postQuery; |
| 76 |
|
| 77 |
// Update live-filter block only if value is different |
| 78 |
updateBlockAttributes(filterBlock.clientId, { |
| 79 |
multiplePostType, |
| 80 |
postQuery, |
| 81 |
}); |
| 82 |
|
| 83 |
// Update all child blocks of live-filter if value differs |
| 84 |
filterInnerBlocks.forEach((block) => { |
| 85 |
updateBlockAttributes(block.clientId, { |
| 86 |
multiplePostType, |
| 87 |
postQuery, |
| 88 |
}); |
| 89 |
}); |
| 90 |
}, [ |
| 91 |
findBlock?.attributes?.multiplePostType, |
| 92 |
filterBlock?.clientId, |
| 93 |
filterInnerBlocks, |
| 94 |
findBlock?.attributes?.postQuery, |
| 95 |
]); |
| 96 |
|
| 97 |
return ( |
| 98 |
<div {...blockProps}> |
| 99 |
<InnerBlocks allowedBlocks={[findBlock?.name]} renderAppender={() => null} templateLock={ false } /> |
| 100 |
</div> |
| 101 |
); |
| 102 |
}; |
| 103 |
|
| 104 |
export default Edit; |
| 105 |
|