Skip to content

Commit a0022da

Browse files
Fix layer visibility toggle (GrapesJS#6564)
* Fix issue on toggling layer view * Update tests for moving inline styles from components to css rules * update style data variable tests
1 parent b0537b7 commit a0022da

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

packages/core/src/dom_components/model/Component.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ export default class Component extends StyleableModel<ComponentProperties> {
331331
cssc.addCollection(styles, { avoidUpdateStyle: true }, { group: `cmp:${type}` });
332332
}
333333

334+
this._moveInlineStyleToRule();
334335
this.__postAdd();
335336
this.init();
336337
isSymbol(this) && initSymbol(this);
@@ -847,8 +848,7 @@ export default class Component extends StyleableModel<ComponentProperties> {
847848
const state = em.get('state');
848849
const cc = em.Css;
849850
const propOrig = this.getStyle({ ...opts, skipResolve: true });
850-
const newStyle = { ...propOrig, ...prop };
851-
this.rule = cc.setIdRule(this.getId(), newStyle, { state, ...opts });
851+
this.rule = cc.setIdRule(this.getId(), prop, { state, ...opts });
852852
const diff = shallowDiff(propOrig, prop);
853853
this.set('style', '', { silent: true });
854854
keys(diff).forEach((pr) => this.trigger(`change:style:${pr}`));
@@ -2029,6 +2029,18 @@ export default class Component extends StyleableModel<ComponentProperties> {
20292029
return rule?.get('selectors')!.at(0);
20302030
}
20312031

2032+
private _moveInlineStyleToRule() {
2033+
const inlineStyle = this.get('style');
2034+
const hasInlineStyle =
2035+
(isString(inlineStyle) && inlineStyle.length > 0) ||
2036+
(isObject(inlineStyle) && Object.keys(inlineStyle).length > 0);
2037+
2038+
if (avoidInline(this.em) && hasInlineStyle) {
2039+
this.addStyle(inlineStyle);
2040+
this.set('style', '');
2041+
}
2042+
}
2043+
20322044
_idUpdated(m: any, v: any, opts: { idUpdate?: boolean } = {}) {
20332045
if (opts.idUpdate) return;
20342046

packages/core/test/specs/data_sources/model/StyleDataVariable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ describe('StyleDataVariable', () => {
246246
})[0];
247247
expect(cmp.getStyle()).toEqual({ color: 'red' });
248248
const width = { type: DataVariableType, path: 'data1.rec1.width' };
249-
cmp.setStyle({ width });
249+
cmp.addStyle({ width });
250250

251251
expect(cmp.getStyle({ skipResolve: true })).toEqual({ color, width });
252252
});

packages/core/test/specs/dom_components/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,10 @@ describe('DOM Components', () => {
263263
<style>
264264
#${id} { background-color: red }
265265
</style>`) as Component;
266-
obj.getComponents().first().addStyle({ margin: '10px' });
267266
const rule = cc.getAll().at(0);
268-
const css = `#${id}{background-color:red;margin:10px;color:red;padding:50px 100px;}`;
267+
expect(rule.toCSS()).toEqual(`#${id}{background-color:red;color:red;padding:50px 100px;}`);
268+
obj.getComponents().first().addStyle({ margin: '10px' });
269+
const css = `#${id}{background-color:red;color:red;padding:50px 100px;margin:10px;}`;
269270
expect(rule.toCSS()).toEqual(css);
270271

271272
setTimeout(() => {
@@ -438,10 +439,16 @@ describe('DOM Components', () => {
438439
expect(em.getHtml({ component })).toEqual(`<div id="${id}">Text</div>`);
439440
expect(Components.getComponents().length).toEqual(1);
440441
const firstComp = Components.getComponents().first();
442+
const rule = Css.getIdRule(id);
443+
expect(rule!.getStyle()).toEqual({
444+
color: 'red',
445+
'background-color': 'red',
446+
padding: '50px 100px',
447+
});
441448
firstComp.addStyle({ margin: '10px' });
442449
firstComp.addStyle('width', '100px');
443450
expect(Css.getAll().length).toEqual(1);
444-
expect(Css.getIdRule(id)!.getStyle()).toEqual({
451+
expect(rule!.getStyle()).toEqual({
445452
color: 'red',
446453
'background-color': 'red',
447454
padding: '50px 100px',

0 commit comments

Comments
 (0)