-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
registration.js
332 lines (304 loc) · 9.62 KB
/
registration.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
/* eslint no-console: [ 'error', { allow: [ 'error' ] } ] */
/**
* External dependencies
*/
import { get, set, isFunction, some } from 'lodash';
/**
* WordPress dependencies
*/
import { applyFilters } from '@wordpress/hooks';
import { select, dispatch } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
/**
* Internal dependencies
*/
import { isIconUnreadable, isValidIcon, normalizeIconObject } from './utils';
/**
* Defined behavior of a block type.
*
* @typedef {WPBlockType}
*
* @property {string} name Block's namespaced name.
* @property {string} title Human-readable label for a block.
* Shown in the block inserter.
* @property {string} category Category classification of block,
* impacting where block is shown in
* inserter results.
* @property {(Object|string|WPElement)} icon Slug of the Dashicon to be shown
* as the icon for the block in the
* inserter, or element or an object describing the icon.
* @property {?string[]} keywords Additional keywords to produce
* block as inserter search result.
* @property {?Object} attributes Block attributes.
* @property {Function} save Serialize behavior of a block,
* returning an element describing
* structure of the block's post
* content markup.
* @property {WPComponent} edit Component rendering element to be
* interacted with in an editor.
*/
/**
* Constant mapping post formats to the expected default block.
*
* @type {Object}
*/
const POST_FORMAT_BLOCK_MAP = {
audio: 'core/audio',
gallery: 'core/gallery',
image: 'core/image',
quote: 'core/quote',
video: 'core/video',
};
/**
* Registers a new block provided a unique name and an object defining its
* behavior. Once registered, the block is made available as an option to any
* editor interface where blocks are implemented.
*
* @param {string} name Block name.
* @param {Object} settings Block settings.
*
* @return {?WPBlock} The block, if it has been successfully registered;
* otherwise `undefined`.
*/
export function registerBlockType( name, settings ) {
settings = {
name,
...get( window._wpBlocks, name ),
...settings,
};
if ( typeof name !== 'string' ) {
console.error(
'Block names must be strings.'
);
return;
}
if ( ! /^[a-z][a-z0-9-]*\/[a-z][a-z0-9-]*$/.test( name ) ) {
console.error(
'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block'
);
return;
}
if ( select( 'core/blocks' ).getBlockType( name ) ) {
console.error(
'Block "' + name + '" is already registered.'
);
return;
}
settings = applyFilters( 'blocks.registerBlockType', settings, name );
if ( ! settings || ! isFunction( settings.save ) ) {
console.error(
'The "save" property must be specified and must be a valid function.'
);
return;
}
if ( 'edit' in settings && ! isFunction( settings.edit ) ) {
console.error(
'The "edit" property must be a valid function.'
);
return;
}
if ( 'keywords' in settings && settings.keywords.length > 3 ) {
console.error(
'The block "' + name + '" can have a maximum of 3 keywords.'
);
return;
}
if ( ! ( 'category' in settings ) ) {
console.error(
'The block "' + name + '" must have a category.'
);
return;
}
if (
'category' in settings &&
! some( select( 'core/blocks' ).getCategories(), { slug: settings.category } )
) {
console.error(
'The block "' + name + '" must have a registered category.'
);
return;
}
if ( ! ( 'title' in settings ) || settings.title === '' ) {
console.error(
'The block "' + name + '" must have a title.'
);
return;
}
if ( typeof settings.title !== 'string' ) {
console.error(
'Block titles must be strings.'
);
return;
}
settings.icon = normalizeIconObject( settings.icon );
if ( ! isValidIcon( settings.icon.src ) ) {
console.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://wordpress.org/gutenberg/handbook/block-api/#icon-optional'
);
return;
}
if ( isIconUnreadable( settings.icon ) && window ) {
window.console.warn(
`The icon background color ${ settings.icon.background } and the foreground color ${ settings.icon.foreground } are not readable together. ` +
'Please try to increase the brightness and/or contrast difference between background and foreground.'
);
}
if ( 'useOnce' in settings ) {
deprecated( 'useOnce', {
version: '3.3',
alternative: 'supports.multiple',
plugin: 'Gutenberg',
hint: 'useOnce property in the settings param passed to wp.block.registerBlockType.',
} );
set( settings, [ 'supports', 'multiple' ], ! settings.useOnce );
}
dispatch( 'core/blocks' ).addBlockTypes( settings );
return settings;
}
/**
* Unregisters a block.
*
* @param {string} name Block name.
*
* @return {?WPBlock} The previous block value, if it has been successfully
* unregistered; otherwise `undefined`.
*/
export function unregisterBlockType( name ) {
const oldBlock = select( 'core/blocks' ).getBlockType( name );
if ( ! oldBlock ) {
console.error(
'Block "' + name + '" is not registered.'
);
return;
}
dispatch( 'core/blocks' ).removeBlockTypes( name );
return oldBlock;
}
/**
* Assigns name of block handling unknown block types.
*
* @param {string} name Block name.
*/
export function setUnknownTypeHandlerName( name ) {
dispatch( 'core/blocks' ).setFallbackBlockName( name );
}
/**
* Retrieves name of block handling unknown block types, or undefined if no
* handler has been defined.
*
* @return {?string} Blog name.
*/
export function getUnknownTypeHandlerName() {
return select( 'core/blocks' ).getFallbackBlockName();
}
/**
* Assigns the default block name.
*
* @param {string} name Block name.
*/
export function setDefaultBlockName( name ) {
dispatch( 'core/blocks' ).setDefaultBlockName( name );
}
/**
* Retrieves the default block name.
*
* @return {?string} Block name.
*/
export function getDefaultBlockName() {
return select( 'core/blocks' ).getDefaultBlockName();
}
/**
* Retrieves the expected default block for the post format.
*
* @param {string} postFormat Post format
* @return {string} Block name.
*/
export function getDefaultBlockForPostFormat( postFormat ) {
const blockName = POST_FORMAT_BLOCK_MAP[ postFormat ];
if ( blockName && getBlockType( blockName ) ) {
return blockName;
}
return null;
}
/**
* Returns a registered block type.
*
* @param {string} name Block name.
*
* @return {?Object} Block type.
*/
export function getBlockType( name ) {
return select( 'core/blocks' ).getBlockType( name );
}
/**
* Returns all registered blocks.
*
* @return {Array} Block settings.
*/
export function getBlockTypes() {
return select( 'core/blocks' ).getBlockTypes();
}
/**
* Returns the block support value for a feature, if defined.
*
* @param {(string|Object)} nameOrType Block name or type object
* @param {string} feature Feature to retrieve
* @param {*} defaultSupports Default value to return if not
* explicitly defined
* @return {?*} Block support value
*/
export function getBlockSupport( nameOrType, feature, defaultSupports ) {
const blockType = 'string' === typeof nameOrType ?
getBlockType( nameOrType ) :
nameOrType;
return get( blockType, [
'supports',
feature,
], defaultSupports );
}
/**
* Returns true if the block defines support for a feature, or false otherwise.
*
* @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( nameOrType, feature, defaultSupports ) {
return !! getBlockSupport( nameOrType, feature, defaultSupports );
}
/**
* Determines whether or not the given block is a shared block. This is a
* special block type that is used to point to a global block stored via the
* API.
*
* @param {Object} blockOrType Block or Block Type to test.
*
* @return {boolean} Whether the given block is a shared block.
*/
export function isSharedBlock( blockOrType ) {
return blockOrType.name === 'core/block';
}
/**
* Returns an array with the child blocks of a given block.
*
* @param {string} blockName Block type name.
*
* @return {Array} Array of child block names.
*/
export const getChildBlockNames = ( blockName ) => {
return select( 'core/blocks' ).getChildBlockNames( blockName );
};
/**
* Returns a boolean indicating if a block has child blocks or not.
*
* @param {string} blockName Block type name.
*
* @return {boolean} True if a block contains child blocks and false otherwise.
*/
export const hasChildBlocks = ( blockName ) => {
return select( 'core/blocks' ).hasChildBlocks( blockName );
};