From b4fc38388d09e642eb840a429c7bdf8f62593437 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Tue, 15 Nov 2022 13:12:50 +0000 Subject: [PATCH] Navigation: Add an edit menu button to the block controls --- .../edit/edit-menu-toolbar-button.js | 63 +++++++++++++++++++ .../src/navigation/edit/index.js | 4 ++ .../src/components/block-editor/index.js | 56 +---------------- 3 files changed, 68 insertions(+), 55 deletions(-) create mode 100644 packages/block-library/src/navigation/edit/edit-menu-toolbar-button.js diff --git a/packages/block-library/src/navigation/edit/edit-menu-toolbar-button.js b/packages/block-library/src/navigation/edit/edit-menu-toolbar-button.js new file mode 100644 index 0000000000000..29f66c9d1a578 --- /dev/null +++ b/packages/block-library/src/navigation/edit/edit-menu-toolbar-button.js @@ -0,0 +1,63 @@ +/** + * WordPress dependencies + */ +import { + BlockControls, +} from '@wordpress/block-editor'; +import { useSelect, useDispatch } from '@wordpress/data'; +import { + ToolbarButton, + ToolbarGroup, +} from '@wordpress/components'; +import { store as interfaceStore } from '@wordpress/interface'; +import { __ } from '@wordpress/i18n'; + +const EditMenuToolbarButton = () => { + // Blocks can be loaded into both post and site editors. + // We use this to determine which editor we are in so that + // we can determine which inspector controls to open. + const { + isPostEditor, + } = useSelect( + ( select ) => { + // eslint-disable-next-line @wordpress/data-no-store-string-literals + const editorSelectors = select( 'core/editor' ); + return { + isPostEditor: !! editorSelectors.getEditedPostAttribute( 'type' ) + }; + } + ); + const { enableComplementaryArea } = useDispatch( interfaceStore ); + // eslint-disable-next-line @wordpress/data-no-store-string-literals + const { openGeneralSidebar } = useDispatch( 'core/edit-post' ); + const openBlockInspector = () => { + if ( isPostEditor ) { + openGeneralSidebar( 'edit-post/block' ); + } else { + enableComplementaryArea( 'core/edit-site', 'edit-site/block-inspector' ); + } + }; + + const isOffCanvasNavigationEditorEnabled = + window?.__experimentalEnableOffCanvasNavigationEditor === true; + + if ( ! isOffCanvasNavigationEditorEnabled ) { + return null; + } + + return ( + + + + { __( 'Edit menu' ) } + + + + ); +}; + +export default EditMenuToolbarButton; diff --git a/packages/block-library/src/navigation/edit/index.js b/packages/block-library/src/navigation/edit/index.js index e15619a2cc74d..8d00b1bdbc87b 100644 --- a/packages/block-library/src/navigation/edit/index.js +++ b/packages/block-library/src/navigation/edit/index.js @@ -69,6 +69,7 @@ import useCreateNavigationMenu from './use-create-navigation-menu'; import { useInnerBlocks } from './use-inner-blocks'; import { detectColors } from './utils'; import ManageMenusButton from './manage-menus-button'; +import EditMenuToolbarButton from './edit-menu-toolbar-button'; function Navigation( { attributes, @@ -736,6 +737,7 @@ function Navigation( { { stylingInspectorControls } + + { __( 'Navigation menu has been deleted or is unavailable. ' @@ -953,6 +956,7 @@ function Navigation( { { ! isLoading && ( + { const { getSettings, getEditedPostType, getEditedPostId, getPage } = @@ -65,10 +56,6 @@ export default function BlockEditor( { setIsInserterOpen } ) { templateType: getEditedPostType(), templateId: getEditedPostId(), page: getPage(), - isNavigationSidebarOpen: - select( interfaceStore ).getActiveComplementaryArea( - editSiteStore.name - ) === NAVIGATION_SIDEBAR_NAME, }; }, [ setIsInserterOpen ] @@ -141,14 +128,6 @@ export default function BlockEditor( { setIsInserterOpen } ) { templateType ); const { setPage } = useDispatch( editSiteStore ); - const { enableComplementaryArea, disableComplementaryArea } = - useDispatch( interfaceStore ); - const toggleNavigationSidebar = useCallback( () => { - const toggleComplementaryArea = isNavigationSidebarOpen - ? disableComplementaryArea - : enableComplementaryArea; - toggleComplementaryArea( editSiteStore.name, NAVIGATION_SIDEBAR_NAME ); - }, [ isNavigationSidebarOpen ] ); const contentRef = useRef(); const mergedRefs = useMergeRefs( [ contentRef, useTypingObserver() ] ); const isMobileViewport = useViewportMatch( 'small', '<' ); @@ -157,30 +136,6 @@ export default function BlockEditor( { setIsInserterOpen } ) { const isTemplatePart = templateType === 'wp_template_part'; const hasBlocks = blocks.length !== 0; - const NavMenuSidebarToggle = () => ( - - - - ); - - // Conditionally include NavMenu sidebar in Plugin only. - // Optimise for dead code elimination. - // See https://github.com/WordPress/gutenberg/blob/trunk/docs/how-to-guides/feature-flags.md#dead-code-elimination. - let MaybeNavMenuSidebarToggle = Fragment; - - if ( process.env.IS_GUTENBERG_PLUGIN ) { - MaybeNavMenuSidebarToggle = NavMenuSidebarToggle; - } return ( ) } - <__unstableBlockToolbarLastItem> - <__unstableBlockNameContext.Consumer> - { ( blockName ) => - blockName === 'core/navigation' && ( - - ) - } - -