forked from LangGraph-GUI/LangGraph-GUI-reactflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanvas.js
217 lines (191 loc) · 7.29 KB
/
Canvas.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Canvas.js
import React, { useState, useCallback, useEffect, useRef } from 'react';
import ReactFlow, { MiniMap, Controls, Background, useReactFlow } from 'reactflow';
import 'reactflow/dist/style.css';
import Node, { addNode, deleteNode } from './Node';
import { createEdge, deleteEdge } from './Edge';
import { createConditionEdge, deleteConditionEdge } from './ConditionEdge';
import { saveJson, loadJson } from './JsonUtils';
import { useGraphManager } from './GraphManager';
import ConfigWindow from '../ConfigWindow';
import RunWindow from './RunWindow';
import FileTransmit from './FileTransmit';
import { useNavigate } from 'react-router-dom';
const nodeTypes = { textUpdater: Node };
function Canvas() {
const {
nodes,
setNodes,
onNodesChange,
edges,
setEdges,
onEdgesChange,
nodeIdCounter,
setNodeIdCounter,
} = useGraphManager();
const [contextMenu, setContextMenu] = useState(null);
const { screenToFlowPosition } = useReactFlow();
const menuBarRef = useRef(null);
const [canvasHeight, setCanvasHeight] = useState(window.innerHeight);
const [showConfig, setShowConfig] = useState(false);
const [showRun, setShowRun] = useState(false);
const navigate = useNavigate();
useEffect(() => {
const handleResize = () => {
if (menuBarRef.current) {
const menuBarHeight = menuBarRef.current.offsetHeight;
setCanvasHeight(window.innerHeight - menuBarHeight - 10);
}
};
window.addEventListener('resize', handleResize);
handleResize();
return () => window.removeEventListener('resize', handleResize);
}, []);
const handleAddNode = useCallback(() => {
const newPosition = screenToFlowPosition({ x: contextMenu.mouseX, y: contextMenu.mouseY });
addNode(nodes, setNodes, nodeIdCounter, setNodeIdCounter, newPosition);
setContextMenu(null);
}, [contextMenu, nodeIdCounter, setNodes, screenToFlowPosition, nodes, setNodeIdCounter]);
const handleDeleteNode = useCallback(() => {
if (contextMenu && contextMenu.nodeId) {
deleteNode(nodes, setNodes, edges, setEdges, contextMenu.nodeId);
}
setContextMenu(null);
}, [contextMenu, setNodes, setEdges, nodes, edges]);
const handleDeleteEdge = useCallback(() => {
if (contextMenu && contextMenu.edgeId) {
const edge = edges.find((e) => e.id === contextMenu.edgeId);
if (edge) {
if (edge.sourceHandle === 'true' || edge.sourceHandle === 'false') {
deleteConditionEdge(edges, setEdges, contextMenu.edgeId, nodes, setNodes);
} else {
deleteEdge(edges, setEdges, contextMenu.edgeId, nodes, setNodes);
}
}
}
setContextMenu(null);
}, [contextMenu, setEdges, edges, nodes, setNodes]);
const handleNodeContextMenu = useCallback((event, node) => {
event.preventDefault();
setContextMenu({
mouseX: event.clientX,
mouseY: event.clientY,
nodeId: node.id,
isNode: true,
isEdge: false,
});
}, []);
const handlePaneContextMenu = useCallback((event) => {
event.preventDefault();
setContextMenu({
mouseX: event.clientX,
mouseY: event.clientY,
isNode: false,
isEdge: false,
});
}, []);
const handleEdgeContextMenu = useCallback((event, edge) => {
event.preventDefault();
setContextMenu({
mouseX: event.clientX,
mouseY: event.clientY,
edgeId: edge.id,
isNode: false,
isEdge: true,
});
}, []);
const handleCloseContextMenu = () => {
setContextMenu(null);
};
const onConnect = useCallback((params) => {
const sourceNode = nodes.find(node => node.id === params.source);
if (params.sourceHandle === 'true') {
if (sourceNode.data.true_next !== null) {
alert('True port already has a connection.');
return;
}
createConditionEdge(edges, setEdges, params, nodes, setNodes);
} else if (params.sourceHandle === 'false') {
if (sourceNode.data.false_next !== null) {
alert('False port already has a connection.');
return;
}
createConditionEdge(edges, setEdges, params, nodes, setNodes);
} else {
createEdge(edges, setEdges, params, nodes, setNodes);
}
}, [setEdges, edges, nodes, setNodes]);
const handleNew = () => {
setNodes([]);
setEdges([]);
setNodeIdCounter(1);
};
const handleSave = async () => {
await saveJson(nodes, nodeIdCounter);
};
const handleLoad = async () => {
await loadJson(setEdges, setNodes, setNodeIdCounter);
};
const handleRun = () => {
setShowRun(true);
};
const handleConfig = () => {
setShowConfig(true);
};
const handleUploadComplete = () => {
console.log('Upload complete.');
};
return (
<div className="h-screen w-full">
<nav ref={menuBarRef} className="p-2 border-b border-gray-300 mb-2">
<button className="mr-2 bg-blue-500 hover:bg-blue-700 text-white font-bold px-2 rounded" onClick={handleNew}>New Graph</button>
<button className="mr-2 bg-blue-500 hover:bg-blue-700 text-white font-bold px-2 rounded" onClick={handleSave}>Save Graph</button>
<button className="mr-2 bg-blue-500 hover:bg-blue-700 text-white font-bold px-2 rounded" onClick={handleLoad}>Load Graph</button>
<button className="mr-2 bg-blue-500 hover:bg-blue-700 text-white font-bold px-2 rounded" onClick={handleRun}>Run Graph</button>
<button className="mr-2 bg-blue-500 hover:bg-blue-700 text-white font-bold px-2 rounded" onClick={handleConfig}>Config</button>
<FileTransmit onUploadComplete={handleUploadComplete} />
</nav>
<div style={{ height: `${canvasHeight}px` }} className="w-full">
<ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onNodeContextMenu={handleNodeContextMenu}
onPaneContextMenu={handlePaneContextMenu}
onEdgeContextMenu={handleEdgeContextMenu}
onClick={handleCloseContextMenu}
onConnect={onConnect}
connectionLineStyle={{ stroke: '#ddd', strokeWidth: 2 }}
connectOnClick={false}
nodeTypes={nodeTypes}
>
<MiniMap />
<Controls />
<Background />
</ReactFlow>
</div>
{contextMenu && (
<div
className="absolute bg-white border border-gray-300 z-1000 p-2"
style={{
top: contextMenu.mouseY,
left: contextMenu.mouseX,
}}
>
{contextMenu.isNode ? (
<button onClick={handleDeleteNode} className="block bg-red-500 hover:bg-red-700 text-white font-bold px-2 rounded">Delete Node</button>
) : contextMenu.isEdge ? (
<button onClick={handleDeleteEdge} className="block bg-red-500 hover:bg-red-700 text-white font-bold px-2 rounded">Delete Edge</button>
) : (
<button onClick={handleAddNode} className="block bg-green-500 hover:bg-green-700 text-white font-bold px-2 rounded">Add Node</button>
)}
<button onClick={handleCloseContextMenu} className="block bg-gray-500 hover:bg-gray-700 text-white font-bold px-2 rounded">Cancel</button>
</div>
)}
{showConfig && <ConfigWindow onClose={() => setShowConfig(false)} />}
{showRun && <RunWindow onClose={() => setShowRun(false)} />}
</div>
);
}
export default Canvas;