-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGraphApp.tsx
144 lines (121 loc) · 6.63 KB
/
GraphApp.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
// Graph/GraphApp.tsx
import React, { useMemo, useCallback, useState, useRef, useEffect } from 'react';
import { ReactFlow, MiniMap, Controls, Background, useReactFlow, ReactFlowProps, NodeChange, EdgeChange, } from '@xyflow/react';
import '@xyflow/react/dist/style.css';
import { useGraph } from './GraphContext';
import GraphPanel from './GraphPanel';
import './GraphApp.css';
import CustomNode from './CustomNode';
import CustomEdge from './CustomEdge';
import { useGraphActions } from './GraphActions';
import { Edge as ReactFlowEdge } from '@xyflow/react';
const GraphApp: React.FC = () => {
const { currentGraphName, updateNodeData, handleNodesChange, handleEdgesChange, getCurrentGraph} = useGraph();
const [contextMenu, setContextMenu] = useState<{mouseX: number, mouseY: number, nodeId: string | null, edgeId:string | null, type: 'panel' | 'node' | 'edge'} | null>(null);
const [canvasHeight, setCanvasHeight] = useState<number>(window.innerHeight);
const menuBarRef = useRef<HTMLDivElement>(null); //ref for menu bar
const { screenToFlowPosition } = useReactFlow();
const { handleAddNode, handleDeleteNode, handleDeleteEdge, handlePanelContextMenu, handleAddEdge } = useGraphActions();
// Always get the current graph, use initial graph data when current graph is not loaded
const currentGraph = useMemo(()=> getCurrentGraph(), [getCurrentGraph]);
const handleCloseContextMenu = useCallback(() => {
setContextMenu(null);
}, []);
const handleNodeDataChange = useCallback((nodeId: string, newData: any) => {
updateNodeData(currentGraphName, nodeId, newData)
}, [updateNodeData, currentGraphName]);
const handleEdgeClick = useCallback((event: React.MouseEvent, edge: ReactFlowEdge) => {
event.preventDefault();
event.stopPropagation();
console.log("handleEdgeClick", edge)
}, [])
const reactFlowProps = useMemo<ReactFlowProps>(() => ({
onContextMenu: (event: React.MouseEvent)=> handlePanelContextMenu(event, setContextMenu),
onClick: handleCloseContextMenu,
onNodesChange: (changes: NodeChange[]) => handleNodesChange(currentGraphName, changes),
onEdgesChange: (changes: EdgeChange[]) => handleEdgesChange(currentGraphName, changes),
onEdgeClick: handleEdgeClick,
onConnect: handleAddEdge,
edgeTypes: {
custom: (props) => {
const {sourceNode, targetNode} = props.data || {}
return <CustomEdge {...props} sourceNode={sourceNode} targetNode={targetNode} />
},
},
}),[handlePanelContextMenu,handleCloseContextMenu, handleNodesChange, handleEdgesChange, handleEdgeClick, handleAddEdge, currentGraphName, setContextMenu])
useEffect(() => {
const handleResize = () => {
if (menuBarRef.current) {
const menuBarHeight = menuBarRef.current.offsetHeight;
setCanvasHeight(window.innerHeight - menuBarHeight - 10);
} else {
setCanvasHeight(window.innerHeight-10);
}
};
window.addEventListener('resize', handleResize);
handleResize();
return () => window.removeEventListener('resize', handleResize);
}, []);
const nodeTypes = useMemo(() => ({
custom: (props: any) => <CustomNode {...props} onNodeDataChange={handleNodeDataChange} />,
}), [handleNodeDataChange]);
return (
<div style={{ width: '100vw', height: '100vh' }}>
{/*Assuming you have a div with ref for menuBar*/}
<div ref={menuBarRef}>
<GraphPanel />
</div>
<div style={{ height: `${canvasHeight}px` }} className="w-full">
<ReactFlow
nodes={currentGraph.nodes}
edges={currentGraph.edges}
{...reactFlowProps}
nodeTypes={nodeTypes}
connectionLineStyle={{ stroke: '#ddd', strokeWidth: 2 }}
>
<MiniMap />
<Background />
<Controls />
</ReactFlow>
{contextMenu && contextMenu.type === 'panel' && (
<div
className="absolute bg-white border border-gray-300 z-1000 p-2"
style={{
top: contextMenu.mouseY,
left: contextMenu.mouseX,
}}
>
<button onClick={()=> handleAddNode({contextMenu, setContextMenu, screenToFlowPosition})} 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>
)}
{contextMenu && contextMenu.type === 'node' &&(
<div
className="absolute bg-white border border-gray-300 z-1000 p-2"
style={{
top: contextMenu.mouseY,
left: contextMenu.mouseX,
}}
>
{/* <button onClick={handleAddEdge} className="block bg-blue-500 hover:bg-blue-700 text-white font-bold px-2 rounded">Add Edge</button> */}
<button onClick={()=> handleDeleteNode(contextMenu, setContextMenu)} className="block bg-red-500 hover:bg-red-700 text-white font-bold px-2 rounded">Delete Node</button>
<button onClick={handleCloseContextMenu} className="block bg-gray-500 hover:bg-gray-700 text-white font-bold px-2 rounded">Cancel</button>
</div>
)}
{contextMenu && contextMenu.type === 'edge' &&(
<div
className="absolute bg-white border border-gray-300 z-1000 p-2"
style={{
top: contextMenu.mouseY,
left: contextMenu.mouseX,
}}
>
<button onClick={()=> handleDeleteEdge(contextMenu, setContextMenu)} className="block bg-red-500 hover:bg-red-700 text-white font-bold px-2 rounded">Delete Edge</button>
<button onClick={handleCloseContextMenu} className="block bg-gray-500 hover:bg-gray-700 text-white font-bold px-2 rounded">Cancel</button>
</div>
)}
</div>
</div>
);
};
export default GraphApp;