-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathutils.js
420 lines (393 loc) · 11 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/**
* External dependencies
*/
import fastDeepEqual from 'fast-deep-equal/es6';
/**
* Internal dependencies
*/
import {
getTypographyFontSizeValue,
getFluidTypographyOptionsFromSettings,
} from './typography-utils';
import { getValueFromObjectPath } from '../../utils/object';
/* Supporting data. */
export const ROOT_BLOCK_NAME = 'root';
export const ROOT_BLOCK_SELECTOR = 'body';
export const ROOT_BLOCK_SUPPORTS = [
'background',
'backgroundColor',
'color',
'linkColor',
'captionColor',
'buttonColor',
'headingColor',
'fontFamily',
'fontSize',
'fontStyle',
'fontWeight',
'lineHeight',
'textDecoration',
'textTransform',
'padding',
];
export const PRESET_METADATA = [
{
path: [ 'color', 'palette' ],
valueKey: 'color',
cssVarInfix: 'color',
classes: [
{ classSuffix: 'color', propertyName: 'color' },
{
classSuffix: 'background-color',
propertyName: 'background-color',
},
{
classSuffix: 'border-color',
propertyName: 'border-color',
},
],
},
{
path: [ 'color', 'gradients' ],
valueKey: 'gradient',
cssVarInfix: 'gradient',
classes: [
{
classSuffix: 'gradient-background',
propertyName: 'background',
},
],
},
{
path: [ 'color', 'duotone' ],
valueKey: 'colors',
cssVarInfix: 'duotone',
valueFunc: ( { slug } ) => `url( '#wp-duotone-${ slug }' )`,
classes: [],
},
{
path: [ 'shadow', 'presets' ],
valueKey: 'shadow',
cssVarInfix: 'shadow',
classes: [],
},
{
path: [ 'typography', 'fontSizes' ],
valueFunc: ( preset, settings ) =>
getTypographyFontSizeValue(
preset,
getFluidTypographyOptionsFromSettings( settings )
),
valueKey: 'size',
cssVarInfix: 'font-size',
classes: [ { classSuffix: 'font-size', propertyName: 'font-size' } ],
},
{
path: [ 'typography', 'fontFamilies' ],
valueKey: 'fontFamily',
cssVarInfix: 'font-family',
classes: [
{ classSuffix: 'font-family', propertyName: 'font-family' },
],
},
{
path: [ 'spacing', 'spacingSizes' ],
valueKey: 'size',
cssVarInfix: 'spacing',
valueFunc: ( { size } ) => size,
classes: [],
},
];
export const STYLE_PATH_TO_CSS_VAR_INFIX = {
'color.background': 'color',
'color.text': 'color',
'filter.duotone': 'duotone',
'elements.link.color.text': 'color',
'elements.link.:hover.color.text': 'color',
'elements.link.typography.fontFamily': 'font-family',
'elements.link.typography.fontSize': 'font-size',
'elements.button.color.text': 'color',
'elements.button.color.background': 'color',
'elements.caption.color.text': 'color',
'elements.button.typography.fontFamily': 'font-family',
'elements.button.typography.fontSize': 'font-size',
'elements.heading.color': 'color',
'elements.heading.color.background': 'color',
'elements.heading.typography.fontFamily': 'font-family',
'elements.heading.gradient': 'gradient',
'elements.heading.color.gradient': 'gradient',
'elements.h1.color': 'color',
'elements.h1.color.background': 'color',
'elements.h1.typography.fontFamily': 'font-family',
'elements.h1.color.gradient': 'gradient',
'elements.h2.color': 'color',
'elements.h2.color.background': 'color',
'elements.h2.typography.fontFamily': 'font-family',
'elements.h2.color.gradient': 'gradient',
'elements.h3.color': 'color',
'elements.h3.color.background': 'color',
'elements.h3.typography.fontFamily': 'font-family',
'elements.h3.color.gradient': 'gradient',
'elements.h4.color': 'color',
'elements.h4.color.background': 'color',
'elements.h4.typography.fontFamily': 'font-family',
'elements.h4.color.gradient': 'gradient',
'elements.h5.color': 'color',
'elements.h5.color.background': 'color',
'elements.h5.typography.fontFamily': 'font-family',
'elements.h5.color.gradient': 'gradient',
'elements.h6.color': 'color',
'elements.h6.color.background': 'color',
'elements.h6.typography.fontFamily': 'font-family',
'elements.h6.color.gradient': 'gradient',
'color.gradient': 'gradient',
shadow: 'shadow',
'typography.fontSize': 'font-size',
'typography.fontFamily': 'font-family',
};
// A static list of block attributes that store global style preset slugs.
export const STYLE_PATH_TO_PRESET_BLOCK_ATTRIBUTE = {
'color.background': 'backgroundColor',
'color.text': 'textColor',
'color.gradient': 'gradient',
'typography.fontSize': 'fontSize',
'typography.fontFamily': 'fontFamily',
};
function findInPresetsBy(
features,
blockName,
presetPath,
presetProperty,
presetValueValue
) {
// Block presets take priority above root level presets.
const orderedPresetsByOrigin = [
getValueFromObjectPath( features, [
'blocks',
blockName,
...presetPath,
] ),
getValueFromObjectPath( features, presetPath ),
];
for ( const presetByOrigin of orderedPresetsByOrigin ) {
if ( presetByOrigin ) {
// Preset origins ordered by priority.
const origins = [ 'custom', 'theme', 'default' ];
for ( const origin of origins ) {
const presets = presetByOrigin[ origin ];
if ( presets ) {
const presetObject = presets.find(
( preset ) =>
preset[ presetProperty ] === presetValueValue
);
if ( presetObject ) {
if ( presetProperty === 'slug' ) {
return presetObject;
}
// If there is a highest priority preset with the same slug but different value the preset we found was overwritten and should be ignored.
const highestPresetObjectWithSameSlug = findInPresetsBy(
features,
blockName,
presetPath,
'slug',
presetObject.slug
);
if (
highestPresetObjectWithSameSlug[
presetProperty
] === presetObject[ presetProperty ]
) {
return presetObject;
}
return undefined;
}
}
}
}
}
}
export function getPresetVariableFromValue(
features,
blockName,
variableStylePath,
presetPropertyValue
) {
if ( ! presetPropertyValue ) {
return presetPropertyValue;
}
const cssVarInfix = STYLE_PATH_TO_CSS_VAR_INFIX[ variableStylePath ];
const metadata = PRESET_METADATA.find(
( data ) => data.cssVarInfix === cssVarInfix
);
if ( ! metadata ) {
// The property doesn't have preset data
// so the value should be returned as it is.
return presetPropertyValue;
}
const { valueKey, path } = metadata;
const presetObject = findInPresetsBy(
features,
blockName,
path,
valueKey,
presetPropertyValue
);
if ( ! presetObject ) {
// Value wasn't found in the presets,
// so it must be a custom value.
return presetPropertyValue;
}
return `var:preset|${ cssVarInfix }|${ presetObject.slug }`;
}
function getValueFromPresetVariable(
features,
blockName,
variable,
[ presetType, slug ]
) {
const metadata = PRESET_METADATA.find(
( data ) => data.cssVarInfix === presetType
);
if ( ! metadata ) {
return variable;
}
const presetObject = findInPresetsBy(
features.settings,
blockName,
metadata.path,
'slug',
slug
);
if ( presetObject ) {
const { valueKey } = metadata;
const result = presetObject[ valueKey ];
return getValueFromVariable( features, blockName, result );
}
return variable;
}
function getValueFromCustomVariable( features, blockName, variable, path ) {
const result =
getValueFromObjectPath( features.settings, [
'blocks',
blockName,
'custom',
...path,
] ) ??
getValueFromObjectPath( features.settings, [ 'custom', ...path ] );
if ( ! result ) {
return variable;
}
// A variable may reference another variable so we need recursion until we find the value.
return getValueFromVariable( features, blockName, result );
}
/**
* Attempts to fetch the value of a theme.json CSS variable.
*
* @param {Object} features GlobalStylesContext config, e.g., user, base or merged. Represents the theme.json tree.
* @param {string} blockName The name of a block as represented in the styles property. E.g., 'root' for root-level, and 'core/${blockName}' for blocks.
* @param {string|*} variable An incoming style value. A CSS var value is expected, but it could be any value.
* @return {string|*|{ref}} The value of the CSS var, if found. If not found, the passed variable argument.
*/
export function getValueFromVariable( features, blockName, variable ) {
if ( ! variable || typeof variable !== 'string' ) {
if ( variable?.ref && typeof variable?.ref === 'string' ) {
const refPath = variable.ref.split( '.' );
variable = getValueFromObjectPath( features, refPath );
// Presence of another ref indicates a reference to another dynamic value.
// Pointing to another dynamic value is not supported.
if ( ! variable || !! variable?.ref ) {
return variable;
}
} else {
return variable;
}
}
const USER_VALUE_PREFIX = 'var:';
const THEME_VALUE_PREFIX = 'var(--wp--';
const THEME_VALUE_SUFFIX = ')';
let parsedVar;
if ( variable.startsWith( USER_VALUE_PREFIX ) ) {
parsedVar = variable.slice( USER_VALUE_PREFIX.length ).split( '|' );
} else if (
variable.startsWith( THEME_VALUE_PREFIX ) &&
variable.endsWith( THEME_VALUE_SUFFIX )
) {
parsedVar = variable
.slice( THEME_VALUE_PREFIX.length, -THEME_VALUE_SUFFIX.length )
.split( '--' );
} else {
// We don't know how to parse the value: either is raw of uses complex CSS such as `calc(1px * var(--wp--variable) )`
return variable;
}
const [ type, ...path ] = parsedVar;
if ( type === 'preset' ) {
return getValueFromPresetVariable(
features,
blockName,
variable,
path
);
}
if ( type === 'custom' ) {
return getValueFromCustomVariable(
features,
blockName,
variable,
path
);
}
return variable;
}
/**
* Function that scopes a selector with another one. This works a bit like
* SCSS nesting except the `&` operator isn't supported.
*
* @example
* ```js
* const scope = '.a, .b .c';
* const selector = '> .x, .y';
* const merged = scopeSelector( scope, selector );
* // merged is '.a > .x, .a .y, .b .c > .x, .b .c .y'
* ```
*
* @param {string} scope Selector to scope to.
* @param {string} selector Original selector.
*
* @return {string} Scoped selector.
*/
export function scopeSelector( scope, selector ) {
const scopes = scope.split( ',' );
const selectors = selector.split( ',' );
const selectorsScoped = [];
scopes.forEach( ( outer ) => {
selectors.forEach( ( inner ) => {
selectorsScoped.push( `${ outer.trim() } ${ inner.trim() }` );
} );
} );
return selectorsScoped.join( ', ' );
}
/**
* Compares global style variations according to their styles and settings properties.
*
* @example
* ```js
* const globalStyles = { styles: { typography: { fontSize: '10px' } }, settings: {} };
* const variation = { styles: { typography: { fontSize: '10000px' } }, settings: {} };
* const isEqual = areGlobalStyleConfigsEqual( globalStyles, variation );
* // false
* ```
*
* @param {Object} original A global styles object.
* @param {Object} variation A global styles object.
*
* @return {boolean} Whether `original` and `variation` match.
*/
export function areGlobalStyleConfigsEqual( original, variation ) {
if ( typeof original !== 'object' || typeof variation !== 'object' ) {
return original === variation;
}
return (
fastDeepEqual( original?.styles, variation?.styles ) &&
fastDeepEqual( original?.settings, variation?.settings )
);
}