Skip to content

Commit b5f7e93

Browse files
authored
Merge pull request #182 from BeAPI/fix/editor-script
fix(editor): Ensure reliable unregistration of block variations
2 parents b6ebab4 + 788473f commit b5f7e93

2 files changed

Lines changed: 64 additions & 23 deletions

File tree

‎inc/Services/Editor.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public function admin_editor_script(): void {
262262

263263
$this->assets_tools->add_inline_script(
264264
'theme-admin-editor-script',
265-
'const BFFEditorSettings = ' . wp_json_encode(
265+
'const BEAPI_EDITOR_SETTINGS = ' . wp_json_encode(
266266
apply_filters(
267267
'bff_editor_custom_settings',
268268
[

‎src/js/common/editor.js‎

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,78 @@
1-
/* global BFFEditorSettings */
1+
/* global BEAPI_EDITOR_SETTINGS */
22

3-
/* Customize BFFEditorSettings in inc/Services/Editor.php or with `bff_editor_custom_settings` filter (see readme). */
3+
/* Customize BEAPI_EDITOR_SETTINGS in inc/Services/Editor.php or with `bff_editor_custom_settings` filter (see readme). */
44
import domReady from '@wordpress/dom-ready';
5+
import { subscribe } from '@wordpress/data';
56
import { addFilter } from '@wordpress/hooks';
67
import {
78
unregisterBlockStyle,
89
getBlockVariations,
10+
getBlockType,
911
unregisterBlockVariation,
1012
} from '@wordpress/blocks';
1113
import './utils/beapi';
1214

15+
const unregisterDisabledBlockStyles = () => {
16+
if (!BEAPI_EDITOR_SETTINGS.disabledBlocksStyles) {
17+
return;
18+
}
19+
20+
Object.entries(BEAPI_EDITOR_SETTINGS.disabledBlocksStyles).forEach(
21+
([blockName, styles]) => {
22+
[].concat(styles).forEach((styleName) => {
23+
unregisterBlockStyle(blockName, styleName);
24+
});
25+
}
26+
);
27+
};
28+
29+
const unregisterDisallowedBlockVariations = () => {
30+
if (!BEAPI_EDITOR_SETTINGS.allowedBlocksVariations) {
31+
return;
32+
}
33+
34+
Object.entries(BEAPI_EDITOR_SETTINGS.allowedBlocksVariations).forEach(
35+
([blockName, allowedVariationNames]) => {
36+
const blockVariations = getBlockVariations(blockName) || [];
37+
38+
blockVariations.forEach((variation) => {
39+
if (!allowedVariationNames.includes(variation.name)) {
40+
unregisterBlockVariation(blockName, variation.name);
41+
}
42+
});
43+
}
44+
);
45+
};
46+
47+
const whenBlocksRegistered = (blockNames, callback) => {
48+
const areBlocksReady = () =>
49+
blockNames.every((blockName) => getBlockType(blockName));
50+
51+
if (areBlocksReady()) {
52+
callback();
53+
return;
54+
}
55+
56+
const unsubscribe = subscribe(() => {
57+
if (!areBlocksReady()) {
58+
return;
59+
}
60+
61+
unsubscribe();
62+
callback();
63+
});
64+
};
65+
1366
// Native Gutenberg
1467
domReady(() => {
15-
// Disable specific block styles
16-
if (BFFEditorSettings.disabledBlocksStyles) {
17-
Object.entries(BFFEditorSettings.disabledBlocksStyles).forEach(
18-
([block, styles]) => {
19-
unregisterBlockStyle(block, styles);
20-
}
21-
);
22-
}
68+
unregisterDisabledBlockStyles();
2369

24-
// Allow blocks variations
25-
if (BFFEditorSettings.allowedBlocksVariations) {
26-
Object.entries(BFFEditorSettings.allowedBlocksVariations).forEach(
27-
([block, variations]) => {
28-
getBlockVariations(block).forEach((variant) => {
29-
if (!variations.includes(variant.name)) {
30-
unregisterBlockVariation(block, variant.name);
31-
}
32-
});
33-
}
70+
if (BEAPI_EDITOR_SETTINGS.allowedBlocksVariations) {
71+
const blockNames = Object.keys(
72+
BEAPI_EDITOR_SETTINGS.allowedBlocksVariations
3473
);
74+
75+
whenBlocksRegistered(blockNames, unregisterDisallowedBlockVariations);
3576
}
3677
});
3778

@@ -46,8 +87,8 @@ addFilter(
4687
function (settings, name) {
4788
// Disable all styles
4889
if (
49-
BFFEditorSettings.disableAllBlocksStyles &&
50-
BFFEditorSettings.disableAllBlocksStyles.includes(name)
90+
BEAPI_EDITOR_SETTINGS.disableAllBlocksStyles &&
91+
BEAPI_EDITOR_SETTINGS.disableAllBlocksStyles.includes(name)
5192
) {
5293
settings.styles = [];
5394
}

0 commit comments

Comments
 (0)