@@ -10,32 +10,34 @@ const initialExample = examples.find(e => e.highlighted)?.title
10
10
11
11
<script >
12
12
import {ppeg} from "ppegjs"
13
+ const DESKTOP_SIZE = 768
13
14
14
- const examplesSelect = document.querySelector<HTMLSelectElement>('#examples')
15
- if (!examplesSelect) throw new Error("No select element found")
16
- const grammar = document.querySelector<HTMLDivElement>('#grammar')
17
- if (!grammar) throw new Error("No grammar div found")
18
- const input = document.querySelector<HTMLDivElement>('#input')
19
- if (!input) throw new Error("No input div found")
20
- const output = document.querySelector<HTMLDivElement>('#output')
21
- if (!output) throw new Error("No output div found")
22
- const jsonCheckbox = document.querySelector<HTMLInputElement>('#json')
23
- if (!jsonCheckbox) throw new Error("No json checkbox found")
15
+ const getElement = <T extends Element>(selector: string): T => {
16
+ const element = document.querySelector<T>(selector);
17
+ if (!element) throw new Error("No element found: " + selector);
18
+ return element;
19
+ }
20
+
21
+ const examplesSelect = getElement<HTMLSelectElement>('#examples')
22
+ const grammar = getElement<HTMLTextAreaElement>('#grammar')
23
+ const input = getElement<HTMLTextAreaElement>('#input')
24
+ const output = getElement<HTMLTextAreaElement>('#output')
25
+ const jsonCheckbox = getElement<HTMLInputElement>('#json')
24
26
25
27
const load = () => {
26
28
const selected = examplesSelect.value
27
- const option = document.querySelector <HTMLOptionElement>(`#option-${selected}`)!
28
- grammar.innerText = option.dataset.grammar ?? "";
29
- input.innerText = option.dataset.input ?? "";
29
+ const option = getElement <HTMLOptionElement>(`#option-${selected}`)
30
+ grammar.value = option.dataset.grammar ?? "";
31
+ input.value = option.dataset.input ?? "";
30
32
run()
31
33
}
32
34
33
35
const run = () => {
34
- const parser = ppeg.compile(grammar.innerText )
35
- const parse = parser.parse(input.innerText )
36
+ const parser = ppeg.compile(grammar.value )
37
+ const parse = parser.parse(input.value )
36
38
const showJson = jsonCheckbox.checked
37
- if (parse.ok) output.textContent = parse.show_ptree(showJson);
38
- else output.textContent = parse.show_err();
39
+ if (parse.ok) output.value = parse.show_ptree(showJson);
40
+ else output.value = parse.show_err();
39
41
}
40
42
41
43
const debounce = function (fn: Function, delay: number = 300) {
@@ -51,7 +53,25 @@ const initialExample = examples.find(e => e.highlighted)?.title
51
53
52
54
const autoparse = debounce(run)
53
55
54
- examplesSelect.addEventListener('change', load)
56
+ examplesSelect.addEventListener('change', load);
57
+ // A little hack to ensure the textarea heights grow with content
58
+ [grammar, input, output].forEach(el => el.addEventListener('input', (e) => {
59
+ if (document.body.clientWidth <= DESKTOP_SIZE) {
60
+ el.style.height = "0"
61
+ el.style.height = el.scrollHeight + "px"
62
+ }
63
+ }))
64
+ // The same code needs to run in case we resize from big to small
65
+ window.addEventListener("resize", () => {
66
+ [grammar, input, output].forEach(el => {
67
+ if (document.body.clientWidth <= DESKTOP_SIZE) {
68
+ el.style.height = "0"
69
+ el.style.height = el.scrollHeight + "px"
70
+ } else {
71
+ el.style.height = "auto"
72
+ }
73
+ })
74
+ })
55
75
grammar.addEventListener('input', autoparse)
56
76
input.addEventListener('input', autoparse)
57
77
jsonCheckbox.addEventListener('change', run)
@@ -162,13 +182,13 @@ const initialExample = examples.find(e => e.highlighted)?.title
162
182
</select >
163
183
</span >
164
184
</div >
165
- <div contenteditable = " true " role = " textbox " id =' grammar' ></div >
185
+ <textarea id =' grammar' ></textarea >
166
186
</div >
167
187
<div class =' input' >
168
188
<div class =' header' >
169
189
<label for =" input" >Input</label >
170
190
</div >
171
- <div contenteditable = " true " role = " textbox " id =' input' "></div >
191
+ <textarea id =' input' "></textarea >
172
192
</div >
173
193
<div class =' output' >
174
194
<div class =' header' >Parse Tree
@@ -177,7 +197,7 @@ const initialExample = examples.find(e => e.highlighted)?.title
177
197
<input type =" checkbox" id =" json" name =" json" >
178
198
</label >
179
199
</div >
180
- <div id =' output' ></div >
200
+ <textarea id =' output' disabled ></textarea >
181
201
</div >
182
202
<div class =" footer" >Copyright 2024</div >
183
203
</Layout >
0 commit comments