PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.12.7
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.12.7
2.12.7 2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 All 97 releases
sureforms / modules / quick-action-sidebar / components / blocks.js

blocks.js in SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz 2.12.7, at modules/quick-action-sidebar/components/blocks.js

89 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Creates sidebar blocks.
3 */
4 import { useSelect } from '@wordpress/data';
5 import { createBlock, getBlockTypes } from '@wordpress/blocks';
6 import DraggableBlock from './draggable-block';
7 import DragAndDropComponent from './move-up-down';
8
9 // Stable reference so `useSelect` doesn't return a fresh empty array on every
10 // call when no allowed blocks are resolved, which would trigger needless re-renders.
11 const EMPTY_ALLOWED_BLOCKS = [];
12
13 const Blocks = ( {
14 defaultAllowedQuickSidebarBlocks,
15 updateDefaultAllowedQuickSidebarBlocks,
16 saveOptionToDatabase,
17 enableRearrange,
18 } ) => {
19 const blocks = getBlockTypes();
20 const {
21 blockInsertionPoint,
22 getBlockRootClientId,
23 getSelectedBlockAllowedBlocks,
24 getSelectedBlockClientId,
25 } = useSelect( ( select ) => {
26 const blockEditor = select( 'core/block-editor' );
27 const { index } = blockEditor.getBlockInsertionPoint();
28 const clientId = blockEditor.getSelectedBlockClientId();
29 const rootClientId = blockEditor.getBlockRootClientId( clientId );
30 const allowedBlocks = blockEditor.getAllowedBlocks( clientId );
31 return {
32 blockInsertionPoint: index,
33 getBlockRootClientId: rootClientId,
34 getSelectedBlockClientId: clientId,
35 getSelectedBlockAllowedBlocks: allowedBlocks || EMPTY_ALLOWED_BLOCKS,
36 };
37 }, [] );
38 const srfmBlocks = blocks.filter( ( block ) => {
39 return defaultAllowedQuickSidebarBlocks.includes( block.name );
40 } );
41 const create = ( name ) => {
42 return createBlock( name );
43 };
44
45 // Loop through each object and add id
46 srfmBlocks.forEach( ( item, index ) => {
47 item.id = `${ index + 1 }`;
48 } );
49
50 const sortedY = defaultAllowedQuickSidebarBlocks
51 .filter( ( item ) => item !== undefined && item !== null )
52 .map( ( item ) => srfmBlocks.find( ( { name } ) => name === item ) )
53 .filter( ( item ) => item !== undefined ); // Remove undefined objects
54
55 return (
56 <>
57 { ! enableRearrange &&
58 sortedY.map( ( block, index ) => (
59 <DraggableBlock
60 key={ index }
61 id={ index }
62 { ...{
63 block,
64 create,
65 blockInsertionPoint,
66 getBlockRootClientId,
67 getSelectedBlockClientId,
68 getSelectedBlockAllowedBlocks,
69 defaultAllowedQuickSidebarBlocks,
70 updateDefaultAllowedQuickSidebarBlocks,
71 saveOptionToDatabase,
72 } }
73 />
74 ) ) }
75 { enableRearrange && (
76 <DragAndDropComponent
77 initialItems={ sortedY }
78 updateDefaultAllowedQuickSidebarBlocks={
79 updateDefaultAllowedQuickSidebarBlocks
80 }
81 saveOptionToDatabase={ saveOptionToDatabase }
82 />
83 ) }
84 </>
85 );
86 };
87
88 export default Blocks;
89