Skip to content

Commit 277341f

Browse files
committed
fix: clean style object for comparison
1 parent 30cc1d3 commit 277341f

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

src/plugins/global-settings/typography/index.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { GlobalTypographyStyles } from './editor-loader'
55
import TypographyPicker from './typography-picker'
66
import { getThemeStyles } from './get-theme-styles'
77
import FREE_FONT_PAIRS from './font-pairs.json'
8-
import { getAppliedTypeScale } from './utils'
8+
import { getAppliedTypeScale, cleanTypographyStyle } from './utils'
99

1010
/**
1111
* External dependencies
@@ -313,13 +313,9 @@ addFilter( 'stackable.global-settings.inspector', 'stackable/global-typography',
313313
* Otherwise, the API will throw an error code 400
314314
* because of incompatible schema type.
315315
*/
316-
Object.keys( styles ).forEach( key => {
317-
if ( styles[ key ] === '' ) {
318-
delete styles[ key ]
319-
}
320-
} )
316+
const cleanStyles = cleanTypographyStyle( styles ) || {}
321317

322-
newSettings[ selector ] = styles
318+
newSettings[ selector ] = cleanStyles
323319
} )
324320

325321
// Update the global styles immediately when reset font size is triggered.
@@ -353,9 +349,10 @@ addFilter( 'stackable.global-settings.inspector', 'stackable/global-typography',
353349

354350
const getIsAllowReset = selector => {
355351
const currentFontPair = getCurrentFontPair()
356-
const typographyStyle = typographySettings[ selector ]
352+
const typographyStyle = typographySettings[ selector ] || {}
357353
if ( ! isEditingFontPair && currentFontPair ) {
358-
const fontPairStyle = currentFontPair.typography[ selector ]
354+
// Clean style object to be consistent with changeStyles operation
355+
const fontPairStyle = cleanTypographyStyle( currentFontPair.typography?.[ selector ] ) || {}
359356
if ( ! isEqual( fontPairStyle, typographyStyle ) && ! Array.isArray( typographyStyle ) ) {
360357
return true
361358
}
@@ -385,8 +382,9 @@ addFilter( 'stackable.global-settings.inspector', 'stackable/global-typography',
385382
if ( isEditingFontPair || ! currentFontPair ) {
386383
return false
387384
}
388-
const fontPairStyle = currentFontPair.typography[ selector ]
389-
const typographyStyle = typographySettings[ selector ]
385+
// Clean style object to be consistent with changeStyles operation
386+
const fontPairStyle = cleanTypographyStyle( currentFontPair.typography?.[ selector ] ) || {}
387+
const typographyStyle = typographySettings[ selector ] || {}
390388

391389
if ( ! Array.isArray( typographyStyle ) &&
392390
fontPairStyle.fontFamily &&

src/plugins/global-settings/typography/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,19 @@ export const getAppliedTypeScale = value => {
2727
'.stk-button__inner-text': { fontSize: '1', fontSizeUnit: 'rem' },
2828
}
2929
}
30+
31+
/**
32+
* Remove empty string properties from the given styles object
33+
*
34+
* @param {Object} styles - The typography styles object
35+
* @return {Object} An object with removed empty string properties
36+
*/
37+
38+
export const cleanTypographyStyle = styles => {
39+
if ( ! styles ) {
40+
return {}
41+
}
42+
return Object.fromEntries(
43+
Object.entries( styles ).filter( ( [ , value ] ) => value !== '' )
44+
)
45+
}

0 commit comments

Comments
 (0)