2
2
* External dependencies
3
3
*/
4
4
// eslint-disable-next-line import/no-extraneous-dependencies
5
- import { get , kebabCase , reduce } from 'lodash' ;
5
+ import { kebabCase } from 'lodash' ;
6
6
/**
7
7
* WordPress dependencies
8
8
*/
@@ -14,8 +14,6 @@ import {
14
14
} from '@wordpress/blocks' ;
15
15
import { getCSSRules , getCSSValueFromRawStyle } from '@wordpress/style-engine' ;
16
16
import {
17
- // eslint-disable-next-line @wordpress/no-unsafe-wp-apis
18
- __unstablePresetDuotoneFilter as PresetDuotoneFilter ,
19
17
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
20
18
__experimentalGetGapCSSValue as getGapCSSValue ,
21
19
} from '@wordpress/block-editor' ;
@@ -25,6 +23,7 @@ import {
25
23
import {
26
24
appendToSelector ,
27
25
getBlockStyleVariationSelector ,
26
+ getDuotoneFilter ,
28
27
getResolvedValue ,
29
28
getValueFromObjectPath ,
30
29
LAYOUT_DEFINITIONS ,
@@ -63,27 +62,35 @@ const BLOCK_SUPPORT_FEATURE_LEVEL_SELECTORS = {
63
62
* @return {Array<Object> } An array of style declarations.
64
63
*/
65
64
function getPresetsDeclarations ( blockPresets = { } , mergedSettings ) {
66
- return reduce (
67
- PRESET_METADATA ,
65
+ return PRESET_METADATA . reduce (
68
66
( declarations , { path, valueKey, valueFunc, cssVarInfix } ) => {
69
- const presetByOrigin = get ( blockPresets , path , [ ] ) ;
70
- if ( presetByOrigin && Array . isArray ( presetByOrigin ) ) {
71
- presetByOrigin . forEach ( ( value ) => {
72
- if ( valueKey && ! valueFunc ) {
73
- declarations . push (
74
- `--wp--preset--${ cssVarInfix } --${ kebabCase (
75
- value . slug
76
- ) } : ${ value [ valueKey ] } `
77
- ) ;
78
- } else if ( valueFunc && typeof valueFunc === 'function' ) {
79
- declarations . push (
80
- `--wp--preset--${ cssVarInfix } --${ kebabCase (
81
- value . slug
82
- ) } : ${ valueFunc ( value , mergedSettings ) } `
83
- ) ;
84
- }
85
- } ) ;
86
- }
67
+ const presetByOrigin = getValueFromObjectPath (
68
+ blockPresets ,
69
+ path ,
70
+ [ ]
71
+ ) ;
72
+ [ 'default' , 'theme' , 'custom' ] . forEach ( ( origin ) => {
73
+ if ( presetByOrigin [ origin ] ) {
74
+ presetByOrigin [ origin ] . forEach ( ( value ) => {
75
+ if ( valueKey && ! valueFunc ) {
76
+ declarations . push (
77
+ `--wp--preset--${ cssVarInfix } --${ kebabCase (
78
+ value . slug
79
+ ) } : ${ value [ valueKey ] } `
80
+ ) ;
81
+ } else if (
82
+ valueFunc &&
83
+ typeof valueFunc === 'function'
84
+ ) {
85
+ declarations . push (
86
+ `--wp--preset--${ cssVarInfix } --${ kebabCase (
87
+ value . slug
88
+ ) } : ${ valueFunc ( value , mergedSettings ) } `
89
+ ) ;
90
+ }
91
+ } ) ;
92
+ }
93
+ } ) ;
87
94
88
95
return declarations ;
89
96
} ,
@@ -98,7 +105,7 @@ function getPresetsDeclarations( blockPresets = {}, mergedSettings ) {
98
105
* @param {Object } blockPresets
99
106
* @return {string } CSS declarations for the preset classes.
100
107
*/
101
- function getPresetsClasses ( blockSelector , blockPresets = { } ) {
108
+ function getPresetsClasses ( blockSelector = '*' , blockPresets = { } ) {
102
109
return PRESET_METADATA . reduce (
103
110
( declarations , { path, cssVarInfix, classes } ) => {
104
111
if ( ! classes ) {
@@ -110,26 +117,28 @@ function getPresetsClasses( blockSelector, blockPresets = {} ) {
110
117
path ,
111
118
[ ]
112
119
) ;
113
- if ( presetByOrigin && Array . isArray ( presetByOrigin ) ) {
114
- presetByOrigin . forEach ( ( { slug } ) => {
115
- classes . forEach ( ( { classSuffix, propertyName } ) => {
116
- const classSelectorToUse = `.has-${ kebabCase (
117
- slug
118
- ) } -${ classSuffix } `;
119
- const selectorToUse = blockSelector
120
- . split ( ',' ) // Selector can be "h1, h2, h3"
121
- . map (
122
- ( selector ) =>
123
- `${ selector } ${ classSelectorToUse } `
124
- )
125
- . join ( ',' ) ;
126
- const value = `var(--wp--preset--${ cssVarInfix } --${ kebabCase (
127
- slug
128
- ) } )`;
129
- declarations += `${ selectorToUse } {${ propertyName } : ${ value } !important;}` ;
120
+ [ 'default' , 'theme' , 'custom' ] . forEach ( ( origin ) => {
121
+ if ( presetByOrigin [ origin ] ) {
122
+ presetByOrigin [ origin ] . forEach ( ( { slug } ) => {
123
+ classes . forEach ( ( { classSuffix, propertyName } ) => {
124
+ const classSelectorToUse = `.has-${ kebabCase (
125
+ slug
126
+ ) } -${ classSuffix } `;
127
+ const selectorToUse = blockSelector
128
+ . split ( ',' ) // Selector can be "h1, h2, h3"
129
+ . map (
130
+ ( selector ) =>
131
+ `${ selector } ${ classSelectorToUse } `
132
+ )
133
+ . join ( ',' ) ;
134
+ const value = `var(--wp--preset--${ cssVarInfix } --${ kebabCase (
135
+ slug
136
+ ) } )`;
137
+ declarations += `${ selectorToUse } {${ propertyName } : ${ value } !important;}` ;
138
+ } ) ;
130
139
} ) ;
131
- } ) ;
132
- }
140
+ }
141
+ } ) ;
133
142
return declarations ;
134
143
} ,
135
144
''
@@ -149,13 +158,14 @@ function getPresetsSvgFilters( blockPresets = {} ) {
149
158
return [ 'default' , 'theme' ]
150
159
. filter ( ( origin ) => presetByOrigin [ origin ] )
151
160
. flatMap ( ( origin ) =>
152
- presetByOrigin [ origin ] . map ( ( preset ) => (
153
- < PresetDuotoneFilter
154
- preset = { preset }
155
- key = { preset . slug }
156
- />
157
- ) )
158
- ) ;
161
+ presetByOrigin [ origin ] . map ( ( preset ) =>
162
+ getDuotoneFilter (
163
+ `wp-duotone-${ preset . slug } ` ,
164
+ preset . colors
165
+ )
166
+ )
167
+ )
168
+ . join ( '' ) ;
159
169
} ) ;
160
170
}
161
171
@@ -291,7 +301,7 @@ const getFeatureDeclarations = ( selectors, styles ) => {
291
301
*
292
302
* @param {Object } tree A theme.json tree containing layout definitions.
293
303
*
294
- * @param {boolean } disableRootPadding
304
+ * @param {boolean } disableRootPadding Whether to force disable the root padding styles.
295
305
* @return {Array } An array of style declarations.
296
306
*/
297
307
export function getStylesDeclarations (
@@ -302,16 +312,19 @@ export function getStylesDeclarations(
302
312
disableRootPadding = false
303
313
) {
304
314
const isRoot = ROOT_BLOCK_SELECTOR === selector ;
305
- const output = reduce (
306
- STYLE_PROPERTY ,
307
- ( declarations , { value, properties, rootOnly } , key ) => {
315
+ const output = Object . entries ( STYLE_PROPERTY ) . reduce (
316
+ (
317
+ declarations ,
318
+ [ key , { value, properties, useEngine, rootOnly } ]
319
+ ) => {
308
320
if ( rootOnly && ! isRoot ) {
309
321
return declarations ;
310
322
}
311
323
const pathToValue = value ;
312
- if ( pathToValue [ 0 ] === 'elements' ) {
324
+ if ( pathToValue [ 0 ] === 'elements' || useEngine ) {
313
325
return declarations ;
314
326
}
327
+
315
328
const styleValue = getValueFromObjectPath (
316
329
blockStyles ,
317
330
pathToValue
@@ -449,7 +462,7 @@ export function getStylesDeclarations(
449
462
* in theme.json, and outputting common layout styles, and specific blockGap values.
450
463
*
451
464
* @param {Object } props
452
- * @param {Object } props.layoutDefinitions A theme.json tree containing layout definitions .
465
+ * @param {Object } props.layoutDefinitions Layout definitions, keyed by layout type .
453
466
* @param {Object } props.style A style object containing spacing values.
454
467
* @param {string } props.selector Selector used to group together layout styling rules.
455
468
* @param {boolean } props.hasBlockGapSupport Whether or not the theme opts-in to blockGap support.
@@ -636,9 +649,9 @@ export const getNodesWithStyles = ( tree, blockSelectors ) => {
636
649
}
637
650
638
651
Object . entries ( ELEMENTS ) . forEach ( ( [ name , selector ] ) => {
639
- if ( tree . styles ?. elements [ name ] ) {
652
+ if ( tree . styles ?. elements ?. [ name ] ) {
640
653
nodes . push ( {
641
- styles : tree . styles ?. elements [ name ] ,
654
+ styles : tree . styles ?. elements ?. [ name ] ,
642
655
selector,
643
656
// Top level elements that don't use a class name should not receive the
644
657
// `:root :where()` wrapper to maintain backwards compatibility.
@@ -864,7 +877,7 @@ export const toCustomProperties = ( tree, blockSelectors ) => {
864
877
}
865
878
866
879
if ( declarations . length > 0 ) {
867
- ruleset = ruleset + `${ selector } {${ declarations . join ( ';' ) } ;}` ;
880
+ ruleset += `${ selector } {${ declarations . join ( ';' ) } ;}` ;
868
881
}
869
882
} ) ;
870
883
@@ -1240,6 +1253,7 @@ export const getBlockSelectors = ( blockTypes, storedPreviewSettings ) => {
1240
1253
1241
1254
if ( blockStyleVariationArray . length !== 0 ) {
1242
1255
blockStyleVariationArray ?. forEach ( ( variation ) => {
1256
+ // eslint-disable-next-line no-unused-vars
1243
1257
Object . entries ( variation ) . forEach ( ( [ key , value ] ) => {
1244
1258
const variationSuffix = blockType ?. apiVersion
1245
1259
? `--${ blockType ?. apiVersion } `
0 commit comments