@@ -22,6 +22,7 @@ import * as Y from "yjs";
2222import { WebsocketProvider } from "y-websocket" ;
2323import { cn } from "@/lib/utils" ;
2424import { useQueryClient } from "@tanstack/react-query" ;
25+ import useYDocStore from "@/store/useYDocStore" ;
2526
2627const proOptions = { hideAttribution : true } ;
2728
@@ -35,23 +36,39 @@ export default function Canvas({ className }: CanvasProps) {
3536 const { pages } = usePages ( ) ;
3637 const queryClient = useQueryClient ( ) ;
3738
38- const ydoc = useRef < Y . Doc > ( ) ;
39+ const { ydoc } = useYDocStore ( ) ;
40+
3941 const provider = useRef < WebsocketProvider > ( ) ;
4042 const existingPageIds = useRef ( new Set < string > ( ) ) ;
4143
4244 useEffect ( ( ) => {
43- const doc = new Y . Doc ( ) ;
45+ if ( ! pages ) return ;
46+
47+ const yMap = ydoc . getMap ( "title" ) ;
48+
49+ pages . forEach ( ( page ) => {
50+ if ( yMap . get ( `title_${ page . id } ` ) ) return ;
51+
52+ const yText = new Y . Text ( ) ;
53+ yText . insert ( 0 , page . title ) ;
54+
55+ yMap . set ( `title_${ page . id } ` , yText ) ;
56+ } ) ;
57+ } , [ pages ] ) ;
58+
59+ useEffect ( ( ) => {
60+ if ( ! ydoc ) return ;
61+
4462 const wsProvider = new WebsocketProvider (
4563 import . meta. env . VITE_WS_URL ,
4664 "flow-room" ,
47- doc ,
65+ ydoc ,
4866 ) ;
4967
50- ydoc . current = doc ;
5168 provider . current = wsProvider ;
5269
53- const nodesMap = doc . getMap ( "nodes" ) ;
54- const edgesMap = doc . getMap ( "edges" ) ;
70+ const nodesMap = ydoc . getMap ( "nodes" ) ;
71+ const edgesMap = ydoc . getMap ( "edges" ) ;
5572
5673 nodesMap . observe ( ( event ) => {
5774 event . changes . keys . forEach ( ( change , key ) => {
@@ -86,14 +103,14 @@ export default function Canvas({ className }: CanvasProps) {
86103
87104 return ( ) => {
88105 wsProvider . destroy ( ) ;
89- doc . destroy ( ) ;
106+ ydoc . destroy ( ) ;
90107 } ;
91- } , [ queryClient ] ) ;
108+ } , [ ydoc , queryClient ] ) ;
92109
93110 useEffect ( ( ) => {
94- if ( ! pages || ! ydoc . current ) return ;
111+ if ( ! pages || ! ydoc ) return ;
95112
96- const nodesMap = ydoc . current . getMap ( "nodes" ) ;
113+ const nodesMap = ydoc . getMap ( "nodes" ) ;
97114 const currentPageIds = new Set ( pages . map ( ( page ) => page . id . toString ( ) ) ) ;
98115
99116 existingPageIds . current . forEach ( ( pageId ) => {
@@ -105,27 +122,27 @@ export default function Canvas({ className }: CanvasProps) {
105122
106123 pages . forEach ( ( page ) => {
107124 const pageId = page . id . toString ( ) ;
108- if ( ! existingPageIds . current . has ( pageId ) ) {
109- const newNode = {
110- id : pageId ,
111- position : {
112- x : Math . random ( ) * 500 ,
113- y : Math . random ( ) * 500 ,
114- } ,
115- data : { title : page . title , id : page . id } ,
116- type : "note" ,
117- } ;
118-
119- nodesMap . set ( pageId , newNode ) ;
120- existingPageIds . current . add ( pageId ) ;
121- }
125+ // if (!existingPageIds.current.has(pageId)) {
126+ const newNode = {
127+ id : pageId ,
128+ position : {
129+ x : Math . random ( ) * 500 ,
130+ y : Math . random ( ) * 500 ,
131+ } ,
132+ data : { title : page . title , id : page . id } ,
133+ type : "note" ,
134+ } ;
135+
136+ nodesMap . set ( pageId , newNode ) ;
137+ existingPageIds . current . add ( pageId ) ;
138+ // }
122139 } ) ;
123140 } , [ pages ] ) ;
124141
125142 const handleNodesChange = useCallback (
126143 ( changes : NodeChange [ ] ) => {
127- if ( ! ydoc . current ) return ;
128- const nodesMap = ydoc . current . getMap ( "nodes" ) ;
144+ if ( ! ydoc ) return ;
145+ const nodesMap = ydoc . getMap ( "nodes" ) ;
129146
130147 onNodesChange ( changes ) ;
131148
@@ -147,8 +164,8 @@ export default function Canvas({ className }: CanvasProps) {
147164
148165 const handleEdgesChange = useCallback (
149166 ( changes : EdgeChange [ ] ) => {
150- if ( ! ydoc . current ) return ;
151- const edgesMap = ydoc . current . getMap ( "edges" ) ;
167+ if ( ! ydoc ) return ;
168+ const edgesMap = ydoc . getMap ( "edges" ) ;
152169
153170 changes . forEach ( ( change ) => {
154171 if ( change . type === "remove" ) {
@@ -163,7 +180,7 @@ export default function Canvas({ className }: CanvasProps) {
163180
164181 const onConnect = useCallback (
165182 ( connection : Connection ) => {
166- if ( ! connection . source || ! connection . target || ! ydoc . current ) return ;
183+ if ( ! connection . source || ! connection . target || ! ydoc ) return ;
167184
168185 const newEdge : Edge = {
169186 id : `e${ connection . source } -${ connection . target } ` ,
@@ -173,7 +190,7 @@ export default function Canvas({ className }: CanvasProps) {
173190 targetHandle : connection . targetHandle || undefined ,
174191 } ;
175192
176- ydoc . current . getMap ( "edges" ) . set ( newEdge . id , newEdge ) ;
193+ ydoc . getMap ( "edges" ) . set ( newEdge . id , newEdge ) ;
177194 setEdges ( ( eds ) => addEdge ( connection , eds ) ) ;
178195 } ,
179196 [ setEdges ] ,
0 commit comments