Skip to content

Commit 7b2697f

Browse files
committed
feat: warn what disabled blocks are needed in design library
1 parent 359a374 commit 7b2697f

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

src/block/column/block.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
"stackable/tab-content"
2020
],
2121
"textdomain": "stackable-ultimate-gutenberg-blocks",
22-
"stk-type": "hidden"
22+
"stk-type": "hidden",
23+
"stk-cannot-be-disabled": true
2324
}

src/block/design-library/edit.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
* Internal dependencies
33
*/
44
import previewImage from './images/preview.jpg'
5-
import { i18n, srcUrl } from 'stackable'
5+
import {
6+
i18n,
7+
srcUrl,
8+
settings,
9+
} from 'stackable'
610
import {
711
Button,
812
ModalDesignLibrary,
@@ -11,7 +15,7 @@ import { SVGStackableIcon } from '~stackable/icons'
1115
import {
1216
deprecateBlockBackgroundColorOpacity, deprecateContainerBackgroundColorOpacity, deprecateTypographyGradientColor,
1317
} from '~stackable/block-components'
14-
import { substituteCoreIfDisabled } from '~stackable/util'
18+
import { substituteCoreIfDisabled, BLOCK_STATE } from '~stackable/util'
1519
import { substitutionRules } from '../../blocks'
1620

1721
/**
@@ -29,14 +33,30 @@ import { useBlockProps } from '@wordpress/block-editor'
2933

3034
// Replaces the current block with a block made out of attributes.
3135
const createBlockWithAttributes = ( blockName, attributes, innerBlocks, design ) => {
36+
const disabledBlocks = settings.stackable_block_states || {} // eslint-disable-line camelcase
37+
const nonSubstitutableBlocks = new Set()
38+
3239
// Recursively substitute core blocks to disabled Stackable blocks
3340
const traverseBlocksAndSubstitute = blocks => {
3441
return blocks.map( block => {
3542
let [ blockName, blockAttributes, innerBlocks ] = block
43+
3644
if ( innerBlocks && innerBlocks.length > 0 ) {
3745
innerBlocks = traverseBlocksAndSubstitute( innerBlocks )
3846
}
47+
3948
const substituted = substituteCoreIfDisabled( blockName, blockAttributes, innerBlocks, substitutionRules )
49+
const blockType = getBlockType( blockName )
50+
51+
// Get the disabled non-substitutable blocks
52+
if ( ! ( blockName in substitutionRules ) &&
53+
( ! blockType || blockType[ 'stk-cannot-be-disabled' ] !== true ) &&
54+
blockName in disabledBlocks &&
55+
disabledBlocks[ blockName ] === BLOCK_STATE.DISABLED
56+
) {
57+
nonSubstitutableBlocks.add( blockName )
58+
}
59+
4060
if ( ! Array.isArray( substituted[ 2 ] ) ) {
4161
substituted[ 2 ] = []
4262
}
@@ -45,6 +65,17 @@ const createBlockWithAttributes = ( blockName, attributes, innerBlocks, design )
4565
}
4666

4767
innerBlocks = traverseBlocksAndSubstitute( innerBlocks )
68+
69+
// Warn the user which blocks are needed to be enabled
70+
if ( nonSubstitutableBlocks.size > 0 ) {
71+
// eslint-disable no-console
72+
console.warn( // eslint-disable-line no-console
73+
`The selected design requires the following block(s) to be enabled for it to function properly:\n\n` +
74+
`${ Array.from( nonSubstitutableBlocks ).join( '\n' ) }`
75+
)
76+
// eslint-enable no-console
77+
}
78+
4879
// const { replaceBlock } = dispatch( 'core/block-editor' )
4980

5081
// For wireframes, we'll need to apply any default block attributes to

src/util/blocks.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,15 +529,15 @@ export const registerBlockType = ( name, _settings ) => {
529529
* @return {Array} The resulting block definition
530530
*/
531531
export const substituteCoreIfDisabled = ( blockName, blockAttributes, innerBlocks, substitutionRules ) => {
532-
const disabled_blocks = stackableSettings.stackable_block_states || {} // eslint-disable-line camelcase
532+
const disabledBlocks = stackableSettings.stackable_block_states || {} // eslint-disable-line camelcase
533533

534534
if ( substitutionRules && blockName in substitutionRules ) {
535535
const substitutionRule = substitutionRules[ blockName ]
536536
// If a block have variants, let the the transform handle all the substitution
537537
if ( 'variants' in substitutionRule ) {
538-
return substitutionRule.transform( blockAttributes, innerBlocks, disabled_blocks )
538+
return substitutionRule.transform( blockAttributes, innerBlocks, disabledBlocks )
539539
}
540-
if ( blockName in disabled_blocks && disabled_blocks[ blockName ] === BLOCK_STATE.DISABLED ) { // eslint-disable-line camelcase
540+
if ( blockName in disabledBlocks && disabledBlocks[ blockName ] === BLOCK_STATE.DISABLED ) { // eslint-disable-line camelcase
541541
return [ substitutionRule.to, substitutionRule.transform( blockAttributes, innerBlocks ) ]
542542
}
543543
}

0 commit comments

Comments
 (0)