diff --git a/packages/block-library/src/navigation/block.json b/packages/block-library/src/navigation/block.json
index 273ac75dd65f3e..c40b0b3d7a9432 100644
--- a/packages/block-library/src/navigation/block.json
+++ b/packages/block-library/src/navigation/block.json
@@ -91,6 +91,7 @@
"__experimentalTextDecoration": true
}
},
+ "usesContext": [ "navigation" ],
"viewScript": "file:./view.min.js",
"editorStyle": "wp-block-navigation-editor",
"style": "wp-block-navigation"
diff --git a/packages/block-library/src/navigation/edit.js b/packages/block-library/src/navigation/edit.js
index d5ef92a4c894f2..6caf58881a2c80 100644
--- a/packages/block-library/src/navigation/edit.js
+++ b/packages/block-library/src/navigation/edit.js
@@ -2,6 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
+import { defaults } from 'lodash';
/**
* WordPress dependencies
@@ -38,17 +39,25 @@ import NavigationPlaceholder from './placeholder';
import PlaceholderPreview from './placeholder-preview';
import ResponsiveWrapper from './responsive-wrapper';
-const ALLOWED_BLOCKS = [
- 'core/navigation-link',
- 'core/search',
- 'core/social-links',
- 'core/page-list',
- 'core/spacer',
- 'core/home-link',
- 'core/site-title',
- 'core/site-logo',
- 'core/navigation-submenu',
-];
+const CONTEXT_DEFAULTS = {
+ allowedBlocks: [
+ 'core/navigation-link',
+ 'core/search',
+ 'core/social-links',
+ 'core/page-list',
+ 'core/spacer',
+ 'core/home-link',
+ 'core/site-title',
+ 'core/site-logo',
+ 'core/navigation-submenu',
+ ],
+ hasSubmenuIndicatorSetting: true,
+ hasItemJustificationControls: true,
+ hasColorSettings: true,
+ placeholder: NavigationPlaceholder,
+ createAppender: ( wouldNavBlockShowAppender ) =>
+ wouldNavBlockShowAppender ? undefined : false,
+};
const DEFAULT_BLOCK = [ 'core/navigation-link' ];
@@ -96,6 +105,7 @@ function Navigation( {
selectedBlockHasDescendants,
attributes,
setAttributes,
+ context: { navigation },
clientId,
hasExistingNavItems,
isImmediateParentOfSelectedBlock,
@@ -110,14 +120,6 @@ function Navigation( {
setOverlayBackgroundColor,
overlayTextColor,
setOverlayTextColor,
-
- // These props are used by the navigation editor to override specific
- // navigation block settings.
- hasSubmenuIndicatorSetting = true,
- hasItemJustificationControls = true,
- hasColorSettings = true,
- customPlaceholder: CustomPlaceholder = null,
- customAppender: CustomAppender = null,
} ) {
const [ isPlaceholderShown, setIsPlaceholderShown ] = useState(
! hasExistingNavItems
@@ -126,6 +128,10 @@ function Navigation( {
false
);
+ // The context is used by the navigation editor to override specific
+ // navigation block settings.
+ const navContext = defaults( navigation, CONTEXT_DEFAULTS );
+
const { selectBlock } = useDispatch( blockEditorStore );
const navRef = useRef();
@@ -162,22 +168,22 @@ function Navigation( {
// When the block is selected itself or has a top level item selected that
// doesn't itself have children, show the standard appender. Else show no
// appender.
- const appender =
+ const wouldNavBlockShowAppender =
isSelected ||
- ( isImmediateParentOfSelectedBlock && ! selectedBlockHasDescendants )
- ? undefined
- : false;
+ ( isImmediateParentOfSelectedBlock && ! selectedBlockHasDescendants );
const innerBlocksProps = useInnerBlocksProps(
{
className: 'wp-block-navigation__container',
},
{
- allowedBlocks: ALLOWED_BLOCKS,
+ allowedBlocks: navContext.allowedBlocks,
__experimentalDefaultBlock: DEFAULT_BLOCK,
__experimentalDirectInsert: DIRECT_INSERT,
orientation: attributes.orientation,
- renderAppender: CustomAppender || appender,
+ renderAppender: navContext.createAppender(
+ wouldNavBlockShowAppender
+ ),
// Ensure block toolbar is not too far removed from item
// being edited when in vertical mode.
@@ -189,7 +195,10 @@ function Navigation( {
// inherit templateLock={ 'all' }.
templateLock: false,
__experimentalLayout: LAYOUT,
- placeholder: ! CustomPlaceholder ? placeholder : undefined,
+ placeholder:
+ navContext.placeholder === CONTEXT_DEFAULTS.placeholder
+ ? placeholder
+ : undefined,
}
);
@@ -226,9 +235,7 @@ function Navigation( {
} );
if ( isPlaceholderShown ) {
- const PlaceholderComponent = CustomPlaceholder
- ? CustomPlaceholder
- : NavigationPlaceholder;
+ const PlaceholderComponent = navContext.placeholder;
return (
@@ -253,7 +260,7 @@ function Navigation( {
return (
<>
- { hasItemJustificationControls && (
+ { navContext.hasItemJustificationControls && (
{ navigatorModal }
- { hasSubmenuIndicatorSetting && (
+ { navContext.hasSubmenuIndicatorSetting && (
) }
- { hasColorSettings && (
+ { navContext.hasColorSettings && (
;
+}
+
export default function Layout( { blockEditorSettings } ) {
const contentAreaRef = useBlockSelectionClearer();
const [ isMenuNameControlFocused, setIsMenuNameControlFocused ] = useState(
@@ -80,16 +88,40 @@ export default function Layout( { blockEditorSettings } ) {
}
);
- const { hasSidebarEnabled, isInserterOpened } = useSelect(
+ const { hasSidebarEnabled, isInserterOpened, noBlockSelected } = useSelect(
( select ) => ( {
hasSidebarEnabled: !! select(
interfaceStore
).getActiveComplementaryArea( 'core/edit-navigation' ),
isInserterOpened: select( editNavigationStore ).isInserterOpened(),
+ noBlockSelected: ! select(
+ blockEditorStore
+ ).getSelectedBlockClientId(),
} ),
[]
);
+ const blockContext = useMemo( () => {
+ const context = {
+ hasSubmenuIndicatorSetting: false,
+ hasItemJustificationControls: false,
+ hasColorSettings: false,
+ placeholder: BlockPlaceholder,
+ createAppender: ( wouldNavBlockShowAppender ) =>
+ wouldNavBlockShowAppender || noBlockSelected
+ ? CustomAppender
+ : false,
+ };
+ if ( ! blockEditorSettings.blockNavMenus ) {
+ context.allowedBlocks = [
+ 'core/navigation',
+ 'core/navigation-link',
+ 'core/navigation-submenu',
+ ];
+ }
+ return { navigation: context };
+ }, [ blockEditorSettings.blockNavMenus, noBlockSelected ] );
+
useEffect( () => {
if ( ! selectedMenuId && menus?.length ) {
selectMenu( menus[ 0 ].id );
@@ -127,77 +159,83 @@ export default function Layout( { blockEditorSettings } ) {
} }
useSubRegistry={ false }
>
- [
- isMenuNameControlFocused,
- setIsMenuNameControlFocused,
- ],
- [ isMenuNameControlFocused ]
- ) }
- >
-
- }
- content={
- <>
-
- { ! hasFinishedInitialLoad && (
-
- ) }
-
- { ! isMenuSelected &&
- hasFinishedInitialLoad && (
-
+
+ [
+ isMenuNameControlFocused,
+ setIsMenuNameControlFocused,
+ ],
+ [ isMenuNameControlFocused ]
+ ) }
+ >
+
+ }
+ content={
+ <>
+
+ { ! hasFinishedInitialLoad && (
+
) }
- { isBlockEditorReady && (
-
-
-
-
-
- ) }
- >
- }
- sidebar={
- hasSidebarEnabled && (
-
- )
- }
- secondarySidebar={
- isInserterOpened &&
- }
- />
- { isMenuSelected && (
-
+
+
+
+
+ ) }
+ >
+ }
+ sidebar={
+ hasSidebarEnabled && (
+
+ )
+ }
+ secondarySidebar={
+ isInserterOpened &&
+ }
/>
- ) }
-
-
+ { isMenuSelected && (
+
+ ) }
+
+
+
diff --git a/packages/edit-navigation/src/filters/add-navigation-editor-custom-appender.js b/packages/edit-navigation/src/filters/add-navigation-editor-custom-appender.js
deleted file mode 100644
index 6a3b5f64e94dce..00000000000000
--- a/packages/edit-navigation/src/filters/add-navigation-editor-custom-appender.js
+++ /dev/null
@@ -1,76 +0,0 @@
-/**
- * WordPress dependencies
- */
-import { useSelect } from '@wordpress/data';
-import { addFilter } from '@wordpress/hooks';
-import { createHigherOrderComponent } from '@wordpress/compose';
-import {
- InnerBlocks,
- store as blockEditorStore,
-} from '@wordpress/block-editor';
-
-function CustomAppender() {
- return ;
-}
-
-function EnhancedNavigationBlock( { blockEdit: BlockEdit, ...props } ) {
- const clientId = props.clientId;
- const {
- noBlockSelected,
- isSelected,
- isImmediateParentOfSelectedBlock,
- selectedBlockHasDescendants,
- } = useSelect(
- ( select ) => {
- const {
- getClientIdsOfDescendants,
- hasSelectedInnerBlock,
- getSelectedBlockClientId,
- } = select( blockEditorStore );
- const _isImmediateParentOfSelectedBlock = hasSelectedInnerBlock(
- clientId,
- false
- );
- const selectedBlockId = getSelectedBlockClientId();
- const _selectedBlockHasDescendants = !! getClientIdsOfDescendants( [
- selectedBlockId,
- ] )?.length;
-
- return {
- isSelected: selectedBlockId === clientId,
- noBlockSelected: ! selectedBlockId,
- isImmediateParentOfSelectedBlock: _isImmediateParentOfSelectedBlock,
- selectedBlockHasDescendants: _selectedBlockHasDescendants,
- };
- },
- [ clientId ]
- );
-
- const customAppender =
- noBlockSelected ||
- isSelected ||
- ( isImmediateParentOfSelectedBlock && ! selectedBlockHasDescendants )
- ? CustomAppender
- : false;
-
- return ;
-}
-
-const addNavigationEditorCustomAppender = createHigherOrderComponent(
- ( BlockEdit ) => ( props ) => {
- if ( props.name !== 'core/navigation' ) {
- return ;
- }
-
- // Use a separate component so that `useSelect` only run on the navigation block.
- return ;
- },
- 'withNavigationEditorCustomAppender'
-);
-
-export default () =>
- addFilter(
- 'editor.BlockEdit',
- 'core/edit-navigation/with-navigation-editor-custom-appender',
- addNavigationEditorCustomAppender
- );
diff --git a/packages/edit-navigation/src/filters/add-navigation-editor-placeholder.js b/packages/edit-navigation/src/filters/add-navigation-editor-placeholder.js
deleted file mode 100644
index 94b62fea180572..00000000000000
--- a/packages/edit-navigation/src/filters/add-navigation-editor-placeholder.js
+++ /dev/null
@@ -1,29 +0,0 @@
-/**
- * WordPress dependencies
- */
-import { addFilter } from '@wordpress/hooks';
-import { createHigherOrderComponent } from '@wordpress/compose';
-
-/**
- * Internal dependencies
- */
-import BlockPlaceholder from '../components/block-placeholder';
-
-const addNavigationEditorPlaceholder = createHigherOrderComponent(
- ( BlockEdit ) => ( props ) => {
- if ( props.name !== 'core/navigation' ) {
- return ;
- }
- return (
-
- );
- },
- 'withNavigationEditorPlaceholder'
-);
-
-export default () =>
- addFilter(
- 'editor.BlockEdit',
- 'core/edit-navigation/with-navigation-editor-placeholder',
- addNavigationEditorPlaceholder
- );
diff --git a/packages/edit-navigation/src/filters/disable-inserting-non-navigation-blocks.js b/packages/edit-navigation/src/filters/disable-inserting-non-navigation-blocks.js
deleted file mode 100644
index f8426c7dd9137d..00000000000000
--- a/packages/edit-navigation/src/filters/disable-inserting-non-navigation-blocks.js
+++ /dev/null
@@ -1,28 +0,0 @@
-/**
- * WordPress dependencies
- */
-import { addFilter } from '@wordpress/hooks';
-/**
- * External dependencies
- */
-import { set } from 'lodash';
-
-function disableInsertingNonNavigationBlocks( settings, name ) {
- if (
- ! [
- 'core/navigation',
- 'core/navigation-link',
- 'core/navigation-submenu',
- ].includes( name )
- ) {
- set( settings, [ 'supports', 'inserter' ], false );
- }
- return settings;
-}
-
-export default () =>
- addFilter(
- 'blocks.registerBlockType',
- 'core/edit-navigation/disable-inserting-non-navigation-blocks',
- disableInsertingNonNavigationBlocks
- );
diff --git a/packages/edit-navigation/src/filters/index.js b/packages/edit-navigation/src/filters/index.js
index 17074eb87c6603..806d07dfa70f60 100644
--- a/packages/edit-navigation/src/filters/index.js
+++ b/packages/edit-navigation/src/filters/index.js
@@ -1,22 +1,10 @@
/**
* Internal dependencies
*/
-import addNavigationEditorCustomAppender from './add-navigation-editor-custom-appender';
-import addNavigationEditorPlaceholder from './add-navigation-editor-placeholder';
import addMenuNameEditor from './add-menu-name-editor';
-import disableInsertingNonNavigationBlocks from './disable-inserting-non-navigation-blocks';
-import removeEditUnsupportedFeatures from './remove-edit-unsupported-features';
import removeSettingsUnsupportedFeatures from './remove-settings-unsupported-features';
-export const addFilters = (
- shouldAddDisableInsertingNonNavigationBlocksFilter
-) => {
- addNavigationEditorCustomAppender();
- addNavigationEditorPlaceholder();
+export const addFilters = () => {
addMenuNameEditor();
- if ( shouldAddDisableInsertingNonNavigationBlocksFilter ) {
- disableInsertingNonNavigationBlocks();
- }
- removeEditUnsupportedFeatures();
removeSettingsUnsupportedFeatures();
};
diff --git a/packages/edit-navigation/src/filters/remove-edit-unsupported-features.js b/packages/edit-navigation/src/filters/remove-edit-unsupported-features.js
deleted file mode 100644
index 2c58b995158d4d..00000000000000
--- a/packages/edit-navigation/src/filters/remove-edit-unsupported-features.js
+++ /dev/null
@@ -1,30 +0,0 @@
-/**
- * WordPress dependencies
- */
-import { addFilter } from '@wordpress/hooks';
-import { createHigherOrderComponent } from '@wordpress/compose';
-
-const removeNavigationBlockEditUnsupportedFeatures = createHigherOrderComponent(
- ( BlockEdit ) => ( props ) => {
- if ( props.name !== 'core/navigation' ) {
- return ;
- }
-
- return (
-
- );
- },
- 'removeNavigationBlockEditUnsupportedFeatures'
-);
-
-export default () =>
- addFilter(
- 'editor.BlockEdit',
- 'core/edit-navigation/remove-navigation-block-edit-unsupported-features',
- removeNavigationBlockEditUnsupportedFeatures
- );
diff --git a/packages/edit-navigation/src/index.js b/packages/edit-navigation/src/index.js
index fe3629e2829494..9d8ba610019718 100644
--- a/packages/edit-navigation/src/index.js
+++ b/packages/edit-navigation/src/index.js
@@ -53,11 +53,9 @@ function NavEditor( { settings } ) {
/**
* Setup and registration of editor.
- *
- * @param {Object} settings blockEditor settings.
*/
-function setUpEditor( settings ) {
- addFilters( ! settings.blockNavMenus );
+function setUpEditor() {
+ addFilters();
registerCoreBlocks();
// Set up the navigation post entity.
@@ -83,7 +81,7 @@ function setUpEditor( settings ) {
* @param {Object} settings blockEditor settings.
*/
export function initialize( id, settings ) {
- setUpEditor( settings );
+ setUpEditor();
render(
,