Skip to content

Commit 2a84a5a

Browse files
author
昔梦
committed
fix:修复条件节点数据不同步问题
1 parent 06c0976 commit 2a84a5a

File tree

3 files changed

+60
-43
lines changed

3 files changed

+60
-43
lines changed

packages/x-flow/src/XFlow.tsx

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,27 @@ const XFlow: FC<FlowProps> = memo(props => {
202202
setEdges(newEdges);
203203
});
204204

205-
const handleNodeValueChange = debounce((data: any) => {
206-
for (let node of nodes) {
207-
if (node.id === data.id) {
208-
node.data = {
209-
...node?.data,
210-
...data?.values,
211-
};
212-
break;
213-
}
205+
const handleNodeValueChange = debounce((data: any, id: string) => {
206+
// for (let node of nodes) {
207+
// if (node.id === data.id) {
208+
// node.data = {
209+
// ...node?.data,
210+
// ...data?.values,
211+
// };
212+
// break;
213+
// }
214+
// }
215+
// setNodes([...nodes], false);
216+
// 同时更新 activeNode 状态,确保面板数据同步
217+
if (activeNode && activeNode.id === id) {
218+
setActiveNode({
219+
...activeNode,
220+
values: {
221+
...activeNode.values,
222+
...data,
223+
},
224+
});
214225
}
215-
setNodes([...nodes], false);
216226
}, 200);
217227

218228
const nodeTypes = useMemo(() => {
@@ -252,6 +262,7 @@ const XFlow: FC<FlowProps> = memo(props => {
252262
}, [layout]);
253263

254264
const NodeEditorWrap = useMemo(() => {
265+
255266
return (
256267
<NodeEditor
257268
ref={nodeEditorRef}
@@ -262,13 +273,14 @@ const XFlow: FC<FlowProps> = memo(props => {
262273

263274
/>
264275
);
276+
// JSON.stringify(activeNode)
265277
}, [activeNode?.id]);
266278

267279
const NodeLogWrap = useMemo(() => {
268280
return (
269281
<NodeLogPanel
270282
data={activeNode?.values}
271-
onChange={handleNodeValueChange}
283+
// onChange={handleNodeValueChange}
272284
nodeType={activeNode?._nodeType}
273285
id={activeNode?.id}
274286
node={activeNode}
@@ -285,18 +297,6 @@ const XFlow: FC<FlowProps> = memo(props => {
285297
const deletable = globalConfig?.edge?.deletable ?? true;
286298
const panelonClose = globalConfig?.nodePanel?.onClose;
287299

288-
const getNodesJ = nodes => {
289-
const result = nodes.map(item => {
290-
const { data, ...rest } = item;
291-
const { _nodeType, ...restData } = data;
292-
return {
293-
...rest,
294-
data: restData,
295-
type: _nodeType,
296-
};
297-
});
298-
return result;
299-
};
300300

301301
return (
302302
<div id="xflow-container" ref={workflowContainerRef}>

packages/x-flow/src/components/NodeEditor/index.tsx

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ import { safeJsonStringify, uuid } from '../../utils';
1717

1818
interface INodeEditorProps {
1919
data: any;
20-
onChange: (data: any) => void;
20+
onChange: (data: any, id?: string) => void;
2121
nodeType: string;
2222
id: string;
2323
ref?: React.Ref<any>; // 添加 ref 属性
24+
// activeNode?: any;
2425
}
2526

2627
const NodeEditor: FC<INodeEditorProps> = forwardRef((props, ref: any) => {
@@ -35,6 +36,20 @@ const NodeEditor: FC<INodeEditorProps> = forwardRef((props, ref: any) => {
3536
const getSettingSchema = nodeSetting['getSettingSchema'];
3637
const [asyncSchema, setAsyncSchema] = useState<Schema>({});
3738
const nodeWidgetRef = useRef(null);
39+
const { nodes, setNodes } = useStore(
40+
(state: any) => ({
41+
nodes: state.nodes,
42+
setNodes: state.setNodes,
43+
}),
44+
shallow
45+
);
46+
const [internalData, setInternalData] = useState();
47+
48+
useEffect(() => {
49+
const activeNode = nodes.find(node => node.id === id);
50+
const { _nodeType, _status, ...restData } = activeNode?.data || {};
51+
setInternalData(restData);
52+
}, []);
3853

3954
useImperativeHandle(ref, () => ({
4055
validateForm: async () => {
@@ -51,7 +66,10 @@ const NodeEditor: FC<INodeEditorProps> = forwardRef((props, ref: any) => {
5166
.catch(err => {
5267
return false;
5368
});
54-
} else if (nodeSetting?.settingWidget && nodeWidgetRef.current?.validateForm) {
69+
} else if (
70+
nodeSetting?.settingWidget &&
71+
nodeWidgetRef.current?.validateForm
72+
) {
5573
result = await nodeWidgetRef.current.validateForm();
5674
}
5775
return result;
@@ -68,20 +86,13 @@ const NodeEditor: FC<INodeEditorProps> = forwardRef((props, ref: any) => {
6886
).catch(() => ({}));
6987
setAsyncSchema(shema);
7088
}
89+
7190
useEffect(() => {
7291
if (isFunction(getSettingSchema)) {
7392
getSchema();
7493
}
7594
}, []);
7695

77-
const { nodes, setNodes } = useStore(
78-
(state: any) => ({
79-
nodes: state.nodes,
80-
setNodes: state.setNodes,
81-
}),
82-
shallow
83-
);
84-
8596
useEffect(() => {
8697
if (nodeSetting?.settingSchema) {
8798
// 自定义Schema
@@ -115,14 +126,14 @@ const NodeEditor: FC<INodeEditorProps> = forwardRef((props, ref: any) => {
115126
if (item?._id) {
116127
return item;
117128
} else {
118-
if (node?.data?.list?.length && node?.data?.list[index]?._id) {
119-
return {
120-
...item,
121-
_id: node?.data?.list[index]?._id,
122-
};
123-
} else {
124-
return { ...item, _id: `id_${uuid()}` };
125-
}
129+
// if (node?.data?.list?.length && node?.data?.list[index]?._id) {
130+
// return {
131+
// ...item,
132+
// _id: node?.data?.list[index]?._id,
133+
// };
134+
// } else {
135+
return { ...item, _id: `id_${uuid()}` };
136+
// }
126137
}
127138
});
128139
}
@@ -131,6 +142,11 @@ const NodeEditor: FC<INodeEditorProps> = forwardRef((props, ref: any) => {
131142
}
132143
});
133144
setNodes(newNodes, false);
145+
146+
// if (onChange) {
147+
// onChange(data, id);
148+
// }
149+
setInternalData(data);
134150
}, 100);
135151

136152
const watch = {
@@ -193,7 +209,8 @@ const NodeEditor: FC<INodeEditorProps> = forwardRef((props, ref: any) => {
193209
onChange={val => {
194210
handleNodeValueChange({ ...val });
195211
}}
196-
value={data}
212+
value={internalData} // data
213+
// value={data}
197214
readOnly={readOnly}
198215
/>
199216
);

packages/x-flow/src/components/NodeLogPanel/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import './index.less';
88

99
interface INodeEditorProps {
1010
data: any;
11-
onChange: (data: any) => void;
11+
onChange?: (data: any) => void;
1212
nodeType: string;
1313
id: string;
1414
node: any;

0 commit comments

Comments
 (0)