From 462c1bc223bd205da70733817d774f67459a7dcc Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Fri, 24 May 2024 17:38:38 +0200 Subject: [PATCH] Simplify detection of metadata changes Removed unnecessary code added in previous commit and instead modified existing functions to add a flag on existing dirtyEntityRecord structures to indicate when metadata changes have been made. --- .../reference-guides/data/data-core-editor.md | 4 -- packages/core-data/src/selectors.ts | 50 ++++++++++++++++--- .../hooks/use-is-dirty.js | 25 ++-------- .../components/entities-saved-states/index.js | 3 -- .../components/post-publish-button/index.js | 16 ++---- packages/editor/src/store/selectors.js | 16 +----- 6 files changed, 52 insertions(+), 62 deletions(-) diff --git a/docs/reference-guides/data/data-core-editor.md b/docs/reference-guides/data/data-core-editor.md index 687a8752f9c22b..a7b5d37da84643 100644 --- a/docs/reference-guides/data/data-core-editor.md +++ b/docs/reference-guides/data/data-core-editor.md @@ -507,10 +507,6 @@ _Returns_ - `Object`: Object of key value pairs comprising unsaved edits. -### getPostEntityBlockMetadataChanges - -Undocumented declaration. - ### getPostLockUser Returns details about the post lock user. diff --git a/packages/core-data/src/selectors.ts b/packages/core-data/src/selectors.ts index a8512f67c0e5df..f153122128a859 100644 --- a/packages/core-data/src/selectors.ts +++ b/packages/core-data/src/selectors.ts @@ -639,6 +639,7 @@ type DirtyEntityRecord = { key: EntityRecordKey; name: string; kind: string; + hasMetaChanges?: boolean; }; /** * Returns the list of dirty entity records. @@ -655,15 +656,46 @@ export const __experimentalGetDirtyEntityRecords = createSelector( const dirtyRecords: DirtyEntityRecord[] = []; Object.keys( records ).forEach( ( kind ) => { Object.keys( records[ kind ] ).forEach( ( name ) => { + const metadataKeys: string[] = []; const primaryKeys = ( Object.keys( records[ kind ][ name ].edits ) as string[] - ).filter( - ( primaryKey ) => - // The entity record must exist (not be deleted), - // and it must have edits. - getEntityRecord( state, kind, name, primaryKey ) && - hasEditsForEntityRecord( state, kind, name, primaryKey ) - ); + ).filter( ( primaryKey ) => { + // The entity record must exist (not be deleted), + // and it must have edits. + const entityRecord = getEntityRecord( + state, + kind, + name, + primaryKey + ); + const nonTransientEdits = getEntityRecordNonTransientEdits( + state, + kind, + name, + primaryKey + ); + + if ( entityRecord ) { + if ( Object.keys( nonTransientEdits ).length > 0 ) { + if ( nonTransientEdits.meta ) { + metadataKeys.push( primaryKey ); + } + return true; + } + + if ( + isSavingEntityRecord( + state, + kind, + name, + primaryKey + ) + ) { + return true; + } + } + return false; + } ); if ( primaryKeys.length ) { const entityConfig = getEntityConfig( state, kind, name ); @@ -686,6 +718,9 @@ export const __experimentalGetDirtyEntityRecords = createSelector( entityConfig?.getTitle?.( entityRecord ) || '', name, kind, + hasMetaChanges: metadataKeys.includes( primaryKey ) + ? true + : false, } ); } ); } @@ -714,7 +749,6 @@ export const __experimentalGetDirtyEntityRecordsEdits = createSelector( ( state ) => [ state.entities.records ] ); - /** * Returns the list of entities currently being saved. * diff --git a/packages/editor/src/components/entities-saved-states/hooks/use-is-dirty.js b/packages/editor/src/components/entities-saved-states/hooks/use-is-dirty.js index 34d20f7a173c57..1103dcabe201ae 100644 --- a/packages/editor/src/components/entities-saved-states/hooks/use-is-dirty.js +++ b/packages/editor/src/components/entities-saved-states/hooks/use-is-dirty.js @@ -6,22 +6,22 @@ import { store as coreStore } from '@wordpress/core-data'; import { useMemo, useState } from '@wordpress/element'; export const useIsDirty = () => { - const { editedEntities, siteEdits, postEdits, siteEntityConfig } = - useSelect( ( select ) => { + const { editedEntities, siteEdits, siteEntityConfig } = useSelect( + ( select ) => { const { __experimentalGetDirtyEntityRecords, getEntityRecordEdits, - __experimentalGetDirtyEntityRecordsEdits, getEntityConfig, } = select( coreStore ); return { editedEntities: __experimentalGetDirtyEntityRecords(), siteEdits: getEntityRecordEdits( 'root', 'site' ), - postEdits: __experimentalGetDirtyEntityRecordsEdits(), siteEntityConfig: getEntityConfig( 'root', 'site' ), }; - }, [] ); + }, + [] + ); const dirtyEntityRecords = useMemo( () => { // Remove site object and decouple into its edited pieces. @@ -43,20 +43,6 @@ export const useIsDirty = () => { return [ ...editedEntitiesWithoutSite, ...editedSiteEntities ]; }, [ editedEntities, siteEdits, siteEntityConfig ] ); - const metaRecords = useMemo( () => { - return Object.keys( postEdits ).map( ( key ) => { - const post = postEdits[ key ]; - return Object.keys( post.meta ).map( ( property ) => { - return { - key: `${ key }_${ property }`, - kind: 'postType', - name: 'post', - title: property, - }; - } ); - } ); - }, [ postEdits ] ); - // Unchecked entities to be ignored by save function. const [ unselectedEntities, _setUnselectedEntities ] = useState( [] ); @@ -86,7 +72,6 @@ export const useIsDirty = () => { return { dirtyEntityRecords, - metaRecords, isDirty, setUnselectedEntities, unselectedEntities, diff --git a/packages/editor/src/components/entities-saved-states/index.js b/packages/editor/src/components/entities-saved-states/index.js index 8b1a4b36e329c6..1cbbece9618b9b 100644 --- a/packages/editor/src/components/entities-saved-states/index.js +++ b/packages/editor/src/components/entities-saved-states/index.js @@ -48,7 +48,6 @@ export function EntitiesSavedStatesExtensible( { saveLabel = __( 'Save' ), renderDialog = undefined, dirtyEntityRecords, - metaRecords, isDirty, setUnselectedEntities, unselectedEntities, @@ -65,8 +64,6 @@ export function EntitiesSavedStatesExtensible( { return acc; }, {} ); - partitionedSavables.meta = metaRecords[ 0 ]; - // Sort entity groups. const { site: siteSavables, diff --git a/packages/editor/src/components/post-publish-button/index.js b/packages/editor/src/components/post-publish-button/index.js index 0f7be7a47f5f39..355986fdf509dd 100644 --- a/packages/editor/src/components/post-publish-button/index.js +++ b/packages/editor/src/components/post-publish-button/index.js @@ -45,21 +45,14 @@ export class PostPublishButton extends Component { createOnClick( callback ) { return ( ...args ) => { - const { - hasNonPostEntityChanges, - getPostEntityBlockMetadataChanges, - setEntitiesSavedStatesCallback, - } = this.props; + const { hasNonPostEntityChanges, setEntitiesSavedStatesCallback } = + this.props; // If a post with non-post entities is published, but the user // elects to not save changes to the non-post entities, those // entities will still be dirty when the Publish button is clicked. // We also need to check that the `setEntitiesSavedStatesCallback` // prop was passed. See https://github.com/WordPress/gutenberg/pull/37383 - if ( - ( hasNonPostEntityChanges || - getPostEntityBlockMetadataChanges ) && - setEntitiesSavedStatesCallback - ) { + if ( hasNonPostEntityChanges && setEntitiesSavedStatesCallback ) { // The modal for multiple entity saving will open, // hold the callback for saving/publishing the post // so that we can call it if the post entity is checked. @@ -216,7 +209,6 @@ export default compose( [ getCurrentPostType, getCurrentPostId, hasNonPostEntityChanges, - getPostEntityBlockMetadataChanges, isSavingNonPostEntityChanges, getEditedPostAttribute, getPostEdits, @@ -237,8 +229,6 @@ export default compose( [ postStatus: getEditedPostAttribute( 'status' ), postStatusHasChanged: getPostEdits()?.status, hasNonPostEntityChanges: hasNonPostEntityChanges(), - getPostEntityBlockMetadataChanges: - getPostEntityBlockMetadataChanges(), isSavingNonPostEntityChanges: isSavingNonPostEntityChanges(), }; } ), diff --git a/packages/editor/src/store/selectors.js b/packages/editor/src/store/selectors.js index ce881ed972c848..08d6dc66142406 100644 --- a/packages/editor/src/store/selectors.js +++ b/packages/editor/src/store/selectors.js @@ -125,24 +125,12 @@ export const hasNonPostEntityChanges = createRegistrySelector( ( entityRecord ) => entityRecord.kind !== 'postType' || entityRecord.name !== type || - entityRecord.key !== id + entityRecord.key !== id || + entityRecord.hasMetaChanges ); } ); -export const getPostEntityBlockMetadataChanges = createRegistrySelector( - ( select ) => ( state ) => { - const postEdits = - select( coreStore ).__experimentalGetDirtyEntityRecordsEdits(); - - const id = getCurrentPostId( state ); - - if ( postEdits && postEdits[ id ]?.meta ) { - return postEdits[ id ].meta; - } - } -); - /** * Returns true if there are no unsaved values for the current edit session and * if the currently edited post is new (has never been saved before).