-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.js
40 lines (36 loc) · 986 Bytes
/
form.js
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
export function registerParsers(parsers) {
return function parseNow(raw) {
for (const [parse, outbox] of parsers) {
Promise.resolve(raw)
.then(parse)
.then(
data => JSON.stringify(data, null, 2),
err => err.stack || err)
.then(
text => outbox.value = text);
}
}
}
export function bindFormState({ form, inputBox, parseNow, location }) {
form.addEventListener('submit', () => {
location.hash = `#${encodeURI(inputBox.value)}`;
});
inputBox.focus();
inputBox.addEventListener('paste', () => {
setTimeout(() => {
location.hash = `#${encodeURI(inputBox.value)}`;
}, 1);
});
form.querySelector('button').disabled = false;
inputBox.placeholder = "input text";
function readHash() {
const {hash} = location;
if (hash?.length > 1) {
const input = decodeURI(hash.slice(1));
inputBox.value = input;
parseNow(input);
}
}
readHash();
return readHash;
}