-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathregistration.js
303 lines (277 loc) · 7.86 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
/* eslint no-console: [ 'error', { allow: [ 'error' ] } ] */
/**
* External dependencies
*/
import { get, isFunction, some } from 'lodash';
/**
* WordPress dependencies
*/
import { applyFilters } from '@wordpress/hooks';
/**
* Internal dependencies
*/
import { getCategories } from './categories';
/**
* 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 {(string|WPElement)} icon Slug of the Dashicon to be shown
* as the icon for the block in the
* inserter, or element.
* @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.
*/
/**
* Block type definitions keyed by block name.
*
* @type {Object.<string,WPBlockType>}
*/
const blocks = {};
const categories = getCategories();
/**
* Name of block handling unknown types.
*
* @type {?string}
*/
let unknownTypeHandlerName;
/**
* Name of the default block.
*
* @type {?string}
*/
let defaultBlockName;
/**
* 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 ( blocks[ 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( categories, { 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;
}
if ( ! settings.icon ) {
settings.icon = 'block-default';
}
return blocks[ name ] = 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 ) {
if ( ! blocks[ name ] ) {
console.error(
'Block "' + name + '" is not registered.'
);
return;
}
const oldBlock = blocks[ name ];
delete blocks[ name ];
return oldBlock;
}
/**
* Assigns name of block handling unknown block types.
*
* @param {string} name Block name.
*/
export function setUnknownTypeHandlerName( name ) {
unknownTypeHandlerName = 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 unknownTypeHandlerName;
}
/**
* Assigns the default block name.
*
* @param {string} name Block name.
*/
export function setDefaultBlockName( name ) {
defaultBlockName = name;
}
/**
* Retrieves the default block name.
*
* @return {?string} Block name.
*/
export function getDefaultBlockName() {
return defaultBlockName;
}
/**
* 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 blocks[ name ];
}
/**
* Returns all registered blocks.
*
* @return {Array} Block settings.
*/
export function getBlockTypes() {
return Object.values( blocks );
}
/**
* 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 reusable 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 reusable block.
*/
export function isReusableBlock( blockOrType ) {
return blockOrType.name === 'core/block';
}