11// Referenced from example in https://www.npmjs.com/package/y-codemirror.next
2- import React , { useEffect , useRef } from "react" ;
2+ import React , { useEffect , useRef , useState } from "react" ;
33import * as Y from "yjs" ;
44import { yCollab } from "y-codemirror.next" ;
55import { WebrtcProvider } from "y-webrtc" ;
66import { EditorView , basicSetup } from "codemirror" ;
7- import { EditorState } from "@codemirror/state" ;
7+ import { EditorState , Compartment } from "@codemirror/state" ;
88import { javascript } from "@codemirror/lang-javascript" ;
9+ import { python , pythonLanguage } from "@codemirror/lang-python" ;
910import "./styles.scss" ;
10- import { message } from "antd" ;
11+ import { message , Select } from "antd" ;
12+ import { language } from "@codemirror/language" ;
13+ import { ProgrammingLanguageOptions } from "@/utils/SelectOptions" ;
1114
1215interface CollaborativeEditorProps {
1316 user : string ;
1417 collaborationId : string ;
18+ language : string ;
1519}
1620
1721export const usercolors = [
@@ -31,6 +35,29 @@ export const userColor =
3135
3236const CollaborativeEditor = ( props : CollaborativeEditorProps ) => {
3337 const editorRef = useRef ( null ) ;
38+ // const viewRef = useRef<EditorView | null>(null);
39+ const [ selectedLanguage , setSelectedLanguage ] = useState ( "javascript" ) ;
40+ const [ trigger , setTrigger ] = useState ( false ) ;
41+
42+ const languageConf = new Compartment ( ) ;
43+
44+ const autoLanguage = EditorState . transactionExtender . of ( ( tr ) => {
45+ if ( ! tr . docChanged ) return null ;
46+ const docIsPython = / ^ \s * d e f \s | \s * c l a s s \s / . test (
47+ tr . newDoc . sliceString ( 0 , 100 )
48+ ) ;
49+ const stateIsPython = tr . startState . facet ( language ) === pythonLanguage ;
50+ if ( docIsPython === stateIsPython ) return null ;
51+
52+ const newLanguage = docIsPython ? "python" : "javascript" ;
53+ setSelectedLanguage ( newLanguage ) ;
54+
55+ return {
56+ effects : languageConf . reconfigure (
57+ newLanguage === "python" ? python ( ) : javascript ( )
58+ ) ,
59+ } ;
60+ } ) ;
3461
3562 const [ messageApi , contextHolder ] = message . useMessage ( ) ;
3663
@@ -55,6 +82,29 @@ const CollaborativeEditor = (props: CollaborativeEditorProps) => {
5582 } ) ;
5683 } ;
5784
85+ // const handleLanguageChange = (val: any) => {
86+ // console.log("came in here");
87+ // console.log(val);
88+ // setSelectedLanguage(val);
89+
90+ // let languageExtension;
91+ // switch (val) {
92+ // case "python":
93+ // languageExtension = python();
94+ // break;
95+ // default:
96+ // languageExtension = javascript();
97+ // }
98+
99+ // // Update the language configuration
100+ // if (viewRef.current) {
101+ // console.log("insude here");
102+ // viewRef.current.dispatch({
103+ // effects: languageConf.reconfigure(languageExtension),
104+ // });
105+ // }
106+ // };
107+
58108 useEffect ( ( ) => {
59109 if ( process . env . NEXT_PUBLIC_SIGNALLING_SERVICE_URL === undefined ) {
60110 error ( "Missing Signalling Service Url" ) ;
@@ -78,7 +128,8 @@ const CollaborativeEditor = (props: CollaborativeEditorProps) => {
78128 doc : ytext . toString ( ) ,
79129 extensions : [
80130 basicSetup ,
81- javascript ( ) ,
131+ languageConf . of ( javascript ( ) ) ,
132+ autoLanguage ,
82133 yCollab ( ytext , provider . awareness , { undoManager } ) ,
83134 ] ,
84135 } ) ;
@@ -88,9 +139,16 @@ const CollaborativeEditor = (props: CollaborativeEditorProps) => {
88139 parent : editorRef . current || undefined ,
89140 } ) ;
90141
142+ // viewRef.current = new EditorView({
143+ // state: state,
144+ // parent: editorRef.current || undefined,
145+ // });
146+
91147 return ( ) => {
92148 // Cleanup on component unmount
149+ console . log ( "unmounting collaboration editor" ) ; // TODO: remove
93150 view . destroy ( ) ;
151+ // viewRef.current?.destroy();
94152 provider . disconnect ( ) ;
95153 ydoc . destroy ( ) ;
96154 } ;
@@ -99,7 +157,27 @@ const CollaborativeEditor = (props: CollaborativeEditorProps) => {
99157 return (
100158 < >
101159 { contextHolder }
102- < div ref = { editorRef } id = "editor" className = "code-editor-yjs" />
160+ < div className = "code-second-container" >
161+ < div className = "code-language" > Select Language:</ div >
162+ < Select
163+ className = "language-select"
164+ defaultValue = { selectedLanguage }
165+ options = { ProgrammingLanguageOptions }
166+ onSelect = { ( val ) => setSelectedLanguage ( val ) }
167+ />
168+ </ div >
169+ < div
170+ ref = { editorRef }
171+ style = { { height : "400px" , border : "1px solid #ddd" } }
172+ />
173+ < div className = "language-detected" >
174+ < strong > Current Language Detected: </ strong > { " " }
175+ {
176+ ProgrammingLanguageOptions . find (
177+ ( language ) => language . value === selectedLanguage
178+ ) ?. label
179+ }
180+ </ div >
103181 </ >
104182 ) ;
105183} ;
0 commit comments