11import heroBg from './images/hero-bg.webp'
2- import { i18n , srcUrl } from 'stackable'
3- import { cleanSerializedBlock , createUniqueClass } from '~stackable/util'
2+ import profile from './images/profile.webp'
3+ import {
4+ i18n , srcUrl , version as VERSION ,
5+ } from 'stackable'
6+
7+ import { CssSaveCompiler } from '../block-css'
8+ import {
9+ cleanSerializedBlock ,
10+ createUniqueClass ,
11+ blockStyleGenerators ,
12+ } from '~stackable/util'
13+ import { PLACEHOLDER_INNER_BLOCKS } from '~stackable/util/block-templates'
414
515import { __ } from '@wordpress/i18n'
616import { RawHTML } from '@wordpress/element'
@@ -9,7 +19,6 @@ import {
919 createBlocksFromInnerBlocksTemplate ,
1020 getBlockVariations ,
1121} from '@wordpress/blocks'
12- import { PLACEHOLDER_INNER_BLOCKS } from '~stackable/util/block-templates'
1322
1423/* eslint-disable jsx-a11y/anchor-is-valid */
1524export const DefaultButton = ( {
@@ -90,19 +99,51 @@ const ADDITIONAL_ATTRIBUTES = {
9099 'stackable/number-box' : { text : __ ( '1' , i18n ) } ,
91100}
92101
102+ const getGeneratedCss = ( blocks , generateForInnerBlocks = false ) => {
103+ return blocks . map ( block => {
104+ if ( ! block . attributes . uniqueId ) {
105+ block . attributes . uniqueId = createUniqueClass ( block . clientId )
106+ }
107+
108+ const blockStyleGenerator = blockStyleGenerators [ block . name ]
109+ const attrNamesWithValues = blockStyleGenerator . getAttributesWithValues ( block . attributes )
110+ const blockStyleDefs = blockStyleGenerator . getBlockStyles ( attrNamesWithValues )
111+
112+ const cssCompiler = new CssSaveCompiler ( )
113+ const saveCss = blockStyleGenerator . generateBlockStylesForSave (
114+ cssCompiler ,
115+ block . attributes ,
116+ blockStyleDefs ,
117+ {
118+ version : VERSION ,
119+ }
120+ )
121+
122+ block . attributes . generatedCss = saveCss
123+
124+ if ( generateForInnerBlocks ) {
125+ block . innerBlocks = getGeneratedCss ( block . innerBlocks , generateForInnerBlocks )
126+ }
127+
128+ return block
129+ } )
130+ }
131+
93132export const RenderBlock = props => {
94133 const {
95134 blockName, attributes, innerBlocks, name = __ ( 'Default' , i18n ) ,
96135 } = props
97136
98137 const block = createBlock ( blockName , attributes , innerBlocks )
99- block . attributes . uniqueId = createUniqueClass ( block . clientId )
100- let serialized = serialize ( [ block ] )
138+ const newBlock = getGeneratedCss ( [ block ] )
139+
140+ let serialized = serialize ( newBlock )
101141
102142 if ( blockName === 'stackable/timeline' ) {
103143 const _block = createBlock ( blockName , attributes , innerBlocks )
104144 _block . attributes . timelineIsLast = true
105- serialized += '\n' + serialize ( [ _block ] )
145+ const duplicateBlock = getGeneratedCss ( [ _block ] )
146+ serialized += '\n' + serialize ( duplicateBlock )
106147 }
107148
108149 return (
@@ -113,6 +154,19 @@ export const RenderBlock = props => {
113154 )
114155}
115156
157+ const INNER_BLOCK_CALLBACKS = {
158+ 'stackable/team-member' : innerBlocks => {
159+ innerBlocks [ 0 ] . attributes . imageExternalUrl = `${ srcUrl } /${ profile } `
160+
161+ return innerBlocks
162+ } ,
163+ 'stackable/testimonial' : innerBlocks => {
164+ innerBlocks [ 1 ] . attributes . imageExternalUrl = `${ srcUrl } /${ profile } `
165+
166+ return innerBlocks
167+ } ,
168+ }
169+
116170export const getPlaceholders = blockName => {
117171 let innerBlocks = [ ]
118172 let attributes = { }
@@ -123,6 +177,14 @@ export const getPlaceholders = blockName => {
123177 innerBlocks = createBlocksFromInnerBlocksTemplate ( PLACEHOLDER_INNER_BLOCKS [ blockName ] )
124178 } else if ( variations . length && variations [ 0 ] . innerBlocks ?. length ) {
125179 innerBlocks = createBlocksFromInnerBlocksTemplate ( variations [ 0 ] . innerBlocks )
180+
181+ if ( blockName in INNER_BLOCK_CALLBACKS ) {
182+ innerBlocks = INNER_BLOCK_CALLBACKS [ blockName ] ( innerBlocks )
183+ }
184+ }
185+
186+ if ( innerBlocks . length ) {
187+ innerBlocks = getGeneratedCss ( innerBlocks , true )
126188 }
127189
128190 if ( variations . length && variations [ 0 ] . attributes ) {
@@ -136,5 +198,7 @@ export const getPlaceholders = blockName => {
136198 }
137199 }
138200
139- return { attributes, innerBlocks }
201+ return {
202+ attributes, innerBlocks,
203+ }
140204}
0 commit comments