9
9
SelectTrigger ,
10
10
SelectValue ,
11
11
} from "@/components/ui/select" ;
12
+ import { Button } from "@/components/ui/button" ;
12
13
import * as Y from "yjs" ;
13
14
import { WebsocketProvider } from "y-websocket" ;
14
15
import { MonacoBinding } from "y-monaco" ;
@@ -42,33 +43,28 @@ const languages: Record<string, LanguageEntry> = {
42
43
export default function CodeEditor ( { roomId } : { roomId : string } ) {
43
44
const monaco = useMonaco ( ) ;
44
45
const [ language , setLanguage ] = useState < string > ( "Javascript" ) ;
46
+ const [ theme , setTheme ] = useState < string > ( "light" ) ;
45
47
const ydoc = useMemo ( ( ) => new Y . Doc ( ) , [ ] ) ;
46
48
const [ editor , setEditor ] =
47
49
useState < MonacoEditor . IStandaloneCodeEditor | null > ( null ) ;
48
50
const [ provider , setProvider ] = useState < WebsocketProvider | null > ( null ) ;
49
51
const languageMap = useMemo ( ( ) => ydoc . getMap ( "language" ) , [ ydoc ] ) ;
50
52
51
- // this effect manages the lifetime of the Yjs document and the provider
52
53
useEffect ( ( ) => {
53
54
const provider = new WebsocketProvider (
54
55
"ws://localhost:3002/yjs" ,
55
56
roomId ,
56
57
ydoc
57
58
) ;
58
- console . log ( provider ) ;
59
59
setProvider ( provider ) ;
60
60
return ( ) => {
61
61
provider ?. destroy ( ) ;
62
62
ydoc . destroy ( ) ;
63
63
} ;
64
64
} , [ ydoc , roomId ] ) ;
65
65
66
- // this effect manages the lifetime of the editor binding
67
66
useEffect ( ( ) => {
68
- if ( provider == null || editor == null ) {
69
- console . log ( provider , editor ) ;
70
- return ;
71
- }
67
+ if ( provider == null || editor == null ) return ;
72
68
const monacoBinding = new MonacoBinding (
73
69
ydoc . getText ( ) ,
74
70
editor . getModel ( ) ! ,
@@ -80,9 +76,7 @@ export default function CodeEditor({ roomId }: { roomId: string }) {
80
76
} ;
81
77
} , [ ydoc , provider , editor ] ) ;
82
78
83
- // Sync language changes across clients
84
79
useEffect ( ( ) => {
85
- // Update language when languageMap changes
86
80
const handleLanguageChange = ( ) => {
87
81
const newLanguage = languageMap . get ( "selectedLanguage" ) as string ;
88
82
if ( newLanguage && newLanguage !== language ) {
@@ -96,7 +90,6 @@ export default function CodeEditor({ roomId }: { roomId: string }) {
96
90
} ;
97
91
} , [ languageMap , language ] ) ;
98
92
99
- // Apply language change in the editor model
100
93
useEffect ( ( ) => {
101
94
if ( editor && monaco ) {
102
95
const model = editor . getModel ( ) ;
@@ -106,6 +99,12 @@ export default function CodeEditor({ roomId }: { roomId: string }) {
106
99
}
107
100
} , [ language , editor , monaco ] ) ;
108
101
102
+ useEffect ( ( ) => {
103
+ if ( monaco ) {
104
+ monaco . editor . setTheme ( theme === "dark" ? "vs-dark" : "light" ) ;
105
+ }
106
+ } , [ theme , monaco ] ) ;
107
+
109
108
return (
110
109
< div className = "w-3/5 p-4" >
111
110
< Card className = "h-full flex flex-col" >
@@ -126,6 +125,12 @@ export default function CodeEditor({ roomId }: { roomId: string }) {
126
125
) ) }
127
126
</ SelectContent >
128
127
</ Select >
128
+ < Button
129
+ onClick = { ( ) => setTheme ( theme === "light" ? "dark" : "light" ) }
130
+ variant = { theme === "light" ? "secondary" : "default" }
131
+ >
132
+ { theme === "light" ? "Light" : "Dark" } Mode
133
+ </ Button >
129
134
</ div >
130
135
< div className = "flex-1 h-7/5" >
131
136
< Editor
@@ -134,6 +139,7 @@ export default function CodeEditor({ roomId }: { roomId: string }) {
134
139
onMount = { ( editor ) => {
135
140
setEditor ( editor ) ;
136
141
} }
142
+ theme = { theme === "dark" ? "vs-dark" : "light" }
137
143
/>
138
144
</ div >
139
145
</ CardContent >
0 commit comments