Skip to content

Commit

Permalink
OffCanvasEditor: Only allow some blocks to be converted to submenus (#…
Browse files Browse the repository at this point in the history
…47974)

* OffCanvasEditor: Only allow some blocks to be converted to submenus

* make the option disabled

* Update packages/block-editor/src/components/off-canvas-editor/leaf-more-menu.js

Co-authored-by: Dave Smith <getdavemail@gmail.com>

---------

Co-authored-by: Dave Smith <getdavemail@gmail.com>
  • Loading branch information
2 people authored and ntsekouras committed Feb 21, 2023
1 parent 325d71b commit 6786a14
Showing 1 changed file with 60 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,68 @@ const POPOVER_PROPS = {
variant: 'toolbar',
};

const BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU = [
'core/navigation-link',
'core/navigation-submenu',
];

function AddSubmenuItem( { block, onClose } ) {
const { insertBlock, replaceBlock, replaceInnerBlocks } =
useDispatch( blockEditorStore );

const clientId = block.clientId;
const isDisabled = ! BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU.includes(
block.name
);
return (
<MenuItem
icon={ addSubmenu }
disabled={ isDisabled }
onClick={ () => {
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' ) }
</MenuItem>
);
}

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 */
Expand All @@ -42,47 +99,7 @@ export default function LeafMoreMenu( props ) {
>
{ ( { onClose } ) => (
<MenuGroup>
<MenuItem
icon={ addSubmenu }
onClick={ () => {
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' ) }
</MenuItem>
<AddSubmenuItem block={ block } onClose={ onClose } />
<MenuItem
onClick={ () => {
removeBlocks( [ clientId ], false );
Expand Down

0 comments on commit 6786a14

Please sign in to comment.