-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
462 lines (391 loc) · 13 KB
/
index.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>
<style type="text/css">
.key {
height: 54px;
background-color: #cccccc;
border-width: 1px;
border-radius: 5px;
border-color: black;
border-style: solid;
}
.key-special { background-color: #7a7a7a; }
.key-special .key-label { background-color: #cccccc; }
.key-dead-key { background-color: #88e2e2; }
.key-dead-key .key-label { background-color: #c2e2e2; }
.key-pressed { background-color: #ffff00; }
.key-selected { border-color: #00aa00; }
.key-label {
top: 3px;
left: 6px;
border-width: 1px;
border-radius: 3px;
border-color: rgba(0, 0, 0, 0.1);
border-style: solid;
height: 40px;
background: #fcfcfc;
display: grid;
grid-template-columns: 1fr 1fr;
padding: 0px 2px;
user-select: none;
font-family: "Segoe UI", "Arial", sans-serif;
font-size: 16px;
}
.key-label-shift {
grid-row: 1;
grid-column: 1;
}
.key-label-base {
grid-row: 1;
grid-column: 1;
align-self: end;
}
.key-label-og {
grid-row: 1;
grid-column: 2;
color: #aaa;
text-align: right;
}
.key-selected .key-label {
border-color: #00aa00;
}
#keyboard-bg div {
position: absolute;
box-sizing: border-box;
background-clip: padding-box;
}
#keyboard-bg {
position: static;
margin: 10px;
box-sizing: content-box;
font-family: "Segoe UI", "Arial", sans-serif;
font-size: 14px;
background-color: #eeeeee;
border-width: 1px;
border-radius: 6px;
border-color: #ddd;
border-style: solid;
padding: 10px;
}
#config-key {
margin: 10px;
padding: 10px;
background-color: #eeeeee;
border-width: 1px;
border-radius: 6px;
border-color: #ddd;
border-style: solid;
display: flex;
flex-direction: column;
}
#config-key .config-field {
margin-bottom: 10px;
font-family: sans-serif;
}
#play-area {
font-family: "Segoe UI", "Arial", sans-serif;
padding: 6px;
margin: 10px;
background-color: #eeeeee;
border-width: 1px;
border-radius: 6px;
border-color: #ddd;
border-style: solid;
white-space: pre;
}
label, p {
font-family: "Segoe UI", "Arial", sans-serif;
}
</style>
<title>Harpsichord</title>
</head>
<body>
<script src="harpsichord.js"></script>
<script src="keyboard.js"></script>
<script type="text/javascript">
let key_dict = {}
let dead_key_context = null
for (const key of KEYS_DATA) {
key_dict[key.scancode] = key
}
function key_desc(name, state) {
const cons = [name, state[0] ? 'SHIFT' : '', state[1] ? 'ALT' : '', state[2] ? 'CAPS' : '']
return `[${cons.filter(Boolean).join(' ').trim()}]`
}
function show_key_name(key_data, state, key_binds) {
if (key_data.special) return key_data.name
const desc = key_desc(key_data.name, state)
if (!(desc in key_binds)) return ''
return key_binds[desc].display ?? key_binds[desc].bind
}
function is_dead(key_data, state, key_binds) {
const desc = key_desc(key_data.name, state)
if (desc in key_binds == false) return false
return Object.keys(key_binds[desc].dead).length > 0
}
function do_key_action(key, state) {
if (key.scancode.startsWith('Shift')) state[0] = true
if (key.scancode.startsWith('Alt')) state[1] = true
return true
}
function undo_key_action(key, state) {
if (key.scancode.startsWith('Shift')) state[0] = false
if (key.scancode.startsWith('Alt')) state[1] = false
return false
}
function insertTextAtCursor(text) {
var sel, range, textNode;
if (window.getSelection) {
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
textNode = document.createTextNode(text);
range.insertNode(textNode);
range.setStart(textNode, textNode.length);
range.setEnd(textNode, textNode.length);
sel.removeAllRanges();
sel.addRange(range);
}
} else if (document.selection && document.selection.createRange) {
range = document.selection.createRange();
range.pasteHTML(text);
}
}
function ignore_input(event) {
if (KEYMAP.some(row => row.includes(event.code)) == false) return true
if (["Tab", "CapsLock", "Enter", "Backspace"].includes(event.code)) return true
// if (event.repeat) return true
return false
}
function focusEl(el) {
el.select()
}
function play_keydown(e, state, key_binds) {
if (e.ctrlKey) return;
if (ignore_input(e)) return;
e.preventDefault()
const name = key_dict[e.code].name
const desc = key_desc(name, state)
document.dispatchEvent(new CustomEvent('pressdown', { detail: { scancode: e.code }, bubbles: true }))
if (dead_key_context && desc in dead_key_context.dead) {
const char_value = dead_key_context.dead[desc]
insertTextAtCursor(char_value)
dead_key_context = null
} else if (!dead_key_context && desc in key_binds && Object.keys(key_binds[desc].dead).length > 0) {
dead_key_context = key_binds[desc]
} else {
if (desc in key_binds == false) return
const char_value = key_binds[desc].bind
if (dead_key_context) {
insertTextAtCursor(dead_key_context.bind)
dead_key_context = null
if (desc == '[Space]' || desc == '[Space SHIFT]') return
}
insertTextAtCursor(char_value)
}
const sel = window.getSelection();
const trange = sel.getRangeAt(0);
const range = document.createRange();
range.setStart(document.getElementById('play-area'), 0);
range.setEnd(trange.endContainer, trange.endOffset);
let range_text = range.toString();
for (const seq in replacers) {
if (range_text.endsWith(seq)) {
range_text = range_text.replace(seq, replacers[seq]);
trange.setStart(document.getElementById('play-area'), 0);
trange.deleteContents();
let node = document.createTextNode(range_text);
trange.insertNode(node);
trange.setStart(node, node.length);
trange.setEnd(node, node.length);
}
}
}
function play_keyup(e) {
if (ignore_input(e)) return
document.dispatchEvent(new CustomEvent('pressup', { detail: { scancode: e.code }, bubbles: true }))
}
let replacers = {_len: 0}
const INCLUDES = {
'en-us': DEFAULT_EN_US,
'pt-br': DEFAULT_PT_BR,
'qwerty-lower': DEFAULT_QWERTY_LOWER,
'qwerty-upper': DEFAULT_QWERTY_UPPER,
}
function parse_keyboard(e, key_binds, error) {
let code = e.srcElement.value;
replacers = {};
let matches;
let included = {};
while (true) {
try {
matches = parse_harpsichord(code)
delete error.msg
} catch (e) {
error.msg = String(e)
}
if (matches == null) return;
for (var k in key_binds){
if (key_binds.hasOwnProperty(k)){
delete key_binds[k];
}
}
let inc_code = code;
for (const match of matches) {
if (match.include && match.include in INCLUDES && !(match.include in included)) {
inc_code = INCLUDES[match.include] + inc_code
included[match.include] = true
}
}
if (inc_code == code) break;
code = inc_code
}
for (const match of matches) {
if (match == null) continue
if (match.delete) {
delete key_binds[`[${match.delete}]`];
continue;
}
if (match.replace) {
replacers[match.replace] = match.to;
if (match.replace.length > replacers._len) {
replacers._len = match.replace.length;
}
continue;
}
if (match.ctx == null) {
if (!(`[${match.key}]` in key_binds)) {
key_binds[`[${match.key}]`] = { bind: match.string, dead: {}, display: match.display }
} else {
key_binds[`[${match.key}]`].bind = match.string
key_binds[`[${match.key}]`].display = match.display
}
} else {
const ctx = `[${match.ctx}]`
if (!(ctx in key_binds)) {
key_binds[ctx] = { bind: null, dead: { [`[${match.key}]`]: match.string }, display: match.display }
} else {
key_binds[ctx].dead[`[${match.key}]`] = match.string
}
}
}
}
function set_font(font) {
let keys = document.getElementsByClassName('key-label');
for (const el of keys) {
el.style.fontFamily = `${font}, "Segoe UI", "Arial", sans-serif`;
}
document.getElementById('play-area').style.fontFamily = `${font}, "Segoe UI", "Arial", sans-serif`;
}
function save_to_local(e) {
let name = prompt('Enter a name');
name = name.replace(' ', '_');
let code = document.getElementById('keyboard-description').value;
localStorage.setItem('#'+name, code);
alert(`Local storage is limited! Please save a copy of the code in your computer!\n\nEnter\n${window.location.origin}${window.location.pathname}#${name}\nto load the keyboard.`);
window.location = `${window.location.origin}${window.location.pathname}#${name}`;
}
function on_load(e, key_binds, error) {
if (window.location.search.length > 0) {
const link = window.location.search.slice(1)
fetch(link, {"mode" : "cors", "method": 'GET'})
.then(resp => {
if (!resp.ok) {
alert('Provided URL failed to load');
throw new Error(`HTTP error! Status: ${response.status}`);
}
return resp.text()
})
.then(resp => {
e.srcElement.value = resp;
parse_keyboard(e, key_binds, error);
})
.catch(err => {
alert('Provided URL failed to load');
})
} else if (window.location.hash.length > 0) {
const value = localStorage.getItem(window.location.hash);
if (value) {
e.srcElement.value = value;
}
}
parse_keyboard(e, key_binds, error);
}
</script>
<div id="keyboard" x-data="{
selected: {},
state: [false, false, false],
show_key_codes: true,
show_shift_keys: false,
display_font: '',
key_binds: {},
error_message: {},
}">
<div id="keyboard-bg" x-data="{ keys: KEYS_DATA, keyboard: KEYBOARD_DATA }" :style="{ width: `${keyboard.width}px`, height: `${keyboard.height}px` }">
<div>
<template x-for="key in keys">
<div class="key" x-data="{ pressed: false, scancode: key.scancode, special: key.special, key_data: key }"
@pressdown.window="$event.detail.scancode == scancode ? pressed = do_key_action(key_data, state) : null"
@pressup.window="$event.detail.scancode == scancode ? pressed = undo_key_action(key_data, state) : null"
:style="{ width: `${key.where[2] * 54}px`, top: `${key.where[0] * 54}px`, left: `${key.where[1] * 54}px` }"
:class="{ 'key-pressed': pressed, 'key-special': special, 'key-selected': selected && selected.scancode == key.scancode, 'key-dead-key': is_dead(key_data, state, key_binds) }">
<div class="key-label" @click="selected = key; $dispatch('keyselect')" :style="{ width: `${key.where[2] * 54 - 14}px` }" >
<span class="key-label-base"
x-text="show_key_name(key_data, state, key_binds)"
></span>
<span class="key-label-shift"
x-show="show_shift_keys && !state[0] && !special"
x-text="show_key_name(key_data, [true, state[1], state[2]], key_binds)"
></span>
<span class="key-label-og"
x-show="show_key_codes || selected && selected.scancode == key.scancode"
x-text="key_data.special ? '' : key_data.name"
></span>
</div>
</div>
</template>
</div>
</div>
<div id="config-key" :style="{ top: `${keyboard.height + 50}px` }">
<div class="config-field">
<label>Show Keycodes:</label>
<input type="checkbox" x-model="show_key_codes">
<label>Show Shift:</label>
<input type="checkbox" x-model="show_shift_keys">
<label>Display Font (Enter to confirm):</label>
<input type="text" x-model="display_font" @keyup.enter="set_font(display_font)">
<br>
<button type="button" @click="save_to_local">Save to local storage</button>
</div>
<textarea id="keyboard-description"
x-init="$nextTick(() => on_load({'srcElement': $el}, key_binds, error_message))"
@input="parse_keyboard($event, key_binds, error_message)" rows="14" spellcheck="false">include 'en-us'
[#7]: [A] > 'á'
[#7]: [E] > 'é'
[#7]: [I] > 'í'
[#7]: [O] > 'ó'
[#7]: [U] > 'ú'
[#7]: [A SHIFT] > 'Á'
[#7]: [E SHIFT] > 'É'
[#7]: [I SHIFT] > 'Í'
[#7]: [O SHIFT] > 'Ó'
[#7]: [U SHIFT] > 'Ú'</textarea>
<p x-text="error_message.msg" x-show='"msg" in error_message'></p>
</div>
<div>
<div id="play-area" contenteditable spellcheck="false"
@focusin="typing_mode = true"
@focusout="typing_mode = false"
@keypress.prevent=""
@keydown="play_keydown($event, state, key_binds)"
@keyup="play_keyup"
></div>
</div>
</div>
</body>
</html>