-
Notifications
You must be signed in to change notification settings - Fork 1
/
section-09.html
456 lines (421 loc) · 14.7 KB
/
section-09.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="./main.css" rel="stylesheet">
</head>
<body>
<form>
<label for="input">Input</label>
<textarea id="input" rows="2"></textarea>
<div id="keyboards-list"><span>Keyboards:</span></div>
<div id="visual-scripts-list"></div>
<div id="keyboard" class="v2"></div>
<button id="submit" type="button">Submit</button>
<hr>
<label>Output</label>
<pre id="output"></pre>
</form>
</body>
<script>
/*
We're organising all the keyboard logic into a single 'KeyboardApp'.
*/
class KeyboardApp {
constructor () {
// Let's register all the HTML elements to make them easier to access later.
this.html = {
input: document.getElementById('input'),
keyboard: document.getElementById('keyboard'),
keyboardsList: document.getElementById('keyboards-list'),
visualScriptsList: document.getElementById('visual-scripts-list'),
submit: document.getElementById('submit'),
output: document.getElementById('output'),
}
// Let's register some event handling
this.html.input.onkeypress = this.onKeyPress.bind(this)
this.html.submit.onclick = this.submit.bind(this)
// Let's prepare the list of selectable keyboards
this.setupKeyboardsList()
// Let's set the default keyboard language
this.activeKeyboard = HEBREW_KEYBOARD
this.activeVisualScript = HEBREW_KEYBOARD.visualScripts['Yemenite Square']
this.setupVisualScriptsList()
this.setupKeyboard()
}
/*
This 'submit' logic is just placeholder for what you actually want to do.
Normally, this is where we'd submit the input data to a server. For our
example, we just print the text into the output field.
*/
submit () {
const text = input.value
output.innerText = text
}
/*
setupKeyboardsList() does exactly what it says on the tin.
*/
setupKeyboardsList () {
Object.entries(KEYBOARDS).forEach(([language, keyboard]) => {
const buttonKeyboard = document.createElement('button')
buttonKeyboard.type = 'button'
buttonKeyboard.innerText = language
buttonKeyboard.onclick = () => {
this.activeKeyboard = keyboard
this.activeVisualScript = undefined
this.setupVisualScriptsList()
this.setupKeyboard()
}
this.html.keyboardsList.appendChild(buttonKeyboard)
})
}
/*
If the selected keyboard (i.e. language) has a visual script reference (i.e.
handwritten image reference for each character), set it up here.
*/
setupVisualScriptsList () {
// First, we erase everything in the current list.
while (this.html.visualScriptsList.firstChild) {
this.html.visualScriptsList.removeChild(this.html.visualScriptsList.firstChild)
}
// If there's no active keyboard, or if the active keyboard doesn't have any
// visual script references, then we can call it quits!
if (!this.activeKeyboard || !this.activeKeyboard.visualScripts) {
this.html.visualScriptsList.style.display = 'none'
return
}
// OK, now let's create the buttons that will allow users to switch between
// the different visual references available.
this.html.visualScriptsList.style.display = 'block'
Object.entries(this.activeKeyboard.visualScripts).forEach(([scriptName, scriptData]) => {
const buttonVisualScript = document.createElement('button')
buttonVisualScript.type = 'button'
buttonVisualScript.innerText = scriptName
buttonVisualScript.onclick = () => {
this.activeVisualScript = scriptData
this.setupKeyboard()
}
this.html.visualScriptsList.appendChild(buttonVisualScript)
})
}
/*
setupKeyboard() does exactly what it says on the tin.
*/
setupKeyboard () {
// First, we erase everything in the current keyboard.
while (this.html.keyboard.firstChild) {
this.html.keyboard.removeChild(this.html.keyboard.firstChild)
}
// If there's no active keyboard, I guess we've got nothing to do!
if (!this.activeKeyboard) {
this.html.input.style.direction = 'ltr' // Default direction
return
}
// Right, now let's add the keys (buttons) for a QWERTY layout
QWERTY_LAYOUT.forEach((row) => {
const divRow = document.createElement('div')
divRow.className = 'row'
row.forEach((key) => {
// Create a button for each key in the QWERTY layout
const buttonKey = document.createElement('button')
buttonKey.className = 'keyboard-key'
buttonKey.type = 'button'
// Is there a character in our custom language keyboard that corresponds
// with this QWERTY key? If so, then pressing the key should insert
// the associated character.
const char = this.activeKeyboard.keys[key]
if (char) {
const hasVisualReference = this.activeVisualScript
&& this.activeKeyboard.visualScriptLayout
&& this.activeKeyboard.visualScriptLayout[char]
// If the character has a visual reference, we add a button with an image.
if (hasVisualReference) {
const characterPositionOnImage = this.activeKeyboard.visualScriptLayout[char]
const characterSizeOnImage = '50px' // We know this in advance
// We're using a CSS trick known as "image sprites".
// The visual reference image has ALL the characters in a given
// script, so we use CSS background image + positioning to only show
// specific parts of the whole reference image, corresponding to
// individual characters.
const visualSpan = document.createElement('div')
visualSpan.style.backgroundImage = 'url(' + this.activeVisualScript + ')'
visualSpan.style.backgroundPosition = -characterPositionOnImage.x + 'px ' + -characterPositionOnImage.y + 'px'
visualSpan.style.width = characterSizeOnImage
visualSpan.style.height = characterSizeOnImage
buttonKey.appendChild(visualSpan)
buttonKey.onclick = () => { this.insertChar(char) }
// Otherwise, we add a standard button.
} else {
buttonKey.innerText = char
buttonKey.onclick = () => { this.insertChar(char) }
}
} else {
buttonKey.innerText = ' '
}
// Add the 'shortcut' (i.e. the QWERTY key)
const shortcutSpan = document.createElement('span')
shortcutSpan.innerText = ENGLISH_KEYBOARD.keys[key]
buttonKey.appendChild(shortcutSpan)
divRow.appendChild(buttonKey)
})
this.html.keyboard.appendChild(divRow)
})
// Some keyboards/languages write from right to left. Update the CSS style
// for the text input field accordingly.
this.html.input.style.direction = this.activeKeyboard.direction || 'ltr'
}
/*
insertChar() inserts a single character to the (end of the) input text box.
*/
insertChar (char) {
// Find the position of the "text cursor" on the text input
const startIndex = this.html.input.selectionStart
const endIndex = this.html.input.selectionEnd
// Minor trivia: if the text cursor has selected some text (e.g. a user
// highlighted a word) then startIndex will not be the same as endIndex ;
// otherwise, startIndex will be the same as endIndex.
// Insert the new character where the text cursor is, OR replace the text
// that the text cursor is selecting.
const text = this.html.input.value
const startText = text.substring(0, startIndex)
const endText = text.substring(endIndex)
this.html.input.value = startText + char + endText
// Return focus to the input text box, and reset the "text cursor" to the
// location where we just inserted the character
this.html.input.focus()
const focusIndex = startIndex + char.length
this.html.input.setSelectionRange(focusIndex, focusIndex)
}
/*
onKeyPress listens for keyboard input on the text input field.
*/
onKeyPress (keyboardEvent) {
// Get the key that the user just pressed
const keyPressed = keyboardEvent.code
// Note: there are different ways to get what the user typed into a text
// field.
// keyboardEvent.code corresponds to the PHYSICAL key on the keyboard.
// keyboardEvent.key corresponds to the TEXT VALUE of the key.
// If a user presses the 'A' key on a US-International QWERTY keyboard,
// we get code='KeyA', and key='a' (if shift/capslock is off) or key='A'
// (if shift/capslock is on)
// Check to see if there's a character on the on-screen keyboard that
// corresponds to the key the user just pressed. If yes, insert that
// character.
const char = this.activeKeyboard && this.activeKeyboard.keys[keyPressed]
if (char) {
this.insertChar(char)
return stopEvent(keyboardEvent)
}
return true
}
}
/*
Stops the default actions for an input event. For example, pressing the 'A' key
on a keyboard will insert the character 'a' into a text input field. If we want
to change the action so the 'A' key inserts the 'あ' character, we first need to
disable the default action.
*/
function stopEvent (e) {
e.preventDefault && e.preventDefault()
e.stopPropagation && e.stopPropagation()
return false
}
/*
English Keyboard + QWERTY Layout: defines the baseline US-International QWERTY
keyboard that we assume each user is using.
*/
const ENGLISH_KEYBOARD = {
direction: 'ltr',
keys: {
'KeyQ': 'Q',
'KeyW': 'W',
'KeyE': 'E',
'KeyR': 'R',
'KeyT': 'T',
'KeyY': 'Y',
'KeyU': 'U',
'KeyI': 'I',
'KeyO': 'O',
'KeyP': 'P',
'BracketLeft': '[',
'BracketRight': ']',
// --------
'KeyA': 'A',
'KeyS': 'S',
'KeyD': 'D',
'KeyF': 'F',
'KeyG': 'G',
'KeyH': 'H',
'KeyJ': 'J',
'KeyK': 'K',
'KeyL': 'L',
'Semicolon': ';',
'Quote': '\'',
// --------
'KeyZ': 'Z',
'KeyX': 'X',
'KeyC': 'C',
'KeyV': 'V',
'KeyB': 'B',
'KeyN': 'N',
'KeyM': 'M',
'Comma': ',',
'Period': '.',
'Slash': '/',
}
}
const QWERTY_LAYOUT = [
[ 'KeyQ', 'KeyW', 'KeyE', 'KeyR', 'KeyT', 'KeyY', 'KeyU', 'KeyI', 'KeyO', 'KeyP', 'BracketLeft', 'BracketRight' ],
[ 'KeyA', 'KeyS', 'KeyD', 'KeyF', 'KeyG', 'KeyH', 'KeyJ', 'KeyK', 'KeyL', 'Semicolon', 'Quote' ],
[ 'KeyZ', 'KeyX', 'KeyC', 'KeyV', 'KeyB', 'KeyN', 'KeyM', 'Comma', 'Period', 'Slash' ]
]
/*
Hebrew Keyboard: translates key presses on a US-International QWERTY keyboard
into Hebrew characters.
*/
const HEBREW_KEYBOARD = {
direction: 'rtl',
keys: {
// 'KeyQ': 'Q',
// 'KeyW': 'W',
'KeyE': 'ק', // Qof
'KeyR': 'ר', // Resh
'KeyT': 'א', // Alef
'KeyY': 'ט', // Tet
'KeyU': 'ו', // Waw/Vav
'KeyI': 'ן', // Nun (2)
'KeyO': 'ם', // Mem (2)
'KeyP': 'פ', // Pe (1)
// 'BracketLeft': '[',
// 'BracketRight': ']',
// --------
'KeyA': 'ש', // Shin
'KeyS': 'ד', // Dalet
'KeyD': 'ג', // Gimel
'KeyF': 'כ', // Kaf
'KeyG': 'ע', // Ayin
'KeyH': 'י', // Yod
'KeyJ': 'ח', // Chet
'KeyK': 'ל', // Lamed
'KeyL': 'ך', // Kaf
'Semicolon': 'ף', // Pe (2)
// 'Quote': '\'',
// --------
'KeyZ': 'ז', // Zayin
'KeyX': 'ס', // Samech
'KeyC': 'ב', // Bet
'KeyV': 'ה', // He
'KeyB': 'נ', // Nun (1)
'KeyN': 'מ', // Mem (1)
'KeyM': 'צ', // Tsadik (1)
'Comma': 'ת', // Tav
'Period': 'ץ', // Tsadik (2)
'Slash': 'ﭏ', // Alef-lamed
},
// The following contains info on how to map each character (i.e. data) with a
// visual reference (i.e. image) of how the character looks like in a
// real-world manuscript written by human hands. (i.e. the Hebrew script)
visualScripts: {
'Byzantine Cursive': 'images/byzantine-cursive.jpg',
'Byzantine Minuscule': 'images/byzantine-minuscule.jpg',
'Byzantine Square': 'images/byzantine-square.jpg',
'Maghrebi Cursive': 'images/maghrebi-cursive.jpg',
'Maghrebi Square': 'images/maghrebi-square.jpg',
'Yemenite Minuscule': 'images/yemenite-minuscule.jpg',
'Yemenite Square': 'images/yemenite-square.jpg',
'none': undefined,
},
// To keep things simple, each image file (representing a Hebrew script and
// containing an instance of every handwritten character) follows a specific
// visual layout. For example, each handwritten Hebrew character's image is a
// 50x50px square located at specific x-y coordinates. This consistency allows
// us to use a CSS technique known as "image sprites" to package multiple
// character images into one file, which in turn lets us rapidly add new
// visual guides for different styles of Hebrew scripts.
visualScriptLayout: {
'ק': { x: 440, y: 256 }, // Qof
'ר': { x: 330, y: 256 }, // Resh
'א': { x: 440, y: 0 }, // Alef
'ט': { x: 110, y: 64 }, // Tet
'ו': { x: 275, y: 64 }, // Waw/Vav
'ן': { x: 385, y: 192 }, // Nun (2)
'ם': { x: 55, y: 128 }, // Mem (2)
'פ': { x: 165, y: 192 }, // Pe (1)
// --------
'ש': { x: 220, y: 256 }, // Shin
'ד': { x: 55, y: 0 }, // Dalet
'ג': { x: 110, y: 0 }, // Gimel
'כ': { x: 0, y: 64 }, // Kaf (1)
'ע': { x: 275, y: 192 }, // Ayin
'י': { x: 55, y: 64 }, // Yod
'ח': { x: 165, y: 64 }, // Chet
'ל': { x: 275, y: 128 }, // Lamed
'ך': { x: 440, y: 128 }, // Kaf (2)
'ף': { x: 110, y: 192 }, // Pe (2)
// --------
'ז': { x: 220, y: 64 }, // Zayin
'ס': { x: 55, y: 128 }, // Samech
'ב': { x: 220, y: 0 }, // Bet
'ה': { x: 440, y: 64 }, // He
'נ': { x: 440, y: 192 }, // Nun (1)
'מ': { x: 110, y: 256 }, // Mem (1)
'צ': { x: 55, y: 192 }, // Tsadik (1)
'ת': { x: 110, y: 256 }, // Tav
'ץ': { x: 0, y: 192 }, // Tsadik (2)
'ﭏ': { x: 330, y: 0 }, // Alef-lamed
},
}
/*
Emoji Keyboard: a joke keyboard to demonstrate how easy it is to add map keys
to characters of any 'language'.
*/
const EMOJI_KEYBOARD = {
direction: 'ltr',
keys: {
'KeyQ': '😃',
'KeyW': '😅',
'KeyE': '🤣',
'KeyR': '🥰',
'KeyT': '😉',
'KeyY': '😂',
'KeyU': '🥵',
'KeyI': '🥶',
'KeyO': '😍',
'KeyP': '😵',
'BracketLeft': '🎉',
'BracketRight': '🍎',
// --------
'KeyA': '🐈',
'KeyS': '🐕',
'KeyD': '🐒',
'KeyF': '🕊️',
'KeyG': '🐓',
'KeyH': '🐟',
'KeyJ': '🐙',
'KeyK': '🦑',
'KeyL': '🦋',
'Semicolon': '🔥',
'Quote': '💧',
// --------
'KeyZ': '❤️',
'KeyX': '🧡',
'KeyC': '💛',
'KeyV': '💚',
'KeyB': '💙',
'KeyN': '💜',
'KeyM': '🤎',
'Comma': '🖤',
'Period': '🤍',
'Slash': '💌',
}
}
const KEYBOARDS = {
'(No keyboard)': undefined,
'English': ENGLISH_KEYBOARD,
'Hebrew': HEBREW_KEYBOARD,
'Emoji': EMOJI_KEYBOARD,
}
var app = new KeyboardApp() // Let's start the app!
</script>
</html>