forked from reactplay/react-play
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeEditor.js
123 lines (115 loc) · 3.4 KB
/
CodeEditor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import PlayHeader from "common/playlists/PlayHeader";
import "./styles.css";
import React, { useState, useEffect } from "react";
import Editor from "./Editor";
import Button from './Button'
import Modal from './Modal';
// WARNING: Do not change the entry componenet name
function CodeEditor(props) {
// Your Code Start below.
const [openedEditor, setOpenedEditor] = useState("html");
const [activeButton, setActiveButton] = useState("html");
const [html, setHtml] = useState("");
const [css, setCss] = useState("");
const [js, setJs] = useState("");
const [srcDoc, setSrcDoc] = useState(``);
const onTabClick = (editorName) => {
setOpenedEditor(editorName);
setActiveButton(editorName);
};
useEffect(() => {
const timeOut = setTimeout(() => {
setSrcDoc(
`
<html>
<body>${html}</body>
<style>${css}</style>
<script>${js}</script>
</html>
`
);
}, 250);
return () => clearTimeout(timeOut);
}, [html, css, js]);
const [showModal, setShowModal] = useState(false);
const toggle = (e) => {
setShowModal(!showModal);
};
return (
<>
<div className="play-details">
<PlayHeader play={props} />
<div className="play-details-body ">
{/* Your Code Starts Here */}
<div className="code-eitor-modal-toogle">
<button onClick={toggle}>How to play ?</button>
</div>
<div className="code-editor-tab-button-container">
<Button
backgroundColor={activeButton === "html" ? "#98AFC7" : ""}
title="HTML"
onClick={() => {
onTabClick("html");
}}
/>
<Button
backgroundColor={activeButton === "css" ? "#98AFC7" : ""}
title="CSS"
onClick={() => {
onTabClick("css");
}}
/>
<Button
backgroundColor={activeButton === "js" ? "#98AFC7" : ""}
title="JavaScript"
onClick={() => {
onTabClick("js");
}}
/>
</div>
<div className="code-editor-container">
{openedEditor === "html" ? (
<Editor
language="xml"
displayName="HTML"
value={html}
setEditorState={setHtml}
/>
) : openedEditor === "css" ? (
<Editor
language="css"
displayName="CSS"
value={css}
setEditorState={setCss}
/>
) : (
<Editor
language="javascript"
displayName="JS"
value={js}
setEditorState={setJs}
/>
)}
</div>
<div className="code-editor-heading-output">
<h1>Output</h1>
</div>
<div className="code-editor-bottom-pane">
<iframe
id="my_iframe"
srcDoc={srcDoc}
title="output"
sandbox="allow-scripts"
frameBorder="1"
width="100%"
height="100%"
/>
</div>
{/* Your Code Ends Here */}
</div>
</div>
<Modal showModal={showModal} toggle={toggle}/>
</>
);
}
export default CodeEditor;