-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
412 lines (376 loc) · 12.5 KB
/
index.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
/**
* Provide helper methods to dynamically locate, load & register Blocks & Plugins.
*/
const {
blocks,
plugins,
richText,
hooks,
data,
} = window.wp;
/**
* No-op function for use as a default argument value.
*/
const noop = () => {};
/**
* Require a set of modules and configure them for hot module replacement.
* The consuming function must opt-in to HMR by passing a callback to
* accept updates for the context where this function is used.
*
* The first argument should be a function returning a `require.context()`
* call. All modules loaded from this context are cached, and on each rebuild
* the incoming updated modules are checked against the cache. Updated modules
* which already exist in the cache are unregistered with the provided function,
* then any incoming (new or updated) modules will be registered.
*
* @param {Object} options Configuration object defining callbacks.
* @param {Function} options.getContext Execute and return a `require.context()` call.
* @param {Function} options.register Function to register accepted modules.
* @param {Function} options.unregister Function to unregister replaced modules.
* @param {Function} options.[before] Function to run before updating modules.
* @param {Function} options.[after] Function to run after updating modules.
* @param {Function} [callback] A callback function which will be passed the
* generated `context` object and `loadModules`
* function, which can be used to opt-in to HMR.
*/
export const autoload = (
{
getContext,
register,
unregister,
before = noop,
after = noop,
},
callback = noop
) => {
const cache = {};
const loadModules = () => {
before();
const context = getContext();
const changed = [];
context.keys().forEach( ( key ) => {
const module = context( key );
if ( module === cache[ key ] ) {
// Module unchanged: no further action needed.
return;
}
const isHotUpdate = cache[ key ];
if ( isHotUpdate && console.groupCollapsed ) {
console.groupCollapsed( `hot update: ${ key }` );
}
if ( isHotUpdate ) {
// Module changed, and prior copy detected: unregister old module.
unregister( cache[ key ] );
}
// Register new module and update cache.
register( module );
changed.push( module );
cache[ key ] = module;
if ( isHotUpdate && console.groupCollapsed ) {
console.groupEnd();
}
} );
after( changed );
// Return the context for HMR initialization.
return context;
};
const context = loadModules();
callback( context, loadModules );
};
// Maintain the selected block ID across HMR updates.
let selectedBlockId = null;
/**
* Register a new or updated block, filters, or style variations.
*
* @param {Object} block The exported block module.
* @param {String} block.name Block name. May be included in configuration object.
* @param {Object} [block.settings] Optional block configuration object.
* @param {Object[]} [block.filters] Optional array of filters to bind.
* @param {Object[]} [block.styles] Optional array of block styles to bind.
*/
export const registerBlock = ( { name, settings, filters, styles } ) => {
if ( ( name || settings?.name ) && settings ) {
blocks.registerBlockType( ( name || settings?.name ), settings );
}
if ( filters && Array.isArray( filters ) ) {
filters.forEach( ( { hook, namespace, callback } ) => {
hooks.addFilter( hook, namespace, callback );
} );
}
if ( styles && Array.isArray( styles ) ) {
styles.forEach( ( style ) => blocks.registerBlockStyle( name, style ) );
}
};
/**
* Unregister an updated or removed block, filters, or style variations.
*
* @param {Object} block The exported block module.
* @param {String} block.name Block name. May be included in configuration object.
* @param {Object} [block.settings] Optional block configuration object.
* @param {Object[]} [block.filters] Optional array of filters to bind.
* @param {Object[]} [block.styles] Optional array of block styles to bind.
*/
export const unregisterBlock = ( { name, settings, filters, styles } ) => {
if ( ( name || settings?.name ) && settings ) {
blocks.unregisterBlockType( ( name || settings?.name ) );
}
if ( filters && Array.isArray( filters ) ) {
filters.forEach( ( { hook, namespace } ) => {
hooks.removeFilter( hook, namespace );
} );
}
if ( styles && Array.isArray( styles ) ) {
styles.forEach( ( style ) => blocks.unregisterBlockStyle( name, style.name ) );
}
};
/**
* Store the selected block to persist selection across block-swaps.
*/
export const beforeUpdateBlocks = () => {
selectedBlockId = data.select( 'core/block-editor' ).getSelectedBlockClientId();
data.dispatch( 'core/block-editor' ).clearSelectedBlock();
};
/**
* Trigger a re-render on all blocks which have changed.
*
* @param {Object[]} changed Array of changed module objects.
*/
export const afterUpdateBlocks = ( changed = [] ) => {
const changedNames = changed.map( ( module ) => module.name );
if ( ! changedNames.length ) {
return;
}
// Refresh all blocks by iteratively selecting each one that has changed.
data.select( 'core/block-editor' ).getBlocks().forEach( ( { name, clientId } ) => {
if ( changedNames.includes( name ) ) {
data.dispatch( 'core/block-editor' ).selectBlock( clientId );
}
} );
// Reselect whatever was selected in the beginning.
if ( selectedBlockId ) {
data.dispatch( 'core/block-editor' ).selectBlock( selectedBlockId );
} else {
data.dispatch( 'core/block-editor' ).clearSelectedBlock();
}
selectedBlockId = null;
};
/**
* Require a set of blocks and configure them for hot module replacement.
*
* @see autoload
*
* @param {Object} options Configuration object defining callbacks.
* @param {Function} options.getContext Execute and return a `require.context()` call.
* @param {Function} options.[register] Function to register accepted blocks.
* @param {Function} options.[unregister] Function to unregister replaced blocks.
* @param {Function} options.[before] Function to run before updating blocks.
* @param {Function} options.[after] Function to run after updating blocks.
* @param {Function} [callback] A callback function which will be passed the
* generated `context` object and `loadModules`
* function, which can be used to opt-in to HMR.
*/
export const autoloadBlocks = (
{
getContext,
register = registerBlock,
unregister = unregisterBlock,
before = beforeUpdateBlocks,
after = afterUpdateBlocks,
},
callback
) => {
autoload(
{
getContext,
register,
unregister,
before,
after,
},
callback
);
};
/**
* Register a new or updated plugin.
*
* @param {Object} plugin The exported plugin module.
* @param {String} plugin.name Plugin name.
* @param {Object} plugin.settings Plugin configuration object.
* @param {Object[]} [plugin.filters] Optional array of filters to bind.
*/
export const registerPlugin = ( { name, settings, filters } ) => {
if ( name && settings ) {
plugins.registerPlugin( name, settings );
}
if ( filters && Array.isArray( filters ) ) {
filters.forEach( ( { hook, namespace, callback } ) => {
hooks.addFilter( hook, namespace, callback );
} );
}
};
/**
* Unregister an updated or removed plugin.
*
* @param {Object} plugin The exported plugin module.
* @param {String} plugin.name Plugin name.
* @param {Object} plugin.settings Plugin configuration object.
* @param {Object[]} [plugin.filters] Optional array of filters to bind.
*/
export const unregisterPlugin = ( { name, settings, filters } ) => {
if ( name && settings ) {
plugins.unregisterPlugin( name );
}
if ( filters && Array.isArray( filters ) ) {
filters.forEach( ( { hook, namespace } ) => {
hooks.removeFilter( hook, namespace );
} );
}
};
/**
* Require a set of plugins and configure them for hot module replacement.
*
* @see autoload
*
* @param {Object} options Configuration object defining callbacks.
* @param {Function} options.getContext Execute and return a `require.context()` call.
* @param {Function} options.[register] Function to register accepted plugins.
* @param {Function} options.[unregister] Function to unregister replaced plugins.
* @param {Function} options.[before] Function to run before updating plugins.
* @param {Function} options.[after] Function to run after updating plugins.
* @param {Function} [callback] A callback function which will be passed the
* generated `context` object and `loadModules`
* function, which can be used to opt-in to HMR.
*/
export const autoloadPlugins = (
{
getContext,
register = registerPlugin,
unregister = unregisterPlugin,
before,
after,
},
callback
) => {
autoload(
{
getContext,
register,
unregister,
before,
after,
},
callback
);
};
/**
* Register a new or updated format type
*
* @param {Object} format The exported format module.
* @param {String} format.name Format type name.
* @param {Object} format.settings Format type configuration object.
*/
export const registerFormat = ( { name, settings } ) => {
if ( name && settings ) {
richText.registerFormatType( name, settings );
}
};
/**
* Unregister an updated or removed format type.
*
* @param {Object} format The exported format module.
* @param {String} format.name Format type name.
* @param {Object} format.settings Format type configuration object.
*/
export const unregisterFormat = ( { name, settings } ) => {
if ( name && settings ) {
richText.unregisterFormatType( name );
}
};
/**
* Require a set of format types and configure them for hot module replacement.
*
* @see autoload
*
* @param {Object} options Configuration object defining callbacks.
* @param {Function} options.getContext Execute and return a `require.context()` call.
* @param {Function} options.[register] Function to register accepted formats.
* @param {Function} options.[unregister] Function to unregister replaced formats.
* @param {Function} options.[before] Function to run before updating formats.
* @param {Function} options.[after] Function to run after updating formats.
* @param {Function} [callback] A callback function which will be passed the
* generated `context` object and `loadModules`
* function, which can be used to opt-in to HMR.
*/
export const autoloadFormats = (
{
getContext,
register = registerFormat,
unregister = unregisterFormat,
before,
after,
},
callback
) => {
autoload(
{
getContext,
register,
unregister,
before,
after,
},
callback
);
};
/* eslint-disable no-underscore-dangle */
/* eslint-disable camelcase */
/**
* Work around a full-page crash in WordPress 5.4 caused by a forced render of
* the BlockListBlock component following the state dispatch triggered upon block
* unregistration.
*
* This function filters the BlockListBlock component to wrap it in an error
* boundary, which catches the error when BlockListBlock tries to access a
* property on the removed block type, suppresses the error by returning null,
* and then schedules the BlockListBlock to try rendering again on the next
* tick (by which point our hot-swapped block type should be available again).
*/
export const _apply_wp_5_4_hmr_patch = () => {
/* eslint-enable */
const React = window.React;
const { Component, Fragment, createElement } = React;
hooks.addFilter(
'editor.BlockListBlock',
'block-editor-hmr/prevent-block-swapping-error',
( BlockListBlock ) => {
class ErrorWrapper extends Component {
constructor( props ) {
super( props );
this.state = { hasError: false };
}
// eslint-disable-next-line no-unused-vars
static getDerivedStateFromError( error ) {
return { hasError: true };
}
componentDidUpdate( prevProps, prevState ) {
if ( this.state.hasError && this.state.hasError !== prevState.hasError ) {
setTimeout( () => {
this.setState( { hasError: false } );
} );
}
}
render() {
if ( this.state.hasError ) {
return null;
}
return createElement(
Fragment,
null,
createElement( BlockListBlock, this.props )
);
}
}
return ErrorWrapper;
}
);
};