fix(data-source): 迭代器数据编译支持容器嵌套

This commit is contained in:
roymondchen 2024-07-15 21:32:13 +08:00
parent a67058e1e5
commit e209aa36e8
2 changed files with 67 additions and 19 deletions

View File

@ -222,7 +222,7 @@ class DataSourceManager extends EventEmitter {
const [dsId, ...keys] = dataSourceField; const [dsId, ...keys] = dataSourceField;
const ds = this.get(dsId); const ds = this.get(dsId);
if (!ds) return items; if (!ds) return items;
return compliedIteratorItems(itemData, items, dsId, keys, this.app.platform === 'editor'); return compliedIteratorItems(itemData, items, dsId, keys, this.data, this.app.platform === 'editor');
} }
public destroy() { public destroy() {

View File

@ -1,7 +1,7 @@
import { cloneDeep, template } from 'lodash-es'; import { cloneDeep, template } from 'lodash-es';
import { isDataSourceTemplate, isUseDataSourceField, Target, Watcher } from '@tmagic/dep'; import { isDataSourceTemplate, isUseDataSourceField, Target, Watcher } from '@tmagic/dep';
import type { DisplayCond, DisplayCondItem, MApp, MNode, MPage, MPageFragment } from '@tmagic/schema'; import type { DepData, DisplayCond, DisplayCondItem, MApp, MNode, MPage, MPageFragment } from '@tmagic/schema';
import { import {
compiledCond, compiledCond,
compiledNode, compiledNode,
@ -109,8 +109,14 @@ export const updateNode = (node: MNode, dsl: MApp) => {
* @param fields dsl节点字段a.b.c * @param fields dsl节点字段a.b.c
* @returns * @returns
*/ */
export const createIteratorContentData = (itemData: any, dsId: string, fields: string[] = []) => { export const createIteratorContentData = (
itemData: any,
dsId: string,
fields: string[] = [],
dsData: DataSourceManagerData = {},
) => {
const data = { const data = {
...dsData,
[dsId]: {}, [dsId]: {},
}; };
@ -184,6 +190,7 @@ export const compliedIteratorItems = (
items: MNode[], items: MNode[],
dsId: string, dsId: string,
keys: string[] = [], keys: string[] = [],
data: DataSourceManagerData,
inEditor = false, inEditor = false,
) => { ) => {
const watcher = new Watcher(); const watcher = new Watcher();
@ -222,26 +229,67 @@ export const compliedIteratorItems = (
return items; return items;
} }
return items.map((item) => { return items.map((item) => compliedIteratorItem({ itemData, data, dsId, keys, inEditor, condDeps, item, deps }));
const ctxData = createIteratorContentData(itemData, dsId, keys); };
if (condDeps[item.id]?.keys.length && !inEditor) { const compliedIteratorItem = ({
item.condResult = compliedConditions(item, ctxData); itemData,
data,
dsId,
keys,
inEditor,
condDeps,
item,
deps,
}: {
itemData: any;
data: DataSourceManagerData;
dsId: string;
keys: string[];
inEditor: boolean;
condDeps: DepData;
item: MNode;
deps: DepData;
}) => {
const { items, ...node } = item;
const newNode = cloneDeep(node);
if (items && !item.iteratorData) {
newNode.items = Array.isArray(items)
? items.map((item) => compliedIteratorItem({ itemData, data, dsId, keys, inEditor, condDeps, item, deps }))
: items;
}
if (Array.isArray(items) && items.length) {
if (item.iteratorData) {
newNode.items = items;
} else {
newNode.items = items.map((item) =>
compliedIteratorItem({ itemData, data, dsId, keys, inEditor, condDeps, item, deps }),
);
} }
} else {
newNode.items = items;
}
if (!deps[item.id]?.keys.length) { const ctxData = createIteratorContentData(itemData, dsId, keys, data);
return item;
}
return compiledNode( if (condDeps[newNode.id]?.keys.length && !inEditor) {
(value: any) => compiledNodeField(value, ctxData), newNode.condResult = compliedConditions(newNode, ctxData);
cloneDeep(item), }
{
[dsId]: deps, if (!deps[newNode.id]?.keys.length) {
}, return newNode;
dsId, }
);
}); return compiledNode(
(value: any) => compiledNodeField(value, ctxData),
newNode,
{
[dsId]: deps,
},
dsId,
);
}; };
/** /**