Skip to content

Commit

Permalink
Remove all edit functionality from Navigation in Browse Mode (#50788)
Browse files Browse the repository at this point in the history
* Remove Add Submenu option

* Remove Link UI entirely
  • Loading branch information
getdave authored May 22, 2023
1 parent 8b12c1c commit 801681b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 141 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks';
import {
addSubmenu,
chevronUp,
chevronDown,
moreVertical,
} from '@wordpress/icons';

import { chevronUp, chevronDown, moreVertical } from '@wordpress/icons';
import { DropdownMenu, MenuItem, MenuGroup } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
Expand All @@ -19,67 +14,6 @@ const POPOVER_PROPS = {
variant: 'toolbar',
};

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

function AddSubmenuItem( { block, onClose, expandedState, expand } ) {
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
);
}
if ( ! expandedState[ block.clientId ] ) {
expand( block.clientId );
}
onClose();
} }
>
{ __( 'Add submenu link' ) }
</MenuItem>
);
}

export default function LeafMoreMenu( props ) {
const { block } = props;
const { clientId } = block;
Expand Down Expand Up @@ -131,13 +65,6 @@ export default function LeafMoreMenu( props ) {
>
{ __( 'Move down' ) }
</MenuItem>
<AddSubmenuItem
block={ block }
onClose={ onClose }
expanded
expandedState={ props.expandedState }
expand={ props.expand }
/>
</MenuGroup>
<MenuGroup>
<MenuItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import {
store as blockEditorStore,
BlockList,
BlockTools,
__experimentalLinkControl as LinkControl,
} from '@wordpress/block-editor';
import { useDispatch, useSelect } from '@wordpress/data';
import { createBlock } from '@wordpress/blocks';
import { Popover, VisuallyHidden } from '@wordpress/components';
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
import { VisuallyHidden } from '@wordpress/components';
import { useCallback, useEffect, useState } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';

Expand All @@ -21,33 +19,6 @@ import { store as coreStore } from '@wordpress/core-data';
import { unlock } from '../../private-apis';
import LeafMoreMenu from './leaf-more-menu';

function CustomLinkAdditionalBlockUI( { block, onClose } ) {
const { updateBlockAttributes } = useDispatch( blockEditorStore );
const { label, url, opensInNewTab } = block.attributes;
const link = {
url,
opensInNewTab,
title: label && stripHTML( label ),
};
return (
<Popover placement="bottom" shift onClose={ onClose }>
<LinkControl
hasTextControl
hasRichPreviews
value={ link }
onChange={ ( updatedValue ) => {
updateBlockAttributes( block.clientId, {
label: updatedValue.title,
url: updatedValue.url,
opensInNewTab: updatedValue.opensInNewTab,
} );
onClose();
} }
onCancel={ onClose }
/>
</Popover>
);
}
// Needs to be kept in sync with the query used at packages/block-library/src/page-list/edit.js.
const MAX_PAGE_COUNT = 100;
const PAGES_QUERY = [
Expand Down Expand Up @@ -102,29 +73,6 @@ export default function NavigationMenuContent( { rootClientId, onSelect } ) {
const { replaceBlock, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );

const [ customLinkEditPopoverOpenId, setIsCustomLinkEditPopoverOpenId ] =
useState( false );

const renderAdditionalBlockUICallback = useCallback(
( block ) => {
if (
customLinkEditPopoverOpenId &&
block.clientId === customLinkEditPopoverOpenId
) {
return (
<CustomLinkAdditionalBlockUI
block={ block }
onClose={ () => {
setIsCustomLinkEditPopoverOpenId( false );
} }
/>
);
}
return null;
},
[ customLinkEditPopoverOpenId, setIsCustomLinkEditPopoverOpenId ]
);

// Delay loading stop by 50ms to avoid flickering.
useEffect( () => {
let timeoutId;
Expand Down Expand Up @@ -156,22 +104,11 @@ export default function NavigationMenuContent( { rootClientId, onSelect } ) {
block.clientId,
createBlock( 'core/navigation-link', block.attributes )
);
} else if (
block.name === 'core/navigation-link' &&
block.attributes.kind === 'custom' &&
block.attributes.url
) {
setIsCustomLinkEditPopoverOpenId( block.clientId );
} else {
onSelect( block );
}
},
[
onSelect,
__unstableMarkNextChangeAsNotPersistent,
replaceBlock,
setIsCustomLinkEditPopoverOpenId,
]
[ onSelect, __unstableMarkNextChangeAsNotPersistent, replaceBlock ]
);

// The hidden block is needed because it makes block edit side effects trigger.
Expand All @@ -188,7 +125,6 @@ export default function NavigationMenuContent( { rootClientId, onSelect } ) {
onSelect={ offCanvasOnselect }
blockSettingsMenu={ LeafMoreMenu }
showAppender={ false }
renderAdditionalBlockUI={ renderAdditionalBlockUICallback }
/>
) }
<VisuallyHidden aria-hidden="true">
Expand Down

1 comment on commit 801681b

@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 801681b.
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/5044585137
📝 Reported issues:

Please sign in to comment.