Skip to content

Commit f27d809

Browse files
committed
feat: allow substitution of core blocks, console error for blocks with no substitution
1 parent 2ed9e67 commit f27d809

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/block/design-library/edit.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ import { SVGStackableIcon } from '~stackable/icons'
1111
import {
1212
deprecateBlockBackgroundColorOpacity, deprecateContainerBackgroundColorOpacity, deprecateTypographyGradientColor,
1313
} from '~stackable/block-components'
14+
import { substituteCoreIfDisabled } from '~stackable/util'
15+
import { substitutionRules } from '../../blocks'
1416

1517
/**
1618
* WordPress dependencies
1719
*/
1820
import { __ } from '@wordpress/i18n'
1921
import { dispatch } from '@wordpress/data'
2022
import {
21-
createBlock, parse, createBlocksFromInnerBlocksTemplate, getBlockVariations,
23+
createBlock, parse, createBlocksFromInnerBlocksTemplate, getBlockVariations, getBlockType,
2224
} from '@wordpress/blocks'
2325
import { useState } from '@wordpress/element'
2426
import { addFilter, applyFilters } from '@wordpress/hooks'
@@ -27,6 +29,22 @@ import { useBlockProps } from '@wordpress/block-editor'
2729

2830
// Replaces the current block with a block made out of attributes.
2931
const createBlockWithAttributes = ( blockName, attributes, innerBlocks, design ) => {
32+
// Recursively substitute core blocks to disabled Stackable blocks
33+
const traverseBlocksAndSubstitute = blocks => {
34+
return blocks.map( block => {
35+
let [ blockName, blockAttributes, innerBlocks ] = block
36+
if ( innerBlocks && innerBlocks.length > 0 ) {
37+
innerBlocks = traverseBlocksAndSubstitute( innerBlocks )
38+
}
39+
const substituted = substituteCoreIfDisabled( blockName, blockAttributes, innerBlocks, substitutionRules )
40+
if ( ! Array.isArray( substituted[ 2 ] ) ) {
41+
substituted[ 2 ] = []
42+
}
43+
return substituted
44+
} )
45+
}
46+
47+
innerBlocks = traverseBlocksAndSubstitute( innerBlocks )
3048
// const { replaceBlock } = dispatch( 'core/block-editor' )
3149

3250
// For wireframes, we'll need to apply any default block attributes to
@@ -44,9 +62,9 @@ const createBlockWithAttributes = ( blockName, attributes, innerBlocks, design )
4462
blocks.forEach( block => {
4563
const blockName = block[ 0 ]
4664

47-
// For blocks with varitions, do not remove the uniqueId
65+
// For blocks with variations, do not remove the uniqueId
4866
// since that will prompt the layout picker to show.
49-
const hasVariations = getBlockVariations( blockName ).length > 0
67+
const hasVariations = !! getBlockType( blockName ) && getBlockVariations( blockName ).length > 0
5068
if ( ! hasVariations && block[ 1 ].uniqueId ) {
5169
delete block[ 1 ].uniqueId
5270
}

src/util/blocks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,5 +545,5 @@ export const substituteCoreIfDisabled = ( blockName, blockAttributes, innerBlock
545545
if ( innerBlocks ) {
546546
return [ blockName, blockAttributes, innerBlocks ]
547547
}
548-
return [ blockName, blockAttributes ]
548+
return [ blockName, blockAttributes, [] ]
549549
}

0 commit comments

Comments
 (0)