-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
selectors.js
360 lines (329 loc) · 10.3 KB
/
selectors.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
/**
* External dependencies
*/
import createSelector from 'rememo';
import {
deburr,
filter,
findLast,
first,
flow,
get,
includes,
map,
some,
} from 'lodash';
/** @typedef {import('../api/registration').WPBlockVariation} WPBlockVariation */
/** @typedef {import('../api/registration').WPBlockVariationScope} WPBlockVariationScope */
/** @typedef {import('./reducer').WPBlockCategory} WPBlockCategory */
/**
* Given a block name or block type object, returns the corresponding
* normalized block type object.
*
* @param {Object} state Blocks state.
* @param {(string|Object)} nameOrType Block name or type object
*
* @return {Object} Block type object.
*/
const getNormalizedBlockType = ( state, nameOrType ) =>
'string' === typeof nameOrType
? getBlockType( state, nameOrType )
: nameOrType;
/**
* Returns all the available block types.
*
* @param {Object} state Data state.
*
* @return {Array} Block Types.
*/
export const getBlockTypes = createSelector(
( state ) => {
return Object.values( state.blockTypes ).map( ( blockType ) => {
return {
...blockType,
variations: getBlockVariations( state, blockType.name ),
};
} );
},
( state ) => [ state.blockTypes, state.blockVariations ]
);
/**
* Returns a block type by name.
*
* @param {Object} state Data state.
* @param {string} name Block type name.
*
* @return {Object?} Block Type.
*/
export function getBlockType( state, name ) {
return state.blockTypes[ name ];
}
/**
* Returns block styles by block name.
*
* @param {Object} state Data state.
* @param {string} name Block type name.
*
* @return {Array?} Block Styles.
*/
export function getBlockStyles( state, name ) {
return state.blockStyles[ name ];
}
/**
* Returns block variations by block name.
*
* @param {Object} state Data state.
* @param {string} blockName Block type name.
* @param {WPBlockVariationScope} [scope] Block variation scope name.
*
* @return {(WPBlockVariation[]|void)} Block variations.
*/
export const getBlockVariations = createSelector(
( state, blockName, scope ) => {
const variations = state.blockVariations[ blockName ];
if ( ! variations || ! scope ) {
return variations;
}
return variations.filter( ( variation ) => {
// For backward compatibility reasons, variation's scope defaults to
// `block` and `inserter` when not set.
return ( variation.scope || [ 'block', 'inserter' ] ).includes(
scope
);
} );
},
( state, blockName ) => [ state.blockVariations[ blockName ] ]
);
/**
* Returns the active block variation for a given block based on its attributes.
* Variations are determined by their `isActive` property.
* Which is either an array of block attribute keys or a function.
*
* In case of an array of block attribute keys, the `attributes` are compared
* to the variation's attributes using strict equality check.
*
* In case of function type, the function should accept a block's attributes
* and the variation's attributes and determines if a variation is active.
* A function that accepts a block's attributes and the variation's attributes and determines if a variation is active.
*
* @param {Object} state Data state.
* @param {string} blockName Name of block (example: “core/columns”).
* @param {Object} attributes Block attributes used to determine active variation.
* @param {WPBlockVariationScope} [scope] Block variation scope name.
*
* @return {(WPBlockVariation|undefined)} Active block variation.
*/
export function getActiveBlockVariation( state, blockName, attributes, scope ) {
const variations = getBlockVariations( state, blockName, scope );
const match = variations?.find( ( variation ) => {
if ( Array.isArray( variation.isActive ) ) {
const blockType = getBlockType( state, blockName );
const attributeKeys = Object.keys( blockType?.attributes || {} );
const definedAttributes = variation.isActive.filter(
( attribute ) => attributeKeys.includes( attribute )
);
if ( definedAttributes.length === 0 ) {
return false;
}
return definedAttributes.every(
( attribute ) =>
attributes[ attribute ] ===
variation.attributes[ attribute ]
);
}
return variation.isActive?.( attributes, variation.attributes );
} );
return match;
}
/**
* Returns the default block variation for the given block type.
* When there are multiple variations annotated as the default one,
* the last added item is picked. This simplifies registering overrides.
* When there is no default variation set, it returns the first item.
*
* @param {Object} state Data state.
* @param {string} blockName Block type name.
* @param {WPBlockVariationScope} [scope] Block variation scope name.
*
* @return {?WPBlockVariation} The default block variation.
*/
export function getDefaultBlockVariation( state, blockName, scope ) {
const variations = getBlockVariations( state, blockName, scope );
return findLast( variations, 'isDefault' ) || first( variations );
}
/**
* Returns all the available categories.
*
* @param {Object} state Data state.
*
* @return {WPBlockCategory[]} Categories list.
*/
export function getCategories( state ) {
return state.categories;
}
/**
* Returns all the available collections.
*
* @param {Object} state Data state.
*
* @return {Object} Collections list.
*/
export function getCollections( state ) {
return state.collections;
}
/**
* Returns the name of the default block name.
*
* @param {Object} state Data state.
*
* @return {string?} Default block name.
*/
export function getDefaultBlockName( state ) {
return state.defaultBlockName;
}
/**
* Returns the name of the block for handling non-block content.
*
* @param {Object} state Data state.
*
* @return {string?} Name of the block for handling non-block content.
*/
export function getFreeformFallbackBlockName( state ) {
return state.freeformFallbackBlockName;
}
/**
* Returns the name of the block for handling unregistered blocks.
*
* @param {Object} state Data state.
*
* @return {string?} Name of the block for handling unregistered blocks.
*/
export function getUnregisteredFallbackBlockName( state ) {
return state.unregisteredFallbackBlockName;
}
/**
* Returns the name of the block for handling unregistered blocks.
*
* @param {Object} state Data state.
*
* @return {string?} Name of the block for handling unregistered blocks.
*/
export function getGroupingBlockName( state ) {
return state.groupingBlockName;
}
/**
* Returns an array with the child blocks of a given block.
*
* @param {Object} state Data state.
* @param {string} blockName Block type name.
*
* @return {Array} Array of child block names.
*/
export const getChildBlockNames = createSelector(
( state, blockName ) => {
return map(
filter( state.blockTypes, ( blockType ) => {
return includes( blockType.parent, blockName );
} ),
( { name } ) => name
);
},
( state ) => [ state.blockTypes ]
);
/**
* Returns the block support value for a feature, if defined.
*
* @param {Object} state Data state.
* @param {(string|Object)} nameOrType Block name or type object
* @param {Array|string} feature Feature to retrieve
* @param {*} defaultSupports Default value to return if not
* explicitly defined
*
* @return {?*} Block support value
*/
export const getBlockSupport = (
state,
nameOrType,
feature,
defaultSupports
) => {
const blockType = getNormalizedBlockType( state, nameOrType );
if ( ! blockType?.supports ) {
return defaultSupports;
}
return get( blockType.supports, feature, defaultSupports );
};
/**
* Returns true if the block defines support for a feature, or false otherwise.
*
* @param {Object} state Data state.
* @param {(string|Object)} nameOrType Block name or type object.
* @param {string} feature Feature to test.
* @param {boolean} defaultSupports Whether feature is supported by
* default if not explicitly defined.
*
* @return {boolean} Whether block supports feature.
*/
export function hasBlockSupport( state, nameOrType, feature, defaultSupports ) {
return !! getBlockSupport( state, nameOrType, feature, defaultSupports );
}
/**
* Returns true if the block type by the given name or object value matches a
* search term, or false otherwise.
*
* @param {Object} state Blocks state.
* @param {(string|Object)} nameOrType Block name or type object.
* @param {string} searchTerm Search term by which to filter.
*
* @return {Object[]} Whether block type matches search term.
*/
export function isMatchingSearchTerm( state, nameOrType, searchTerm ) {
const blockType = getNormalizedBlockType( state, nameOrType );
const getNormalizedSearchTerm = flow( [
// Disregard diacritics.
// Input: "média"
deburr,
// Lowercase.
// Input: "MEDIA"
( term ) => term.toLowerCase(),
// Strip leading and trailing whitespace.
// Input: " media "
( term ) => term.trim(),
] );
const normalizedSearchTerm = getNormalizedSearchTerm( searchTerm );
const isSearchMatch = flow( [
getNormalizedSearchTerm,
( normalizedCandidate ) =>
includes( normalizedCandidate, normalizedSearchTerm ),
] );
return (
isSearchMatch( blockType.title ) ||
some( blockType.keywords, isSearchMatch ) ||
isSearchMatch( blockType.category )
);
}
/**
* Returns a boolean indicating if a block has child blocks or not.
*
* @param {Object} state Data state.
* @param {string} blockName Block type name.
*
* @return {boolean} True if a block contains child blocks and false otherwise.
*/
export const hasChildBlocks = ( state, blockName ) => {
return getChildBlockNames( state, blockName ).length > 0;
};
/**
* Returns a boolean indicating if a block has at least one child block with inserter support.
*
* @param {Object} state Data state.
* @param {string} blockName Block type name.
*
* @return {boolean} True if a block contains at least one child blocks with inserter support
* and false otherwise.
*/
export const hasChildBlocksWithInserterSupport = ( state, blockName ) => {
return some( getChildBlockNames( state, blockName ), ( childBlockName ) => {
return hasBlockSupport( state, childBlockName, 'inserter', true );
} );
};