Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Variations: Compare objects based on given properties #62272

Merged
merged 6 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
13 changes: 12 additions & 1 deletion packages/blocks/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { RichTextData } from '@wordpress/rich-text';
/**
* Internal dependencies
*/
import { getValueFromObjectPath } from './utils';
import { getValueFromObjectPath, matchesAttributes } from './utils';

/** @typedef {import('../api/registration').WPBlockVariation} WPBlockVariation */
/** @typedef {import('../api/registration').WPBlockVariationScope} WPBlockVariationScope */
Expand Down Expand Up @@ -277,6 +277,17 @@ export function getActiveBlockVariation( state, blockName, attributes, scope ) {
if ( blockAttributeValue instanceof RichTextData ) {
blockAttributeValue = blockAttributeValue.toHTMLString();
}
// If the attribute value is an object, we need to compare its properties.
Copy link
Member

Choose a reason for hiding this comment

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

How far is it from isShallowEqual from @wordpress/is-shallow-equal at this point?

export default function isShallowEqual( a, b ) {
if ( a && b ) {
if ( a.constructor === Object && b.constructor === Object ) {
return isShallowEqualObjects( a, b );
} else if ( Array.isArray( a ) && Array.isArray( b ) ) {
return isShallowEqualArrays( a, b );
}
}
return a === b;
}

if (
variationAttributeValue !== null &&
typeof variationAttributeValue === 'object' &&
variationAttributeValue.constructor === Object
) {
return matchesAttributes(
blockAttributeValue,
variationAttributeValue
);
}
return variationAttributeValue === blockAttributeValue;
} );
if ( isMatch && definedAttributesLength > maxMatchedAttributes ) {
Expand Down
77 changes: 77 additions & 0 deletions packages/blocks/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,83 @@ describe( 'selectors', () => {
} )
).toEqual( variations[ 1 ] );
} );
it( 'should compare object attributes in the isActive array based on given properties', () => {
const variations = [
{
name: 'variation-1',
attributes: {
firstTestAttribute: {
nestedProperty: 1,
secondNestedProperty: 10,
},
secondTestAttribute: {
nestedProperty: {
firstDeeplyNestedProperty: 'a1',
secondDeeplyNestedProperty: 'a2',
},
},
},
isActive: [
'firstTestAttribute',
'secondTestAttribute.nestedProperty',
],
},
{
name: 'variation-2',
attributes: {
firstTestAttribute: {
nestedProperty: 2,
secondNestedProperty: 20,
},
secondTestAttribute: {
nestedProperty: {
firstDeeplyNestedProperty: 'b1',
secondDeeplyNestedProperty: 'b2',
},
},
},
isActive: [
'firstTestAttribute',
'secondTestAttribute.nestedProperty',
],
},
];
const state =
createBlockVariationsStateWithTestBlockType( variations );

expect(
getActiveBlockVariation( state, blockName, {
firstTestAttribute: {
nestedProperty: 1,
secondNestedProperty: 10,
otherNestedProperty: 5555,
},
secondTestAttribute: {
nestedProperty: {
firstDeeplyNestedProperty: 'a1',
secondDeeplyNestedProperty: 'a2',
otherDeeplyNestedProperty: 'ffff',
},
},
} )
).toEqual( variations[ 0 ] );
expect(
getActiveBlockVariation( state, blockName, {
firstTestAttribute: {
nestedProperty: 2,
secondNestedProperty: 20,
otherNestedProperty: 5555,
},
secondTestAttribute: {
nestedProperty: {
firstDeeplyNestedProperty: 'b1',
secondDeeplyNestedProperty: 'b2',
otherDeeplyNestedProperty: 'ffff',
},
},
} )
).toEqual( variations[ 1 ] );
} );
it( 'should return the active variation based on the given isActive array (multiple values)', () => {
const variations = [
{
Expand Down
30 changes: 30 additions & 0 deletions packages/blocks/src/store/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,33 @@ export const getValueFromObjectPath = ( object, path, defaultValue ) => {
} );
return value ?? defaultValue;
};

ockham marked this conversation as resolved.
Show resolved Hide resolved
function isObject( candidate ) {
return (
typeof candidate === 'object' &&
Copy link
Contributor

Choose a reason for hiding this comment

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

We called it item in Interactivity. I like this nomenclature, maybe we should update to keep consistency in the codebase. Ping @DAreRodz @luisherranz

Copy link
Member

Choose a reason for hiding this comment

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

I need more context. What exactly are we referring to as "item" in the Interactivity API? What is being referred to here?

Copy link
Contributor

Choose a reason for hiding this comment

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

const isObject = ( item: unknown ): item is Record< string, unknown > =>

In Interactivity API, we check that several "items" are a JSON object in the same file (sources, state, config, proxy handlers, etc). So we call the variable to check "item".

"Candidate" seems also a good general definition, and would be ok to use the same name in all functions that are just checking that an item is an object (there are a few of them across the workspace).

Copy link
Member

Choose a reason for hiding this comment

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

Oh, ok. You can change that if you want 🙂

candidate.constructor === Object &&
candidate !== null
);
}

/**
* Determine whether a set of object properties matches a given object.
*
* Given an object of block attributes and an object of variation attributes,
* this function checks recursively whether all the variation attributes are
* present in the block attributes object.
*
* @param {Object} blockAttributes The object to inspect.
* @param {Object} variationAttributes The object of property values to match.
* @return {boolean} Whether the block attributes match the variation attributes.
*/
export function matchesAttributes( blockAttributes, variationAttributes ) {
if ( isObject( blockAttributes ) && isObject( variationAttributes ) ) {
return Object.entries( variationAttributes ).every(
( [ key, value ] ) =>
matchesAttributes( blockAttributes?.[ key ], value )
);
}

return blockAttributes === variationAttributes;
}
Loading