-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.js
More file actions
92 lines (87 loc) · 2.84 KB
/
build.js
File metadata and controls
92 lines (87 loc) · 2.84 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.refreshEditor = exports.deregisterBlock = void 0;
/**
* Provide helper methods to dynamically reload & reregister blocks in hot-reloading contexts.
*/
const {
unregisterBlockType,
unregisterBlockStyle,
unregisterBlockVariation
} = window.wp.blocks;
const {
removeFilter
} = window.wp.hooks;
const {
dispatch,
select
} = window.wp.data;
/**
* Deregister a block that's being hot-swapped out, so that the updated version
* can be registered afresh without "block already registered" errors.
*
* Also keeps track of the selected block client ID, which is needed because
* we have to deselect a block before unregistering it to swap without error.
* The active block will be reselected in the refresh function.
*
* @param {string} hotBlockName Name of block being hot-reloaded.
* @param {object} [variants] Dictionary of { styles, filters, variations } arrays to optionally unbind.
* @returns {(data: object) => void} Callback for module.hot.dispose() to deregister the specified block.
*/
const deregisterBlock = (hotBlockName, variants = {}) => data => {
if (hotBlockName && !hotBlockName.startsWith('core/')) {
unregisterBlockType(hotBlockName);
}
if (Array.isArray(variants?.styles)) {
variants.styles.forEach(style => {
unregisterBlockStyle(hotBlockName, style.name);
});
}
if (Array.isArray(variants?.filters)) {
variants.filters.forEach(({
hook,
namespace
}) => {
removeFilter(hook, namespace);
});
}
if (Array.isArray(variants?.variations)) {
variants.variations.forEach(variation => {
unregisterBlockVariation(hotBlockName, variation.name);
});
}
const selectedBlockId = select('core/block-editor').getSelectedBlockClientId();
if (selectedBlockId) {
dispatch('core/block-editor').clearSelectedBlock();
}
// Pass selected ID through hot-reload cycle.
data.value = selectedBlockId;
};
/**
* Process an updated block module, refreshing the editor view as needed.
*
* @param {string} hotBlockName Name of block being hot-reloaded.
* @param {{value?: string}} [data] HMR module.hot.data context object, if present.
*/
exports.deregisterBlock = deregisterBlock;
const refreshEditor = (hotBlockName, data) => {
// Refresh all copies of our changed block by iteratively selecting them.
select('core/block-editor').getBlocks().forEach(({
name,
clientId
}) => {
if (name === hotBlockName) {
dispatch('core/block-editor').selectBlock(clientId);
}
});
// Reselect whatever block was selected in the beginning.
if (data?.value) {
// Reselect after a timeout to finish hot-block processing before changing focus.
setTimeout(() => {
dispatch('core/block-editor').selectBlock(data.value);
});
}
};
exports.refreshEditor = refreshEditor;