From b8858dc77c886e980857101ab0bf94cbd499f906 Mon Sep 17 00:00:00 2001 From: danstarns Date: Sun, 4 Aug 2024 16:53:32 -0700 Subject: [PATCH] refactor: * --- src/data_sources/model/StyleDataVariable.ts | 44 +++++++++++++---- src/domain_abstract/model/StyleableModel.ts | 55 +++++++++------------ test/specs/data_sources/index.ts | 9 ++-- 3 files changed, 62 insertions(+), 46 deletions(-) diff --git a/src/data_sources/model/StyleDataVariable.ts b/src/data_sources/model/StyleDataVariable.ts index 03c927182..99e4848cb 100644 --- a/src/data_sources/model/StyleDataVariable.ts +++ b/src/data_sources/model/StyleDataVariable.ts @@ -1,14 +1,38 @@ -import { Model, ObjectAny } from '../../common'; -export const type = 'data-variable-css'; +import { Model } from '../../common'; +import EditorModel from '../../editor/model/Editor'; +import { get, stringToPath } from '../../utils/mixins'; -interface StyleDataVariableProps extends ObjectAny { - type: string; - path: string; - value: string; -} +export default class StyleDataVariable extends Model { + em?: EditorModel; + + defaults() { + return { + type: 'data-variable-css', + value: '', + path: '', + }; + } + + initialize(attrs: any, options: any) { + super.initialize(attrs, options); + this.em = options.em; + this.listenToDataSource(); + + return this; + } + + listenToDataSource() { + const { path } = this.attributes; + const resolvedPath = stringToPath(path).join('.'); + + if (this.em) { + this.listenTo(this.em.DataSources, `change:${resolvedPath}`, this.onDataSourceChange); + } + } -export default class StyleDataVariable extends Model { - constructor(props: T, opts = {}) { - super(props, opts); + onDataSourceChange(model: any) { + const { path } = this.attributes; + const newValue = get(model, stringToPath(path).join('.'), ''); + this.set({ value: newValue }); } } diff --git a/src/domain_abstract/model/StyleableModel.ts b/src/domain_abstract/model/StyleableModel.ts index 8b9709beb..20e39e48c 100644 --- a/src/domain_abstract/model/StyleableModel.ts +++ b/src/domain_abstract/model/StyleableModel.ts @@ -2,7 +2,7 @@ import { isArray, isString, keys } from 'underscore'; import { Model, ObjectAny, ObjectHash, SetOptions } from '../../common'; import ParserHtml from '../../parser/model/ParserHtml'; import Selectors from '../../selector_manager/model/Selectors'; -import { shallowDiff, get, stringToPath } from '../../utils/mixins'; +import { shallowDiff, stringToPath } from '../../utils/mixins'; import EditorModel from '../../editor/model/Editor'; import StyleDataVariable from '../../data_sources/model/StyleDataVariable'; import { DataSourcesEvents, DataVariableListener } from '../../data_sources/types'; @@ -91,54 +91,46 @@ export default class StyleableModel extends Model const propNew = { ...prop }; const newStyle = { ...propNew }; - // Remove empty style properties - keys(newStyle).forEach(prop => { - if (newStyle[prop] === '') { - delete newStyle[prop]; + + keys(newStyle).forEach(key => { + // Remove empty style properties + if (newStyle[key] === '') { + delete newStyle[key]; + + return; } - }); - this.convertToStyleDataVariable(newStyle); + const styleValue = newStyle[key]; + if (typeof styleValue === 'object' && styleValue.type === 'data-variable-css') { + newStyle[key] = new StyleDataVariable(styleValue, { em: this.em }); + } + }); this.set('style', newStyle, opts as any); + const diff = shallowDiff(propOrig, newStyle); // Delete the property used for partial updates delete diff.__p; keys(diff).forEach(pr => { - // @ts-ignore const { em } = this; - if (opts.noEvent) return; + if (opts.noEvent) { + return; + } + this.trigger(`change:style:${pr}`); if (em) { em.trigger('styleable:change', this, pr, opts); em.trigger(`styleable:change:${pr}`, this, pr, opts); - } - }); - this.processDataVariableStyles(newStyle); - - return newStyle; - } - - convertToStyleDataVariable(style: StyleProps) { - keys(style).forEach(key => { - const styleValue = style[key]; - // @ts-ignore - if (typeof styleValue === 'object' && styleValue.type === 'data-variable-css') { - // @ts-ignore - style[key] = new StyleDataVariable(styleValue, { em: this.em }); + const styleValue = newStyle[pr]; + if (styleValue instanceof StyleDataVariable) { + this.listenToDataVariable(styleValue, pr); + } } }); - } - processDataVariableStyles(style: StyleProps) { - keys(style).forEach(key => { - const styleValue = style[key]; - if (styleValue instanceof StyleDataVariable) { - this.listenToDataVariable(styleValue, key); - } - }); + return newStyle; } listenToDataVariable(dataVar: StyleDataVariable, styleProp: string) { @@ -155,6 +147,7 @@ export default class StyleableModel extends Model dataListeners.forEach(ls => this.listenTo(ls.obj, ls.event, () => { + console.log('data variable change', ls.obj, ls.event); const newValue = em?.DataSources.getValue(normPath, dataVar.get('value')); this.updateStyleProp(styleProp, newValue); }) diff --git a/test/specs/data_sources/index.ts b/test/specs/data_sources/index.ts index 55de7dfa0..886c58192 100644 --- a/test/specs/data_sources/index.ts +++ b/test/specs/data_sources/index.ts @@ -34,7 +34,7 @@ describe('DataSourceManager', () => { expect(dsm).toBeTruthy(); }); - describe.only('Style', () => { + describe('Style', () => { let fixtures: HTMLElement; let cmpRoot: ComponentWrapper; @@ -66,16 +66,15 @@ describe('DataSourceManager', () => { content: 'Hello World', style: { color: { - type: 'data-variable', + type: 'data-variable-css', value: 'black', path: 'colors-data.id1.color', }, }, })[0]; - const el = cmp.getEl(); - console.log('el', el?.style); - expect(el?.style.color).toBe('red'); + const style = cmp.getStyle(); + expect(style).toHaveProperty('color', 'red'); }); });