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: Detect active variation correctly based on RichText attribute #62325

Merged
merged 4 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
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
17 changes: 11 additions & 6 deletions packages/blocks/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import removeAccents from 'remove-accents';
* WordPress dependencies
*/
import { createSelector } from '@wordpress/data';
import { RichTextData } from '@wordpress/rich-text';

/**
* Internal dependencies
Expand Down Expand Up @@ -262,17 +263,21 @@ export function getActiveBlockVariation( state, blockName, attributes, scope ) {
continue;
}
const isMatch = definedAttributes.every( ( attribute ) => {
const attributeValue = getValueFromObjectPath(
attributes,
const variationAttributeValue = getValueFromObjectPath(
variation.attributes,
attribute
);
if ( attributeValue === undefined ) {
if ( variationAttributeValue === undefined ) {
return false;
}
return (
attributeValue ===
getValueFromObjectPath( variation.attributes, attribute )
let blockAttributeValue = getValueFromObjectPath(
attributes,
attribute
);
if ( blockAttributeValue instanceof RichTextData ) {
blockAttributeValue = blockAttributeValue.toHTMLString();
}
return variationAttributeValue === blockAttributeValue;
} );
if ( isMatch && definedAttributesLength > maxMatchedAttributes ) {
match = variation;
Expand Down
42 changes: 42 additions & 0 deletions packages/blocks/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import {
getActiveBlockVariation,
} from '../selectors';

/**
* WordPress dependencies
*/
import { RichTextData } from '@wordpress/rich-text';

const keyBlocksByName = ( blocks ) =>
blocks.reduce(
( result, block ) => ( { ...result, [ block.name ]: block } ),
Expand Down Expand Up @@ -452,6 +457,43 @@ describe( 'selectors', () => {
} )
).toEqual( variations[ 1 ] );
} );
it( 'should support RichText attributes in the isActive array', () => {
const variations = [
{
name: 'variation-1',
attributes: {
firstTestAttribute:
'This is a <strong>RichText</strong> attribute.',
},
isActive: [ 'firstTestAttribute' ],
},
{
name: 'variation-2',
attributes: {
firstTestAttribute:
'This is a <em>RichText</em> attribute.',
},
isActive: [ 'firstTestAttribute' ],
},
];
const state =
createBlockVariationsStateWithTestBlockType( variations );

expect(
getActiveBlockVariation( state, blockName, {
firstTestAttribute: RichTextData.fromHTMLString(
'This is a <strong>RichText</strong> attribute.'
),
} )
).toEqual( variations[ 0 ] );
expect(
getActiveBlockVariation( state, blockName, {
firstTestAttribute: RichTextData.fromHTMLString(
'This is a <em>RichText</em> attribute.'
),
} )
).toEqual( variations[ 1 ] );
} );
it( 'should return the active variation based on the given isActive array (multiple values)', () => {
const variations = [
{
Expand Down
Loading