Skip to content

Commit cce2acb

Browse files
Create utils.js
1 parent 69e674a commit cce2acb

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { useCanvas } from "@opentiny/tiny-engine";
2+
3+
const { getCurrentSchema, getPageSchema, operateNode, getNodeWithParentById } =
4+
useCanvas();
5+
6+
export const sortFormModel = (target, reference) => {
7+
const targetOrder = new Map();
8+
reference.forEach((item, index) => {
9+
targetOrder.set(item.beta, index);
10+
});
11+
const sortableItems = target.filter((item) => item.condition !== false);
12+
const nonSortableItems = target.filter((item) => item.condition === false);
13+
sortableItems.sort((a, b) => {
14+
const indexA = targetOrder.get(a.label);
15+
const indexB = targetOrder.get(b.label);
16+
return indexA - indexB;
17+
});
18+
return sortableItems.concat(nonSortableItems);
19+
};
20+
21+
const setStateProps = (target, modelDetail) => {
22+
const value = modelDetail.value;
23+
const unused = modelDetail.unused;
24+
value.forEach((item) => {
25+
// 如果没有该属性,则初始化为null
26+
if (!target?.[item.prop]) {
27+
target[item.prop] = null;
28+
}
29+
});
30+
unused.forEach((item) => {
31+
// 如果有该属性且值不为null,则删除该字段
32+
if (item.prop in target) {
33+
delete target[item.prop];
34+
}
35+
});
36+
};
37+
38+
export const setFormModelCondition = () => {
39+
const currentSchema = getCurrentSchema();
40+
const parentNode = getNodeWithParentById(currentSchema.id).parent;
41+
if (parentNode?.componentType === "ModelPage") {
42+
const pageSchema = getPageSchema();
43+
const serviceModel = currentSchema.props.serviceModel;
44+
// 给搜索表单和弹窗表单添加condition: false
45+
const setConditions = (target) => {
46+
target.forEach((item) => {
47+
if (
48+
serviceModel.unused.find(
49+
(tableModelItem) =>
50+
tableModelItem.prop === item.children[0].props.prop
51+
)
52+
) {
53+
item.condition = false;
54+
} else {
55+
item.condition = true;
56+
}
57+
});
58+
};
59+
setConditions(parentNode.children[0].children); // tinyForm->tinycol
60+
setConditions(parentNode.children[2].children[0].children[0].children); // dialogbox->div(slot)->tinyForm->tinycol
61+
setStateProps(
62+
pageSchema.state[`modelState_${parentNode.id}`].search,
63+
serviceModel
64+
);
65+
setStateProps(
66+
pageSchema.state[`modelState_${parentNode.id}`].detail,
67+
serviceModel
68+
);
69+
operateNode({
70+
type: "updateAttributes",
71+
id: parentNode.id,
72+
value: { children: parentNode.children },
73+
});
74+
useCanvas().canvasApi.value.updateRect();
75+
}
76+
};
77+
78+
79+
export const transformNode = (node) => {
80+
const result = {
81+
prop: node.name,
82+
type: node.type,
83+
required: node.isRequired,
84+
description: node.description === null ? '--' : node.description,
85+
parameterIn: node.parameterIn,
86+
};
87+
88+
if (node.children && node.children.length > 0 && node.type !== 'array') {
89+
result.children = node.children.map((child) => transformNode(child));
90+
}
91+
92+
return result;
93+
};

0 commit comments

Comments
 (0)