From db7862c1cba2258fcdaa35b8c873acfb9c4176a4 Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Tue, 21 May 2024 04:54:29 +0200 Subject: [PATCH] Add rough prototype showing meta in entities panel --- .../reference-guides/data/data-core-editor.md | 4 +++ packages/core-data/src/selectors.ts | 18 +++++++++++++ .../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 | 13 ++++++++++ 6 files changed, 71 insertions(+), 8 deletions(-) diff --git a/docs/reference-guides/data/data-core-editor.md b/docs/reference-guides/data/data-core-editor.md index a7b5d37da84643..687a8752f9c22b 100644 --- a/docs/reference-guides/data/data-core-editor.md +++ b/docs/reference-guides/data/data-core-editor.md @@ -507,6 +507,10 @@ _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 242d516170d27d..a8512f67c0e5df 100644 --- a/packages/core-data/src/selectors.ts +++ b/packages/core-data/src/selectors.ts @@ -697,6 +697,24 @@ export const __experimentalGetDirtyEntityRecords = createSelector( ( state ) => [ state.entities.records ] ); +/** + * Returns the list of dirty entity records. + * + * @param state State tree. + * + * @return The list of updated records + */ +export const __experimentalGetDirtyEntityRecordsEdits = createSelector( + ( state: State ): any => { + const { + entities: { records }, + } = state; + return records.postType.post.edits; + }, + ( 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 1103dcabe201ae..34d20f7a173c57 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, siteEntityConfig } = useSelect( - ( select ) => { + const { editedEntities, siteEdits, postEdits, 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,6 +43,20 @@ 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( [] ); @@ -72,6 +86,7 @@ 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 1cbbece9618b9b..8b1a4b36e329c6 100644 --- a/packages/editor/src/components/entities-saved-states/index.js +++ b/packages/editor/src/components/entities-saved-states/index.js @@ -48,6 +48,7 @@ export function EntitiesSavedStatesExtensible( { saveLabel = __( 'Save' ), renderDialog = undefined, dirtyEntityRecords, + metaRecords, isDirty, setUnselectedEntities, unselectedEntities, @@ -64,6 +65,8 @@ 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 355986fdf509dd..0f7be7a47f5f39 100644 --- a/packages/editor/src/components/post-publish-button/index.js +++ b/packages/editor/src/components/post-publish-button/index.js @@ -45,14 +45,21 @@ export class PostPublishButton extends Component { createOnClick( callback ) { return ( ...args ) => { - const { hasNonPostEntityChanges, setEntitiesSavedStatesCallback } = - this.props; + const { + hasNonPostEntityChanges, + getPostEntityBlockMetadataChanges, + 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 && setEntitiesSavedStatesCallback ) { + if ( + ( hasNonPostEntityChanges || + getPostEntityBlockMetadataChanges ) && + 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. @@ -209,6 +216,7 @@ export default compose( [ getCurrentPostType, getCurrentPostId, hasNonPostEntityChanges, + getPostEntityBlockMetadataChanges, isSavingNonPostEntityChanges, getEditedPostAttribute, getPostEdits, @@ -229,6 +237,8 @@ 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 e1a6be18e46017..ce881ed972c848 100644 --- a/packages/editor/src/store/selectors.js +++ b/packages/editor/src/store/selectors.js @@ -130,6 +130,19 @@ export const hasNonPostEntityChanges = createRegistrySelector( } ); +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).