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). */
44import domReady from '@wordpress/dom-ready' ;
5+ import { subscribe } from '@wordpress/data' ;
56import { addFilter } from '@wordpress/hooks' ;
67import {
78 unregisterBlockStyle ,
89 getBlockVariations ,
10+ getBlockType ,
911 unregisterBlockVariation ,
1012} from '@wordpress/blocks' ;
1113import './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
1467domReady ( ( ) => {
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