Skip to content

Commit

Permalink
Add rough prototype showing meta in entities panel
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiomorales committed May 21, 2024
1 parent 995607a commit 4b9bbd7
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
4 changes: 4 additions & 0 deletions docs/reference-guides/data/data-core-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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( [] );

Expand Down Expand Up @@ -72,6 +86,7 @@ export const useIsDirty = () => {

return {
dirtyEntityRecords,
metaRecords,
isDirty,
setUnselectedEntities,
unselectedEntities,
Expand Down
3 changes: 3 additions & 0 deletions packages/editor/src/components/entities-saved-states/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function EntitiesSavedStatesExtensible( {
saveLabel = __( 'Save' ),
renderDialog = undefined,
dirtyEntityRecords,
metaRecords,
isDirty,
setUnselectedEntities,
unselectedEntities,
Expand All @@ -64,6 +65,8 @@ export function EntitiesSavedStatesExtensible( {
return acc;
}, {} );

partitionedSavables.meta = metaRecords[ 0 ];

// Sort entity groups.
const {
site: siteSavables,
Expand Down
16 changes: 13 additions & 3 deletions packages/editor/src/components/post-publish-button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -209,6 +216,7 @@ export default compose( [
getCurrentPostType,
getCurrentPostId,
hasNonPostEntityChanges,
getPostEntityBlockMetadataChanges,
isSavingNonPostEntityChanges,
getEditedPostAttribute,
getPostEdits,
Expand All @@ -229,6 +237,8 @@ export default compose( [
postStatus: getEditedPostAttribute( 'status' ),
postStatusHasChanged: getPostEdits()?.status,
hasNonPostEntityChanges: hasNonPostEntityChanges(),
getPostEntityBlockMetadataChanges:
getPostEntityBlockMetadataChanges(),
isSavingNonPostEntityChanges: isSavingNonPostEntityChanges(),
};
} ),
Expand Down
13 changes: 13 additions & 0 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit 4b9bbd7

Please sign in to comment.