-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript_ide.html
147 lines (130 loc) · 3.74 KB
/
javascript_ide.html
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
141
142
143
144
145
146
147
<!DOCTYPE html>
<html>
<head>
<title>JavaScript IDE - эмуляция вывода в console.log()</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css">
<style>
body {
font-family: Arial, sans-serif;
color: rgb(148 163 184);
background-color: rgb(15 23 42);
}
.btn:focus,.btn:active {
outline: none !important;
box-shadow: none;
}
#editor {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.ace_editor {
border-radius: 5px;
border: 5px;
}
.window {
display: inline-block;
vertical-align: top;
width: 100%;
height: 300px;
padding: 10px;
box-sizing: border-box;
position: relative;
border-radius: 5px;
}
#consoleOutput {
white-space: pre-wrap;
}
#consoleOutputContainer {
background-color: #282a36;
color: rgb(226 232 240);
font-family: 'Courier New', Courier, monospace;
}
@media (max-width: 768px) {
.window {
display: block;
width: 100%;
}
#editorContainer {
margin-bottom: 20px;
}
}
@media (max-width: 576px) {
.my-2 {
flex-direction: column;
}
#runButton, #clearButton {
margin: 10px 0;
}
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ace.js"></script>
</head>
<body>
<h1 class="text-center mt-5 fw-bold">JavaScript IDE</h1>
<h6 class="text-center mt-2">эмуляция вывода в console.log</h6>
<div class="container">
<div class="row">
<div class="col-md-6">
<div class="text-center">
<h6 class="text-uppercase fw-bold">Код</h6>
</div>
<div id="editorContainer" class="window mx-2">
<div id="editor"></div>
</div>
<div class="my-4">
<button id="runButton" class="btn btn-primary mx-2">Выполнить</button>
<button id="clearButton" class="btn btn-danger mx-2">Очистить вывод</button>
</div>
</div>
<div class="col-md-6">
<div class="text-center">
<h6 class="text-uppercase fw-bold">Результат</h6>
</div>
<div id="consoleOutputContainer" class="window mx-2">
<div id="consoleOutput"></div>
</div>
</div>
</div>
</div>
<script>
const editor = ace.edit("editor");
editor.setTheme("ace/theme/dracula");
editor.session.setMode("ace/mode/javascript");
editor.setShowPrintMargin(false);
const runButton = document.getElementById("runButton");
const clearButton = document.getElementById("clearButton");
const consoleOutput = document.getElementById("consoleOutput");
window.addEventListener("load", () => {
let savedCode = localStorage.getItem("userCode");
if (savedCode) {
editor.setValue(savedCode);
}
});
editor.on("change", () => {
let userCode = editor.getValue();
localStorage.setItem("userCode", userCode);
});
runButton.addEventListener("click", () => {
consoleOutput.innerHTML = "";
try {
let logOutput = "";
console.log = function(msg) {
logOutput += msg + "\n";
};
eval(editor.getValue());
consoleOutput.innerHTML = logOutput;
} catch (error) {
consoleOutput.innerHTML = error.message;
}
});
function clearOutput() {
consoleOutput.innerHTML = "";
}
clearButton.addEventListener("click", clearOutput);
</script>
</body>
</html>