-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
get-block-settings.js
225 lines (204 loc) · 6.54 KB
/
get-block-settings.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
/**
* WordPress dependencies
*/
import {
__EXPERIMENTAL_PATHS_WITH_MERGE as PATHS_WITH_MERGE,
hasBlockSupport,
} from '@wordpress/blocks';
import { applyFilters } from '@wordpress/hooks';
/**
* Internal dependencies
*/
import { getValueFromObjectPath } from '../utils/object';
import { getBlockName, getSettings, getBlockAttributes } from './selectors';
const blockedPaths = [
'color',
'border',
'dimensions',
'typography',
'spacing',
];
const deprecatedFlags = {
'color.palette': ( settings ) => settings.colors,
'color.gradients': ( settings ) => settings.gradients,
'color.custom': ( settings ) =>
settings.disableCustomColors === undefined
? undefined
: ! settings.disableCustomColors,
'color.customGradient': ( settings ) =>
settings.disableCustomGradients === undefined
? undefined
: ! settings.disableCustomGradients,
'typography.fontSizes': ( settings ) => settings.fontSizes,
'typography.customFontSize': ( settings ) =>
settings.disableCustomFontSizes === undefined
? undefined
: ! settings.disableCustomFontSizes,
'typography.lineHeight': ( settings ) => settings.enableCustomLineHeight,
'spacing.units': ( settings ) => {
if ( settings.enableCustomUnits === undefined ) {
return;
}
if ( settings.enableCustomUnits === true ) {
return [ 'px', 'em', 'rem', 'vh', 'vw', '%' ];
}
return settings.enableCustomUnits;
},
'spacing.padding': ( settings ) => settings.enableCustomSpacing,
};
const prefixedFlags = {
/*
* These were only available in the plugin
* and can be removed when the minimum WordPress version
* for the plugin is 5.9.
*/
'border.customColor': 'border.color',
'border.customStyle': 'border.style',
'border.customWidth': 'border.width',
'typography.customFontStyle': 'typography.fontStyle',
'typography.customFontWeight': 'typography.fontWeight',
'typography.customLetterSpacing': 'typography.letterSpacing',
'typography.customTextDecorations': 'typography.textDecoration',
'typography.customTextTransforms': 'typography.textTransform',
/*
* These were part of WordPress 5.8 and we need to keep them.
*/
'border.customRadius': 'border.radius',
'spacing.customMargin': 'spacing.margin',
'spacing.customPadding': 'spacing.padding',
'typography.customLineHeight': 'typography.lineHeight',
};
/**
* Remove `custom` prefixes for flags that did not land in 5.8.
*
* This provides continued support for `custom` prefixed properties. It will
* be removed once third party devs have had sufficient time to update themes,
* plugins, etc.
*
* @see https://github.com/WordPress/gutenberg/pull/34485
*
* @param {string} path Path to desired value in settings.
* @return {string} The value for defined setting.
*/
const removeCustomPrefixes = ( path ) => {
return prefixedFlags[ path ] || path;
};
/**
* For settings like `color.palette`, which have a value that is an object
* with `default`, `theme`, `custom`, with field values that are arrays of
* items, merge these three arrays into one and return it. The calculation
* is memoized so that identical input values produce identical output.
* @param {Object} value Object to merge
* @return {Array} Array of merged items
*/
export function mergeOrigins( value ) {
let result = mergeCache.get( value );
if ( ! result ) {
result = [ 'default', 'theme', 'custom' ].flatMap(
( key ) => value[ key ] ?? []
);
mergeCache.set( value, result );
}
return result;
}
const mergeCache = new WeakMap();
/**
* For settings like `color.palette`, which have a value that is an object
* with `default`, `theme`, `custom`, with field values that are arrays of
* items, see if any of the three origins have values.
*
* @param {Object} value Object to check
* @return {boolean} Whether the object has values in any of the three origins
*/
export function hasMergedOrigins( value ) {
return [ 'default', 'theme', 'custom' ].some(
( key ) => value?.[ key ]?.length
);
}
export function getBlockSettings( state, clientId, ...paths ) {
const blockName = getBlockName( state, clientId );
const candidates = [];
if ( clientId ) {
let id = clientId;
do {
const name = getBlockName( state, id );
if ( hasBlockSupport( name, '__experimentalSettings', false ) ) {
candidates.push( id );
}
} while ( ( id = state.blocks.parents.get( id ) ) );
}
return paths.map( ( path ) => {
if ( blockedPaths.includes( path ) ) {
// eslint-disable-next-line no-console
console.warn(
'Top level useSetting paths are disabled. Please use a subpath to query the information needed.'
);
return undefined;
}
// 0. Allow third parties to filter the block's settings at runtime.
let result = applyFilters(
'blockEditor.useSetting.before',
undefined,
path,
clientId,
blockName
);
if ( undefined !== result ) {
return result;
}
const normalizedPath = removeCustomPrefixes( path );
// 1. Take settings from the block instance or its ancestors.
// Start from the current block and work our way up the ancestors.
for ( const candidateClientId of candidates ) {
const candidateAtts = getBlockAttributes(
state,
candidateClientId
);
result =
getValueFromObjectPath(
candidateAtts.settings?.blocks?.[ blockName ],
normalizedPath
) ??
getValueFromObjectPath(
candidateAtts.settings,
normalizedPath
);
if ( result !== undefined ) {
// Stop the search for more distant ancestors and move on.
break;
}
}
// 2. Fall back to the settings from the block editor store (__experimentalFeatures).
const settings = getSettings( state );
if ( result === undefined && blockName ) {
result = getValueFromObjectPath(
settings.__experimentalFeatures?.blocks?.[ blockName ],
normalizedPath
);
}
if ( result === undefined ) {
result = getValueFromObjectPath(
settings.__experimentalFeatures,
normalizedPath
);
}
// Return if the setting was found in either the block instance or the store.
if ( result !== undefined ) {
if ( PATHS_WITH_MERGE[ normalizedPath ] ) {
return mergeOrigins( result );
}
return result;
}
// 3. Otherwise, use deprecated settings.
const deprecatedSettingsValue =
deprecatedFlags[ normalizedPath ]?.( settings );
if ( deprecatedSettingsValue !== undefined ) {
return deprecatedSettingsValue;
}
// 4. Fallback for typography.dropCap:
// This is only necessary to support typography.dropCap.
// when __experimentalFeatures are not present (core without plugin).
// To remove when __experimentalFeatures are ported to core.
return normalizedPath === 'typography.dropCap' ? true : undefined;
} );
}