1- import { ReactFlow } from "@xyflow/react" ;
1+ import React , { useCallback } from 'react' ;
2+ import {
3+ ReactFlow ,
4+ Background ,
5+ useNodesState ,
6+ useEdgesState ,
7+ addEdge ,
8+ getIncomers ,
9+ getOutgoers ,
10+ getConnectedEdges ,
11+ } from '@xyflow/react' ;
212
313// style
414import "@xyflow/react/dist/style.css" ;
@@ -58,11 +68,69 @@ const MindMapComponent = ({ setScripts }: MindMapComponentProps) => {
5868 const [ mode , setMode ] = useState < string > ( "none" ) ;
5969
6070 const initialNodes = [
61- { id : "1" , position : { x : 0 , y : 0 } , data : { label : "2" } } ,
62- { id : "2" , position : { x : 0 , y : 100 } , data : { label : "2" } } ,
71+ {
72+ id : '1' ,
73+ type : 'input' ,
74+ data : { label : 'Start here...' } ,
75+ position : { x : - 150 , y : 0 } ,
76+ } ,
77+ {
78+ id : '2' ,
79+ type : 'input' ,
80+ data : { label : '...or here!' } ,
81+ position : { x : 150 , y : 0 } ,
82+ } ,
83+ { id : '3' , data : { label : 'Delete me.' } , position : { x : 0 , y : 100 } } ,
84+ { id : '4' , data : { label : 'Then me!' } , position : { x : 0 , y : 200 } } ,
85+ {
86+ id : '5' ,
87+ type : 'output' ,
88+ data : { label : 'End here!' } ,
89+ position : { x : 0 , y : 300 } ,
90+ } ,
6391 ] ;
6492
65- const initialEdges = [ { id : "e1-2" , source : "1" , target : "2" } ] ;
93+ const initialEdges = [
94+ { id : '1->3' , source : '1' , target : '3' } ,
95+ { id : '2->3' , source : '2' , target : '3' } ,
96+ { id : '3->4' , source : '3' , target : '4' } ,
97+ { id : '4->5' , source : '4' , target : '5' } ,
98+ ] ;
99+
100+ const [ nodes , setNodes , onNodesChange ] = useNodesState ( initialNodes ) ;
101+ const [ edges , setEdges , onEdgesChange ] = useEdgesState ( initialEdges ) ;
102+
103+ const onConnect = useCallback (
104+ ( params : any ) => setEdges ( addEdge ( params , edges ) ) ,
105+ [ edges ] ,
106+ ) ;
107+
108+ const onNodesDelete = useCallback (
109+ ( deleted : any [ ] ) => {
110+ setEdges (
111+ deleted . reduce ( ( acc , node ) => {
112+ const incomers = getIncomers ( node , nodes , edges ) ;
113+ const outgoers = getOutgoers ( node , nodes , edges ) ;
114+ const connectedEdges = getConnectedEdges ( [ node ] , edges ) ;
115+
116+ const remainingEdges = acc . filter (
117+ ( edge : { id : string ; source : string ; target : string ; } ) => ! connectedEdges . includes ( edge ) ,
118+ ) ;
119+
120+ const createdEdges = incomers . flatMap ( ( { id : source } ) =>
121+ outgoers . map ( ( { id : target } ) => ( {
122+ id : `${ source } ->${ target } ` ,
123+ source,
124+ target,
125+ } ) ) ,
126+ ) ;
127+
128+ return [ ...remainingEdges , ...createdEdges ] ;
129+ } , edges ) ,
130+ ) ;
131+ } ,
132+ [ nodes , edges ] ,
133+ ) ;
66134
67135 return (
68136 < div className = "mind-map-container" >
@@ -83,7 +151,17 @@ const MindMapComponent = ({ setScripts }: MindMapComponentProps) => {
83151 ) : (
84152 < div className = "mind-map-main" >
85153 < div className = "mind-map-wrap" >
86- < ReactFlow nodes = { initialNodes } edges = { initialEdges } />
154+ < ReactFlow
155+ nodes = { nodes }
156+ edges = { edges }
157+ onNodesChange = { onNodesChange }
158+ onNodesDelete = { onNodesDelete }
159+ onEdgesChange = { onEdgesChange }
160+ onConnect = { onConnect }
161+ fitView
162+ attributionPosition = "top-right"
163+ >
164+ </ ReactFlow >
87165 </ div >
88166 < div className = "middle-bar" >
89167 < div className = "record-length-wrap" >
0 commit comments