1
- import React , { useRef , useState , useEffect } from "react" ;
2
- import { ControlledEditor } from "@monaco-editor/react" ;
3
- import { executeCode , setLoadingTrue } from "../../actions/code" ;
4
- import { useDispatch , useSelector } from "react-redux" ;
5
- import Input from "./Input" ;
6
- import Submit from "./Submit" ;
7
- import Split from "react-split" ;
8
- import styled from "styled-components" ;
9
- import styles from "./styles/editor.module.css" ;
10
- import "./styles/style.css" ;
11
- import io from "socket.io-client" ;
12
- import { Play } from "react-feather" ;
13
-
14
- const ENDPOINT = "http://localhost:3000" ;
1
+ import React , { useRef , useState , useEffect } from 'react' ;
2
+ import { ControlledEditor } from '@monaco-editor/react' ;
3
+ import { executeCode , setLoadingTrue } from '../../actions/code' ;
4
+ import { useDispatch , useSelector } from 'react-redux' ;
5
+ import Input from './Input' ;
6
+ import Submit from './Submit' ;
7
+ import Split from 'react-split' ;
8
+ import styled from 'styled-components' ;
9
+ import styles from './styles/editor.module.css' ;
10
+ import './styles/style.css' ;
11
+ import io from 'socket.io-client' ;
12
+ import { Play } from 'react-feather' ;
13
+ import {
14
+ getDefaultCode ,
15
+ setLanguageLocalStorage ,
16
+ getLanguageLocalStorage ,
17
+ setCodeLocalStorage ,
18
+ getCodeLocalStorage
19
+ } from './utils/code-settings' ;
20
+
21
+ const ENDPOINT = 'http://localhost:3000' ;
15
22
16
23
const socket = io ( ENDPOINT ) ;
17
24
@@ -27,28 +34,38 @@ const OutputWindow = styled.div`
27
34
box-sizing: border-box;
28
35
overflow: auto;
29
36
flex: 1;
30
- color: ${ ( props ) => ( props . error ? " red" : " black" ) } ;
37
+ color: ${ props => ( props . error ? ' red' : ' black' ) } ;
31
38
` ;
32
39
33
40
const CodeEditor = ( { theme } ) => {
34
- const loading = useSelector ( ( state ) => state . code . isFetching ) ;
41
+ const loading = useSelector ( state => state . code . isFetching ) ;
35
42
const [ isEditorReady , setIsEditorReady ] = useState ( false ) ;
36
43
const [ windowWidth , setWindowWidth ] = useState ( window . innerWidth ) ;
37
44
const [ windowHeight , setWindowHeight ] = useState ( window . innerHeight ) ;
38
- const [ language , setLanguage ] = useState ( "c" ) ;
39
- const [ input , setInput ] = useState ( "" ) ;
45
+ const [ language , setLanguage ] = useState ( 'java' ) ;
46
+ const [ input , setInput ] = useState ( '' ) ;
40
47
// const [output, setOutput] = useState("");
41
- const [ code , setCode ] = useState ( "" ) ;
48
+ const [ code , setCode ] = useState ( '' ) ;
42
49
const dispatch = useDispatch ( ) ;
43
50
const valueGetter = useRef ( ) ;
44
- let output = useSelector ( ( state ) => state . code . output ) ;
51
+ let output = useSelector ( state => state . code . output ) ;
45
52
46
- let error = useSelector ( ( state ) => state . code . error ) ;
53
+ let error = useSelector ( state => state . code . error ) ;
47
54
48
55
useEffect ( ( ) => {
49
- window . addEventListener ( "resize" , updateWindowDimensions ) ;
56
+ window . addEventListener ( 'resize' , updateWindowDimensions ) ;
57
+
58
+ if ( getLanguageLocalStorage ( ) ) {
59
+ setLanguage ( getLanguageLocalStorage ( ) ) ;
60
+ if ( getCodeLocalStorage ( ) ) {
61
+ setCode ( getCodeLocalStorage ( ) ) ;
62
+ } else setCode ( getDefaultCode ( getLanguageLocalStorage ( ) ) ) ;
63
+ } else {
64
+ setCode ( getDefaultCode ( language ) ) ;
65
+ }
66
+
50
67
return ( ) => {
51
- window . removeEventListener ( " resize" , updateWindowDimensions ) ;
68
+ window . removeEventListener ( ' resize' , updateWindowDimensions ) ;
52
69
} ;
53
70
} , [ ] ) ;
54
71
@@ -58,23 +75,23 @@ const CodeEditor = ({ theme }) => {
58
75
} ;
59
76
60
77
useEffect ( ( ) => {
61
- console . log ( " socket: browser says ping (1)" ) ;
62
- socket . on ( " setLanguage" , function ( data ) {
78
+ console . log ( ' socket: browser says ping (1)' ) ;
79
+ socket . on ( ' setLanguage' , function ( data ) {
63
80
// console.log(data);
64
81
setLanguage ( data ) ;
65
82
} ) ;
66
- socket . on ( " setInput" , ( data ) => {
83
+ socket . on ( ' setInput' , data => {
67
84
setInput ( data ) ;
68
85
} ) ;
69
- socket . on ( " setOutput" , ( data ) => {
86
+ socket . on ( ' setOutput' , data => {
70
87
dispatch ( data ) ;
71
88
} ) ;
72
- socket . on ( " setCodeExec" , ( data ) => {
89
+ socket . on ( ' setCodeExec' , data => {
73
90
setCode ( data ) ;
74
91
} ) ;
75
92
} , [ ] ) ;
76
93
77
- const handleEditorDidMount = ( _valueGetter ) => {
94
+ const handleEditorDidMount = _valueGetter => {
78
95
setIsEditorReady ( true ) ;
79
96
valueGetter . current = _valueGetter ;
80
97
} ;
@@ -84,9 +101,11 @@ const CodeEditor = ({ theme }) => {
84
101
// socket.emit("getOutput", output);
85
102
// setOutput(out);
86
103
} ;
104
+
87
105
const onChangeCode = ( newValue , e ) => {
88
106
// console.log("onChange" + e);
89
- socket . emit ( "getCodeExec" , e ) ;
107
+ socket . emit ( 'getCodeExec' , e ) ;
108
+ setCodeLocalStorage ( e ) ;
90
109
setCode ( e ) ;
91
110
} ;
92
111
@@ -95,23 +114,26 @@ const CodeEditor = ({ theme }) => {
95
114
const res = await executeCode ( code , language , input ) ;
96
115
dispatch ( res ) ;
97
116
// getOutput();
98
- socket . emit ( " getOutput" , res ) ;
117
+ socket . emit ( ' getOutput' , res ) ;
99
118
} ;
100
119
101
- const changeLanguage = ( e ) => {
102
- socket . emit ( "getLanguage" , e . target . value ) ;
120
+ const changeLanguage = e => {
121
+ setLanguageLocalStorage ( e . target . value ) ;
122
+ setCode ( getDefaultCode ( e . target . value ) ) ;
123
+ setCodeLocalStorage ( getDefaultCode ( e . target . value ) ) ;
124
+ socket . emit ( 'getLanguage' , e . target . value ) ;
103
125
setLanguage ( e . target . value ) ;
104
126
} ;
105
127
return (
106
128
< >
107
129
< div className = { styles . row } >
108
130
< Split
109
- direction = { windowWidth > 800 ? " horizontal" : " vertical" }
131
+ direction = { windowWidth > 800 ? ' horizontal' : ' vertical' }
110
132
sizes = { [ 60 , 40 ] }
111
133
minSize = { windowWidth > 800 ? 0 : 500 }
112
134
snapOffset = { windowWidth > 800 ? 200 : 0 }
113
135
gutterSize = { 20 }
114
- gutterAlign = " center"
136
+ gutterAlign = ' center'
115
137
className = { styles . splitHor }
116
138
>
117
139
< div className = { styles . left } >
@@ -122,8 +144,8 @@ const CodeEditor = ({ theme }) => {
122
144
onClick = { SubmitCode }
123
145
disabled = { ! isEditorReady }
124
146
>
125
- { loading ? " Loading.." : " Run Code" }
126
- < Play style = { { paddingLeft : 10 , fontSize : " 1em" } } />
147
+ { loading ? ' Loading..' : ' Run Code' }
148
+ < Play style = { { paddingLeft : 10 , fontSize : ' 1em' } } />
127
149
</ div >
128
150
</ div >
129
151
{ /* <Editor
@@ -134,9 +156,9 @@ const CodeEditor = ({ theme }) => {
134
156
/> */ }
135
157
< ControlledEditor
136
158
// wrapperClassName="editor"
137
- className = " editor"
159
+ className = ' editor'
138
160
language = { language }
139
- theme = { theme === " dark" ? " vs-dark" : " light" }
161
+ theme = { theme === ' dark' ? ' vs-dark' : ' light' }
140
162
editorDidMount = { handleEditorDidMount }
141
163
value = { code }
142
164
onChange = { onChangeCode }
@@ -145,20 +167,20 @@ const CodeEditor = ({ theme }) => {
145
167
< div className = { styles . right } >
146
168
< div className = { styles . column } >
147
169
< Split
148
- direction = " vertical"
170
+ direction = ' vertical'
149
171
sizes = { windowWidth > 800 ? [ 75 , 25 ] : [ 100 , 0 ] }
150
172
minSize = { 0 }
151
173
snapOffset = { windowWidth > 800 ? 100 : 0 }
152
174
gutterSize = { 20 }
153
- gutterAlign = " center"
175
+ gutterAlign = ' center'
154
176
className = { styles . splitVer }
155
177
>
156
178
< div className = { styles . output } >
157
179
< div className = { styles . outputHead } > Output</ div >
158
- < OutputWindow error = { error === "" ? false : true } >
180
+ < OutputWindow error = { error === '' ? false : true } >
159
181
{ output ? console . log ( output ) : null }
160
- < pre style = { { width : " 100%" } } >
161
- { output === "" ? error : output }
182
+ < pre style = { { width : ' 100%' } } >
183
+ { output === '' ? error : output }
162
184
</ pre >
163
185
</ OutputWindow >
164
186
</ div >
0 commit comments