-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathhooks.js
366 lines (327 loc) · 8.56 KB
/
hooks.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
/**
* External dependencies
*/
import fastDeepEqual from 'fast-deep-equal/es6';
import { get, set } from 'lodash';
import { colord, extend } from 'colord';
import a11yPlugin from 'colord/plugins/a11y';
/**
* WordPress dependencies
*/
import { _x } from '@wordpress/i18n';
import { useContext, useCallback, useMemo } from '@wordpress/element';
import {
getBlockType,
__EXPERIMENTAL_PATHS_WITH_MERGE as PATHS_WITH_MERGE,
__EXPERIMENTAL_STYLE_PROPERTY as STYLE_PROPERTY,
} from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { getValueFromVariable, getPresetVariableFromValue } from './utils';
import { GlobalStylesContext } from './context';
// Enable colord's a11y plugin.
extend( [ a11yPlugin ] );
const EMPTY_CONFIG = { settings: {}, styles: {} };
export const useGlobalStylesReset = () => {
const { user: config, setUserConfig } = useContext( GlobalStylesContext );
const canReset = !! config && ! fastDeepEqual( config, EMPTY_CONFIG );
return [
canReset,
useCallback(
() => setUserConfig( () => EMPTY_CONFIG ),
[ setUserConfig ]
),
];
};
export function useSetting( path, blockName, source = 'all' ) {
const {
merged: mergedConfig,
base: baseConfig,
user: userConfig,
setUserConfig,
} = useContext( GlobalStylesContext );
const fullPath = ! blockName
? `settings.${ path }`
: `settings.blocks.${ blockName }.${ path }`;
const setSetting = ( newValue ) => {
setUserConfig( ( currentConfig ) => {
// Deep clone `currentConfig` to avoid mutating it later.
const newUserConfig = JSON.parse( JSON.stringify( currentConfig ) );
const pathToSet = PATHS_WITH_MERGE[ path ]
? fullPath + '.custom'
: fullPath;
set( newUserConfig, pathToSet, newValue );
return newUserConfig;
} );
};
const getSettingValueForContext = ( name ) => {
const currentPath = ! name
? `settings.${ path }`
: `settings.blocks.${ name }.${ path }`;
const getSettingValue = ( configToUse ) => {
const result = get( configToUse, currentPath );
if ( PATHS_WITH_MERGE[ path ] ) {
return result?.custom ?? result?.theme ?? result?.default;
}
return result;
};
let result;
switch ( source ) {
case 'all':
result = getSettingValue( mergedConfig );
break;
case 'user':
result = getSettingValue( userConfig );
break;
case 'base':
result = getSettingValue( baseConfig );
break;
default:
throw 'Unsupported source';
}
return result;
};
// Unlike styles settings get inherited from top level settings.
const resultWithFallback =
getSettingValueForContext( blockName ) ?? getSettingValueForContext();
return [ resultWithFallback, setSetting ];
}
export function useStyle( path, blockName, source = 'all' ) {
const {
merged: mergedConfig,
base: baseConfig,
user: userConfig,
setUserConfig,
} = useContext( GlobalStylesContext );
const finalPath = ! blockName
? `styles.${ path }`
: `styles.blocks.${ blockName }.${ path }`;
const setStyle = ( newValue ) => {
setUserConfig( ( currentConfig ) => {
// Deep clone `currentConfig` to avoid mutating it later.
const newUserConfig = JSON.parse( JSON.stringify( currentConfig ) );
set(
newUserConfig,
finalPath,
getPresetVariableFromValue(
mergedConfig.settings,
blockName,
path,
newValue
)
);
return newUserConfig;
} );
};
let result;
switch ( source ) {
case 'all':
result = getValueFromVariable(
mergedConfig,
blockName,
// The stlyes.css path is allowed to be empty, so don't revert to base if undefined.
finalPath === 'styles.css'
? get( userConfig, finalPath )
: get( userConfig, finalPath ) ??
get( baseConfig, finalPath )
);
break;
case 'user':
result = getValueFromVariable(
mergedConfig,
blockName,
get( userConfig, finalPath )
);
break;
case 'base':
result = getValueFromVariable(
baseConfig,
blockName,
get( baseConfig, finalPath )
);
break;
default:
throw 'Unsupported source';
}
return [ result, setStyle ];
}
const ROOT_BLOCK_SUPPORTS = [
'background',
'backgroundColor',
'color',
'linkColor',
'buttonColor',
'fontFamily',
'fontSize',
'fontStyle',
'fontWeight',
'lineHeight',
'textDecoration',
'padding',
'contentSize',
'wideSize',
'blockGap',
];
export function getSupportedGlobalStylesPanels( name ) {
if ( ! name ) {
return ROOT_BLOCK_SUPPORTS;
}
const blockType = getBlockType( name );
if ( ! blockType ) {
return [];
}
const supportKeys = [];
// Check for blockGap support.
// Block spacing support doesn't map directly to a single style property, so needs to be handled separately.
// Also, only allow `blockGap` support if serialization has not been skipped, to be sure global spacing can be applied.
if (
blockType?.supports?.spacing?.blockGap &&
blockType?.supports?.spacing?.__experimentalSkipSerialization !==
true &&
! blockType?.supports?.spacing?.__experimentalSkipSerialization?.some?.(
( spacingType ) => spacingType === 'blockGap'
)
) {
supportKeys.push( 'blockGap' );
}
Object.keys( STYLE_PROPERTY ).forEach( ( styleName ) => {
if ( ! STYLE_PROPERTY[ styleName ].support ) {
return;
}
// Opting out means that, for certain support keys like background color,
// blocks have to explicitly set the support value false. If the key is
// unset, we still enable it.
if ( STYLE_PROPERTY[ styleName ].requiresOptOut ) {
if (
STYLE_PROPERTY[ styleName ].support[ 0 ] in
blockType.supports &&
get(
blockType.supports,
STYLE_PROPERTY[ styleName ].support
) !== false
) {
return supportKeys.push( styleName );
}
}
if (
get(
blockType.supports,
STYLE_PROPERTY[ styleName ].support,
false
)
) {
return supportKeys.push( styleName );
}
} );
return supportKeys;
}
export function useColorsPerOrigin( name ) {
const [ customColors ] = useSetting( 'color.palette.custom', name );
const [ themeColors ] = useSetting( 'color.palette.theme', name );
const [ defaultColors ] = useSetting( 'color.palette.default', name );
const [ shouldDisplayDefaultColors ] = useSetting( 'color.defaultPalette' );
return useMemo( () => {
const result = [];
if ( themeColors && themeColors.length ) {
result.push( {
name: _x(
'Theme',
'Indicates this palette comes from the theme.'
),
colors: themeColors,
} );
}
if (
shouldDisplayDefaultColors &&
defaultColors &&
defaultColors.length
) {
result.push( {
name: _x(
'Default',
'Indicates this palette comes from WordPress.'
),
colors: defaultColors,
} );
}
if ( customColors && customColors.length ) {
result.push( {
name: _x(
'Custom',
'Indicates this palette is created by the user.'
),
colors: customColors,
} );
}
return result;
}, [ customColors, themeColors, defaultColors ] );
}
export function useGradientsPerOrigin( name ) {
const [ customGradients ] = useSetting( 'color.gradients.custom', name );
const [ themeGradients ] = useSetting( 'color.gradients.theme', name );
const [ defaultGradients ] = useSetting( 'color.gradients.default', name );
const [ shouldDisplayDefaultGradients ] = useSetting(
'color.defaultGradients'
);
return useMemo( () => {
const result = [];
if ( themeGradients && themeGradients.length ) {
result.push( {
name: _x(
'Theme',
'Indicates this palette comes from the theme.'
),
gradients: themeGradients,
} );
}
if (
shouldDisplayDefaultGradients &&
defaultGradients &&
defaultGradients.length
) {
result.push( {
name: _x(
'Default',
'Indicates this palette comes from WordPress.'
),
gradients: defaultGradients,
} );
}
if ( customGradients && customGradients.length ) {
result.push( {
name: _x(
'Custom',
'Indicates this palette is created by the user.'
),
gradients: customGradients,
} );
}
return result;
}, [ customGradients, themeGradients, defaultGradients ] );
}
export function useColorRandomizer( name ) {
const [ themeColors, setThemeColors ] = useSetting(
'color.palette.theme',
name
);
function randomizeColors() {
/* eslint-disable no-restricted-syntax */
const randomRotationValue = Math.floor( Math.random() * 225 );
/* eslint-enable no-restricted-syntax */
const newColors = themeColors.map( ( colorObject ) => {
const { color } = colorObject;
const newColor = colord( color )
.rotate( randomRotationValue )
.toHex();
return {
...colorObject,
color: newColor,
};
} );
setThemeColors( newColors );
}
return window.__experimentalEnableColorRandomizer
? [ randomizeColors ]
: [];
}