Skip to content

Commit 7d9bc09

Browse files
gregorybchristekknolagi
authored andcommitted
Add submit button for repl on mobile (#65)
1 parent 17c1f38 commit 7d9bc09

2 files changed

Lines changed: 53 additions & 25 deletions

File tree

repl.html

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
Output:
3838
</div>
3939
<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" />
4141
</div>
4242
<script type="module">
4343
"use strict";
@@ -72,7 +72,8 @@
7272
let input = document.getElementById("input");
7373
const output = document.getElementById("output");
7474
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");
7677

7778
function renderHistory() {
7879
for (const [inp, out] of history) {
@@ -120,6 +121,23 @@
120121
input.addEventListener("keyup", e => inputHandler(e));
121122
}
122123

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+
123141
loadFromLocalStorage();
124142
input.focus();
125143
async function inputHandler(e) {
@@ -131,35 +149,26 @@
131149
expandInput();
132150
return;
133151
}
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-
}
152152
if (input.tagName === "TEXTAREA") {
153-
shrinkInput();
153+
// Enter in a textarea should not submit input.
154+
if (e.shiftKey) {
155+
shrinkInput();
156+
}
157+
return;
154158
}
159+
submitInput()
155160
}
156161
}
157162
input.addEventListener("keyup", e => inputHandler(e));
158-
button.addEventListener("click", () => {
163+
clearButton.addEventListener("click", () => {
159164
window.localStorage.clear();
160165
loadFromLocalStorage();
161166
input.focus();
162167
});
168+
submitButton.addEventListener("click", () => {
169+
submitInput()
170+
input.focus();
171+
});
163172
</script>
164173
</main>
165174
<script data-goatcounter="https://scrapscript.goatcounter.com/count"

style.css

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,34 @@ input[type="text"], textarea {
193193
background-color: #2d2d2d;
194194
color: #d4d4d4;
195195
border: none;
196-
padding: 0.5em 0.8em;
197-
border-radius: 5px;
196+
padding: 0.5em 1em;
197+
border-radius: 5px 0px 0px 5px;
198198
transition: background-color 200ms;
199+
width: 20em;
200+
}
201+
202+
#submit-input {
203+
font-family: "Nunito Sans", sans-serif;
204+
background-color: #3a3a3a;
205+
color: #c0c0c0;
206+
border: none;
207+
padding: 0.5em 1.2em;
208+
font-weight: bold;
209+
border-radius: 0px 5px 5px 0px;
210+
transition: background-color 200ms;
211+
}
212+
#submit-input:hover {
213+
cursor: pointer;
214+
background-color: #434343;
215+
}
216+
#submit-input:active {
217+
background-color: #3a3a3a;
199218
}
200219

201220
#clear-local-storage {
202221
font-family: "Nunito Sans", sans-serif;
203222
background-color: #2d2d2d;
204-
color: #d4d4d4;
223+
color: #c0c0c0;
205224
border: none;
206225
padding: 0.5em 1.2em;
207226
font-weight: bold;

0 commit comments

Comments
 (0)