Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions packages/editor/src/hooks/custom-sources-backwards-compatibility.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { pickBy, mapValues, isEmpty } from 'lodash';
import { mapValues, isEmpty } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -70,18 +70,17 @@ const createWithMetaAttributeSource = ( metaAttributes ) =>
attributes={ mergedAttributes }
setAttributes={ ( nextAttributes ) => {
const nextMeta = Object.fromEntries(
Object.entries(
// Filter to intersection of keys between the updated
// attributes and those with an associated meta key.
pickBy(
nextAttributes,
( value, key ) => metaAttributes[ key ]
Object.entries( nextAttributes ?? {} )

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nextAttributes could theoretically be nullish, so we're falling back to an empty object just in case.

.filter(
// Filter to intersection of keys between the updated
// attributes and those with an associated meta key.
( [ key ] ) => key in metaAttributes
)
).map( ( [ attributeKey, value ] ) => [
// Rename the keys to the expected meta key name.
metaAttributes[ attributeKey ],
value,
] )
.map( ( [ attributeKey, value ] ) => [
// Rename the keys to the expected meta key name.
metaAttributes[ attributeKey ],
value,
] )
);

if ( ! isEmpty( nextMeta ) ) {
Expand All @@ -108,7 +107,11 @@ const createWithMetaAttributeSource = ( metaAttributes ) =>
function shimAttributeSource( settings ) {
/** @type {WPMetaAttributeMapping} */
const metaAttributes = mapValues(
pickBy( settings.attributes, { source: 'meta' } ),
Object.fromEntries(
Object.entries( settings.attributes ?? {} ).filter(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

settings.attributes could theoretically be nullish, so we're falling back to an empty object just in case.

( [ , { source } ] ) => source === 'meta'
)
),
'meta'
);
if ( ! isEmpty( metaAttributes ) ) {
Expand Down