1
0
mirror of synced 2026-04-03 14:38:37 +08:00

feat(data-source,utils,runtime): 数据源setData支持指定路径

This commit is contained in:
roymondchen
2024-01-02 20:57:37 +08:00
parent 6b4bfae30b
commit d3777b236d
18 changed files with 229 additions and 97 deletions

View File

@@ -18,9 +18,9 @@
import EventEmitter from 'events';
import type { AppCore, CodeBlockContent, DataSchema, DataSourceSchema } from '@tmagic/schema';
import { getDefaultValueFromFields } from '@tmagic/utils';
import { getDefaultValueFromFields, setValueByKeyPath } from '@tmagic/utils';
import type { DataSourceOptions } from '@data-source/types';
import type { ChangeEvent, DataSourceOptions } from '@data-source/types';
/**
* 静态数据源
@@ -101,10 +101,20 @@ export default class DataSource<T extends DataSourceSchema = DataSourceSchema> e
this.#methods = methods;
}
public setData(data: Record<string, any>) {
// todo: 校验数据,看是否符合 schema
this.data = data;
this.emit('change');
public setData(data: any, path?: string) {
if (path) {
setValueByKeyPath(path, data, this.data);
} else {
// todo: 校验数据,看是否符合 schema
this.data = data;
}
const changeEvent: ChangeEvent = {
updateData: data,
path,
};
this.emit('change', changeEvent);
}
public getDefaultData() {