|
| 1 | +import React, { useRef, useState, useEffect } from "react"; |
| 2 | +import Editor from "@monaco-editor/react"; |
| 3 | +import { executeCode, setLoadingTrue } from "../../actions/code"; |
| 4 | +import { useDispatch, useSelector } from "react-redux"; |
| 5 | +import InputOutput from "../output/InputOutput"; |
| 6 | +import Submit from "./Submit"; |
| 7 | +import Split from "react-split"; |
| 8 | +import styled from "styled-components"; |
| 9 | +import styles from "./style.module.css"; |
| 10 | +import "./style.css"; |
| 11 | +import { Play } from "react-feather"; |
| 12 | + |
| 13 | +const Row = styled.div` |
| 14 | + display: flex; |
| 15 | + flex-direction: row; |
| 16 | + flex-grow: 1; |
| 17 | + // background: blue; |
| 18 | +`; |
| 19 | + |
| 20 | +const OutputWindow = styled.div` |
| 21 | + // height: 100%; |
| 22 | + // margin: 20px; |
| 23 | + // border: 2px solid #eeeeee; |
| 24 | + border-radius: 5px; |
| 25 | + padding: 0 20px; |
| 26 | + box-sizing: border-box; |
| 27 | + color: ${(props) => (props.error ? "red" : "black")}; |
| 28 | +`; |
| 29 | + |
| 30 | +const CodeEditor = ({ theme }) => { |
| 31 | + const loading = useSelector((state) => state.code.isFetching); |
| 32 | + const [isEditorReady, setIsEditorReady] = useState(false); |
| 33 | + const [language, setLanguage] = useState("c"); |
| 34 | + const [input, setInput] = useState(""); |
| 35 | + const dispatch = useDispatch(); |
| 36 | + const valueGetter = useRef(); |
| 37 | + let output = useSelector((state) => state.code.output); |
| 38 | + let error = useSelector((state) => state.code.error); |
| 39 | + |
| 40 | + const handleEditorDidMount = (_valueGetter) => { |
| 41 | + setIsEditorReady(true); |
| 42 | + valueGetter.current = _valueGetter; |
| 43 | + }; |
| 44 | + |
| 45 | + const SubmitCode = async () => { |
| 46 | + dispatch(setLoadingTrue()); |
| 47 | + const res = await executeCode(valueGetter.current(), language, input); |
| 48 | + console.log(res); |
| 49 | + dispatch(res); |
| 50 | + }; |
| 51 | + |
| 52 | + const changeLanguage = (e) => { |
| 53 | + setLanguage(e.target.value); |
| 54 | + }; |
| 55 | + |
| 56 | + return ( |
| 57 | + <> |
| 58 | + <Row> |
| 59 | + <Split |
| 60 | + sizes={[50, 50]} |
| 61 | + minSize={100} |
| 62 | + expandToMin={false} |
| 63 | + gutterSize={10} |
| 64 | + gutterAlign="center" |
| 65 | + snapOffset={30} |
| 66 | + dragInterval={1} |
| 67 | + direction="horizontal" |
| 68 | + cursor="ew-resize" |
| 69 | + class={styles.splitHor} |
| 70 | + > |
| 71 | + <div className={styles.left}> |
| 72 | + <div className={styles.runCode}> |
| 73 | + <Submit language={language} changeLanguage={changeLanguage} /> |
| 74 | + <div |
| 75 | + className={styles.submitButton} |
| 76 | + onClick={SubmitCode} |
| 77 | + disabled={!isEditorReady} |
| 78 | + > |
| 79 | + {loading ? "Loading.." : "Run Code"} |
| 80 | + <Play style={{ paddingLeft: 10, fontSize: "1em" }} /> |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + <Editor |
| 84 | + className="editor" |
| 85 | + language={language} |
| 86 | + theme={theme === "dark" ? "vs-dark" : "light"} |
| 87 | + editorDidMount={handleEditorDidMount} |
| 88 | + /> |
| 89 | + </div> |
| 90 | + <div className={styles.right}> |
| 91 | + <Split |
| 92 | + sizes={[75, 25]} |
| 93 | + minSize={[25, 25]} |
| 94 | + expandToMin={false} |
| 95 | + gutterSize={10} |
| 96 | + gutterAlign="center" |
| 97 | + snapOffset={30} |
| 98 | + dragInterval={1} |
| 99 | + direction="vertical" |
| 100 | + cursor="ew-resize" |
| 101 | + class={styles.splitVer} |
| 102 | + > |
| 103 | + <div className={styles.output}> |
| 104 | + <div className={styles.outputHead}>Output</div> |
| 105 | + <OutputWindow error={error === "" ? false : true}> |
| 106 | + {output ? console.log(output) : null} |
| 107 | + <pre style={{ width: "100%" }}> |
| 108 | + {output === "" ? error : output} |
| 109 | + </pre> |
| 110 | + </OutputWindow> |
| 111 | + </div> |
| 112 | + <div className={styles.input}> |
| 113 | + <InputOutput input={input} setInput={setInput} theme={theme} /> |
| 114 | + </div> |
| 115 | + </Split> |
| 116 | + </div> |
| 117 | + </Split> |
| 118 | + </Row> |
| 119 | + </> |
| 120 | + ); |
| 121 | +}; |
| 122 | + |
| 123 | +export default CodeEditor; |
0 commit comments