From 1636b5955dca2706562b4912fac57c4ef0f5ca45 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Tue, 15 Oct 2024 19:16:16 +0400 Subject: [PATCH] A few fixes/improvements for the DataSource module (#6219) --- packages/core/src/common/index.ts | 8 +++++++- packages/core/src/css_composer/index.ts | 2 +- packages/core/src/data_sources/index.ts | 7 ++++++- packages/core/src/data_sources/model/DataRecord.ts | 13 +++++++------ packages/core/src/data_sources/model/DataSource.ts | 7 ++++++- .../core/src/data_sources/model/DataVariable.ts | 2 +- .../src/domain_abstract/model/StyleableModel.ts | 13 +------------ packages/core/src/editor/model/Editor.ts | 2 +- packages/core/src/undo_manager/index.ts | 2 +- 9 files changed, 31 insertions(+), 25 deletions(-) diff --git a/packages/core/src/common/index.ts b/packages/core/src/common/index.ts index c46a7dcb8..90d9ac5d9 100644 --- a/packages/core/src/common/index.ts +++ b/packages/core/src/common/index.ts @@ -4,9 +4,15 @@ export { default as $ } from '../utils/cash-dom'; interface NOOP {} +export const collectionEvents = 'add remove reset change'; + export type Debounced = Function & { cancel(): void }; -export type SetOptions = Backbone.ModelSetOptions & { avoidStore?: boolean; avoidTransformers?: boolean }; +export type SetOptions = Backbone.ModelSetOptions & { + avoidStore?: boolean; + avoidTransformers?: boolean; + partial?: boolean; +}; export type AddOptions = Backbone.AddOptions & { temporary?: boolean; action?: string }; diff --git a/packages/core/src/css_composer/index.ts b/packages/core/src/css_composer/index.ts index f7106d6f1..562b77bff 100644 --- a/packages/core/src/css_composer/index.ts +++ b/packages/core/src/css_composer/index.ts @@ -254,7 +254,7 @@ export default class CssComposer extends ItemManagerModule em.changesUp(o || c)); + } } diff --git a/packages/core/src/data_sources/model/DataRecord.ts b/packages/core/src/data_sources/model/DataRecord.ts index 0ddbc7b64..85ebe12a2 100644 --- a/packages/core/src/data_sources/model/DataRecord.ts +++ b/packages/core/src/data_sources/model/DataRecord.ts @@ -146,26 +146,27 @@ export default class DataRecord ext const onRecordSetValue = this.dataSource?.transformers?.onRecordSetValue; - const applySet = (key: string, val: unknown) => { + const applySet = (key: string, val: unknown, opts: SetOptions = {}) => { const newValue = - options?.avoidTransformers || !onRecordSetValue + opts?.avoidTransformers || !onRecordSetValue ? val : onRecordSetValue({ id: this.id, key, value: val, }); - - super.set(key, newValue, options); + super.set(key, newValue, opts); + // This ensures to trigger the change event with partial updates + super.set({ __p: opts.partial ? true : undefined } as any, opts); }; if (typeof attributeName === 'object' && attributeName !== null) { const attributes = attributeName as Partial; for (const [key, val] of Object.entries(attributes)) { - applySet(key, val); + applySet(key, val, value as SetOptions); } } else { - applySet(attributeName as string, value); + applySet(attributeName as string, value, options); } return this; diff --git a/packages/core/src/data_sources/model/DataSource.ts b/packages/core/src/data_sources/model/DataSource.ts index f64ff1e99..858758be5 100644 --- a/packages/core/src/data_sources/model/DataSource.ts +++ b/packages/core/src/data_sources/model/DataSource.ts @@ -29,7 +29,7 @@ * @extends {Model} */ -import { AddOptions, CombinedModelConstructorOptions, Model, RemoveOptions } from '../../common'; +import { AddOptions, collectionEvents, CombinedModelConstructorOptions, Model, RemoveOptions } from '../../common'; import EditorModel from '../../editor/model/Editor'; import { DataRecordProps, DataSourceProps, DataSourceTransformers } from '../types'; import DataRecord from './DataRecord'; @@ -74,6 +74,7 @@ export default class DataSource extends Model { } this.listenTo(this.records, 'add', this.onAdd); + this.listenTo(this.records, collectionEvents, this.handleChanges); } /** @@ -174,4 +175,8 @@ export default class DataSource extends Model { this.records.add(record); }); } + + private handleChanges(m: any, c: any, o: any) { + this.em.changesUp(o || c); + } } diff --git a/packages/core/src/data_sources/model/DataVariable.ts b/packages/core/src/data_sources/model/DataVariable.ts index fce884565..a03e1d677 100644 --- a/packages/core/src/data_sources/model/DataVariable.ts +++ b/packages/core/src/data_sources/model/DataVariable.ts @@ -37,7 +37,7 @@ export default class DataVariable extends Model { getDataValue() { const { path, defaultValue } = this.attributes; - const val = this.em?.DataSources?.getValue?.(path, defaultValue); + const val = this.em?.DataSources.getValue(path, defaultValue); return val; } diff --git a/packages/core/src/domain_abstract/model/StyleableModel.ts b/packages/core/src/domain_abstract/model/StyleableModel.ts index 02b4980f8..684450919 100644 --- a/packages/core/src/domain_abstract/model/StyleableModel.ts +++ b/packages/core/src/domain_abstract/model/StyleableModel.ts @@ -151,22 +151,11 @@ export default class StyleableModel extends Model model: this, em: this.em!, dataVariable: dataVar, - updateValueFromDataVariable: (newValue: string) => this.updateStyleProp(styleProp, newValue), + updateValueFromDataVariable: () => this.updateView(), }); } } - /** - * Update a specific style property - */ - updateStyleProp(prop: string, value: string) { - const style = this.getStyle(); - style[prop] = value; - this.setStyle(style, { noEvent: true }); - this.trigger(`change:style:${prop}`); - this.updateView(); - } - getView(frame?: Frame) { let { views, em } = this; const frm = frame || em?.getCurrentFrameModel(); diff --git a/packages/core/src/editor/model/Editor.ts b/packages/core/src/editor/model/Editor.ts index e64418548..93c4d7c99 100644 --- a/packages/core/src/editor/model/Editor.ts +++ b/packages/core/src/editor/model/Editor.ts @@ -445,7 +445,7 @@ export default class EditorModel extends Model { * */ handleUpdates(model: any, val: any, opt: any = {}) { // Component has been added temporarily - do not update storage or record changes - if (this.__skip || opt.temporary || opt.noCount || opt.avoidStore || !this.get('ready')) { + if (this.__skip || opt.temporary || opt.noCount || opt.avoidStore || opt.partial || !this.get('ready')) { return; } diff --git a/packages/core/src/undo_manager/index.ts b/packages/core/src/undo_manager/index.ts index 7e60aa5c0..5ff3c2ba0 100644 --- a/packages/core/src/undo_manager/index.ts +++ b/packages/core/src/undo_manager/index.ts @@ -36,7 +36,7 @@ export interface UndoGroup { labels: string[]; } -const hasSkip = (opts: any) => opts.avoidStore || opts.noUndo; +const hasSkip = (opts: any) => opts.avoidStore || opts.noUndo || opts.partial; const getChanged = (obj: any) => Object.keys(obj.changedAttributes());