Skip to content

Commit c158a40

Browse files
committed
fix: add presets to contentHeight and innerBlockRowGap
1 parent 6ed023b commit c158a40

File tree

7 files changed

+118
-11
lines changed

7 files changed

+118
-11
lines changed

src/block-components/alignment/attributes.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
export const addAttributes = attrObject => {
1+
import { deprecatedAddAttributes } from './deprecated/index'
2+
3+
export const addAttributes = ( attrObject, attrNameTemplate = '%s' ) => {
4+
deprecatedAddAttributes( attrObject, attrNameTemplate )
5+
26
// Assume that the block uses the BlockDiv Block Component and has a
37
// uniqueId attribute
48
attrObject.add( {
@@ -53,13 +57,26 @@ export const addAttributes = attrObject => {
5357
type: 'number',
5458
default: '',
5559
},
60+
},
61+
versionAdded: '3.0.0',
62+
versionDeprecated: '',
63+
} )
64+
65+
attrObject.add( {
66+
attributes: {
5667
innerBlockRowGap: {
5768
stkResponsive: true,
58-
type: 'number',
69+
type: 'string',
70+
default: '',
71+
},
72+
containerHeight: {
73+
stkResponsive: true,
74+
type: 'string',
5975
default: '',
6076
},
6177
},
62-
versionAdded: '3.0.0',
78+
attrNameTemplate,
79+
versionAdded: '3.15.3',
6380
versionDeprecated: '',
6481
} )
6582
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { getAttrNameFunction } from '~stackable/util'
2+
3+
export const deprecatedAddAttributes = ( attrObject, attrNameTemplate = '%s' ) => {
4+
attrObject.add( {
5+
attributes: {
6+
innerBlockRowGap: {
7+
stkResponsive: true,
8+
type: 'number',
9+
default: '',
10+
},
11+
},
12+
attrNameTemplate,
13+
versionAdded: '3.0.0',
14+
versionDeprecated: '3.15.3',
15+
} )
16+
}
17+
18+
export const deprecateInnerBlockRowGapAndContainerHeight = {
19+
isEligible: attrNameTemplate => attributes => {
20+
const getAttrName = getAttrNameFunction( attrNameTemplate )
21+
const getAttribute = _attrName => attributes[ getAttrName( _attrName ) ]
22+
23+
const containerHeight = getAttribute( 'containerHeight' )
24+
const innerBlockRowGap = getAttribute( 'innerBlockRowGap' )
25+
26+
return typeof containerHeight === 'number' || typeof innerBlockRowGap === 'number'
27+
},
28+
migrate: attrNameTemplate => attributes => {
29+
const getAttrName = getAttrNameFunction( attrNameTemplate )
30+
const getAttribute = _attrName => attributes[ getAttrName( _attrName ) ]
31+
32+
const containerHeight = getAttribute( 'containerHeight' )
33+
const innerBlockRowGap = getAttribute( 'innerBlockRowGap' )
34+
35+
const newAttributes = {
36+
...attributes,
37+
[ getAttrName( 'containerHeight' ) ]: String( containerHeight ),
38+
[ getAttrName( 'innerBlockRowGap' ) ]: String( innerBlockRowGap ),
39+
}
40+
41+
return newAttributes
42+
},
43+
}
44+

src/block-components/alignment/edit.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
useBlockAttributesContext,
1414
useBlockSetAttributesContext,
1515
useDeviceType,
16+
usePresetControls,
1617
} from '~stackable/hooks'
1718

1819
/**
@@ -86,6 +87,9 @@ export const Edit = memo( props => {
8687
enableContentAlign = true,
8788
} = props
8889

90+
const blockHeightMarks = usePresetControls( 'blockHeights' )?.getPresetMarks() || null
91+
const spacingSizeMarks = usePresetControls( 'spacingSizes' )?.getPresetMarks() || null
92+
8993
const containerSize = props.hasContainerSize && <>
9094
<ControlSeparator />
9195
{ props.hasContainerHeight &&
@@ -100,6 +104,7 @@ export const Edit = memo( props => {
100104
allowReset={ true }
101105
placeholder="0"
102106
visualGuide={ { selector: '.stk-%s-container', highlight: 'outline' } }
107+
marks={ blockHeightMarks }
103108
/>
104109
}
105110
<AdvancedRangeControl
@@ -331,6 +336,7 @@ export const Edit = memo( props => {
331336
highlight: 'row-gap',
332337
value: innerBlockRowGap,
333338
} }
339+
marks={ spacingSizeMarks }
334340
/>
335341
}
336342
{ ( innerBlockOrientation && innerBlockWrap === 'wrap' ) &&

src/block-components/alignment/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { addStyles } from './style'
44

55
export * from './use-alignment'
66

7+
export { deprecateInnerBlockRowGapAndContainerHeight } from './deprecated/index'
8+
79
export const Alignment = () => {
810
return null
911
}

src/block-components/alignment/style.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,14 @@ export const addStyles = ( blockStyleGenerator, props = {} ) => {
211211
styleRule: 'columnGap',
212212
attrName: 'innerBlockColumnGap',
213213
key: 'innerBlockColumnGapEdit',
214-
format: `%spx`,
215214
responsive: 'all',
215+
valueCallback: value => {
216+
// Substitute with using format to work with preset controls
217+
if ( typeof value === 'string' && value.startsWith( 'var' ) ) {
218+
return value
219+
}
220+
return value + 'px'
221+
},
216222
enabledCallback: getAttribute => getAttribute( 'innerBlockOrientation' ) === 'horizontal',
217223
dependencies: [
218224
'innerBlockOrientation',
@@ -225,8 +231,14 @@ export const addStyles = ( blockStyleGenerator, props = {} ) => {
225231
styleRule: 'columnGap',
226232
attrName: 'innerBlockColumnGap',
227233
key: 'innerBlockColumnGapSave',
228-
format: `%spx`,
229234
responsive: 'all',
235+
valueCallback: value => {
236+
// Substitute with using format to work with preset controls
237+
if ( typeof value === 'string' && value.startsWith( 'var' ) ) {
238+
return value
239+
}
240+
return value + 'px'
241+
},
230242
enabledCallback: getAttribute => getAttribute( 'innerBlockOrientation' ) === 'horizontal',
231243
dependencies: [
232244
'innerBlockOrientation',
@@ -241,12 +253,18 @@ export const addStyles = ( blockStyleGenerator, props = {} ) => {
241253
styleRule: 'rowGap',
242254
attrName: 'innerBlockRowGap',
243255
key: 'innerBlockRowGapEdit',
244-
format: `%spx`,
245256
responsive: 'all',
246257
enabledCallback: getAttribute => {
247258
return getAttribute( 'innerBlockOrientation' ) !== 'horizontal' ||
248259
( getAttribute( 'innerBlockOrientation' ) === 'horizontal' && getAttribute( 'innerBlockWrap' ) === 'wrap' )
249260
},
261+
valueCallback: value => {
262+
// Substitute with using format to work with preset controls
263+
if ( typeof value === 'string' && value.startsWith( 'var' ) ) {
264+
return value
265+
}
266+
return value + 'px'
267+
},
250268
dependencies: [
251269
'innerBlockOrientation',
252270
'innerBlockWrap',
@@ -259,8 +277,14 @@ export const addStyles = ( blockStyleGenerator, props = {} ) => {
259277
styleRule: 'rowGap',
260278
attrName: 'innerBlockRowGap',
261279
key: 'innerBlockRowGapSave',
262-
format: `%spx`,
263280
responsive: 'all',
281+
valueCallback: value => {
282+
// Substitute with using format to work with preset controls
283+
if ( typeof value === 'string' && value.startsWith( 'var' ) ) {
284+
return value
285+
}
286+
return value + 'px'
287+
},
264288
enabledCallback: getAttribute => {
265289
return getAttribute( 'innerBlockOrientation' ) !== 'horizontal' ||
266290
( getAttribute( 'innerBlockOrientation' ) === 'horizontal' && getAttribute( 'innerBlockWrap' ) === 'wrap' )

src/components/advanced-range-control/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,15 @@ const AdvancedRangeControl = props => {
248248
const currentSize = props.marks.find( mark => {
249249
return derivedValue === mark.value
250250
} )?.size
251-
const [ _newValue, _unit ] = extractNumbersAndUnits( currentSize )[ 0 ]
252-
rangeValue = _newValue
251+
let [ _newValue, _unit ] = extractNumbersAndUnits( currentSize )[ 0 ]
252+
253+
// If the attribute has no support for rem or em, and the
254+
// preset units is rem or em, convert to px
255+
const converted = convertToPxIfUnsupported( props.units, _unit, _newValue )
256+
_newValue = converted.value
257+
_unit = converted.unit
258+
259+
rangeValue = parseFloat( _newValue )
253260

254261
if ( _unit && ! isConversionDone.current ) {
255262
dispatch( 'core/block-editor' ).__unstableMarkNextChangeAsNotPersistent()

src/components/four-range-control/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,15 @@ const FourRangeControl = memo( props => {
418418
const currentSize = props.marks.find( mark => {
419419
return initialValue === mark.value
420420
} )?.size
421-
const [ _newValue, _unit ] = extractNumbersAndUnits( currentSize )[ 0 ]
422-
rangeValue = _newValue
421+
let [ _newValue, _unit ] = extractNumbersAndUnits( currentSize )[ 0 ]
422+
423+
// If the attribute has no support for rem or em, and the
424+
// preset units is rem or em, convert to px
425+
const converted = convertToPxIfUnsupported( props.units, _unit, _newValue )
426+
_newValue = converted.value
427+
_unit = converted.unit
428+
429+
rangeValue = parseFloat( _newValue )
423430

424431
if ( _unit && conversionKey && ! isConversionDone.current[ conversionKey ] ) {
425432
isConversionDone.current[ conversionKey ] = true

0 commit comments

Comments
 (0)