@@ -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