Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 86 additions & 10 deletions routes/navigation-edit/editor/leaf-more-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { useDispatch, useSelect } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
// @ts-expect-error - No type declarations available for @wordpress/block-editor
import { BlockTitle, store as blockEditorStore } from '@wordpress/block-editor';
import {
hasBlockSupport,
store as blocksStore,
// @ts-expect-error - No type declarations available for @wordpress/blocks
} from '@wordpress/blocks';

const POPOVER_PROPS = {
className: 'block-editor-block-settings-menu__popover',
Expand All @@ -18,26 +23,63 @@ export default function LeafMoreMenu( {
block,
...props
}: {
block: { clientId: string };
block: { clientId: string; name: string };
} ) {
const { clientId } = block;
const { moveBlocksDown, moveBlocksUp, removeBlocks } =
useDispatch( blockEditorStore );
const {
moveBlocksDown,
moveBlocksUp,
removeBlocks,
duplicateBlocks,
insertBeforeBlock,
insertAfterBlock,
} = useDispatch( blockEditorStore );

const removeLabel = sprintf(
/* translators: %s: block name */
__( 'Remove %s' ),
BlockTitle( { clientId, maximumLength: 25 } )
);

const rootClientId = useSelect(
( select ) => {
const { getBlockRootClientId } = select( blockEditorStore );
const { rootClientId, canDuplicate, canInsertBlock, isFirst, isLast } =
useSelect(
( select ) => {
const {
getBlockRootClientId,
canInsertBlockType,
getDirectInsertBlock,
getBlockIndex,
getBlockCount,
} = select( blockEditorStore );
const { getDefaultBlockName } = select( blocksStore );

return getBlockRootClientId( clientId );
},
[ clientId ]
);
const _rootClientId = getBlockRootClientId( clientId );
const canInsertDefaultBlock = canInsertBlockType(
getDefaultBlockName(),
_rootClientId
);
const directInsertBlock = _rootClientId
? getDirectInsertBlock( _rootClientId )
: null;

return {
rootClientId: _rootClientId,
canDuplicate:
!! block &&
hasBlockSupport( block.name, 'multiple', true ) &&
canInsertBlockType( block.name, _rootClientId ),
canInsertBlock:
( canInsertDefaultBlock || !! directInsertBlock ) &&
!! block &&
canInsertBlockType( block.name, _rootClientId ),
isFirst: getBlockIndex( clientId ) === 0,
isLast:
getBlockIndex( clientId ) ===
getBlockCount( _rootClientId ) - 1,
};
},
[ clientId, block ]
);

return (
<DropdownMenu
Expand All @@ -53,6 +95,8 @@ export default function LeafMoreMenu( {
<MenuGroup>
<MenuItem
icon={ chevronUp }
disabled={ isFirst }
accessibleWhenDisabled
onClick={ () => {
moveBlocksUp( [ clientId ], rootClientId );
onClose();
Expand All @@ -62,13 +106,45 @@ export default function LeafMoreMenu( {
</MenuItem>
<MenuItem
icon={ chevronDown }
disabled={ isLast }
accessibleWhenDisabled
onClick={ () => {
moveBlocksDown( [ clientId ], rootClientId );
onClose();
} }
>
{ __( 'Move down' ) }
</MenuItem>
{ canDuplicate && (
<MenuItem
onClick={ () => {
duplicateBlocks( [ clientId ] );
onClose();
} }
>
{ __( 'Duplicate' ) }
</MenuItem>
) }
{ canInsertBlock && (
<>
<MenuItem
onClick={ () => {
insertBeforeBlock( clientId );
onClose();
} }
>
{ __( 'Add before' ) }
</MenuItem>
<MenuItem
onClick={ () => {
insertAfterBlock( clientId );
onClose();
} }
>
{ __( 'Add after' ) }
</MenuItem>
</>
) }
</MenuGroup>
<MenuGroup>
<MenuItem
Expand Down
Loading