diff --git a/inc/Services/Editor.php b/inc/Services/Editor.php index efcc6e1..b650df1 100644 --- a/inc/Services/Editor.php +++ b/inc/Services/Editor.php @@ -262,7 +262,7 @@ public function admin_editor_script(): void { $this->assets_tools->add_inline_script( 'theme-admin-editor-script', - 'const BFFEditorSettings = ' . wp_json_encode( + 'const BEAPI_EDITOR_SETTINGS = ' . wp_json_encode( apply_filters( 'bff_editor_custom_settings', [ diff --git a/src/js/common/editor.js b/src/js/common/editor.js index 7b36b46..1580d54 100644 --- a/src/js/common/editor.js +++ b/src/js/common/editor.js @@ -1,37 +1,78 @@ -/* global BFFEditorSettings */ +/* global BEAPI_EDITOR_SETTINGS */ -/* Customize BFFEditorSettings in inc/Services/Editor.php or with `bff_editor_custom_settings` filter (see readme). */ +/* Customize BEAPI_EDITOR_SETTINGS in inc/Services/Editor.php or with `bff_editor_custom_settings` filter (see readme). */ import domReady from '@wordpress/dom-ready'; +import { subscribe } from '@wordpress/data'; import { addFilter } from '@wordpress/hooks'; import { unregisterBlockStyle, getBlockVariations, + getBlockType, unregisterBlockVariation, } from '@wordpress/blocks'; import './utils/beapi'; +const unregisterDisabledBlockStyles = () => { + if (!BEAPI_EDITOR_SETTINGS.disabledBlocksStyles) { + return; + } + + Object.entries(BEAPI_EDITOR_SETTINGS.disabledBlocksStyles).forEach( + ([blockName, styles]) => { + [].concat(styles).forEach((styleName) => { + unregisterBlockStyle(blockName, styleName); + }); + } + ); +}; + +const unregisterDisallowedBlockVariations = () => { + if (!BEAPI_EDITOR_SETTINGS.allowedBlocksVariations) { + return; + } + + Object.entries(BEAPI_EDITOR_SETTINGS.allowedBlocksVariations).forEach( + ([blockName, allowedVariationNames]) => { + const blockVariations = getBlockVariations(blockName) || []; + + blockVariations.forEach((variation) => { + if (!allowedVariationNames.includes(variation.name)) { + unregisterBlockVariation(blockName, variation.name); + } + }); + } + ); +}; + +const whenBlocksRegistered = (blockNames, callback) => { + const areBlocksReady = () => + blockNames.every((blockName) => getBlockType(blockName)); + + if (areBlocksReady()) { + callback(); + return; + } + + const unsubscribe = subscribe(() => { + if (!areBlocksReady()) { + return; + } + + unsubscribe(); + callback(); + }); +}; + // Native Gutenberg domReady(() => { - // Disable specific block styles - if (BFFEditorSettings.disabledBlocksStyles) { - Object.entries(BFFEditorSettings.disabledBlocksStyles).forEach( - ([block, styles]) => { - unregisterBlockStyle(block, styles); - } - ); - } + unregisterDisabledBlockStyles(); - // Allow blocks variations - if (BFFEditorSettings.allowedBlocksVariations) { - Object.entries(BFFEditorSettings.allowedBlocksVariations).forEach( - ([block, variations]) => { - getBlockVariations(block).forEach((variant) => { - if (!variations.includes(variant.name)) { - unregisterBlockVariation(block, variant.name); - } - }); - } + if (BEAPI_EDITOR_SETTINGS.allowedBlocksVariations) { + const blockNames = Object.keys( + BEAPI_EDITOR_SETTINGS.allowedBlocksVariations ); + + whenBlocksRegistered(blockNames, unregisterDisallowedBlockVariations); } }); @@ -46,8 +87,8 @@ addFilter( function (settings, name) { // Disable all styles if ( - BFFEditorSettings.disableAllBlocksStyles && - BFFEditorSettings.disableAllBlocksStyles.includes(name) + BEAPI_EDITOR_SETTINGS.disableAllBlocksStyles && + BEAPI_EDITOR_SETTINGS.disableAllBlocksStyles.includes(name) ) { settings.styles = []; }