Skip to content

Commit

Permalink
fix generatedCss
Browse files Browse the repository at this point in the history
  • Loading branch information
mxkae committed Sep 4, 2024
1 parent 217dbf2 commit abd5594
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
36 changes: 31 additions & 5 deletions src/components/block-css/css-save-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class CssSaveCompiler {
this.styles = null
this.isDirty = false
this.previousCss = ''
this.addedStyles = {}
}

addStyle( selector, rule, value = undefined, device = 'desktop' ) {
Expand All @@ -34,13 +35,30 @@ class CssSaveCompiler {
this.styles[ device ][ selector ][ rule ] = value
this.isDirty = true // Make sure our next compile will return a new result.
}

this.addedStyles[ `${ device }|${ selector }|${ rule }` ] = true
}

removeStyle( selector, rule, device = 'desktop' ) {
if ( this.styles && this.styles[ device ] && this.styles[ device ][ selector ] && this.styles[ device ][ selector ][ rule ] ) {
delete this.styles[ device ][ selector ][ rule ]
this.isDirty = true
}
}

checkUnremovedStyles() {
const hasUnremovedStyles = Object.values( this.addedStyles ).some( val => val === false )
if ( hasUnremovedStyles ) {
this.isDirty = true
}
}

// Compile all this.styles into a single string.
compile() {
if ( ! this.styles ) {
return ''
}
this.checkUnremovedStyles()

// If nothing was added, then just return the previous results
if ( ! this.isDirty ) {
Expand All @@ -62,15 +80,23 @@ class CssSaveCompiler {
let selectorCss = ''
const rules = Object.keys( this.styles[ device ][ selector ] )
rules.forEach( rule => {
const value = this.styles[ device ][ selector ][ rule ]
selectorCss += `${ rule }:${ value };`
// If the style was not added, then delete it from this.styles
if ( this.addedStyles[ `${ device }|${ selector }|${ rule }` ] === false ) {
delete this.styles[ device ][ selector ][ rule ]
} else {
const value = this.styles[ device ][ selector ][ rule ]
selectorCss += `${ rule }:${ value };`
// Reset the flag
this.addedStyles[ `${ device }|${ selector }|${ rule }` ] = false
}
} )

css += `${ selector }{${ selectorCss }}`
if ( selectorCss ) {
css += `${ selector }{${ selectorCss }}`
}
} )

const mediaQuery = getMediaQuery( device )
if ( mediaQuery ) {
if ( mediaQuery && css ) {
css = `${ mediaQuery }{${ css }}`
}

Expand Down
19 changes: 18 additions & 1 deletion src/components/block-css/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ const BlockCss = props => {
}

// Skip everything if all values are null.
if ( typeof valueDesktop === 'undefined' &&
if ( props.editorMode &&
typeof valueDesktop === 'undefined' &&
typeof valueDesktopHover === 'undefined' &&
typeof valueDesktopParentHover === 'undefined' &&
typeof valueDesktopCollapsed === 'undefined' &&
Expand Down Expand Up @@ -381,6 +382,22 @@ const BlockCss = props => {
collapsedSelector = prependCSSClass( collapsedSelector, blockUniqueClassName, blockUniqueClassName, editorMode ? '.editor-styles-wrapper' : '' )
}

if ( ! props.editorMode &&
typeof valueDesktop === 'undefined' &&
typeof valueDesktopHover === 'undefined' &&
typeof valueDesktopParentHover === 'undefined' &&
typeof valueDesktopCollapsed === 'undefined' &&
typeof valueTablet === 'undefined' &&
typeof valueTabletHover === 'undefined' &&
typeof valueTabletParentHover === 'undefined' &&
typeof valueTabletCollapsed === 'undefined' &&
typeof valueMobile === 'undefined' &&
typeof valueMobileHover === 'undefined' &&
typeof valueMobileParentHover === 'undefined' &&
typeof valueMobileCollapsed === 'undefined' ) {
compileCssTo.removeStyle( selector, styleRule, desktopQuery )
return null
}
let css = ''

// If rendering for the ditor, output the css, if saving, compile css to an object.
Expand Down

0 comments on commit abd5594

Please sign in to comment.