Skip to content

Commit

Permalink
fix(data-source): 迭代器数据编译支持容器嵌套
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Jul 15, 2024
1 parent a67058e commit e209aa3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/data-source/src/DataSourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class DataSourceManager extends EventEmitter {
const [dsId, ...keys] = dataSourceField;
const ds = this.get(dsId);
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() {
Expand Down
84 changes: 66 additions & 18 deletions packages/data-source/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cloneDeep, template } from 'lodash-es';

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 {
compiledCond,
compiledNode,
Expand Down Expand Up @@ -109,8 +109,14 @@ export const updateNode = (node: MNode, dsl: MApp) => {
* @param fields dsl节点字段,如a.b.c
* @returns 数据上下文
*/
export const createIteratorContentData = (itemData: any, dsId: string, fields: string[] = []) => {
export const createIteratorContentData = (
itemData: any,
dsId: string,
fields: string[] = [],
dsData: DataSourceManagerData = {},
) => {
const data = {
...dsData,
[dsId]: {},
};

Expand Down Expand Up @@ -184,6 +190,7 @@ export const compliedIteratorItems = (
items: MNode[],
dsId: string,
keys: string[] = [],
data: DataSourceManagerData,
inEditor = false,
) => {
const watcher = new Watcher();
Expand Down Expand Up @@ -222,26 +229,67 @@ export const compliedIteratorItems = (
return items;
}

return items.map((item) => {
const ctxData = createIteratorContentData(itemData, dsId, keys);
return items.map((item) => compliedIteratorItem({ itemData, data, dsId, keys, inEditor, condDeps, item, deps }));
};

if (condDeps[item.id]?.keys.length && !inEditor) {
item.condResult = compliedConditions(item, ctxData);
}
const compliedIteratorItem = ({
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 (!deps[item.id]?.keys.length) {
return item;
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;
}

return compiledNode(
(value: any) => compiledNodeField(value, ctxData),
cloneDeep(item),
{
[dsId]: deps,
},
dsId,
);
});
const ctxData = createIteratorContentData(itemData, dsId, keys, data);

if (condDeps[newNode.id]?.keys.length && !inEditor) {
newNode.condResult = compliedConditions(newNode, ctxData);
}

if (!deps[newNode.id]?.keys.length) {
return newNode;
}

return compiledNode(
(value: any) => compiledNodeField(value, ctxData),
newNode,
{
[dsId]: deps,
},
dsId,
);
};

/**
Expand Down

0 comments on commit e209aa3

Please sign in to comment.