-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
109 lines (101 loc) · 2.77 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Twitter Eval</title>
<style>
html {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
#container {
width: 100%;
height: 100%;
display: flex;
}
#editor-container {
width: 50%;
height: 100%;
padding: 10px;
}
#iframe-container {
width: 50%;
height: 100%;
padding: 10px;
}
#output {
width: 100%;
height: 100%;
border: none;
}
</style>
</head>
<body>
<div id="container">
<div id="editor-container"></div>
<div id="iframe-container">
<iframe id="output"></iframe>
</div>
</div>
<!-- OR ANY OTHER AMD LOADER HERE INSTEAD OF loader.js -->
<script src="./node_modules/monaco-editor/min/vs/loader.js"></script>
<script>
require.config({ paths: { vs: "./node_modules/monaco-editor/min/vs" } });
require(["vs/editor/editor.main"], function () {
const editor = monaco.editor.create(
document.getElementById("editor-container"),
{
value:
localStorage.getItem("code") ||
[
// TODO - include example tweet here
"function x() {",
'\tconsole.log("Hello world!");',
"}",
"x();",
].join("\n"),
language: "javascript",
automaticLayout: true,
minimap: { enabled: false },
}
);
editor.getModel().updateOptions({ tabSize: 2 });
function debounce(func, wait = 300, immediate) {
var timeout;
return function () {
var context = this,
args = arguments;
var later = function () {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
function eval() {
const iframe = document.getElementById("output");
let code = editor.getValue();
// prevent empty code from not matching the URL which requires something
if (code === "") {
code = "// no code to evaluate";
}
iframe.src = `/eval/${encodeURIComponent(code)}`;
localStorage.setItem("code", editor.getValue());
}
eval();
editor.onDidChangeModelContent(debounce(eval));
});
</script>
</body>
</html>