From 2f51785ce3fbae261a7b989a299ee88364e4fa9d Mon Sep 17 00:00:00 2001 From: scruffian Date: Fri, 10 Feb 2023 16:33:15 +0000 Subject: [PATCH 1/4] OffCanvasEditor: Only allow some blocks to be converted to submenus --- .../off-canvas-editor/leaf-more-menu.js | 108 +++++++++++------- 1 file changed, 65 insertions(+), 43 deletions(-) diff --git a/packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js b/packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js index 5d3eaef3cd87f0..b18a74a381ce65 100644 --- a/packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js +++ b/packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js @@ -19,11 +19,69 @@ const POPOVER_PROPS = { variant: 'toolbar', }; +const BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU = [ + 'core/navigation-link', + 'core/navigation-submenu', +]; + +function AddSubmenuItem( { block, clientId, onClose } ) { + const { insertBlock, replaceBlock, replaceInnerBlocks } = + useDispatch( blockEditorStore ); + + if ( + BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU.indexOf( block.name ) === -1 + ) { + return null; + } + + return ( + { + const updateSelectionOnInsert = false; + const newLink = createBlock( 'core/navigation-link' ); + + if ( block.name === 'core/navigation-submenu' ) { + insertBlock( + newLink, + block.innerBlocks.length, + clientId, + updateSelectionOnInsert + ); + } else { + // Convert to a submenu if the block currently isn't one. + const newSubmenu = createBlock( + 'core/navigation-submenu', + block.attributes, + block.innerBlocks + ); + + // The following must happen as two independent actions. + // Why? Because the offcanvas editor relies on the getLastInsertedBlocksClientIds + // selector to determine which block is "active". As the UX needs the newLink to be + // the "active" block it must be the last block to be inserted. + // Therefore the Submenu is first created and **then** the newLink is inserted + // thus ensuring it is the last inserted block. + replaceBlock( clientId, newSubmenu ); + + replaceInnerBlocks( + newSubmenu.clientId, + [ newLink ], + updateSelectionOnInsert + ); + } + onClose(); + } } + > + { __( 'Add submenu link' ) } + + ); +} + export default function LeafMoreMenu( props ) { const { clientId, block } = props; - const { insertBlock, replaceBlock, removeBlocks, replaceInnerBlocks } = - useDispatch( blockEditorStore ); + const { removeBlocks } = useDispatch( blockEditorStore ); const label = sprintf( /* translators: %s: block name */ @@ -42,47 +100,11 @@ export default function LeafMoreMenu( props ) { > { ( { onClose } ) => ( - { - const updateSelectionOnInsert = false; - const newLink = createBlock( - 'core/navigation-link' - ); - if ( block.name === 'core/navigation-submenu' ) { - insertBlock( - newLink, - block.innerBlocks.length, - clientId, - updateSelectionOnInsert - ); - } else { - // Convert to a submenu if the block currently isn't one. - const newSubmenu = createBlock( - 'core/navigation-submenu', - block.attributes, - block.innerBlocks - ); - - // The following must happen as two independent actions. - // Why? Because the offcanvas editor relies on the getLastInsertedBlocksClientIds - // selector to determine which block is "active". As the UX needs the newLink to be - // the "active" block it must be the last block to be inserted. - // Therefore the Submenu is first created and **then** the newLink is inserted - // thus ensuring it is the last inserted block. - replaceBlock( clientId, newSubmenu ); - - replaceInnerBlocks( - newSubmenu.clientId, - [ newLink ], - updateSelectionOnInsert - ); - } - onClose(); - } } - > - { __( 'Add submenu link' ) } - + { removeBlocks( [ clientId ], false ); From 24ad174ffbfa03992f68515758ddc8c46164e0d3 Mon Sep 17 00:00:00 2001 From: scruffian Date: Mon, 13 Feb 2023 13:53:43 +0000 Subject: [PATCH 2/4] make the option disabled --- .../off-canvas-editor/leaf-more-menu.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js b/packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js index b18a74a381ce65..f7793d29119ace 100644 --- a/packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js +++ b/packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js @@ -24,19 +24,17 @@ const BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU = [ 'core/navigation-submenu', ]; -function AddSubmenuItem( { block, clientId, onClose } ) { +function AddSubmenuItem( { block, onClose } ) { const { insertBlock, replaceBlock, replaceInnerBlocks } = useDispatch( blockEditorStore ); - if ( - BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU.indexOf( block.name ) === -1 - ) { - return null; - } - + const clientId = block.clientId; + const isDisabled = + BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU.indexOf( block.name ) === -1; return ( { const updateSelectionOnInsert = false; const newLink = createBlock( 'core/navigation-link' ); @@ -100,11 +98,7 @@ export default function LeafMoreMenu( props ) { > { ( { onClose } ) => ( - + { removeBlocks( [ clientId ], false ); From a00c1d0028c137f13878f721dd67b5edf441af79 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Mon, 13 Feb 2023 15:33:01 +0000 Subject: [PATCH 3/4] Update packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js Co-authored-by: Dave Smith --- .../src/components/off-canvas-editor/leaf-more-menu.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js b/packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js index f7793d29119ace..c4db1fdd2ca723 100644 --- a/packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js +++ b/packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js @@ -30,7 +30,7 @@ function AddSubmenuItem( { block, onClose } ) { const clientId = block.clientId; const isDisabled = - BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU.indexOf( block.name ) === -1; + ! BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU.includes( block.name ); return ( Date: Tue, 14 Feb 2023 10:21:43 +0000 Subject: [PATCH 4/4] fix linter --- .../src/components/off-canvas-editor/leaf-more-menu.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js b/packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js index c4db1fdd2ca723..a266f0d9377c36 100644 --- a/packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js +++ b/packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js @@ -29,8 +29,9 @@ function AddSubmenuItem( { block, onClose } ) { useDispatch( blockEditorStore ); const clientId = block.clientId; - const isDisabled = - ! BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU.includes( block.name ); + const isDisabled = ! BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU.includes( + block.name + ); return (