-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
actions.js
462 lines (426 loc) · 12.3 KB
/
actions.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
/**
* External dependencies
*/
import { isPlainObject } from 'is-plain-object';
import { castArray, pick, some } from 'lodash';
/**
* WordPress dependencies
*/
import { applyFilters } from '@wordpress/hooks';
/**
* Internal dependencies
*/
import { isValidIcon, normalizeIconObject, omit } from '../api/utils';
import { DEPRECATED_ENTRY_KEYS } from '../api/constants';
/** @typedef {import('../api/registration').WPBlockVariation} WPBlockVariation */
/** @typedef {import('../api/registration').WPBlockType} WPBlockType */
/** @typedef {import('./reducer').WPBlockCategory} WPBlockCategory */
const { error, warn } = window.console;
/**
* Mapping of legacy category slugs to their latest normal values, used to
* accommodate updates of the default set of block categories.
*
* @type {Record<string,string>}
*/
const LEGACY_CATEGORY_MAPPING = {
common: 'text',
formatting: 'text',
layout: 'design',
};
/**
* Whether the argument is a function.
*
* @param {*} maybeFunc The argument to check.
* @return {boolean} True if the argument is a function, false otherwise.
*/
function isFunction( maybeFunc ) {
return typeof maybeFunc === 'function';
}
/**
* Takes the unprocessed block type data and applies all the existing filters for the registered block type.
* Next, it validates all the settings and performs additional processing to the block type definition.
*
* @param {WPBlockType} blockType Unprocessed block type settings.
* @param {Object} thunkArgs Argument object for the thunk middleware.
* @param {Function} thunkArgs.select Function to select from the store.
*
* @return {?WPBlockType} The block, if it has been successfully registered; otherwise `undefined`.
*/
const processBlockType = ( blockType, { select } ) => {
const { name } = blockType;
const settings = applyFilters(
'blocks.registerBlockType',
{ ...blockType },
name,
null
);
if ( settings.deprecated ) {
settings.deprecated = settings.deprecated.map( ( deprecation ) =>
pick(
// Only keep valid deprecation keys.
applyFilters(
'blocks.registerBlockType',
// Merge deprecation keys with pre-filter settings
// so that filters that depend on specific keys being
// present don't fail.
{
// Omit deprecation keys here so that deprecations
// can opt out of specific keys like "supports".
...omit( blockType, DEPRECATED_ENTRY_KEYS ),
...deprecation,
},
name,
deprecation
),
DEPRECATED_ENTRY_KEYS
)
);
}
if ( ! isPlainObject( settings ) ) {
error( 'Block settings must be a valid object.' );
return;
}
if ( ! isFunction( settings.save ) ) {
error( 'The "save" property must be a valid function.' );
return;
}
if ( 'edit' in settings && ! isFunction( settings.edit ) ) {
error( 'The "edit" property must be a valid function.' );
return;
}
// Canonicalize legacy categories to equivalent fallback.
if ( LEGACY_CATEGORY_MAPPING.hasOwnProperty( settings.category ) ) {
settings.category = LEGACY_CATEGORY_MAPPING[ settings.category ];
}
if (
'category' in settings &&
! some( select.getCategories(), {
slug: settings.category,
} )
) {
warn(
'The block "' +
name +
'" is registered with an invalid category "' +
settings.category +
'".'
);
delete settings.category;
}
if ( ! ( 'title' in settings ) || settings.title === '' ) {
error( 'The block "' + name + '" must have a title.' );
return;
}
if ( typeof settings.title !== 'string' ) {
error( 'Block titles must be strings.' );
return;
}
settings.icon = normalizeIconObject( settings.icon );
if ( ! isValidIcon( settings.icon.src ) ) {
error(
'The icon passed is invalid. ' +
'The icon should be a string, an element, a function, or an object following the specifications documented in https://developer.wordpress.org/block-editor/developers/block-api/block-registration/#icon-optional'
);
return;
}
return settings;
};
/**
* Returns an action object used in signalling that block types have been added.
* Ignored from documentation as the recommended usage for this action through registerBlockType from @wordpress/blocks.
*
* @ignore
*
* @param {WPBlockType|WPBlockType[]} blockTypes Object or array of objects representing blocks to added.
*
*
* @return {Object} Action object.
*/
export function addBlockTypes( blockTypes ) {
return {
type: 'ADD_BLOCK_TYPES',
blockTypes: castArray( blockTypes ),
};
}
/**
* Signals that the passed block type's settings should be stored in the state.
*
* @param {WPBlockType} blockType Unprocessed block type settings.
*/
export const __experimentalRegisterBlockType =
( blockType ) =>
( { dispatch, select } ) => {
dispatch( {
type: 'ADD_UNPROCESSED_BLOCK_TYPE',
blockType,
} );
const processedBlockType = processBlockType( blockType, { select } );
if ( ! processedBlockType ) {
return;
}
dispatch.addBlockTypes( processedBlockType );
};
/**
* Signals that all block types should be computed again.
* It uses stored unprocessed block types and all the most recent list of registered filters.
*
* It addresses the issue where third party block filters get registered after third party blocks. A sample sequence:
* 1. Filter A.
* 2. Block B.
* 3. Block C.
* 4. Filter D.
* 5. Filter E.
* 6. Block F.
* 7. Filter G.
* In this scenario some filters would not get applied for all blocks because they are registered too late.
*/
export const __experimentalReapplyBlockTypeFilters =
() =>
( { dispatch, select } ) => {
const unprocessedBlockTypes =
select.__experimentalGetUnprocessedBlockTypes();
const processedBlockTypes = Object.keys( unprocessedBlockTypes ).reduce(
( accumulator, blockName ) => {
const result = processBlockType(
unprocessedBlockTypes[ blockName ],
{ select }
);
if ( result ) {
accumulator.push( result );
}
return accumulator;
},
[]
);
if ( ! processedBlockTypes.length ) {
return;
}
dispatch.addBlockTypes( processedBlockTypes );
};
/**
* Returns an action object used to remove a registered block type.
* Ignored from documentation as the recommended usage for this action through unregisterBlockType from @wordpress/blocks.
*
* @ignore
*
* @param {string|string[]} names Block name or array of block names to be removed.
*
*
* @return {Object} Action object.
*/
export function removeBlockTypes( names ) {
return {
type: 'REMOVE_BLOCK_TYPES',
names: castArray( names ),
};
}
/**
* Returns an action object used in signalling that new block styles have been added.
* Ignored from documentation as the recommended usage for this action through registerBlockStyle from @wordpress/blocks.
*
* @param {string} blockName Block name.
* @param {Array|Object} styles Block style object or array of block style objects.
*
* @ignore
*
* @return {Object} Action object.
*/
export function addBlockStyles( blockName, styles ) {
return {
type: 'ADD_BLOCK_STYLES',
styles: castArray( styles ),
blockName,
};
}
/**
* Returns an action object used in signalling that block styles have been removed.
* Ignored from documentation as the recommended usage for this action through unregisterBlockStyle from @wordpress/blocks.
*
* @ignore
*
* @param {string} blockName Block name.
* @param {Array|string} styleNames Block style names or array of block style names.
*
* @return {Object} Action object.
*/
export function removeBlockStyles( blockName, styleNames ) {
return {
type: 'REMOVE_BLOCK_STYLES',
styleNames: castArray( styleNames ),
blockName,
};
}
/**
* Returns an action object used in signalling that new block variations have been added.
* Ignored from documentation as the recommended usage for this action through registerBlockVariation from @wordpress/blocks.
*
* @ignore
*
* @param {string} blockName Block name.
* @param {WPBlockVariation|WPBlockVariation[]} variations Block variations.
*
* @return {Object} Action object.
*/
export function addBlockVariations( blockName, variations ) {
return {
type: 'ADD_BLOCK_VARIATIONS',
variations: castArray( variations ),
blockName,
};
}
/**
* Returns an action object used in signalling that block variations have been removed.
* Ignored from documentation as the recommended usage for this action through unregisterBlockVariation from @wordpress/blocks.
*
* @ignore
*
* @param {string} blockName Block name.
* @param {string|string[]} variationNames Block variation names.
*
* @return {Object} Action object.
*/
export function removeBlockVariations( blockName, variationNames ) {
return {
type: 'REMOVE_BLOCK_VARIATIONS',
variationNames: castArray( variationNames ),
blockName,
};
}
/**
* Returns an action object used to set the default block name.
* Ignored from documentation as the recommended usage for this action through setDefaultBlockName from @wordpress/blocks.
*
* @ignore
*
* @param {string} name Block name.
*
* @return {Object} Action object.
*/
export function setDefaultBlockName( name ) {
return {
type: 'SET_DEFAULT_BLOCK_NAME',
name,
};
}
/**
* Returns an action object used to set the name of the block used as a fallback
* for non-block content.
* Ignored from documentation as the recommended usage for this action through setFreeformContentHandlerName from @wordpress/blocks.
*
* @ignore
*
* @param {string} name Block name.
*
* @return {Object} Action object.
*/
export function setFreeformFallbackBlockName( name ) {
return {
type: 'SET_FREEFORM_FALLBACK_BLOCK_NAME',
name,
};
}
/**
* Returns an action object used to set the name of the block used as a fallback
* for unregistered blocks.
* Ignored from documentation as the recommended usage for this action through setUnregisteredTypeHandlerName from @wordpress/blocks.
*
* @ignore
*
* @param {string} name Block name.
*
* @return {Object} Action object.
*/
export function setUnregisteredFallbackBlockName( name ) {
return {
type: 'SET_UNREGISTERED_FALLBACK_BLOCK_NAME',
name,
};
}
/**
* Returns an action object used to set the name of the block used
* when grouping other blocks
* eg: in "Group/Ungroup" interactions
* Ignored from documentation as the recommended usage for this action through setGroupingBlockName from @wordpress/blocks.
*
* @ignore
*
* @param {string} name Block name.
*
* @return {Object} Action object.
*/
export function setGroupingBlockName( name ) {
return {
type: 'SET_GROUPING_BLOCK_NAME',
name,
};
}
/**
* Returns an action object used to set block categories.
* Ignored from documentation as the recommended usage for this action through setCategories from @wordpress/blocks.
*
* @ignore
*
* @param {WPBlockCategory[]} categories Block categories.
*
* @return {Object} Action object.
*/
export function setCategories( categories ) {
return {
type: 'SET_CATEGORIES',
categories,
};
}
/**
* Returns an action object used to update a category.
* Ignored from documentation as the recommended usage for this action through updateCategory from @wordpress/blocks.
*
* @ignore
*
* @param {string} slug Block category slug.
* @param {Object} category Object containing the category properties that should be updated.
*
* @return {Object} Action object.
*/
export function updateCategory( slug, category ) {
return {
type: 'UPDATE_CATEGORY',
slug,
category,
};
}
/**
* Returns an action object used to add block collections
* Ignored from documentation as the recommended usage for this action through registerBlockCollection from @wordpress/blocks.
*
* @ignore
*
* @param {string} namespace The namespace of the blocks to put in the collection
* @param {string} title The title to display in the block inserter
* @param {Object} icon (optional) The icon to display in the block inserter
*
* @return {Object} Action object.
*/
export function addBlockCollection( namespace, title, icon ) {
return {
type: 'ADD_BLOCK_COLLECTION',
namespace,
title,
icon,
};
}
/**
* Returns an action object used to remove block collections
* Ignored from documentation as the recommended usage for this action through unregisterBlockCollection from @wordpress/blocks.
*
* @ignore
*
* @param {string} namespace The namespace of the blocks to put in the collection
*
* @return {Object} Action object.
*/
export function removeBlockCollection( namespace ) {
return {
type: 'REMOVE_BLOCK_COLLECTION',
namespace,
};
}