|
37 | 37 | Output: |
38 | 38 | </div> |
39 | 39 | <div> |
40 | | -<code><span id="prompt-string">>>></span></code><input id="input" type="text" /> |
| 40 | +<input id="input" type="text" /><input id="submit-input" type="submit" value="Submit" /> |
41 | 41 | </div> |
42 | 42 | <script type="module"> |
43 | 43 | "use strict"; |
|
72 | 72 | let input = document.getElementById("input"); |
73 | 73 | const output = document.getElementById("output"); |
74 | 74 | const history = []; |
75 | | -const button = document.getElementById("clear-local-storage"); |
| 75 | +const clearButton = document.getElementById("clear-local-storage"); |
| 76 | +const submitButton = document.getElementById("submit-input"); |
76 | 77 |
|
77 | 78 | function renderHistory() { |
78 | 79 | for (const [inp, out] of history) { |
|
120 | 121 | input.addEventListener("keyup", e => inputHandler(e)); |
121 | 122 | } |
122 | 123 |
|
| 124 | +async function submitInput() { |
| 125 | + const response = await sendRequest(document.env, input.value); |
| 126 | + if (response.ok) { |
| 127 | + const {env, result} = await response.json(); |
| 128 | + updateHistory(input.value, result); |
| 129 | + history.push([input.value, result]); |
| 130 | + input.value = ""; |
| 131 | + document.env = env; |
| 132 | + window.localStorage.setItem('env', env) |
| 133 | + window.localStorage.setItem('history', JSON.stringify(history)); |
| 134 | + } else { |
| 135 | + const msg = await response.text(); |
| 136 | + updateHistory(input.value, msg); |
| 137 | + input.value = ""; |
| 138 | + } |
| 139 | +} |
| 140 | + |
123 | 141 | loadFromLocalStorage(); |
124 | 142 | input.focus(); |
125 | 143 | async function inputHandler(e) { |
|
131 | 149 | expandInput(); |
132 | 150 | return; |
133 | 151 | } |
134 | | - if (!e.shiftKey && input.tagName === "TEXTAREA") { |
135 | | - // Normal Enter in a textarea should not submit input. |
136 | | - return; |
137 | | - } |
138 | | - const response = await sendRequest(document.env, input.value); |
139 | | - if (response.ok) { |
140 | | - const {env, result} = await response.json(); |
141 | | - updateHistory(input.value, result); |
142 | | - history.push([input.value, result]); |
143 | | - input.value = ""; |
144 | | - document.env = env; |
145 | | - window.localStorage.setItem('env', env) |
146 | | - window.localStorage.setItem('history', JSON.stringify(history)); |
147 | | - } else { |
148 | | - const msg = await response.text(); |
149 | | - updateHistory(input.value, msg); |
150 | | - input.value = ""; |
151 | | - } |
152 | 152 | if (input.tagName === "TEXTAREA") { |
153 | | - shrinkInput(); |
| 153 | + // Enter in a textarea should not submit input. |
| 154 | + if (e.shiftKey) { |
| 155 | + shrinkInput(); |
| 156 | + } |
| 157 | + return; |
154 | 158 | } |
| 159 | + submitInput() |
155 | 160 | } |
156 | 161 | } |
157 | 162 | input.addEventListener("keyup", e => inputHandler(e)); |
158 | | -button.addEventListener("click", () => { |
| 163 | +clearButton.addEventListener("click", () => { |
159 | 164 | window.localStorage.clear(); |
160 | 165 | loadFromLocalStorage(); |
161 | 166 | input.focus(); |
162 | 167 | }); |
| 168 | +submitButton.addEventListener("click", () => { |
| 169 | + submitInput() |
| 170 | + input.focus(); |
| 171 | +}); |
163 | 172 | </script> |
164 | 173 | </main> |
165 | 174 | <script data-goatcounter="https://scrapscript.goatcounter.com/count" |
|
0 commit comments