Browse Source

A few fixes/improvements for the DataSource module (#6219)

pull/6220/head
Artur Arseniev 2 years ago
committed by GitHub
parent
commit
1636b5955d
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 8
      packages/core/src/common/index.ts
  2. 2
      packages/core/src/css_composer/index.ts
  3. 7
      packages/core/src/data_sources/index.ts
  4. 13
      packages/core/src/data_sources/model/DataRecord.ts
  5. 7
      packages/core/src/data_sources/model/DataSource.ts
  6. 2
      packages/core/src/data_sources/model/DataVariable.ts
  7. 13
      packages/core/src/domain_abstract/model/StyleableModel.ts
  8. 2
      packages/core/src/editor/model/Editor.ts
  9. 2
      packages/core/src/undo_manager/index.ts

8
packages/core/src/common/index.ts

@ -4,9 +4,15 @@ export { default as $ } from '../utils/cash-dom';
interface NOOP {} interface NOOP {}
export const collectionEvents = 'add remove reset change';
export type Debounced = Function & { cancel(): void }; 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 }; export type AddOptions = Backbone.AddOptions & { temporary?: boolean; action?: string };

2
packages/core/src/css_composer/index.ts

@ -254,7 +254,7 @@ export default class CssComposer extends ItemManagerModule<CssComposerConfig & {
if (updateStyle) { if (updateStyle) {
const styleUpdate = opts.extend ? { ...model.get('style'), ...style } : style; const styleUpdate = opts.extend ? { ...model.get('style'), ...style } : style;
model.set('style', styleUpdate, opts); model.setStyle(styleUpdate, opts);
} }
result.push(model); result.push(model);

7
packages/core/src/data_sources/index.ts

@ -36,7 +36,7 @@
*/ */
import { ItemManagerModule, ModuleConfig } from '../abstract/Module'; import { ItemManagerModule, ModuleConfig } from '../abstract/Module';
import { AddOptions, ObjectAny, RemoveOptions } from '../common'; import { AddOptions, collectionEvents, ObjectAny, RemoveOptions } from '../common';
import EditorModel from '../editor/model/Editor'; import EditorModel from '../editor/model/Editor';
import { get, stringToPath } from '../utils/mixins'; import { get, stringToPath } from '../utils/mixins';
import DataRecord from './model/DataRecord'; import DataRecord from './model/DataRecord';
@ -177,4 +177,9 @@ export default class DataSourceManager extends ItemManagerModule<ModuleConfig, D
load(data: any) { load(data: any) {
return this.loadProjectData(data); return this.loadProjectData(data);
} }
postLoad() {
const { em, all } = this;
em.listenTo(all, collectionEvents, (m, c, o) => em.changesUp(o || c));
}
} }

13
packages/core/src/data_sources/model/DataRecord.ts

@ -146,26 +146,27 @@ export default class DataRecord<T extends DataRecordProps = DataRecordProps> ext
const onRecordSetValue = this.dataSource?.transformers?.onRecordSetValue; const onRecordSetValue = this.dataSource?.transformers?.onRecordSetValue;
const applySet = (key: string, val: unknown) => { const applySet = (key: string, val: unknown, opts: SetOptions = {}) => {
const newValue = const newValue =
options?.avoidTransformers || !onRecordSetValue opts?.avoidTransformers || !onRecordSetValue
? val ? val
: onRecordSetValue({ : onRecordSetValue({
id: this.id, id: this.id,
key, key,
value: val, value: val,
}); });
super.set(key, newValue, opts);
super.set(key, newValue, options); // 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) { if (typeof attributeName === 'object' && attributeName !== null) {
const attributes = attributeName as Partial<T>; const attributes = attributeName as Partial<T>;
for (const [key, val] of Object.entries(attributes)) { for (const [key, val] of Object.entries(attributes)) {
applySet(key, val); applySet(key, val, value as SetOptions);
} }
} else { } else {
applySet(attributeName as string, value); applySet(attributeName as string, value, options);
} }
return this; return this;

7
packages/core/src/data_sources/model/DataSource.ts

@ -29,7 +29,7 @@
* @extends {Model<DataSourceProps>} * @extends {Model<DataSourceProps>}
*/ */
import { AddOptions, CombinedModelConstructorOptions, Model, RemoveOptions } from '../../common'; import { AddOptions, collectionEvents, CombinedModelConstructorOptions, Model, RemoveOptions } from '../../common';
import EditorModel from '../../editor/model/Editor'; import EditorModel from '../../editor/model/Editor';
import { DataRecordProps, DataSourceProps, DataSourceTransformers } from '../types'; import { DataRecordProps, DataSourceProps, DataSourceTransformers } from '../types';
import DataRecord from './DataRecord'; import DataRecord from './DataRecord';
@ -74,6 +74,7 @@ export default class DataSource extends Model<DataSourceProps> {
} }
this.listenTo(this.records, 'add', this.onAdd); this.listenTo(this.records, 'add', this.onAdd);
this.listenTo(this.records, collectionEvents, this.handleChanges);
} }
/** /**
@ -174,4 +175,8 @@ export default class DataSource extends Model<DataSourceProps> {
this.records.add(record); this.records.add(record);
}); });
} }
private handleChanges(m: any, c: any, o: any) {
this.em.changesUp(o || c);
}
} }

2
packages/core/src/data_sources/model/DataVariable.ts

@ -37,7 +37,7 @@ export default class DataVariable extends Model {
getDataValue() { getDataValue() {
const { path, defaultValue } = this.attributes; const { path, defaultValue } = this.attributes;
const val = this.em?.DataSources?.getValue?.(path, defaultValue); const val = this.em?.DataSources.getValue(path, defaultValue);
return val; return val;
} }

13
packages/core/src/domain_abstract/model/StyleableModel.ts

@ -151,22 +151,11 @@ export default class StyleableModel<T extends ObjectHash = any> extends Model<T>
model: this, model: this,
em: this.em!, em: this.em!,
dataVariable: dataVar, 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) { getView(frame?: Frame) {
let { views, em } = this; let { views, em } = this;
const frm = frame || em?.getCurrentFrameModel(); const frm = frame || em?.getCurrentFrameModel();

2
packages/core/src/editor/model/Editor.ts

@ -445,7 +445,7 @@ export default class EditorModel extends Model {
* */ * */
handleUpdates(model: any, val: any, opt: any = {}) { handleUpdates(model: any, val: any, opt: any = {}) {
// Component has been added temporarily - do not update storage or record changes // 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; return;
} }

2
packages/core/src/undo_manager/index.ts

@ -36,7 +36,7 @@ export interface UndoGroup {
labels: string[]; 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()); const getChanged = (obj: any) => Object.keys(obj.changedAttributes());

Loading…
Cancel
Save