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). */
64import domReady from '@wordpress/dom-ready'
5+ import { subscribe } from '@wordpress/data'
76import { 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
4472addFilter ( '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