-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathindex.tsx
194 lines (184 loc) · 5.05 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import FormRender, { Schema, useForm } from 'form-render';
import produce from 'immer';
import { debounce, isFunction } from 'lodash';
import React, {
FC,
forwardRef,
useContext,
useEffect,
useImperativeHandle,
useState,
} from 'react';
import { shallow } from 'zustand/shallow';
import { useStore } from '../../hooks/useStore';
import { ConfigContext } from '../../models/context';
import { safeJsonStringify, uuid } from '../../utils';
interface INodeEditorProps {
data: any;
onChange: (data: any) => void;
nodeType: string;
id: string;
}
const NodeEditor: FC<INodeEditorProps> = forwardRef((props, ref) => {
const { data, onChange, nodeType, id } = props;
const form = useForm();
// // 1.获取节点配置信息
const { settingMap, widgets, readOnly } = useContext(ConfigContext);
const nodeSetting = settingMap[nodeType] || {};
const [customVal, setCustomVal] = useState(data);
const CustomSettingWidget = widgets[`${nodeType}NodeSettingWidget`]; // 内置setting组件
const NodeWidget = widgets[nodeSetting?.settingWidget]; // 自定义面板配置组件
const getSettingSchema = nodeSetting['getSettingSchema'];
const [asyncSchema, setAsyncSchema] = useState<Schema>({});
useImperativeHandle(ref, () => ({
validateForm: async () => {
let result = true;
if (
nodeSetting?.settingSchema ||
(isFunction(getSettingSchema) && Object.keys(asyncSchema).length > 0)
) {
result = await form
.validateFields()
.then(() => {
return true;
})
.catch(err => {
return false;
});
}
return result;
},
}));
async function getSchema() {
const shema = await getSettingSchema(
id,
nodeType,
nodeSetting,
data,
form
).catch(() => ({}));
setAsyncSchema(shema);
}
useEffect(() => {
if (isFunction(getSettingSchema)) {
getSchema();
}
}, []);
const { nodes, setNodes } = useStore(
(state: any) => ({
nodes: state.nodes,
setNodes: state.setNodes,
}),
shallow
);
useEffect(() => {
if (nodeSetting?.settingSchema) {
// 自定义Schema
form.resetFields();
form.setValues(data || {});
} else if (nodeSetting?.settingWidget) {
// 自定义组件
setCustomVal(data);
} else {
//可能为内置schema或者是没有
}
}, [safeJsonStringify(data), id]);
const handleNodeValueChange = debounce((data: any) => {
const newNodes = produce(nodes, draft => {
let node = null;
// 反向查询ID,因为有多个ID相同的元素
for (let i = draft?.length - 1; i >= 0; i--) {
if (draft[i].id === id) {
node = draft[i];
break;
}
}
if (node) {
// 更新节点的 data
if (
node?.data?._nodeType === 'Switch' ||
node?.data?._nodeType === 'Parallel'
) {
data['list'] = (data?.list || [])?.map((item, index) => {
if (item?._id) {
return item;
} else {
if (node?.data?.list?.length && node?.data?.list[index]?._id) {
return {
...item,
_id: node?.data?.list[index]?._id,
};
} else {
return { ...item, _id: `id_${uuid()}` };
}
}
});
}
const { _nodeType, _status, _isCandidate, title, desc } = node?.data;
node.data = { _nodeType, _status, _isCandidate, title, desc, ...data }; // form-render的list如果为空,不会返回list相应的字段,只能全部替换data
}
});
setNodes(newNodes, false);
}, 100);
const watch = {
'#': (allValues: any) => {
handleNodeValueChange({ ...allValues });
},
};
if (nodeSetting?.settingWidget && NodeWidget) {
return (
<NodeWidget
{...nodeSetting?.settingWidgetProps}
value={customVal}
onChange={values => {
setCustomVal(values);
handleNodeValueChange({ ...values });
}}
readOnly={readOnly}
/>
);
} else if (nodeSetting?.settingSchema) {
return (
<FormRender
schema={nodeSetting?.settingSchema}
form={form}
widgets={widgets}
watch={watch}
size={'small'}
readOnly={readOnly}
onMount={() => {
const initialValues = form.getValues();
handleNodeValueChange(initialValues);
}}
/>
);
} else if (
isFunction(getSettingSchema) &&
Object.keys(asyncSchema).length > 0
) {
return (
<FormRender
schema={asyncSchema}
form={form}
widgets={widgets}
watch={watch}
size={'small'}
readOnly={readOnly}
/>
);
} else if (CustomSettingWidget) {
// 内置节点
return (
<CustomSettingWidget
onChange={val => {
handleNodeValueChange({ ...val });
}}
value={data}
readOnly={readOnly}
/>
);
} else {
return null;
}
});
export default NodeEditor;