From 7469f26a42081de9c41d15c9b02e62b5a80a052c Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Wed, 8 Mar 2023 19:04:04 +0000 Subject: [PATCH] Fix: Show UI to update custom links on the sidebar navigation. --- .../off-canvas-editor/block-contents.js | 4 ++ .../src/components/off-canvas-editor/index.js | 3 + packages/edit-site/package.json | 1 + .../navigation-menu-content.js | 65 ++++++++++++++++++- 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/components/off-canvas-editor/block-contents.js b/packages/block-editor/src/components/off-canvas-editor/block-contents.js index 77922df8441ea0..796240b0a143cc 100644 --- a/packages/block-editor/src/components/off-canvas-editor/block-contents.js +++ b/packages/block-editor/src/components/off-canvas-editor/block-contents.js @@ -19,6 +19,7 @@ import { store as blockEditorStore } from '../../store'; import { updateAttributes } from './update-attributes'; import { LinkUI } from './link-ui'; import { useInsertedBlock } from './use-inserted-block'; +import { useListViewContext } from './context'; const BLOCKS_WITH_LINK_UI_SUPPORT = [ 'core/navigation-link', @@ -90,6 +91,8 @@ const ListViewBlockContents = forwardRef( hasExistingLinkValue, ] ); + const { renderAdditionalBlockUI } = useListViewContext(); + const isBlockMoveTarget = blockMovingClientId && selectedBlockInBlockEditor === clientId; @@ -107,6 +110,7 @@ const ListViewBlockContents = forwardRef( return ( <> + { renderAdditionalBlockUI && renderAdditionalBlockUI( block ) } { isLinkUIOpen && ( + { + updateBlockAttributes( block.clientId, { + label: updatedValue.title, + url: updatedValue.url, + opensInNewTab: updatedValue.opensInNewTab, + } ); + onClose(); + } } + onCancel={ onClose } + /> + + ); +} + export default function NavigationMenuContent( { rootClientId, onSelect } ) { const { clientIdsTree, isLoading } = useSelect( ( select ) => { @@ -35,6 +67,23 @@ 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 renderAdditionalBlockUI( block, () => { + setIsCustomLinkEditPopoverOpenId( false ); + } ); + } + }, + [ customLinkEditPopoverOpenId, setIsCustomLinkEditPopoverOpenId ] + ); + const { OffCanvasEditor, LeafMoreMenu } = unlock( blockEditorPrivateApis ); const offCanvasOnselect = useCallback( @@ -48,11 +97,22 @@ 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 ] + [ + onSelect, + __unstableMarkNextChangeAsNotPersistent, + replaceBlock, + setIsCustomLinkEditPopoverOpenId, + ] ); // The hidden block is needed because it makes block edit side effects trigger. @@ -66,6 +126,7 @@ export default function NavigationMenuContent( { rootClientId, onSelect } ) { onSelect={ offCanvasOnselect } LeafMoreMenu={ LeafMoreMenu } showAppender={ false } + renderAdditionalBlockUI={ renderAdditionalBlockUICallback } /> ) }