1
- import { EditorState , Extension , type ReactCodeMirrorRef } from '@uiw/react-codemirror' ;
1
+ import { Extension , type ReactCodeMirrorRef } from '@uiw/react-codemirror' ;
2
2
import { useEffect , useMemo , useRef , useState } from 'react' ;
3
3
import { yCollab } from 'y-codemirror.next' ;
4
4
import * as Y from 'yjs' ;
@@ -26,8 +26,9 @@ const getRandomColor = () => {
26
26
27
27
// TODO: Test if collab logic works
28
28
export const useCollab = ( roomId : string ) => {
29
+ const [ code , setCode ] = useState ( '' ) ;
29
30
const editorRef = useRef < ReactCodeMirrorRef > ( null ) ;
30
- const [ extensions , setExtensions ] = useState < Array < Extension > > ( [ baseExtensions ] ) ;
31
+ const [ extensions , setExtensions ] = useState < Array < Extension > > ( baseExtensions ) ;
31
32
const [ language , setLanguage ] = useState < LanguageName > ( 'python' ) ;
32
33
const langExtension = useMemo ( ( ) => {
33
34
return getLanguage ( language ) ;
@@ -46,10 +47,6 @@ export const useCollab = (roomId: string) => {
46
47
) ;
47
48
return ;
48
49
}
49
- if ( ! provider . socket . connected ) {
50
- console . log ( 'Failed to connect' ) ;
51
- return ;
52
- }
53
50
const ytext = doc . getText ( 'codemirror' ) ;
54
51
const undoManager = new Y . UndoManager ( ytext ) ;
55
52
const awareness = provider . awareness ;
@@ -60,9 +57,7 @@ export const useCollab = (roomId: string) => {
60
57
colorLight : light ,
61
58
} ) ;
62
59
const collabExt = yCollab ( ytext , awareness , { undoManager } ) ;
63
- editorRef . current . state = EditorState . create ( {
64
- doc : ytext . toJSON ( ) ,
65
- } ) ;
60
+ setCode ( ytext . toString ( ) ) ;
66
61
setExtensions ( [ ...extensions , collabExt ] ) ;
67
62
return ( ) => {
68
63
doc . destroy ( ) ;
@@ -75,5 +70,7 @@ export const useCollab = (roomId: string) => {
75
70
extensions : [ ...extensions , langExtension ] ,
76
71
language,
77
72
setLanguage,
73
+ code,
74
+ setCode,
78
75
} ;
79
76
} ;
0 commit comments