-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayground.html
248 lines (235 loc) · 9.04 KB
/
playground.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<!doctype html>
<title>Playground</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {
box-sizing: border-box;
font-size: 14px;
}
html, body {
height: 100%;
margin: 0;
}
#code {
height: 100%;
width: 100%;
border: none;
background-color: transparent;
resize: none;
font-family: Lucida Console, Monaco, Monospace;
}
.CodeMirror {
height: 100% !important;
background-color: #ffd !important;
font-family: Lucida Console, Monaco, Monospace !important;
}
#input {
position:absolute;
left:0;
top:32px;
width:100%;
bottom:33%;
}
#output {
position: absolute;
width: 100%;
top: 67%;
bottom: 0;
overflow-y: scroll;
}
#output div {
white-space: pre-wrap;
word-break: break-all;
padding: 0.2em 0.5em;
}
#output div.title {
font-size: 90%;
background: #eee;
}
#output div.content {
font-family: Lucida Console, Monaco, Monospace;
padding: 0.5em;
}
</style>
<body>
<div style="position:relative;height:100%">
<div style="line-height: 32px;height:32px;padding:0 0.5em">
<button onclick="run()">Run (F9)</button>
<select id='output_fields'>
<option value="all">Return all</option>
<option value="stdout">Stdout only</option>
<option value="result">Results only</option>
</select>
<span style="float:right">
<select id='cm_cdn' onchange="localStorage.setItem('cdn',this.value);location.reload()">
<option value="-">Plain editor</option>
<option value="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.0">CodeMirror (cloudflare)</option>
<option value="https://cdn.bootcdn.net/ajax/libs/codemirror/5.65.0">CodeMirror (bootcdn)</option>
</select>
</span>
</div>
<div id="input">
<div style="border:solid 1px #aaa;border-width:1px 0 1px 0;height:100%;background: #ffd;">
<textarea id="code">__CODE__</textarea>
</div>
</div>
<div id="output"></div>
</div>
</body>
<script>
var output = document.getElementById("output"), editor = document.getElementById('code');
async function loadEditor(textarea, prefix) {
prefix = prefix || localStorage.getItem('cdn') || "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.0"
document.getElementById('cm_cdn').value = prefix;
if (prefix == "-") {
loadPlainEditor(editor);
return;
}
if (window.EDITOR) return;
var createScript = function(src, f) {
return new Promise(function(resolve) {
var s = document.createElement("script")
s.onload = resolve; s.src = src;
document.body.appendChild(s);
})
}, createCSS = function(src, f) {
return new Promise(function(resolve) {
var s = document.createElement("link")
s.onload = resolve; s.href = src; s.rel = 'stylesheet'; s.type = 'text/css'; s.media = 'all';
document.body.appendChild(s);
})
};
await createScript(prefix + "/codemirror.min.js");
await createScript(prefix + "/mode/lua/lua.min.js");
await createScript(prefix + "/addon/hint/show-hint.min.js");
await createCSS(prefix + "/codemirror.min.css");
await createCSS(prefix + "/addon/hint/show-hint.min.css");
const controlKeys = [
8, 9, 13, 16, 17, 18, 19, 20, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 91, 92, 93, 107, 109, 110, 111, 112,
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 144, 145, 186, 187, 188, 189, 190, 191, 192, 220, 222,
];
const keywords = [
"and", "or", "local", "break", "continue", "else", "function", "lambda", "if", "elseif",
"then", "end", "not", "return", "for", "while", "repeat", "until", "do", "in", "goto", "is",
]
const names = [__NAMES__];
CodeMirror.hint.lua = function(cm) {
const cursor = cm.getCursor();
const token = cm.getTokenAt(cursor);
const word = (token.string.match(/\w+$/) || [token.string])[0];
const candidates = names.concat(word == token.string ? keywords : []);
const res = {from: cursor, to: cursor, list: []};
for (const i in candidates) {
const raw = candidates[i];
const data = raw.replace(/\(.+\)\./, '');
if (data.startsWith(word))
res.list.push({text: data, displayText: raw})
}
if (token.string.length <= 1 || !word || keywords.includes(token.string)) {
res.list = [];
} else {
CodeMirror.on(res, "pick", function(obj) {
const doc = window.EDITOR.getDoc(), w = obj.text;
window.EDITOR.execCommand("delWordBefore");
doc.replaceRange(keywords.includes(w) ? (w + ' ') : w, doc.getCursor());
});
}
return res;
};
window.EDITOR = CodeMirror.fromTextArea(textarea, {
mode: 'lua',
lineNumbers: true,
smartIndent: true,
indentUnit: 4,
extraKeys: { "F9": run },
hintOptions: { completeSingle: false },
});
window.EDITOR.on("keyup", function(editor, event) {
var keyCode = parseInt(event.keyCode || event.which);
var cursor = editor.getDoc().getCursor();
var token = editor.getTokenAt(cursor);
var word = editor.findWordAt(cursor);
var currentWord = editor.getRange(word.anchor, word.head);
if (!editor.state.completionActive && !controlKeys.includes(keyCode) && token.type != 'number' && token.type != 'string') {
editor.showHint();
return;
}
});
}
loadEditor(code).then(function() {});
function loadPlainEditor(editor) {
editor.addEventListener('keydown', function(e) {
var start = this.selectionStart, end = this.selectionEnd;
if (e.key == 'Tab') {
e.preventDefault();
this.value = this.value.substring(0, start) + "\t" + this.value.substring(end);
this.selectionStart = this.selectionEnd = start + 1;
}
if (e.key == 'F9') {
e.preventDefault();
run();
}
if (e.key == 'Enter') {
var v = this.value.substring(0, start);
var idx = v.lastIndexOf('\n');
if (idx) {
v = v.substring(idx + 1)
var spaces = '';
for (var i = 0; i < v.length; i++) {
var c = v.charAt(i);
if (c == ' ' || c == '\t')
spaces += c;
else
break;
}
if (spaces) {
e.preventDefault()
this.value = this.value.substring(0, start) + "\n" + spaces + this.value.substring(end);
this.selectionStart = this.selectionEnd = start + 1 + spaces.length;
}
}
}
});
}
function div(html, title) {
var el = document.createElement("div");
el.className = title ? 'title' : 'content';
el.innerText = html;
return el;
}
function run() {
const outf = document.getElementById('output_fields').value;
const code = encodeURIComponent(window.EDITOR ? window.EDITOR.getValue() : editor.value);
localStorage.setItem('output', outf);
fetch('?output=' + outf + '&code=' + code)
.then(response => response.json())
.then(function(data) {
var el = output;
el.innerHTML = '';
if (data.error) {
el.appendChild(div("Error", true))
el.appendChild(div(data.error))
} else if (data.result) {
el.appendChild(div("Results", true))
v = data.result;
el.appendChild(div(typeof v === 'object' ? JSON.stringify(v) : v));
}
if (data.stdout) {
el.appendChild(div("Stdout", true));
el.appendChild(div(data.stdout));
el.appendChild(div("Elapsed", true))
el.appendChild(div(data.elapsed + 's'));
}
if (data.opcode) {
el.appendChild(div("Compiled", true));
el.appendChild(div(data.opcode));
}
if (data.survey) {
el.appendChild(div("Survey", true));
el.appendChild(div(JSON.stringify(data.survey)));
}
});
}
document.getElementById('output_fields').value = localStorage.getItem('output') || 'all';
</script>