|
2 | 2 |
|
3 | 3 | /* Customize BFFEditorSettings in inc/Services/Editor.php or with `bff_editor_custom_settings` filter (see readme). */ |
4 | 4 | import domReady from '@wordpress/dom-ready'; |
| 5 | +import { subscribe } from '@wordpress/data'; |
5 | 6 | import { addFilter } from '@wordpress/hooks'; |
6 | 7 | import { |
7 | 8 | unregisterBlockStyle, |
8 | 9 | getBlockVariations, |
| 10 | + getBlockType, |
9 | 11 | unregisterBlockVariation, |
10 | 12 | } from '@wordpress/blocks'; |
11 | 13 | import './utils/beapi'; |
12 | 14 |
|
| 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 | + |
13 | 66 | // Native Gutenberg |
14 | 67 | 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(); |
23 | 69 |
|
24 | | - // Allow blocks variations |
25 | 70 | 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 |
34 | 73 | ); |
| 74 | + |
| 75 | + whenBlocksRegistered(blockNames, unregisterDisallowedBlockVariations); |
35 | 76 | } |
36 | 77 | }); |
37 | 78 |
|
|
0 commit comments