diff --git a/src/VisualProgrammingUI/VisProgUI.tsx b/src/VisualProgrammingUI/VisProgUI.tsx index 2f58a64..40e858d 100644 --- a/src/VisualProgrammingUI/VisProgUI.tsx +++ b/src/VisualProgrammingUI/VisProgUI.tsx @@ -1,14 +1,17 @@ import './VisProgUI.css' -import { useState, useCallback } from 'react'; import { + useCallback, + useRef +} from 'react'; +import { + Background, + Controls, ReactFlow, - applyNodeChanges, - applyEdgeChanges, - addEdge, - type NodeChange, - type EdgeChange, - type Edge, type Connection + useNodesState, + useEdgesState, + reconnectEdge, + addEdge, type Edge, type Connection, } from '@xyflow/react'; import '@xyflow/react/dist/style.css'; @@ -18,39 +21,55 @@ const initialNodes = [ ]; const initialEdges = [{id: 'n1-n2', source: 'n1', target: 'n2'}]; -export default function App() { - const [nodes, setNodes] = useState(initialNodes); - const [edges, setEdges] = useState(initialEdges); +const VisualProgrammingUI = ()=> { + const edgeReconnectSuccessful = useRef(true); + const [nodes, , onNodesChange] = useNodesState(initialNodes); + const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges); + - const onNodesChange = useCallback( - (changes: NodeChange<{ - id: string; - position: { x: number; y: number; }; - data: { label: string; }; - }>[]) => setNodes((nodesSnapshot) => applyNodeChanges(changes, nodesSnapshot)), - [], - ); - const onEdgesChange = useCallback( - (changes: EdgeChange<{ id: string; source: string; target: string; }>[]) => setEdges((edgesSnapshot) => applyEdgeChanges(changes, edgesSnapshot)), - [], - ); - const onConnect = useCallback( - (params: Edge | Connection) => setEdges((edgesSnapshot) => addEdge(params, edgesSnapshot)), - [], + (params: Edge | Connection) => setEdges((els) => addEdge(params, els)), + [setEdges], ); + const onReconnectStart = useCallback(() => { + edgeReconnectSuccessful.current = false; + }, []); + + const onReconnect = useCallback((oldEdge: Edge, newConnection: Connection) => { + edgeReconnectSuccessful.current = true; + setEdges((els) => reconnectEdge(oldEdge, newConnection, els)); + }, [setEdges]); + + const onReconnectEnd = useCallback((_: unknown, edge: { id: string; }) => { + if (!edgeReconnectSuccessful.current) { + setEdges((eds) => eds.filter((e) => e.id !== edge.id)); + } + + edgeReconnectSuccessful.current = true; + }, [setEdges]); + return ( -
+
+ attributionPosition="top-right" + > + + +
); -} \ No newline at end of file +}; + +export default VisualProgrammingUI \ No newline at end of file diff --git a/src/main.tsx b/src/main.tsx index 21c1bf7..9d67972 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -1,10 +1,12 @@ import { StrictMode } from 'react' import { createRoot } from 'react-dom/client' import './index.css' -import App from './VisualProgrammingUI/VisProgUI.tsx' +import App from './App.tsx' +import VisualProgrammingUI from "./VisualProgrammingUI/VisProgUI.tsx"; createRoot(document.getElementById('root')!).render( + , )