-
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
Simplfy handling of save of Nav block uncontrolled inner blocks #45517
Changes from all commits
c55d2b8
f4fa98a
b582445
98da28c
b0aecfa
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 |
---|---|---|
|
@@ -6,7 +6,13 @@ import classnames from 'classnames'; | |
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState, useEffect, useRef, Platform } from '@wordpress/element'; | ||
import { | ||
useState, | ||
useEffect, | ||
useRef, | ||
Platform, | ||
useMemo, | ||
} from '@wordpress/element'; | ||
import { addQueryArgs } from '@wordpress/url'; | ||
import { | ||
__experimentalOffCanvasEditor as OffCanvasEditor, | ||
|
@@ -197,9 +203,6 @@ function Navigation( { | |
__unstableMarkNextChangeAsNotPersistent, | ||
} = useDispatch( blockEditorStore ); | ||
|
||
const [ hasSavedUnsavedInnerBlocks, setHasSavedUnsavedInnerBlocks ] = | ||
useState( false ); | ||
|
||
const [ isResponsiveMenuOpen, setResponsiveMenuVisibility ] = | ||
useState( false ); | ||
|
||
|
@@ -233,8 +236,16 @@ function Navigation( { | |
classicMenuConversionStatus === CLASSIC_MENU_CONVERSION_PENDING; | ||
|
||
// Only autofallback to published menus. | ||
const fallbackNavigationMenus = navigationMenus?.filter( | ||
( menu ) => menu.status === 'publish' | ||
const fallbackNavigationMenus = useMemo( | ||
() => | ||
navigationMenus | ||
?.filter( ( menu ) => menu.status === 'publish' ) | ||
?.sort( ( menuA, menuB ) => { | ||
const menuADate = new Date( menuA.date ); | ||
const menuBDate = new Date( menuB.date ); | ||
return menuADate.getTime() < menuBDate.getTime(); | ||
} ), | ||
[ navigationMenus ] | ||
); | ||
|
||
// Attempt to retrieve and prioritize any existing navigation menu unless: | ||
|
@@ -254,12 +265,6 @@ function Navigation( { | |
return; | ||
} | ||
|
||
fallbackNavigationMenus.sort( ( menuA, menuB ) => { | ||
const menuADate = new Date( menuA.date ); | ||
const menuBDate = new Date( menuB.date ); | ||
return menuADate.getTime() < menuBDate.getTime(); | ||
} ); | ||
|
||
/** | ||
* This fallback displays (both in editor and on front) | ||
* a list of pages only if no menu (user assigned or | ||
|
@@ -269,7 +274,12 @@ function Navigation( { | |
*/ | ||
__unstableMarkNextChangeAsNotPersistent(); | ||
setRef( fallbackNavigationMenus[ 0 ].id ); | ||
}, [ navigationMenus ] ); | ||
}, [ | ||
ref, | ||
isCreatingNavigationMenu, | ||
fallbackNavigationMenus, | ||
getdave marked this conversation as resolved.
Show resolved
Hide resolved
|
||
hasUncontrolledInnerBlocks, | ||
] ); | ||
|
||
useEffect( () => { | ||
if ( | ||
|
@@ -680,7 +690,7 @@ function Navigation( { | |
/> | ||
); | ||
|
||
if ( hasUnsavedBlocks ) { | ||
if ( hasUnsavedBlocks && ! isCreatingNavigationMenu ) { | ||
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. Addition of |
||
return ( | ||
<TagName { ...blockProps }> | ||
<InspectorControls> | ||
|
@@ -744,24 +754,11 @@ function Navigation( { | |
overlayTextColor={ overlayTextColor } | ||
> | ||
<UnsavedInnerBlocks | ||
createNavigationMenu={ createNavigationMenu } | ||
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. Here's where we pass down the primary creation function. |
||
blocks={ uncontrolledInnerBlocks } | ||
clientId={ clientId } | ||
templateLock={ templateLock } | ||
navigationMenus={ navigationMenus } | ||
hasSelection={ isSelected || isInnerBlockSelected } | ||
hasSavedUnsavedInnerBlocks={ | ||
hasSavedUnsavedInnerBlocks | ||
} | ||
onSave={ ( post ) => { | ||
// Set some state used as a guard to prevent the creation of multiple posts. | ||
setHasSavedUnsavedInnerBlocks( true ); | ||
// Switch to using the wp_navigation entity. | ||
setRef( post.id ); | ||
|
||
showNavigationMenuStatusNotice( | ||
__( `New Navigation Menu created.` ) | ||
); | ||
} } | ||
Comment on lines
-755
to
-764
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. This is now all defer to the centralised logic within the |
||
/> | ||
</ResponsiveWrapper> | ||
</TagName> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
* WordPress dependencies | ||
*/ | ||
import { useInnerBlocksProps } from '@wordpress/block-editor'; | ||
import { Disabled, Spinner } from '@wordpress/components'; | ||
import { Disabled } from '@wordpress/components'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { useContext, useEffect, useRef, useMemo } from '@wordpress/element'; | ||
|
@@ -11,7 +11,6 @@ import { useContext, useEffect, useRef, useMemo } from '@wordpress/element'; | |
* Internal dependencies | ||
*/ | ||
import useNavigationMenu from '../use-navigation-menu'; | ||
import useCreateNavigationMenu from './use-create-navigation-menu'; | ||
|
||
const EMPTY_OBJECT = {}; | ||
const DRAFT_MENU_PARAMS = [ | ||
|
@@ -38,9 +37,8 @@ const ALLOWED_BLOCKS = [ | |
|
||
export default function UnsavedInnerBlocks( { | ||
blocks, | ||
clientId, | ||
hasSavedUnsavedInnerBlocks, | ||
onSave, | ||
createNavigationMenu, | ||
|
||
hasSelection, | ||
} ) { | ||
const originalBlocks = useRef(); | ||
|
@@ -75,7 +73,6 @@ export default function UnsavedInnerBlocks( { | |
// The block will be disabled in a block preview, use this as a way of | ||
// avoiding the side-effects of this component for block previews. | ||
const isDisabled = useContext( Disabled.Context ); | ||
const savingLock = useRef( false ); | ||
|
||
const innerBlocksProps = useInnerBlocksProps( | ||
{ | ||
|
@@ -121,9 +118,6 @@ export default function UnsavedInnerBlocks( { | |
|
||
const { hasResolvedNavigationMenus, navigationMenus } = useNavigationMenu(); | ||
|
||
const { create: createNavigationMenu } = | ||
useCreateNavigationMenu( clientId ); | ||
|
||
// Automatically save the uncontrolled blocks. | ||
useEffect( () => { | ||
// The block will be disabled when used in a BlockPreview. | ||
|
@@ -140,9 +134,7 @@ export default function UnsavedInnerBlocks( { | |
// which is an indication they want to start editing. | ||
if ( | ||
isDisabled || | ||
hasSavedUnsavedInnerBlocks || | ||
isSaving || | ||
savingLock.current || | ||
! hasResolvedDraftNavigationMenus || | ||
! hasResolvedNavigationMenus || | ||
! hasSelection || | ||
|
@@ -151,11 +143,7 @@ export default function UnsavedInnerBlocks( { | |
return; | ||
} | ||
|
||
savingLock.current = true; | ||
createNavigationMenu( null, blocks ).then( ( menu ) => { | ||
onSave( menu ); | ||
savingLock.current = false; | ||
} ); | ||
createNavigationMenu( null, blocks ); | ||
}, [ | ||
isDisabled, | ||
isSaving, | ||
|
@@ -170,17 +158,5 @@ export default function UnsavedInnerBlocks( { | |
|
||
const Wrapper = isSaving ? Disabled : 'div'; | ||
|
||
return ( | ||
<> | ||
{ isSaving ? ( | ||
<Spinner | ||
className={ | ||
'wp-block-navigation__uncontrolled-inner-blocks-loading-indicator' | ||
} | ||
/> | ||
) : ( | ||
<Wrapper { ...innerBlocksProps } /> | ||
) } | ||
Comment on lines
-175
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. We no longer require this logic as the component will not be rendered when creating a menu. |
||
</> | ||
); | ||
return <Wrapper { ...innerBlocksProps } />; | ||
} |
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.
We shouldn't need this state in the parent.
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.
Yeah, agree I don't think it's needed. I think it was only there to support the previous behavior where a menu would be created whenever
UnsavedInnerBlocks
mounted. From memory there where issues whereUnsavedInnerBlocks
would be unmounted and remounted and could cause duplicate menus to be created—the parent block needed a way to guard the menu creation process.Now this only happens when a user selects/modifies the inner blocks, and I don't think as much guarding is needed. 👍
I think it's worth giving this a lot of manual testing to be sure, as it's hard to reason about some aspects of the nav block code and also core data resolution.