@@ -14,6 +14,7 @@ import {
1414 Edge ,
1515 EdgeChange ,
1616 Connection ,
17+ ReactFlowProvider ,
1718} from "@xyflow/react" ;
1819import "@xyflow/react/dist/style.css" ;
1920import { usePages } from "@/hooks/usePages" ;
@@ -30,7 +31,7 @@ interface CanvasProps {
3031 className ?: string ;
3132}
3233
33- export default function Canvas ( { className } : CanvasProps ) {
34+ function Flow ( { className } : CanvasProps ) {
3435 const [ nodes , setNodes , onNodesChange ] = useNodesState < Node > ( [ ] ) ;
3536 const [ edges , setEdges , onEdgesChange ] = useEdgesState < Edge > ( [ ] ) ;
3637 const { pages } = usePages ( ) ;
@@ -70,11 +71,14 @@ export default function Canvas({ className }: CanvasProps) {
7071 const nodesMap = ydoc . getMap ( "nodes" ) ;
7172 const edgesMap = ydoc . getMap ( "edges" ) ;
7273
74+ const initialNodes = Array . from ( nodesMap . values ( ) ) as Node [ ] ;
75+ setNodes ( initialNodes ) ;
76+
7377 nodesMap . observe ( ( event ) => {
7478 event . changes . keys . forEach ( ( change , key ) => {
7579 const nodeId = key ;
7680 if ( change . action === "add" || change . action === "update" ) {
77- const node = nodesMap . get ( nodeId ) as Node ;
81+ const updatedNode = nodesMap . get ( nodeId ) as Node ;
7882
7983 if ( change . action === "add" ) {
8084 queryClient . invalidateQueries ( { queryKey : [ "pages" ] } ) ;
@@ -83,12 +87,13 @@ export default function Canvas({ className }: CanvasProps) {
8387 setNodes ( ( nds ) => {
8488 const index = nds . findIndex ( ( n ) => n . id === nodeId ) ;
8589 if ( index === - 1 ) {
86- return [ ...nds , node ] ;
90+ return [ ...nds , updatedNode ] ;
8791 }
8892 const newNodes = [ ...nds ] ;
8993 newNodes [ index ] = {
9094 ...newNodes [ index ] ,
91- position : node . position ,
95+ position : updatedNode . position ,
96+ selected : false ,
9297 } ;
9398 return newNodes ;
9499 } ) ;
@@ -106,7 +111,6 @@ export default function Canvas({ className }: CanvasProps) {
106111
107112 return ( ) => {
108113 wsProvider . destroy ( ) ;
109- ydoc . destroy ( ) ;
110114 } ;
111115 } , [ ydoc , queryClient ] ) ;
112116
@@ -127,22 +131,23 @@ export default function Canvas({ className }: CanvasProps) {
127131 const pageId = page . id . toString ( ) ;
128132 const existingNode = nodesMap . get ( pageId ) as Node | undefined ;
129133
130- const newNode = {
131- id : pageId ,
132- position : existingNode ?. position || {
133- x : Math . random ( ) * 500 ,
134- y : Math . random ( ) * 500 ,
135- } ,
136- data : { title : page . title , id : page . id } ,
137- type : "note" ,
138- } ;
139-
140134 if ( ! existingNode ) {
135+ const newNode = {
136+ id : pageId ,
137+ type : "note" ,
138+ data : { title : page . title , id : page . id } ,
139+ position : {
140+ x : Math . random ( ) * 500 ,
141+ y : Math . random ( ) * 500 ,
142+ } ,
143+ selected : false ,
144+ } ;
145+
141146 nodesMap . set ( pageId , newNode ) ;
142147 existingPageIds . current . add ( pageId ) ;
143148 }
144149 } ) ;
145- } , [ pages ] ) ;
150+ } , [ pages , ydoc ] ) ;
146151
147152 const handleNodesChange = useCallback (
148153 ( changes : NodeChange [ ] ) => {
@@ -151,11 +156,12 @@ export default function Canvas({ className }: CanvasProps) {
151156
152157 changes . forEach ( ( change ) => {
153158 if ( change . type === "position" && change . position ) {
154- const existingNode = nodesMap . get ( change . id ) as Node | undefined ;
155- if ( existingNode ) {
159+ const node = nodes . find ( ( n ) => n . id === change . id ) ;
160+ if ( node ) {
156161 const updatedNode = {
157- ...existingNode ,
162+ ...node ,
158163 position : change . position ,
164+ selected : false ,
159165 } ;
160166 nodesMap . set ( change . id , updatedNode ) ;
161167 }
@@ -164,7 +170,7 @@ export default function Canvas({ className }: CanvasProps) {
164170
165171 onNodesChange ( changes ) ;
166172 } ,
167- [ onNodesChange ] ,
173+ [ nodes , onNodesChange ] ,
168174 ) ;
169175
170176 const handleEdgesChange = useCallback (
@@ -214,6 +220,7 @@ export default function Canvas({ className }: CanvasProps) {
214220 proOptions = { proOptions }
215221 nodeTypes = { nodeTypes }
216222 connectionMode = { ConnectionMode . Loose }
223+ selectNodesOnDrag = { false }
217224 >
218225 < Controls />
219226 < MiniMap />
@@ -222,3 +229,11 @@ export default function Canvas({ className }: CanvasProps) {
222229 </ div >
223230 ) ;
224231}
232+
233+ export default function Canvas ( props : CanvasProps ) {
234+ return (
235+ < ReactFlowProvider >
236+ < Flow { ...props } />
237+ </ ReactFlowProvider >
238+ ) ;
239+ }
0 commit comments