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
scruffian and getdave committed Feb 14, 2023
1 parent 01641fd commit 6017898
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

1 comment on commit 6017898

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 6017898.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4175063035
📝 Reported issues:

Please sign in to comment.