PluginProbe ʕ •ᴥ•ʔ
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / trunk
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz vtrunk
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 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8 0.0.9 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.1.2 1.10.0 1.10.1 1.11.0 1.12.0 1.12.1 1.12.2 1.12.3 1.13.0 1.13.1 1.13.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.5.0 1.5.1 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.8.0 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.2 2.6.0
sureforms / modules / quick-action-sidebar / components / blocks.js
sureforms / modules / quick-action-sidebar / components Last commit date
Modal.js 2 years ago Sidebar.js 1 week ago blocks.js 1 week ago draggable-block.js 2 years ago move-up-down.js 2 years ago
blocks.js
89 lines
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