Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A few fixes/improvements for the DataSource module #6219

Merged
merged 4 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/core/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/css_composer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export default class CssComposer extends ItemManagerModule<CssComposerConfig & {

if (updateStyle) {
const styleUpdate = opts.extend ? { ...model.get('style'), ...style } : style;
model.set('style', styleUpdate, opts);
model.setStyle(styleUpdate, opts);
}

result.push(model);
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/data_sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*/

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 { get, stringToPath } from '../utils/mixins';
import DataRecord from './model/DataRecord';
Expand Down Expand Up @@ -177,4 +177,9 @@ export default class DataSourceManager extends ItemManagerModule<ModuleConfig, D
load(data: any) {
return this.loadProjectData(data);
}

postLoad() {
const { em, all } = this;
em.listenTo(all, collectionEvents, (m, c, o) => em.changesUp(o || c));
}
Comment on lines +181 to +184
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trigger editor update on data source collection changes

}
13 changes: 7 additions & 6 deletions packages/core/src/data_sources/model/DataRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,26 +146,27 @@ export default class DataRecord<T extends DataRecordProps = DataRecordProps> 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<T>;
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;
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/data_sources/model/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @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 { DataRecordProps, DataSourceProps, DataSourceTransformers } from '../types';
import DataRecord from './DataRecord';
Expand Down Expand Up @@ -74,6 +74,7 @@ export default class DataSource extends Model<DataSourceProps> {
}

this.listenTo(this.records, 'add', this.onAdd);
this.listenTo(this.records, collectionEvents, this.handleChanges);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trigger editor updates on data record collection changes.

I think some data sources (eg. with data loaded asynchronously) could be updated without triggering any update.

}

/**
Expand Down Expand Up @@ -174,4 +175,8 @@ export default class DataSource extends Model<DataSourceProps> {
this.records.add(record);
});
}

private handleChanges(m: any, c: any, o: any) {
this.em.changesUp(o || c);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/data_sources/model/DataVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 1 addition & 12 deletions packages/core/src/domain_abstract/model/StyleableModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,11 @@ export default class StyleableModel<T extends ObjectHash = any> extends Model<T>
model: this,
em: this.em!,
dataVariable: dataVar,
updateValueFromDataVariable: (newValue: string) => this.updateStyleProp(styleProp, newValue),
updateValueFromDataVariable: () => this.updateView(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to update the model as it's already resolved inside getStyle

});
}
}

/**
* 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();
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/editor/model/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/undo_manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down