Skip to content

Commit

Permalink
Revise getPostMetaChanges to use existing deep comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiomorales committed Jun 29, 2024
1 parent b4405bd commit a9b7067
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function getTranslation( key ) {
* @param {string} parentPath A key/value pair object of block names and their rendered titles.
* @return {string[]} An array of paths whose values have changed.
*/
function deepCompare( changedObject, originalObject, parentPath = '' ) {
export function deepCompare( changedObject, originalObject, parentPath = '' ) {
// We have two non-object values to compare.
if ( ! isObject( changedObject ) && ! isObject( originalObject ) ) {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,13 @@ export default function EntityRecordItem( { record, checked, onChange } ) {
onChange={ onChange }
/>
</PanelRow>
{ Object.keys( postMetaChanges ).length > 0 && (
{ postMetaChanges.length > 0 && (
<div className="entities-saved-states__changes">
<p>Post Meta</p>
<ul>
{ Object.keys( postMetaChanges ).map(
( postMetaKey ) => (
<li key={ postMetaKey }>{ postMetaKey }</li>
)
) }
{ postMetaChanges.map( ( postMeta ) => (
<li key={ postMeta }>{ postMeta }</li>
) ) }
</ul>
</div>
) }
Expand Down
3 changes: 1 addition & 2 deletions packages/editor/src/components/post-publish-button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ export class PostPublishButton extends Component {
// TODO: Explore how to manage `getPostMetaChanges` and pre-publish workflow properly.
if (
( hasNonPostEntityChanges ||
( Object.keys( postMetaChanges ).length > 0 &&
isPublished ) ) &&
( postMetaChanges.length > 0 && isPublished ) ) &&
setEntitiesSavedStatesCallback
) {
// The modal for multiple entity saving will open,
Expand Down
5 changes: 1 addition & 4 deletions packages/editor/src/components/save-publish-panels/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ export default function SavePublishPanels( {
PostPublishExtension={ PluginPostPublishPanel.Slot }
/>
);
} else if (
hasNonPostEntityChanges ||
Object.keys( postMetaChanges ).length > 0
) {
} else if ( hasNonPostEntityChanges || postMetaChanges.length > 0 ) {
unmountableContent = (
<div className="editor-layout__toggle-entities-saved-states-panel">
<Button
Expand Down
16 changes: 3 additions & 13 deletions packages/editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import { TEMPLATE_PART_POST_TYPE } from './constants';
import { getFilteredTemplatePartBlocks } from './utils/get-filtered-template-parts';
import { getEntityActions as _getEntityActions } from '../dataviews/store/private-selectors';
import { deepCompare } from '../../../block-editor/src/components/global-styles/get-global-styles-changes';

const EMPTY_INSERTION_POINT = {
rootClientId: undefined,
Expand Down Expand Up @@ -165,21 +166,10 @@ export const getPostMetaChanges = createRegistrySelector(
);

if ( ! original?.meta || ! edits?.meta ) {
return {};
return [];
}

const keys = Object.keys( original.meta );
const differences = {};
for ( const key of keys ) {
if ( key === 'footnotes' ) {
continue;
}
if ( original.meta[ key ] !== edits.meta[ key ] ) {
differences[ key ] = edits.meta[ key ];
}
}

return differences;
return deepCompare( edits.meta, original.meta );
}
);

Expand Down

0 comments on commit a9b7067

Please sign in to comment.