PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 4.0.8
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v4.0.8
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 2.3.5 2.3.6 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.16 2.4.17 2.4.18 2.4.19 2.4.2 2.4.20 2.4.21 All 88 releases
post-carousel / src / blocks / smart-post-parent / edit.js

edit.js in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 4.0.8, at src/blocks/smart-post-parent/edit.js

105 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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