-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
style.js
474 lines (432 loc) · 12.2 KB
/
style.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';
import {
getBlockSupport,
hasBlockSupport,
__EXPERIMENTAL_ELEMENTS as ELEMENTS,
} from '@wordpress/blocks';
import { useInstanceId } from '@wordpress/compose';
import { getCSSRules, compileCSS } from '@wordpress/style-engine';
/**
* Internal dependencies
*/
import { BACKGROUND_SUPPORT_KEY, BackgroundImagePanel } from './background';
import { BORDER_SUPPORT_KEY, BorderPanel, SHADOW_SUPPORT_KEY } from './border';
import { COLOR_SUPPORT_KEY, ColorEdit } from './color';
import {
TypographyPanel,
TYPOGRAPHY_SUPPORT_KEY,
TYPOGRAPHY_SUPPORT_KEYS,
} from './typography';
import {
DIMENSIONS_SUPPORT_KEY,
SPACING_SUPPORT_KEY,
DimensionsPanel,
} from './dimensions';
import {
shouldSkipSerialization,
useStyleOverride,
useBlockSettings,
} from './utils';
import { scopeSelector } from '../components/global-styles/utils';
import { useBlockEditingMode } from '../components/block-editing-mode';
const styleSupportKeys = [
...TYPOGRAPHY_SUPPORT_KEYS,
BORDER_SUPPORT_KEY,
COLOR_SUPPORT_KEY,
DIMENSIONS_SUPPORT_KEY,
BACKGROUND_SUPPORT_KEY,
SPACING_SUPPORT_KEY,
SHADOW_SUPPORT_KEY,
];
const hasStyleSupport = ( nameOrType ) =>
styleSupportKeys.some( ( key ) => hasBlockSupport( nameOrType, key ) );
/**
* Returns the inline styles to add depending on the style object
*
* @param {Object} styles Styles configuration.
*
* @return {Object} Flattened CSS variables declaration.
*/
export function getInlineStyles( styles = {} ) {
const output = {};
// The goal is to move everything to server side generated engine styles
// This is temporary as we absorb more and more styles into the engine.
getCSSRules( styles ).forEach( ( rule ) => {
output[ rule.key ] = rule.value;
} );
return output;
}
/**
* Filters registered block settings, extending attributes to include `style` attribute.
*
* @param {Object} settings Original block settings.
*
* @return {Object} Filtered block settings.
*/
function addAttribute( settings ) {
if ( ! hasStyleSupport( settings ) ) {
return settings;
}
// Allow blocks to specify their own attribute definition with default values if needed.
if ( ! settings.attributes.style ) {
Object.assign( settings.attributes, {
style: {
type: 'object',
},
} );
}
return settings;
}
/**
* A dictionary of paths to flag skipping block support serialization as the key,
* with values providing the style paths to be omitted from serialization.
*
* @constant
* @type {Record<string, string[]>}
*/
const skipSerializationPathsEdit = {
[ `${ BORDER_SUPPORT_KEY }.__experimentalSkipSerialization` ]: [ 'border' ],
[ `${ COLOR_SUPPORT_KEY }.__experimentalSkipSerialization` ]: [
COLOR_SUPPORT_KEY,
],
[ `${ TYPOGRAPHY_SUPPORT_KEY }.__experimentalSkipSerialization` ]: [
TYPOGRAPHY_SUPPORT_KEY,
],
[ `${ DIMENSIONS_SUPPORT_KEY }.__experimentalSkipSerialization` ]: [
DIMENSIONS_SUPPORT_KEY,
],
[ `${ SPACING_SUPPORT_KEY }.__experimentalSkipSerialization` ]: [
SPACING_SUPPORT_KEY,
],
[ `${ SHADOW_SUPPORT_KEY }.__experimentalSkipSerialization` ]: [
SHADOW_SUPPORT_KEY,
],
};
/**
* A dictionary of paths to flag skipping block support serialization as the key,
* with values providing the style paths to be omitted from serialization.
*
* Extends the Edit skip paths to enable skipping additional paths in just
* the Save component. This allows a block support to be serialized within the
* editor, while using an alternate approach, such as server-side rendering, when
* the support is saved.
*
* @constant
* @type {Record<string, string[]>}
*/
const skipSerializationPathsSave = {
...skipSerializationPathsEdit,
[ `${ DIMENSIONS_SUPPORT_KEY }.aspectRatio` ]: [
`${ DIMENSIONS_SUPPORT_KEY }.aspectRatio`,
], // Skip serialization of aspect ratio in save mode.
[ `${ BACKGROUND_SUPPORT_KEY }` ]: [ BACKGROUND_SUPPORT_KEY ], // Skip serialization of background support in save mode.
};
const skipSerializationPathsSaveChecks = {
[ `${ DIMENSIONS_SUPPORT_KEY }.aspectRatio` ]: true,
[ `${ BACKGROUND_SUPPORT_KEY }` ]: true,
};
/**
* A dictionary used to normalize feature names between support flags, style
* object properties and __experimentSkipSerialization configuration arrays.
*
* This allows not having to provide a migration for a support flag and possible
* backwards compatibility bridges, while still achieving consistency between
* the support flag and the skip serialization array.
*
* @constant
* @type {Record<string, string>}
*/
const renamedFeatures = { gradients: 'gradient' };
/**
* A utility function used to remove one or more paths from a style object.
* Works in a way similar to Lodash's `omit()`. See unit tests and examples below.
*
* It supports a single string path:
*
* ```
* omitStyle( { color: 'red' }, 'color' ); // {}
* ```
*
* or an array of paths:
*
* ```
* omitStyle( { color: 'red', background: '#fff' }, [ 'color', 'background' ] ); // {}
* ```
*
* It also allows you to specify paths at multiple levels in a string.
*
* ```
* omitStyle( { typography: { textDecoration: 'underline' } }, 'typography.textDecoration' ); // {}
* ```
*
* You can remove multiple paths at the same time:
*
* ```
* omitStyle(
* {
* typography: {
* textDecoration: 'underline',
* textTransform: 'uppercase',
* }
* },
* [
* 'typography.textDecoration',
* 'typography.textTransform',
* ]
* );
* // {}
* ```
*
* You can also specify nested paths as arrays:
*
* ```
* omitStyle(
* {
* typography: {
* textDecoration: 'underline',
* textTransform: 'uppercase',
* }
* },
* [
* [ 'typography', 'textDecoration' ],
* [ 'typography', 'textTransform' ],
* ]
* );
* // {}
* ```
*
* With regards to nesting of styles, infinite depth is supported:
*
* ```
* omitStyle(
* {
* border: {
* radius: {
* topLeft: '10px',
* topRight: '0.5rem',
* }
* }
* },
* [
* [ 'border', 'radius', 'topRight' ],
* ]
* );
* // { border: { radius: { topLeft: '10px' } } }
* ```
*
* The third argument, `preserveReference`, defines how to treat the input style object.
* It is mostly necessary to properly handle mutation when recursively handling the style object.
* Defaulting to `false`, this will always create a new object, avoiding to mutate `style`.
* However, when recursing, we change that value to `true` in order to work with a single copy
* of the original style object.
*
* @see https://lodash.com/docs/4.17.15#omit
*
* @param {Object} style Styles object.
* @param {Array|string} paths Paths to remove.
* @param {boolean} preserveReference True to mutate the `style` object, false otherwise.
* @return {Object} Styles object with the specified paths removed.
*/
export function omitStyle( style, paths, preserveReference = false ) {
if ( ! style ) {
return style;
}
let newStyle = style;
if ( ! preserveReference ) {
newStyle = JSON.parse( JSON.stringify( style ) );
}
if ( ! Array.isArray( paths ) ) {
paths = [ paths ];
}
paths.forEach( ( path ) => {
if ( ! Array.isArray( path ) ) {
path = path.split( '.' );
}
if ( path.length > 1 ) {
const [ firstSubpath, ...restPath ] = path;
omitStyle( newStyle[ firstSubpath ], [ restPath ], true );
} else if ( path.length === 1 ) {
delete newStyle[ path[ 0 ] ];
}
} );
return newStyle;
}
/**
* Override props assigned to save component to inject the CSS variables definition.
*
* @param {Object} props Additional props applied to save element.
* @param {Object|string} blockNameOrType Block type.
* @param {Object} attributes Block attributes.
* @param {?Record<string, string[]>} skipPaths An object of keys and paths to skip serialization.
*
* @return {Object} Filtered props applied to save element.
*/
export function addSaveProps(
props,
blockNameOrType,
attributes,
skipPaths = skipSerializationPathsSave
) {
if ( ! hasStyleSupport( blockNameOrType ) ) {
return props;
}
let { style } = attributes;
Object.entries( skipPaths ).forEach( ( [ indicator, path ] ) => {
const skipSerialization =
skipSerializationPathsSaveChecks[ indicator ] ||
getBlockSupport( blockNameOrType, indicator );
if ( skipSerialization === true ) {
style = omitStyle( style, path );
}
if ( Array.isArray( skipSerialization ) ) {
skipSerialization.forEach( ( featureName ) => {
const feature = renamedFeatures[ featureName ] || featureName;
style = omitStyle( style, [ [ ...path, feature ] ] );
} );
}
} );
props.style = {
...getInlineStyles( style ),
...props.style,
};
return props;
}
function BlockStyleControls( {
clientId,
name,
setAttributes,
__unstableParentLayout,
} ) {
const settings = useBlockSettings( name, __unstableParentLayout );
const blockEditingMode = useBlockEditingMode();
const passedProps = {
clientId,
name,
setAttributes,
settings: {
...settings,
typography: {
...settings.typography,
// The text alignment UI for individual blocks is rendered in
// the block toolbar, so disable it here.
textAlign: false,
},
},
};
if ( blockEditingMode !== 'default' ) {
return null;
}
return (
<>
<ColorEdit { ...passedProps } />
<BackgroundImagePanel { ...passedProps } />
<TypographyPanel { ...passedProps } />
<BorderPanel { ...passedProps } />
<DimensionsPanel { ...passedProps } />
</>
);
}
export default {
edit: BlockStyleControls,
hasSupport: hasStyleSupport,
addSaveProps,
attributeKeys: [ 'style' ],
useBlockProps,
};
// Defines which element types are supported, including their hover styles or
// any other elements that have been included under a single element type
// e.g. heading and h1-h6.
const elementTypes = [
{ elementType: 'button' },
{ elementType: 'link', pseudo: [ ':hover' ] },
{
elementType: 'heading',
elements: [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ],
},
];
function useBlockProps( { name, style } ) {
const blockElementsContainerIdentifier = `wp-elements-${ useInstanceId(
useBlockProps
) }`;
// The .editor-styles-wrapper selector is required on elements styles. As it is
// added to all other editor styles, not providing it causes reset and global
// styles to override element styles because of higher specificity.
const baseElementSelector = `.editor-styles-wrapper .${ blockElementsContainerIdentifier }`;
const blockElementStyles = style?.elements;
const styles = useMemo( () => {
if ( ! blockElementStyles ) {
return;
}
const elementCSSRules = [];
elementTypes.forEach( ( { elementType, pseudo, elements } ) => {
const skipSerialization = shouldSkipSerialization(
name,
COLOR_SUPPORT_KEY,
elementType
);
if ( skipSerialization ) {
return;
}
const elementStyles = blockElementStyles?.[ elementType ];
// Process primary element type styles.
if ( elementStyles ) {
const selector = scopeSelector(
baseElementSelector,
ELEMENTS[ elementType ]
);
elementCSSRules.push(
compileCSS( elementStyles, { selector } )
);
// Process any interactive states for the element type.
if ( pseudo ) {
pseudo.forEach( ( pseudoSelector ) => {
if ( elementStyles[ pseudoSelector ] ) {
elementCSSRules.push(
compileCSS( elementStyles[ pseudoSelector ], {
selector: scopeSelector(
baseElementSelector,
`${ ELEMENTS[ elementType ] }${ pseudoSelector }`
),
} )
);
}
} );
}
}
// Process related elements e.g. h1-h6 for headings
if ( elements ) {
elements.forEach( ( element ) => {
if ( blockElementStyles[ element ] ) {
elementCSSRules.push(
compileCSS( blockElementStyles[ element ], {
selector: scopeSelector(
baseElementSelector,
ELEMENTS[ element ]
),
} )
);
}
} );
}
} );
return elementCSSRules.length > 0
? elementCSSRules.join( '' )
: undefined;
}, [ baseElementSelector, blockElementStyles, name ] );
useStyleOverride( { css: styles } );
return addSaveProps(
{ className: blockElementsContainerIdentifier },
name,
{ style },
skipSerializationPathsEdit
);
}
addFilter(
'blocks.registerBlockType',
'core/style/addAttribute',
addAttribute
);