Skip to content

Commit

Permalink
use page list for placeholder, always create new menu, select most re…
Browse files Browse the repository at this point in the history
…cent when more than one
  • Loading branch information
draganescu committed Aug 2, 2022
1 parent ff5669f commit 5d91de2
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from '@wordpress/block-editor';
import { EntityProvider } from '@wordpress/core-data';

import { useDispatch, useSelect, useRegistry } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';
import {
PanelBody,
ToggleControl,
Expand All @@ -40,6 +40,7 @@ import {
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
import { createBlock } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -130,7 +131,6 @@ function Navigation( {

const ref = attributes.ref;

const registry = useRegistry();
const setRef = ( postId ) => {
setAttributes( { ref: postId } );
};
Expand Down Expand Up @@ -266,12 +266,18 @@ function Navigation( {
hasUncontrolledInnerBlocks ||
isCreatingNavigationMenu ||
ref ||
! navigationMenus?.length ||
navigationMenus?.length > 1
! navigationMenus?.length
) {
return;
}

navigationMenus.sort( ( menuA, menuB ) => {
const menuADate = new Date( menuA.date );
const menuBDate = new Date( menuB.date );
return menuADate.getTime() < menuBDate.getTime();
} );

__unstableMarkNextChangeAsNotPersistent();
setRef( navigationMenus[ 0 ].id );
}, [ navigationMenus ] );

Expand Down Expand Up @@ -304,6 +310,11 @@ function Navigation( {
hasResolvedNavigationMenus &&
! hasUncontrolledInnerBlocks;

if ( isPlaceholder && ! ref ) {
__unstableMarkNextChangeAsNotPersistent();
replaceInnerBlocks( clientId, [ createBlock( 'core/page-list' ) ] );
}

const isEntityAvailable =
! isNavigationMenuMissing && isNavigationMenuResolved;

Expand Down Expand Up @@ -541,17 +552,6 @@ function Navigation( {
shouldFocusNavigationSelector,
] );

const resetToEmptyBlock = useCallback( () => {
registry.batch( () => {
setAttributes( {
ref: undefined,
} );
if ( ! ref ) {
replaceInnerBlocks( clientId, [] );
}
} );
}, [ clientId, ref ] );

const isResponsive = 'never' !== overlayMenu;

const overlayMenuPreviewClasses = classnames(
Expand Down Expand Up @@ -700,6 +700,20 @@ function Navigation( {
if ( hasUnsavedBlocks ) {
return (
<TagName { ...blockProps }>
<BlockControls>
<ToolbarGroup className="wp-block-navigation__toolbar-menu-selector">
<NavigationMenuSelector
ref={ null }
currentMenuId={ null }
clientId={ clientId }
onSelect={ handleSelectNavigation }
onCreateNew={ () => createNavigationMenu( '', [] ) }
/* translators: %s: The name of a menu. */
actionLabel={ __( "Switch to '%s'" ) }
showManageActions
/>
</ToolbarGroup>
</BlockControls>
{ stylingInspectorControls }
<ResponsiveWrapper
id={ clientId }
Expand Down Expand Up @@ -744,7 +758,10 @@ function Navigation( {
{ __(
'Navigation menu has been deleted or is unavailable. '
) }
<Button onClick={ resetToEmptyBlock } variant="link">
<Button
onClick={ () => createNavigationMenu( '', [] ) }
variant="link"
>
{ __( 'Create a new menu?' ) }
</Button>
</Warning>
Expand All @@ -766,7 +783,7 @@ function Navigation( {
? CustomPlaceholder
: Placeholder;

if ( isPlaceholder ) {
if ( isPlaceholder && CustomPlaceholder ) {
return (
<TagName { ...blockProps }>
<PlaceholderComponent
Expand Down Expand Up @@ -795,7 +812,9 @@ function Navigation( {
currentMenuId={ ref }
clientId={ clientId }
onSelect={ handleSelectNavigation }
onCreateNew={ resetToEmptyBlock }
onCreateNew={ () =>
createNavigationMenu( '', [] )
}
/* translators: %s: The name of a menu. */
actionLabel={ __( "Switch to '%s'" ) }
showManageActions
Expand All @@ -814,7 +833,7 @@ function Navigation( {
canUserDeleteNavigationMenu && (
<NavigationMenuDeleteControl
onDelete={ ( deletedMenuTitle = '' ) => {
resetToEmptyBlock();
createNavigationMenu( '', [] );
showNavigationMenuDeleteNotice(
sprintf(
// translators: %s: the name of a menu (e.g. Header navigation).
Expand Down

0 comments on commit 5d91de2

Please sign in to comment.