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' ;
1522
1623const socket = io ( ENDPOINT ) ;
1724
@@ -27,28 +34,38 @@ const OutputWindow = styled.div`
2734 box-sizing: border-box;
2835 overflow: auto;
2936 flex: 1;
30- color: ${ ( props ) => ( props . error ? " red" : " black" ) } ;
37+ color: ${ props => ( props . error ? ' red' : ' black' ) } ;
3138` ;
3239
3340const CodeEditor = ( { theme } ) => {
34- const loading = useSelector ( ( state ) => state . code . isFetching ) ;
41+ const loading = useSelector ( state => state . code . isFetching ) ;
3542 const [ isEditorReady , setIsEditorReady ] = useState ( false ) ;
3643 const [ windowWidth , setWindowWidth ] = useState ( window . innerWidth ) ;
3744 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 ( '' ) ;
4047 // const [output, setOutput] = useState("");
41- const [ code , setCode ] = useState ( "" ) ;
48+ const [ code , setCode ] = useState ( '' ) ;
4249 const dispatch = useDispatch ( ) ;
4350 const valueGetter = useRef ( ) ;
44- let output = useSelector ( ( state ) => state . code . output ) ;
51+ let output = useSelector ( state => state . code . output ) ;
4552
46- let error = useSelector ( ( state ) => state . code . error ) ;
53+ let error = useSelector ( state => state . code . error ) ;
4754
4855 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+
5067 return ( ) => {
51- window . removeEventListener ( " resize" , updateWindowDimensions ) ;
68+ window . removeEventListener ( ' resize' , updateWindowDimensions ) ;
5269 } ;
5370 } , [ ] ) ;
5471
@@ -58,23 +75,23 @@ const CodeEditor = ({ theme }) => {
5875 } ;
5976
6077 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 ) {
6380 // console.log(data);
6481 setLanguage ( data ) ;
6582 } ) ;
66- socket . on ( " setInput" , ( data ) => {
83+ socket . on ( ' setInput' , data => {
6784 setInput ( data ) ;
6885 } ) ;
69- socket . on ( " setOutput" , ( data ) => {
86+ socket . on ( ' setOutput' , data => {
7087 dispatch ( data ) ;
7188 } ) ;
72- socket . on ( " setCodeExec" , ( data ) => {
89+ socket . on ( ' setCodeExec' , data => {
7390 setCode ( data ) ;
7491 } ) ;
7592 } , [ ] ) ;
7693
77- const handleEditorDidMount = ( _valueGetter ) => {
94+ const handleEditorDidMount = _valueGetter => {
7895 setIsEditorReady ( true ) ;
7996 valueGetter . current = _valueGetter ;
8097 } ;
@@ -84,9 +101,11 @@ const CodeEditor = ({ theme }) => {
84101 // socket.emit("getOutput", output);
85102 // setOutput(out);
86103 } ;
104+
87105 const onChangeCode = ( newValue , e ) => {
88106 // console.log("onChange" + e);
89- socket . emit ( "getCodeExec" , e ) ;
107+ socket . emit ( 'getCodeExec' , e ) ;
108+ setCodeLocalStorage ( e ) ;
90109 setCode ( e ) ;
91110 } ;
92111
@@ -95,23 +114,26 @@ const CodeEditor = ({ theme }) => {
95114 const res = await executeCode ( code , language , input ) ;
96115 dispatch ( res ) ;
97116 // getOutput();
98- socket . emit ( " getOutput" , res ) ;
117+ socket . emit ( ' getOutput' , res ) ;
99118 } ;
100119
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 ) ;
103125 setLanguage ( e . target . value ) ;
104126 } ;
105127 return (
106128 < >
107129 < div className = { styles . row } >
108130 < Split
109- direction = { windowWidth > 800 ? " horizontal" : " vertical" }
131+ direction = { windowWidth > 800 ? ' horizontal' : ' vertical' }
110132 sizes = { [ 60 , 40 ] }
111133 minSize = { windowWidth > 800 ? 0 : 500 }
112134 snapOffset = { windowWidth > 800 ? 200 : 0 }
113135 gutterSize = { 20 }
114- gutterAlign = " center"
136+ gutterAlign = ' center'
115137 className = { styles . splitHor }
116138 >
117139 < div className = { styles . left } >
@@ -122,8 +144,8 @@ const CodeEditor = ({ theme }) => {
122144 onClick = { SubmitCode }
123145 disabled = { ! isEditorReady }
124146 >
125- { loading ? " Loading.." : " Run Code" }
126- < Play style = { { paddingLeft : 10 , fontSize : " 1em" } } />
147+ { loading ? ' Loading..' : ' Run Code' }
148+ < Play style = { { paddingLeft : 10 , fontSize : ' 1em' } } />
127149 </ div >
128150 </ div >
129151 { /* <Editor
@@ -134,9 +156,9 @@ const CodeEditor = ({ theme }) => {
134156 /> */ }
135157 < ControlledEditor
136158 // wrapperClassName="editor"
137- className = " editor"
159+ className = ' editor'
138160 language = { language }
139- theme = { theme === " dark" ? " vs-dark" : " light" }
161+ theme = { theme === ' dark' ? ' vs-dark' : ' light' }
140162 editorDidMount = { handleEditorDidMount }
141163 value = { code }
142164 onChange = { onChangeCode }
@@ -145,20 +167,20 @@ const CodeEditor = ({ theme }) => {
145167 < div className = { styles . right } >
146168 < div className = { styles . column } >
147169 < Split
148- direction = " vertical"
170+ direction = ' vertical'
149171 sizes = { windowWidth > 800 ? [ 75 , 25 ] : [ 100 , 0 ] }
150172 minSize = { 0 }
151173 snapOffset = { windowWidth > 800 ? 100 : 0 }
152174 gutterSize = { 20 }
153- gutterAlign = " center"
175+ gutterAlign = ' center'
154176 className = { styles . splitVer }
155177 >
156178 < div className = { styles . output } >
157179 < div className = { styles . outputHead } > Output</ div >
158- < OutputWindow error = { error === "" ? false : true } >
180+ < OutputWindow error = { error === '' ? false : true } >
159181 { output ? console . log ( output ) : null }
160- < pre style = { { width : " 100%" } } >
161- { output === "" ? error : output }
182+ < pre style = { { width : ' 100%' } } >
183+ { output === '' ? error : output }
162184 </ pre >
163185 </ OutputWindow >
164186 </ div >
0 commit comments