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 |