Skip to content

Commit ddf8c37

Browse files
committed
fix: import substitution rules and apply
1 parent 76e9075 commit ddf8c37

File tree

3 files changed

+31
-70
lines changed

3 files changed

+31
-70
lines changed

src/blocks.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
BLOCK_STATE,
2323
} from '~stackable/util'
2424
import { withVisualGuideContext } from '~stackable/higher-order'
25+
import { omit } from 'lodash'
2526

2627
/**
2728
* WordPress dependencies
@@ -33,6 +34,19 @@ import { addFilter } from '@wordpress/hooks'
3334
// Register our block category.
3435
addStackableBlockCategory()
3536

37+
// Fetch all substitution rules before registering
38+
const fetchSubstitutionRules = r => {
39+
const substitutionRules = {}
40+
r.keys().forEach( key => {
41+
const { substitute } = r( key )
42+
if ( ! substitute ) {
43+
return
44+
}
45+
substitutionRules[ substitute.from ] = omit( substitute, 'from' )
46+
} )
47+
return substitutionRules
48+
}
49+
3650
// Register all the blocks found
3751
const importAllAndRegister = r => {
3852
r.keys().forEach( key => {
@@ -71,4 +85,6 @@ addFilter( 'stackable.registerBlockType.edit', 'stackable', edit => {
7185
return withVisualGuideContext( edit )
7286
} )
7387

88+
export const substitutionRules = fetchSubstitutionRules( require.context( './block', true, /substitute\.js$/ ) )
89+
7490
importAllAndRegister( require.context( './block', true, /index\.js$/ ) )

src/disabled-blocks.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
BLOCK_STATE,
99
substituteCoreIfDisabled,
1010
} from '~stackable/util'
11-
import _ from 'lodash'
11+
import { substitutionRules } from './blocks'
12+
import { cloneDeep } from 'lodash'
1213

1314
const getDefaultVariation = variations => {
1415
return variations?.find( ( { isDefault } ) => isDefault )?.name
@@ -34,7 +35,7 @@ const traverseBlocksAndSubstitute = ( blocks, whitelist ) => {
3435
}
3536

3637
if ( whitelist.includes( blockName ) ) {
37-
return substituteCoreIfDisabled( blockName, blockAttributes, innerBlocks )
38+
return substituteCoreIfDisabled( blockName, blockAttributes, innerBlocks, substitutionRules )
3839
}
3940

4041
if ( innerBlocks ) {
@@ -83,7 +84,7 @@ const applySettingsToMeta = metadata => {
8384
const whitelist = metadata[ 'stk-substitution-blocks' ]
8485
if ( whitelist ) {
8586
variations = variations.map( variation => {
86-
const newVariation = _.cloneDeep( variation )
87+
const newVariation = cloneDeep( variation )
8788
if ( newVariation.innerBlocks && Array.isArray( newVariation.innerBlocks ) ) {
8889
newVariation.innerBlocks = traverseBlocksAndSubstitute( newVariation.innerBlocks, whitelist )
8990
}

src/util/blocks.js

Lines changed: 11 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -523,83 +523,27 @@ export const registerBlockType = ( name, _settings ) => {
523523
*
524524
* @param {string} blockName The block name
525525
* @param {Object} blockAttributes The block attributes
526-
* @param {Array} children The children blocks
526+
* @param {Array} innerBlocks The children blocks
527+
* @param {Object} substitutionRules The substitution rules for transforming from stackable to core blocks
527528
*
528529
* @return {Array} The resulting block definition
529530
*/
530-
export const substituteCoreIfDisabled = ( blockName, blockAttributes, children ) => {
531+
export const substituteCoreIfDisabled = ( blockName, blockAttributes, innerBlocks, substitutionRules ) => {
531532
const disabled_blocks = stackableSettings.stackable_block_states || {} // eslint-disable-line camelcase
532533

533-
if ( blockName === 'stackable/text' ) {
534-
if ( blockName in disabled_blocks && disabled_blocks[ blockName ] === BLOCK_STATE.DISABLED ) { // eslint-disable-line camelcase
535-
return [ 'core/paragraph', {
536-
content: blockAttributes.text,
537-
} ]
538-
}
539-
return [ 'stackable/text', blockAttributes ]
540-
}
541-
542-
if ( blockName === 'stackable/heading' ) {
543-
if ( blockName in disabled_blocks && disabled_blocks[ blockName ] === BLOCK_STATE.DISABLED ) { // eslint-disable-line camelcase
544-
return [ 'core/heading', {
545-
content: blockAttributes.text,
546-
level: blockAttributes.textTag ? Number( blockAttributes.textTag.replace( 'h', '' ) ) : 2,
547-
} ]
548-
}
549-
return [ 'stackable/heading', { ...blockAttributes } ]
550-
}
551-
552-
if ( blockName === 'stackable/subtitle' ) {
553-
if ( blockName in disabled_blocks && disabled_blocks[ blockName ] === BLOCK_STATE.DISABLED ) { // eslint-disable-line camelcase
554-
return [ 'core/paragraph', {
555-
content: blockAttributes.text,
556-
} ]
557-
}
558-
return [ 'stackable/subtitle', blockAttributes ]
559-
}
560-
561-
if ( blockName === 'stackable/button-group' ) {
562-
if ( 'stackable/button-group|button' in disabled_blocks && disabled_blocks[ 'stackable/button-group|button' ] === BLOCK_STATE.DISABLED ) { // eslint-disable-line camelcase
563-
return [ 'core/buttons', {}, children ]
564-
}
565-
if ( 'stackable/button-group|icon-button' in disabled_blocks && disabled_blocks[ 'stackable/button-group|icon-button' ] === BLOCK_STATE.DISABLED && // eslint-disable-line camelcase
566-
children.length &&
567-
children[ 0 ][ 0 ] === 'stackable/icon-button'
568-
) {
569-
return [ 'core/social-links',
570-
{ align: blockAttributes.contentAlign },
571-
[
572-
[ 'core/social-link', { service: 'facebook' } ],
573-
[ 'core/social-link', { service: 'twitter' } ],
574-
],
575-
]
576-
}
577-
return [ 'stackable/button-group', blockAttributes, children ]
578-
}
579-
580-
if ( blockName === 'stackable/button' ) {
581-
if ( 'stackable/button-group|button' in disabled_blocks && disabled_blocks[ 'stackable/button-group|button' ] === BLOCK_STATE.DISABLED ) { // eslint-disable-line camelcase
582-
return [ 'core/button', {
583-
text: blockAttributes.text,
584-
} ]
534+
if ( substitutionRules && blockName in substitutionRules ) {
535+
const substitutionRule = substitutionRules[ blockName ]
536+
// If a block have variants, let the the transform handle all the substitution
537+
if ( 'variants' in substitutionRule ) {
538+
return substitutionRule.transform( blockAttributes, innerBlocks, disabled_blocks )
585539
}
586-
return [ 'stackable/button', blockAttributes ]
587-
}
588-
589-
if ( blockName === 'stackable/image' ) {
590540
if ( blockName in disabled_blocks && disabled_blocks[ blockName ] === BLOCK_STATE.DISABLED ) { // eslint-disable-line camelcase
591-
if ( blockAttributes ) {
592-
return [ 'core/image', {
593-
height: blockAttributes.imageHeight,
594-
} ]
595-
}
596-
return [ 'core/image' ]
541+
return [ substitutionRule.to, substitutionRule.transform( blockAttributes, innerBlocks ) ]
597542
}
598-
return [ 'stackable/image', blockAttributes ]
599543
}
600544

601-
if ( children ) {
602-
return [ blockName, blockAttributes, children ]
545+
if ( innerBlocks ) {
546+
return [ blockName, blockAttributes, innerBlocks ]
603547
}
604548
return [ blockName, blockAttributes ]
605549
}

0 commit comments

Comments
 (0)