Skip to content

Commit 0379c8b

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 bdad2ee commit 0379c8b

2 files changed

Lines changed: 57 additions & 29 deletions

File tree

inc/Services/Editor.php

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

9696
$this->assets_tools->add_inline_script(
9797
'theme-admin-editor-script',
98-
'const BFFEditorSettings = ' . wp_json_encode(
98+
'const BEAPI_EDITOR_SETTINGS = ' . wp_json_encode(
9999
apply_filters(
100100
'bff_editor_custom_settings',
101101
[

src/js/editor.js

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,66 @@
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). */
4-
5-
import lazySizes from 'lazysizes'
3+
/* Customize BEAPI_EDITOR_SETTINGS in inc/Services/Editor.php or with `bff_editor_custom_settings` filter (see readme). */
64
import domReady from '@wordpress/dom-ready'
5+
import { subscribe } from '@wordpress/data'
76
import { addFilter } from '@wordpress/hooks'
8-
import { unregisterBlockStyle, getBlockVariations, unregisterBlockVariation } from '@wordpress/blocks'
9-
10-
/**
11-
* LazySizes configuration
12-
* https://github.com/aFarkas/lazysizes/#js-api---options
13-
*/
14-
lazySizes.cfg.nativeLoading = {
15-
setLoadingAttribute: false,
16-
}
7+
import { unregisterBlockStyle, getBlockVariations, getBlockType, unregisterBlockVariation } from '@wordpress/blocks'
8+
import './utils/beapi'
179

18-
// Native Gutenberg
19-
domReady(() => {
20-
// Disable specific block styles
21-
if (BFFEditorSettings.disabledBlocksStyles) {
22-
Object.entries(BFFEditorSettings.disabledBlocksStyles).forEach(([block, styles]) => {
23-
unregisterBlockStyle(block, styles)
10+
const unregisterDisabledBlockStyles = () => {
11+
if (!BEAPI_EDITOR_SETTINGS.disabledBlocksStyles) {
12+
return
13+
}
14+
15+
Object.entries(BEAPI_EDITOR_SETTINGS.disabledBlocksStyles).forEach(([blockName, styles]) => {
16+
;[].concat(styles).forEach((styleName) => {
17+
unregisterBlockStyle(blockName, styleName)
2418
})
19+
})
20+
}
21+
22+
const unregisterDisallowedBlockVariations = () => {
23+
if (!BEAPI_EDITOR_SETTINGS.allowedBlocksVariations) {
24+
return
2525
}
2626

27-
// Allow blocks variations
28-
if (BFFEditorSettings.allowedBlocksVariations) {
29-
Object.entries(BFFEditorSettings.allowedBlocksVariations).forEach(([block, variations]) => {
30-
getBlockVariations(block).forEach((variant) => {
31-
if (!variations.includes(variant.name)) {
32-
unregisterBlockVariation(block, variant.name)
33-
}
34-
})
27+
Object.entries(BEAPI_EDITOR_SETTINGS.allowedBlocksVariations).forEach(([blockName, allowedVariationNames]) => {
28+
const blockVariations = getBlockVariations(blockName) || []
29+
30+
blockVariations.forEach((variation) => {
31+
if (!allowedVariationNames.includes(variation.name)) {
32+
unregisterBlockVariation(blockName, variation.name)
33+
}
3534
})
35+
})
36+
}
37+
38+
const whenBlocksRegistered = (blockNames, callback) => {
39+
const areBlocksReady = () => blockNames.every((blockName) => getBlockType(blockName))
40+
41+
if (areBlocksReady()) {
42+
callback()
43+
return
44+
}
45+
46+
const unsubscribe = subscribe(() => {
47+
if (!areBlocksReady()) {
48+
return
49+
}
50+
51+
unsubscribe()
52+
callback()
53+
})
54+
}
55+
56+
// Native Gutenberg
57+
domReady(() => {
58+
unregisterDisabledBlockStyles()
59+
60+
if (BEAPI_EDITOR_SETTINGS.allowedBlocksVariations) {
61+
const blockNames = Object.keys(BEAPI_EDITOR_SETTINGS.allowedBlocksVariations)
62+
63+
whenBlocksRegistered(blockNames, unregisterDisallowedBlockVariations)
3664
}
3765
})
3866

@@ -43,7 +71,7 @@ if (window.acf) {
4371

4472
addFilter('blocks.registerBlockType', 'beapi-framework', function (settings, name) {
4573
// Disable all styles
46-
if (BFFEditorSettings.disableAllBlocksStyles && BFFEditorSettings.disableAllBlocksStyles.includes(name)) {
74+
if (BEAPI_EDITOR_SETTINGS.disableAllBlocksStyles && BEAPI_EDITOR_SETTINGS.disableAllBlocksStyles.includes(name)) {
4775
settings.styles = []
4876
}
4977

0 commit comments

Comments
 (0)