-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Navigation: Add an edit menu button to the block controls #45772
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if it's neither edit-post nor edit-post. How should the navigation block behave? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothing I guess! |
||
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 ( | ||
<BlockControls> | ||
<ToolbarGroup> | ||
<ToolbarButton | ||
className="components-toolbar__control" | ||
label={ __( 'Open list view' ) } | ||
onClick={ openBlockInspector } // TODO - focus the menu part | ||
> | ||
{ __( 'Edit menu' ) } | ||
</ToolbarButton> | ||
</ToolbarGroup> | ||
</BlockControls> | ||
); | ||
}; | ||
|
||
export default EditMenuToolbarButton; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,27 +7,21 @@ import classnames from 'classnames'; | |
* WordPress dependencies | ||
*/ | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { useCallback, useMemo, useRef, Fragment } from '@wordpress/element'; | ||
import { useCallback, useMemo, useRef } from '@wordpress/element'; | ||
import { useEntityBlockEditor, store as coreStore } from '@wordpress/core-data'; | ||
import { | ||
BlockList, | ||
BlockEditorProvider, | ||
__experimentalLinkControl, | ||
BlockInspector, | ||
BlockTools, | ||
__unstableBlockToolbarLastItem, | ||
__unstableBlockSettingsMenuFirstItem, | ||
__unstableUseTypingObserver as useTypingObserver, | ||
BlockEditorKeyboardShortcuts, | ||
store as blockEditorStore, | ||
__unstableBlockNameContext, | ||
} from '@wordpress/block-editor'; | ||
import { useMergeRefs, useViewportMatch } from '@wordpress/compose'; | ||
import { ReusableBlocksMenuItems } from '@wordpress/reusable-blocks'; | ||
import { listView } from '@wordpress/icons'; | ||
import { ToolbarButton, ToolbarGroup } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { store as interfaceStore } from '@wordpress/interface'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -46,15 +40,12 @@ const LAYOUT = { | |
alignments: [], | ||
}; | ||
|
||
const NAVIGATION_SIDEBAR_NAME = 'edit-site/navigation-menu'; | ||
|
||
export default function BlockEditor( { setIsInserterOpen } ) { | ||
const { | ||
storedSettings, | ||
templateType, | ||
templateId, | ||
page, | ||
isNavigationSidebarOpen, | ||
} = useSelect( | ||
( select ) => { | ||
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 = () => ( | ||
<ToolbarGroup> | ||
<ToolbarButton | ||
className="components-toolbar__control" | ||
label={ | ||
isNavigationSidebarOpen | ||
? __( 'Close list view' ) | ||
: __( 'Open list view' ) | ||
} | ||
onClick={ toggleNavigationSidebar } | ||
icon={ listView } | ||
isActive={ isNavigationSidebarOpen } | ||
/> | ||
</ToolbarGroup> | ||
); | ||
|
||
// 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; | ||
} | ||
Comment on lines
-181
to
-183
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So just to be clear the "global" navigation sidebar is now officially abandoned? Is just want to be sure before we start removing all of this code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No I don't think this change means that, it's just this isn't the way we want to access it. |
||
|
||
return ( | ||
<BlockEditorProvider | ||
|
@@ -244,15 +199,6 @@ export default function BlockEditor( { setIsInserterOpen } ) { | |
<BlockInspectorButton onClick={ onClose } /> | ||
) } | ||
</__unstableBlockSettingsMenuFirstItem> | ||
<__unstableBlockToolbarLastItem> | ||
<__unstableBlockNameContext.Consumer> | ||
{ ( blockName ) => | ||
blockName === 'core/navigation' && ( | ||
<MaybeNavMenuSidebarToggle /> | ||
) | ||
} | ||
</__unstableBlockNameContext.Consumer> | ||
</__unstableBlockToolbarLastItem> | ||
</BlockTools> | ||
<ReusableBlocksMenuItems /> | ||
</BlockEditorProvider> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd probably avoid importing the store here as well as this will bundle the interface package into block-library and that package is only meant to be used in top level packages (edit-post, edit-site, edit-widgets...)