Skip to content

Commit 6bbb713

Browse files
committed
Wipe traces of getBlockTypeActiveVariation
1 parent c3ca16c commit 6bbb713

File tree

3 files changed

+54
-104
lines changed

3 files changed

+54
-104
lines changed

packages/blocks/README.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -190,24 +190,6 @@ _Returns_
190190

191191
- `?Object`: Block type.
192192

193-
### getBlockTypeActiveVariation
194-
195-
Returns the active block variation for a given block based on its attributes. Variations are determined by their `isActive` property. Which is either an array of block attribute keys or a function.
196-
197-
In case of an array of block attribute keys, the `attributes` are compared to the variation's attributes using strict equality check.
198-
199-
In case of function type, the function should accept a block's attributes and the variation's attributes and determines if a variation is active. A function that accepts a block's attributes and the variation's attributes and determines if a variation is active.
200-
201-
_Parameters_
202-
203-
- _variations_ `Array`: Data state.
204-
- _blockType_ `Object`: Name of block (example: “core/columns”).
205-
- _attributes_ `Object`: Block attributes used to determine active variation.
206-
207-
_Returns_
208-
209-
- `(WPBlockVariation|undefined)`: Active block variation.
210-
211193
### getBlockTypes
212194

213195
Returns all registered blocks.

packages/blocks/src/store/selectors.js

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import removeAccents from 'remove-accents';
77
* WordPress dependencies
88
*/
99
import { createSelector } from '@wordpress/data';
10+
import { RichTextData } from '@wordpress/rich-text';
1011

1112
/**
1213
* Internal dependencies
1314
*/
14-
import { getValueFromObjectPath, getBlockTypeActiveVariation } from './utils';
15+
import { getValueFromObjectPath, matchesAttributes } from './utils';
1516

1617
/** @typedef {import('../api/registration').WPBlockVariation} WPBlockVariation */
1718
/** @typedef {import('../api/registration').WPBlockVariationScope} WPBlockVariationScope */
@@ -173,6 +174,7 @@ export function getBlockStyles( state, name ) {
173174
export const getBlockVariations = createSelector(
174175
( state, blockName, scope ) => {
175176
const variations = state.blockVariations[ blockName ];
177+
176178
if ( ! variations || ! scope ) {
177179
return variations;
178180
}
@@ -241,8 +243,57 @@ export function getActiveBlockVariation( state, blockName, attributes, scope ) {
241243
}
242244

243245
const blockType = getBlockType( state, blockName );
244-
245-
return getBlockTypeActiveVariation( variations, blockType, attributes );
246+
const attributeKeys = Object.keys( blockType?.attributes || {} );
247+
let match;
248+
let maxMatchedAttributes = 0;
249+
250+
for ( const variation of variations ) {
251+
if ( Array.isArray( variation.isActive ) ) {
252+
const definedAttributes = variation.isActive.filter(
253+
( attribute ) => {
254+
// We support nested attribute paths, e.g. `layout.type`.
255+
// In this case, we need to check if the part before the
256+
// first dot is a known attribute.
257+
const topLevelAttribute = attribute.split( '.' )[ 0 ];
258+
return attributeKeys.includes( topLevelAttribute );
259+
}
260+
);
261+
const definedAttributesLength = definedAttributes.length;
262+
if ( definedAttributesLength === 0 ) {
263+
continue;
264+
}
265+
const isMatch = definedAttributes.every( ( attribute ) => {
266+
const variationAttributeValue = getValueFromObjectPath(
267+
variation.attributes,
268+
attribute
269+
);
270+
if ( variationAttributeValue === undefined ) {
271+
return false;
272+
}
273+
let blockAttributeValue = getValueFromObjectPath(
274+
attributes,
275+
attribute
276+
);
277+
if ( blockAttributeValue instanceof RichTextData ) {
278+
blockAttributeValue = blockAttributeValue.toHTMLString();
279+
}
280+
return matchesAttributes(
281+
blockAttributeValue,
282+
variationAttributeValue
283+
);
284+
} );
285+
if ( isMatch && definedAttributesLength > maxMatchedAttributes ) {
286+
match = variation;
287+
maxMatchedAttributes = definedAttributesLength;
288+
}
289+
} else if ( variation.isActive?.( attributes, variation.attributes ) ) {
290+
// If isActive is a function, we cannot know how many attributes it matches.
291+
// This means that we cannot compare the specificity of our matches,
292+
// and simply return the best match we have found.
293+
return match || variation;
294+
}
295+
}
296+
return match;
246297
}
247298

248299
/**

packages/blocks/src/store/utils.js

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/**
2-
* WordPress dependencies
3-
*/
4-
import { RichTextData } from '@wordpress/rich-text';
5-
61
/**
72
* Helper util to return a value from a certain path of the object.
83
* Path is specified as either:
@@ -53,81 +48,3 @@ export function matchesAttributes( blockAttributes, variationAttributes ) {
5348

5449
return blockAttributes === variationAttributes;
5550
}
56-
57-
/** @typedef {import('../api/registration').WPBlockVariation} WPBlockVariation */
58-
59-
/**
60-
* Returns the active block variation for a given block based on its attributes.
61-
* Variations are determined by their `isActive` property.
62-
* Which is either an array of block attribute keys or a function.
63-
*
64-
* In case of an array of block attribute keys, the `attributes` are compared
65-
* to the variation's attributes using strict equality check.
66-
*
67-
* In case of function type, the function should accept a block's attributes
68-
* and the variation's attributes and determines if a variation is active.
69-
* A function that accepts a block's attributes and the variation's attributes and determines if a variation is active.
70-
*
71-
* @param {Array} variations Data state.
72-
* @param {Object} blockType Name of block (example: “core/columns”).
73-
* @param {Object} attributes Block attributes used to determine active variation.
74-
*
75-
* @return {(WPBlockVariation|undefined)} Active block variation.
76-
*/
77-
export function getBlockTypeActiveVariation(
78-
variations,
79-
blockType,
80-
attributes
81-
) {
82-
const attributeKeys = Object.keys( blockType?.attributes || {} );
83-
let match;
84-
let maxMatchedAttributes = 0;
85-
86-
for ( const variation of variations ) {
87-
if ( Array.isArray( variation.isActive ) ) {
88-
const definedAttributes = variation.isActive.filter(
89-
( attribute ) => {
90-
// We support nested attribute paths, e.g. `layout.type`.
91-
// In this case, we need to check if the part before the
92-
// first dot is a known attribute.
93-
const topLevelAttribute = attribute.split( '.' )[ 0 ];
94-
return attributeKeys.includes( topLevelAttribute );
95-
}
96-
);
97-
const definedAttributesLength = definedAttributes.length;
98-
if ( definedAttributesLength === 0 ) {
99-
continue;
100-
}
101-
const isMatch = definedAttributes.every( ( attribute ) => {
102-
const variationAttributeValue = getValueFromObjectPath(
103-
variation.attributes,
104-
attribute
105-
);
106-
if ( variationAttributeValue === undefined ) {
107-
return false;
108-
}
109-
let blockAttributeValue = getValueFromObjectPath(
110-
attributes,
111-
attribute
112-
);
113-
if ( blockAttributeValue instanceof RichTextData ) {
114-
blockAttributeValue = blockAttributeValue.toHTMLString();
115-
}
116-
return matchesAttributes(
117-
blockAttributeValue,
118-
variationAttributeValue
119-
);
120-
} );
121-
if ( isMatch && definedAttributesLength > maxMatchedAttributes ) {
122-
match = variation;
123-
maxMatchedAttributes = definedAttributesLength;
124-
}
125-
} else if ( variation.isActive?.( attributes, variation.attributes ) ) {
126-
// If isActive is a function, we cannot know how many attributes it matches.
127-
// This means that we cannot compare the specificity of our matches,
128-
// and simply return the best match we have found.
129-
return match || variation;
130-
}
131-
}
132-
return match;
133-
}

0 commit comments

Comments
 (0)