forked from LangGraph-GUI/LangGraph-GUI-reactflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdge.js
54 lines (45 loc) · 1.38 KB
/
Edge.js
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
// Edge.js
import { addEdge } from 'reactflow';
export const createEdge = (edges, setEdges, params, nodes, setNodes) => {
const { source, target } = params;
const newEdge = {
...params,
id: `e${source}-${target}`,
animated: false,
style: { stroke: '#000000', strokeWidth: 2 },
markerEnd: {
type: 'arrowclosed',
width: 20,
height: 20,
},
};
setEdges((eds) => addEdge(newEdge, eds));
setNodes((nds) =>
nds.map((node) => {
if (node.id === source) {
const newData = { ...node.data, nexts: [...node.data.nexts, target] };
return { ...node, data: newData };
} else if (node.id === target) {
return { ...node, prevs: [...(node.prevs || []), source] };
}
return node;
})
);
return newEdge;
};
export const deleteEdge = (edges, setEdges, edgeId, nodes, setNodes) => {
const edge = edges.find((e) => e.id === edgeId);
if (!edge) return;
setEdges((eds) => eds.filter((edge) => edge.id !== edgeId));
setNodes((nds) =>
nds.map((node) => {
if (node.id === edge.source) {
const newData = { ...node.data, nexts: node.data.nexts.filter((id) => id !== edge.target) };
return { ...node, data: newData };
} else if (node.id === edge.target) {
return { ...node, prevs: node.prevs.filter((id) => id !== edge.source) };
}
return node;
})
);
};