Skip to content

Commit b5785c0

Browse files
committed
fix(editor): Ensure reliable unregistration of block variations
Introduces a mechanism to wait for target blocks to be fully registered in the editor store before attempting to unregister their variations. This prevents potential issues where variations might not be unregistered if blocks are not yet available when `domReady` fires. Additionally, this change refactors block style and variation unregistration into dedicated helper functions for improved code organization and readability.
1 parent 9fbaf3c commit b5785c0

1 file changed

Lines changed: 58 additions & 17 deletions

File tree

‎src/js/common/editor.js‎

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,77 @@
22

33
/* Customize BFFEditorSettings 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 (!BFFEditorSettings.disabledBlocksStyles) {
17+
return;
18+
}
19+
20+
Object.entries(BFFEditorSettings.disabledBlocksStyles).forEach(
21+
([blockName, styles]) => {
22+
[].concat(styles).forEach((styleName) => {
23+
unregisterBlockStyle(blockName, styleName);
24+
});
25+
}
26+
);
27+
};
28+
29+
const unregisterDisallowedBlockVariations = () => {
30+
if (!BFFEditorSettings.allowedBlocksVariations) {
31+
return;
32+
}
33+
34+
Object.entries(BFFEditorSettings.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
2570
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-
}
71+
const blockNames = Object.keys(
72+
BFFEditorSettings.allowedBlocksVariations
3473
);
74+
75+
whenBlocksRegistered(blockNames, unregisterDisallowedBlockVariations);
3576
}
3677
});
3778

0 commit comments

Comments
 (0)