|
1 |
| -/** |
2 |
| - * WordPress dependencies |
3 |
| - */ |
4 |
| -import { RichTextData } from '@wordpress/rich-text'; |
5 |
| - |
6 | 1 | /**
|
7 | 2 | * Helper util to return a value from a certain path of the object.
|
8 | 3 | * Path is specified as either:
|
@@ -53,81 +48,3 @@ export function matchesAttributes( blockAttributes, variationAttributes ) {
|
53 | 48 |
|
54 | 49 | return blockAttributes === variationAttributes;
|
55 | 50 | }
|
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