-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrepl.html
177 lines (166 loc) · 5.7 KB
/
repl.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Scrapscript Web REPL</title>
<link rel="shortcut icon" type="image/x-icon" href="data:image/vnd.microsoft.icon;base64,AAABAAEAEBAQAAAAAAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAAAAAAAD/AAAA////AAAA/wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAREBEQAAAAAQAQEAEAAAABABAQAQAAAAAREBEQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACIiIiIiIiIiIiIiIiIiIiIiERIiESEiIiIiESEiISIiIhEiISIhEiIiIREiESEhIiIiIiIiIiIiIiIiIiIiIiIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA">
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Fira+Code&display=swap"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css2?family=Rubik:ital,wght@0,500;0,700&display=swap"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css2?family=Nunito+Sans:ital,wght@0,400;0,700;0,900;1,400&display=swap"
rel="stylesheet"
/>
<link rel="stylesheet" href="/style.css" />
</head>
<body>
<main>
<div>
<p>See <a href="https://scrapscript.org/">scrapscript.org</a> for a slightly
out of date language reference.</p>
<p>This REPL sends programs to a server to be evaluated, but the server is
completely stateless. All persistence is in the browser.</p>
</div>
<div>
<!-- TODO(max): Add button to save to/load from disk. -->
<button id="clear-local-storage">Clear</button>
</div>
<div id="output" style="height: 400px; overflow: auto;">
Output:
</div>
<div>
<input id="input" type="text" /><input id="submit-input" type="submit" value="Submit" />
</div>
<script type="module">
"use strict";
function updateHistory(inp, out) {
const wrap = document.createElement("div");
const pre_inp = document.createElement("pre");
pre_inp.setAttribute("class", "language-text");
const code_inp = document.createElement("code");
code_inp.setAttribute("class", "language-text");
code_inp.append(`>>> ${inp}`);
pre_inp.append(code_inp);
const pre_out = document.createElement("pre");
pre_out.setAttribute("class", "result language-text");
const code_out = document.createElement("code");
code_out.setAttribute("class", "language-text");
code_out.append(`${out}`);
pre_out.append(code_out);
wrap.append(pre_inp);
wrap.append(pre_out);
output.append(wrap);
output.scrollTop = output.scrollHeight;
}
async function sendRequest(env, exp) {
const params = env === null ? {exp} : {exp, env};
return fetch("/eval?" + new URLSearchParams(params));
}
let input = document.getElementById("input");
const output = document.getElementById("output");
const history = [];
const clearButton = document.getElementById("clear-local-storage");
const submitButton = document.getElementById("submit-input");
function renderHistory() {
for (const [inp, out] of history) {
updateHistory(inp, out);
}
}
function loadFromLocalStorage() {
history.length = 0;
output.innerHTML = "";
const hist = window.localStorage.getItem('history');
if (hist !== null) {
history.push(...JSON.parse(hist));
}
renderHistory();
document.env = window.localStorage.getItem('env');
}
function expandInput() {
if (input.tagName === "TEXTAREA") {
return;
}
const textarea = document.createElement("textarea");
textarea.setAttribute("id", "input");
textarea.setAttribute("rows", "1");
textarea.setAttribute("style", "height: 1.5em;");
textarea.value = input.value;
input.replaceWith(textarea);
input = textarea;
input.focus();
input.addEventListener("keyup", e => inputHandler(e));
}
function shrinkInput() {
if (input.tagName === "INPUT") {
return;
}
const newInput = document.createElement("input");
newInput.setAttribute("id", "input");
newInput.setAttribute("type", "text");
newInput.value = input.value;
input.replaceWith(newInput);
input = newInput;
input.focus();
input.addEventListener("keyup", e => inputHandler(e));
}
async function submitInput() {
const response = await sendRequest(document.env, input.value);
if (response.ok) {
const {env, result} = await response.json();
updateHistory(input.value, result);
history.push([input.value, result]);
input.value = "";
document.env = env;
window.localStorage.setItem('env', env)
window.localStorage.setItem('history', JSON.stringify(history));
} else {
const msg = await response.text();
updateHistory(input.value, msg);
input.value = "";
}
}
loadFromLocalStorage();
input.focus();
async function inputHandler(e) {
// TODO(max): Make up/down arrow keys navigate history
if (e.key === "Enter") {
if (e.shiftKey && input.tagName === "INPUT") {
// Shift-Enter expands the input to a textarea and does not submit
// input.
expandInput();
return;
}
if (input.tagName === "TEXTAREA") {
// Enter in a textarea should not submit input.
if (e.shiftKey) {
shrinkInput();
}
return;
}
submitInput()
}
}
input.addEventListener("keyup", e => inputHandler(e));
clearButton.addEventListener("click", () => {
window.localStorage.clear();
loadFromLocalStorage();
input.focus();
});
submitButton.addEventListener("click", () => {
submitInput()
input.focus();
});
</script>
</main>
<script data-goatcounter="https://scrapscript.goatcounter.com/count"
async src="//gc.zgo.at/count.js"></script>
</body>
</html>