forked from FrontendFreaks/Official-Website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode-editor.tsx
140 lines (125 loc) · 5.03 KB
/
code-editor.tsx
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"use client";
import { useEffect, useState } from "react";
import Editor from "@monaco-editor/react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faHtml5, faCss3, faJs } from "@fortawesome/free-brands-svg-icons";
import { faPlay, faCircleXmark } from "@fortawesome/free-solid-svg-icons";
interface File {
name: string;
language: string;
value: string;
}
const files: Record<string, File> = {
"index.html": {
name: "index.html",
language: "html",
value: `<!-- Your HTML code goes here -->\n<!DOCTYPE html>\n<html>\n<head>\n <title>My Page</title>\n</head>\n<body>\n <h1>Hello, HTML!</h1>\n</body>\n</html>`,
},
"style.css": {
name: "style.css",
language: "css",
value: `/* Your CSS code goes here */\nbody { background-color: #f0f0f0; }`,
},
"script.js": {
name: "script.js",
language: "javascript",
value: `// Your JavaScript code goes here\nconsole.log('Hello, JavaScript!');`,
},
};
export default function CodeEditor() {
const [fileName, setFileName] = useState("index.html");
const [htmlCode, setHtmlCode] = useState("");
const [cssCode, setCssCode] = useState("");
const [jsCode, setJsCode] = useState("");
const [outputVisible, setOutputVisible] = useState(false);
useEffect(() => {
const runBtn = document.getElementById("runCode");
const clsBtn = document.getElementById("closeWindow");
runBtn?.addEventListener("click", () => {
setHtmlCode(files["index.html"].value);
setCssCode(files["style.css"].value);
setJsCode(files["script.js"].value);
setOutputVisible(true);
});
clsBtn?.addEventListener("click", () => {
setOutputVisible(false);
});
}, []);
const file = files[fileName];
const handleEditorChange: import("@monaco-editor/react").OnChange = (value) => {
if (value !== undefined) {
files[fileName].value = value;
}
};
return (
<>
<div className="bg-gray-900 bg-opacity-90 p-4 flex gap-4">
{Object.keys(files).map((fileKey) => (
<button
key={fileKey}
className={`px-2 py-1 rounded ${fileName === fileKey
? "bg-gray-500 cursor-not-allowed text-white"
: "bg-gray-700 hover:bg-gray-600 text-white"
}`}
disabled={fileName === fileKey}
onClick={() => setFileName(fileKey)}
>
<div className="flex items-center">
<div className="mr-2">
{file.language === "javascript" && <FontAwesomeIcon icon={faJs} />}
{file.language === "css" && <FontAwesomeIcon icon={faCss3} />}
{file.language === "html" && <FontAwesomeIcon icon={faHtml5} />}
</div>
<div>{fileKey}</div>
</div>
</button>
))}
<button
className="flex items-center bg-blue-500 hover:bg-blue-600 text-white px-2 py-1 rounded"
id="runCode"
>
<div className="mr-2">
<FontAwesomeIcon icon={faPlay} />
</div>
Run
</button>
</div>
<Editor
height="70vh"
theme="vs-dark"
saveViewState={true}
path={file.name}
defaultLanguage={file.language}
defaultValue={file.value}
onChange={handleEditorChange}
value={file.value}
/>
{outputVisible && (
<div className="fixed min-h-[40vh] min-w-[40vh] inset-0 flex items-center justify-center bg-black bg-opacity-70 z-50">
<div className="bg-white relative p-4 rounded-lg shadow-lg text-center">
<button
className="absolute top-1 right-1 px-1 rounded-2xl bg-gray-400"
id="closeWindow"
onClick={() => setOutputVisible(false)}
>
<div>
<FontAwesomeIcon icon={faCircleXmark} />
</div>
</button>
<iframe
title="output"
srcDoc={`
<html>
<body>${htmlCode}</body>
<style>${cssCode}</style>
<script>${jsCode}</script>
</html>
`}
className="w-full h-96 py-5 mx-auto"
/>
</div>
</div>
)}
</>
);
}