1
1
import Editor from "@monaco-editor/react" ;
2
2
import styles from "./FileEditor.module.css" ;
3
- import { FC , useRef , useContext } from "react" ;
3
+ import { FC , useRef , useContext , useEffect , useState } from "react" ;
4
4
import EditorBar from "../editorBar" ;
5
5
import { AppContext } from "../context" ;
6
6
7
7
const FileEditor : FC = ( ) => {
8
8
const { username, editorState, code, setCode, io } = useContext ( AppContext ) ;
9
9
const editorRef = useRef ( ) ;
10
+ const monacoRef = useRef ( ) ;
11
+ const [ lang , setLang ] = useState ( "text" ) ;
12
+
13
+ const languages = {
14
+ html : "html" ,
15
+ css : "css" ,
16
+ js : "javascript" ,
17
+ ts : "typescript" ,
18
+ htm : "html" ,
19
+ txt : "text" ,
20
+ json : "json" ,
21
+ md : "markdown" ,
22
+ py : "python" ,
23
+ cpp : "cpp" ,
24
+ sass : "sass" ,
25
+ scss : "scss" ,
26
+ java : "java" ,
27
+ less : "less" ,
28
+ xml : "xml" ,
29
+ } ;
10
30
11
31
function handleEditorChange ( value : string | undefined , e : any ) {
12
32
if ( editorState . activeTab ) {
@@ -20,15 +40,26 @@ const FileEditor: FC = () => {
20
40
21
41
function handleEditorDidMount ( editor : any , monaco : any ) {
22
42
editorRef . current = editor ;
43
+ monacoRef . current = monaco ;
23
44
}
24
45
46
+ useEffect ( ( ) => {
47
+ setTimeout ( ( ) => {
48
+ // @ts -ignore
49
+ setLang ( languages [ editorState . activeTab . split ( "." ) . pop ( ) ] || "text" ) ;
50
+ } , 1500 ) ;
51
+ // eslint-disable-next-line react-hooks/exhaustive-deps
52
+ } , [ editorState . activeTab ] ) ;
53
+
25
54
return (
26
55
< div className = { styles . container } >
27
56
< EditorBar />
28
57
< div className = { styles . editor__wrapper } >
29
58
< Editor
30
59
theme = "vs-dark"
31
60
height = "100%"
61
+ language = { lang }
62
+ // language={editorState.activeTab.split(".").pop()}
32
63
value = { code [ editorState . activeTab ] }
33
64
onChange = { handleEditorChange }
34
65
onMount = { handleEditorDidMount }
0 commit comments