-
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
Try always generating navigation post title #36760
Conversation
Size Change: -179 B (0%) Total Size: 1.1 MB
ℹ️ View Unchanged
|
if ( ! title ) { | ||
return; | ||
} |
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.
Testing and this will return undefined
here because there's now no title. It looks like the way this is currently works in the PR is to trigger showing the UnsavedInnerBlocks
component and have that create the wp_navigation
post. I really think it'd be best to rely less on UnsavedInnerBlocks
for that—that component was designed to handle a specific backwards compatibility edge case for patterns and not a whole lot else.
I feel like it'd be better to refactor or duplicate the code that generates a title in UnsavedInnerBlocks
so that it can be used in the Placeholder too. That way there's always a title when we call createNavigationMenu
and the change is a bit less involved.
It shouldn't be too much to achieve that. There's a few prequisites like calling the useTemplatePartName
hook in the Placeholder component to get the area name. And also getting the entire list of menus so that the number added to the end of the name is correct. This bit of code below does the naming, it could be moved into a shared function or hook so that it can be used by both UnsavedInnerBlocks
and NavigationPlaceholder
:
gutenberg/packages/block-library/src/navigation/edit/unsaved-inner-blocks.js
Lines 137 to 161 in 88883a3
const title = area | |
? sprintf( | |
// translators: %s: the name of a menu (e.g. Header navigation). | |
__( '%s navigation' ), | |
area | |
) | |
: // translators: 'navigation' as in website navigation. | |
__( 'Navigation' ); | |
// Determine how many menus start with the automatic title. | |
const matchingMenuTitleCount = [ | |
...draftNavigationMenus, | |
...navigationMenus, | |
].reduce( | |
( count, menu ) => | |
menu?.title?.raw?.startsWith( title ) ? count + 1 : count, | |
0 | |
); | |
// Append a number to the end of the title if a menu with | |
// the same name exists. | |
const titleWithCount = | |
matchingMenuTitleCount > 0 | |
? `${ title } ${ matchingMenuTitleCount + 1 }` | |
: title; |
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 added useCreateNavigationMenu
that can be reused in both places. The process doesn't fully work yet and there is some flickering involved, but it's one step closer now. cc @tellthemachines
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.
Thanks @adamziel ! I fixed all the bugs I could find and everything seems to be working correctly now. Would appreciate some extra testing 😄
packages/block-library/src/navigation/edit/use-default-navigation-title.js
Outdated
Show resolved
Hide resolved
All comments have been addressed and features are working as intended, so I'm marking this ready for review. |
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.
Thank you for all the work that's going into this. I gave it a spin and found the following issues:
Blank Menu
Creating a new Navigation via Select Menu -> Create New
results in a blank menu being created.
- Delete all Navigations.
- Go to Site Editor and add Nav block.
- Click
Start empty
and don't add any blocks. - No click
Select menu
again - notice there's a blank menu entry. - Delete your Nav block.
- Re-add a new Nav block. Notice how
Select menu
appears. - Notice how under
Select menu
there is a blank menu entry. - Go to the
wp_navigation
Post screen. See that no Posts have been created. It appears this blank entry is a phantom.
Note this phantom entry persists even after you have added items to your Nav block and saved the resulting menu.
Editing Menu in Trash
- Create a Nav block and add some items.
- Save.
- Go to
/wp-admin/edit.php?post_type=wp_navigation
and trash all your Navigations. - Go back to the Site Editor.
- See that your Navigation items are still loaded into the Nav block even though they are in trash.
- Add a new item to the Navigation.
- Save.
- See how the item gets restored from Trash and is back to Publish state.
Creating new Nav persists current Nav items
- Create a Navigation block and add some items.
- Save the Template so it is persisted.
- Go to Nav block and click
Select menu -> Create new
. - Notice how a fresh Navigation Post is created but the same items are carried across from the current Navigation.
Instead you should receive an entirely blank Navigation with no items.
@@ -356,6 +355,7 @@ function Navigation( { | |||
onClose(); | |||
} } | |||
onCreateNew={ () => { | |||
replaceInnerBlocks( clientId, [] ); |
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 added this because when you clicked "Create new menu" it would use the current inner blocks (i.e. from the current menu) and automatically create a new menu from those.
What we want is actually to remove all the inner blocks and reset in order that the placeholder will be shown.
|
Previously you could get into an error state if the Nav block referenced a Nav Post which had been deleted. You could not recover from this as the UI provided no way to "start over". This addition affords that ability.
74c49ad
to
1885a87
Compare
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 think this works well 🚢 . There are now three different "create empty" buttons that do at least two different things:
Let's address that in a follow up PR. Great collaboration @getdave @tellthemachines !
* Try always generating navigation title * Show placeholder preview * Add useCreateNavigationMenu * Update packages/block-library/src/navigation/edit/use-default-navigation-title.js * Fix placeholder and remove unecessary logic. * Pass clientId to placeholder. * Reset inner blocks when creating new menu * Only consider published Navigations as valid * Correct bug with checking status of Nav post * UX to allow creating new menu if associated Nav post not found Previously you could get into an error state if the Nav block referenced a Nav Post which had been deleted. You could not recover from this as the UI provided no way to "start over". This addition affords that ability. * Switch to useGenerateDefaultNavigationTitle * Wrap createNewMenu in useCallback * Rename createNewMenu to startWithEmptyMenu Co-authored-by: Adam Zieliński <[email protected]> Co-authored-by: Dave Smith <[email protected]>
if ( navigationMenuId && isNavigationMenuMissing ) { | ||
return ( | ||
<div { ...blockProps }> | ||
<Warning> | ||
{ __( | ||
'Navigation menu has been deleted or is unavailable. ' | ||
) } | ||
<Button onClick={ startWithEmptyMenu } variant="link"> | ||
{ __( 'Create a new menu?' ) } | ||
</Button> | ||
</Warning> | ||
</div> | ||
); | ||
} |
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.
This was removed in #36507, I'm not sure why it made a reappearance here.
// getEditedEntityRecord will return the post regardless of status. | ||
// Therefore if the found post is not published then we should ignore it. | ||
if ( navigationMenu?.status !== 'publish' ) { | ||
navigationMenu = null; | ||
} |
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.
Navigation menus can be drafts too, that's an important part of the pattern insertion flow. Just wondering what the reasoning behind this change was?
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.
Related - #37354
Description
Fixes #36593 and #36575.
Fully automates the Navigation post type naming process. When creating a new Navigation block, user doesn't have to give it a title or save it before starting to edit. It should get saved as part of the editor saving process.
How has this been tested?
Screenshots
Types of changes
Checklist:
*.native.js
files for terms that need renaming or removal).