-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGreekTyper.js
427 lines (383 loc) · 14.3 KB
/
GreekTyper.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
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
window.onload = function () { // faster than $()
/*
To do:
- support non-Firefox codes for punctuation keys
- allow customization of shortcuts?
Compress with jscompress.com?
*/
'use strict';
const textBox = $('textarea'); // jQuery object
const textBoxNode = textBox[0]; // Node object
const options = $('#options');
options.shownText = '⏷options';
options.hiddenText = '⏵options';
const optionsButton = $('#options-button');
const description = $('#description');
description.shownText = '⏷description';
description.hiddenText = '⏵description';
const descriptionButton = $('#description-button');
const composedCharsToggle = $('#compose-toggle');
const backspaceToggle = $('#backspace-toggle');
const reorderDiacriticsToggle = $('#reorder-diacritics-toggle');
const validateDiacriticsToggle = $('#validate-diacritics-toggle');
const replaceDiacriticsToggle = $('#replace-diacritics-toggle');
const macronAndBreveToggle = $('#macron-and-breve-toggle');
/*
Some crucial key codes are different in Firefox, Chrome or Opera,
and Edge or Internet Explorer (http://unixpapa.com/js/key.html):
Firefox Edge Chrome
;: 59 186 59
=+ 61 187 61
-_ 173 189 45
/? 191 191 49
\| 220 220 92
*/
// Mapping from event.which (key code) to character,
// if different from QWERTY layout.
const convert = {
// Greek letters
// lowercase
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: 'Ζ',
// uppercase
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: 'ζ',
// diacritics
94: '\u0306', 95: '\u0304', // ^, _ → breve, macron
40: '\u0314', 41: '\u0313', 43: '\u0308', // (, ) → rough breathing, smooth breathing, diaeresis
47: '\u0301', 92: '\u0300', 61: '\u0342', // /, \, = → acute, grave, Greek circumflex
124: '\u0345', // | → iota subscript
// punctuation
58: '·', 59: '·', // middle dot (Greek semicolon or colon)
63: ';', // semicolon
// Greek question mark (;, U+37E) and ano teleia (·, U+387) are normalized
// to middle dot and semicolon (NFD and NFC), and because normalization is
// done frequently in this script, it would be hard to keep the "correct"
// codepoint in the textbox.
39: '’' // right single quotation mark (curly apostrophe), U+2019
};
const showCombiningCharacters = function (str) {
return str.replace(/[\u0300-\u0345]/g, '\u25CC$&');
};
const diacriticOrder = {
'\u0304': 1, // macron
'\u0306': 2, // breve
'\u0314': 3, '\u0313': 3, '\u0308': 3, // rough breathing, smooth breathing, diaeresis
'\u0301': 4, '\u0300': 4, '\u0342': 4, // acute, grave, Greek circumflex
'\u0345': 5, // iota subscript
};
const compareDiacritics = function (diacritic1, diacritic2) {
return diacriticOrder[diacritic1] - diacriticOrder[diacritic2];
};
// If two or more diacritics have the same sort value (diacriticOrder), keep the last of them.
const filterDiacritics = function (diacriticsArray) {
let i = 0, diacritic, pos, prevPos;
while ( diacritic = diacriticsArray[i] ) {
pos = diacriticOrder[diacritic];
if ( pos === prevPos ) {
diacriticsArray.splice(i - 1, 1);
} else {
++i;
}
prevPos = pos;
}
return diacriticsArray;
};
const reorderDiacritics = function (diacritics) {
if ( validateDiacriticsToggle.prop('checked') ) {
return filterDiacritics(diacritics.split('').sort(compareDiacritics)).join('');
} else {
return diacritics.split('').sort(compareDiacritics).join('');
}
};
const setTextAndCursor = function (textBoxNode, beforeCursor, afterCursor, insideSelection) {
beforeCursor = composeEnd(beforeCursor);
if ( insideSelection ) {
textBoxNode.value = beforeCursor + insideSelection + afterCursor;
textBoxNode.setSelectionRange(beforeCursor.length, beforeCursor.length + insideSelection.length);
} else {
textBoxNode.value = beforeCursor + afterCursor;
textBoxNode.setSelectionRange(beforeCursor.length, beforeCursor.length);
}
};
/*
If "composed characters" toggle is checked, find a non-diacritic code
unit followed by one or more diacritics at the end of the string (where
it has just been typed), and convert it to a composed character if possible,
Normalization Form C (canonical composition).
α + ◌̓ → ἀ
Also corrects the order of any sequences of two or more diacritics
if that setting is enabled.
*/
const composeEnd = function (text) {
return composedCharsToggle.prop('checked') ?
reorderDiacriticsToggle.prop('checked') ?
text.replace(
/[^\u0300-\u0345][\u0300-\u0345]+$/,
function (match) {
return match
.normalize('NFD')
.replace(/[\u0300-\u0345]{2,}$/, reorderDiacritics)
.normalize('NFC');
}) :
text.replace(
/[^\u0300-\u0345][\u0300-\u0345]+$/,
function (match) {
return match
.normalize('NFC');
}) :
text;
};
const multipleDiacritics = /[\u0300-\u0345]{2,}/g;
const reorderAllDiacritics = function () {
if ( composedCharsToggle.prop('checked') ) {
textBoxNode.value = textBoxNode.value
.normalize('NFD')
.replace(multipleDiacritics, reorderDiacritics)
.normalize('NFC');
} else {
textBoxNode.value = textBoxNode.value
.replace(multipleDiacritics, reorderDiacritics);
}
};
const setButtonText = function (element, buttonNode) {
return function (animate) {
buttonNode.innerHTML = element.hidden ?
(element.hide(animate ? 200 : undefined), element.hiddenText) :
(element.show(animate ? 200 : undefined), element.shownText);
};
};
const createToggle = function (element) {
return function () {
element.hidden = !element.hidden;
element.setButtonText(true);
};
};
options.setButtonText = setButtonText(options, optionsButton[0]);
description.setButtonText = setButtonText(description, descriptionButton[0]);
optionsButton.click(createToggle(options));
descriptionButton.click(createToggle(description));
const composeTextBox = function () {
const normalization = composedCharsToggle.prop('checked') ? 'NFC' : 'NFD';
let { value: content, selectionStart: start, selectionEnd: end } = textBoxNode;
setTextAndCursor(
textBoxNode,
content.slice(0, start).normalize(normalization),
content.slice(end).normalize(normalization),
content.slice(start, end).normalize(normalization));
textBox.focus();
// console.log(showCombiningCharacters(textBoxNode.value));
};
// Replacing diacritics requires validating diacritics, which requires reordering diacritics.
const reorderDiacriticsToggleEvent = function () {
if ( reorderDiacriticsToggle.prop('checked') ) {
validateDiacriticsToggle.removeAttr('disabled');
} else {
validateDiacriticsToggle.attr('disabled', true);
}
};
const validateToggleEvent = function () {
if ( validateDiacriticsToggle.prop('checked') ) {
reorderAllDiacritics();
replaceDiacriticsToggle.removeAttr('disabled');
macronAndBreveToggle.removeAttr('disabled');
} else {
replaceDiacriticsToggle.attr('disabled', true);
macronAndBreveToggle.attr('disabled', true);
}
};
const macronAndBreveToggleEvent = function () {
if ( macronAndBreveToggle.prop('checked') ) {
diacriticOrder['\u0306'] = 2;
} else {
// Put breve at same sort level as macron, so that one or the other
// will be removed by the filterDiacritics function.
diacriticOrder['\u0306'] = 1;
let text = textBoxNode.value;
const composed = composedCharsToggle.prop('checked');
if ( composed ) {
text = text.normalize('NFD');
}
text = text.replace(/\u0304\u0306/g, '\u0304'); // replace macron–breve combination with macron
if ( composed ) {
text = text.normalize('NFC');
}
textBoxNode.value = text
}
};
// Edge sends an error if localStorage is accessed, at least when this
// script is saved locally.
const canUseLocalStorage = (function () {
try {
const storage = window.localStorage;
storage.setItem('test', '1');
storage.removeItem('test');
return true
} catch (error) {
// console.log (error)
return false;
}
} ());
// Retrieve content of text box, as well as position of cursor.
const retrieveStorage = function () {
if ( !canUseLocalStorage ) {
return;
}
const storage = window.localStorage;
const content = storage.getItem('textBox'),
start = storage.getItem('selectionStart'),
end = storage.getItem('selectionEnd'),
scrollTop = storage.getItem('scrollTop');
if ( content ) {
textBox.val(content);
}
if (scrollTop) {
textBoxNode.scrollTop = scrollTop;
}
if ( start && end ) {
textBox.focus();
textBoxNode.setSelectionRange(Number(start), Number(end));
}
$('input[type=checkbox]').each(function (index, element) {
element = $(element);
// '1' -> true, '0' -> false
element.prop('checked', storage.getItem(element.attr('id')) === '1');
});
options.hidden = storage.getItem('optionsHidden') === '1';
options.setButtonText();
description.hidden = storage.getItem('descriptionHidden') === '1';
description.setButtonText();
};
$(window).on('beforeunload', function () {
if ( !canUseLocalStorage ) {
return;
}
const storage = window.localStorage;
storage.setItem('textBox', textBox.val());
storage.setItem('selectionStart', textBoxNode.selectionStart);
storage.setItem('selectionEnd', textBoxNode.selectionEnd);
storage.setItem('scrollTop', textBoxNode.scrollTop);
$('input[type=checkbox]').each(function (index, element) {
element = $(element);
// true -> '1', false -> '0': setItem coerces to string
storage.setItem(element.attr('id'), Number(element.prop('checked')));
});
storage.setItem('optionsHidden', Number(options.css('display') === 'none'));
storage.setItem('descriptionHidden', Number(description.css('display') === 'none'));
})
// change event
composedCharsToggle.change(composeTextBox);
validateDiacriticsToggle.change(validateToggleEvent);
reorderDiacriticsToggle.change(reorderDiacriticsToggleEvent);
macronAndBreveToggle.change(macronAndBreveToggleEvent);
// document ready event
retrieveStorage();
const littleSigmas = [ 'ς', 'σ' ];
const handleSigma = function (sigma, following) {
// punctuation, whitespace, or nothing ... any way to make this simpler?
if ( littleSigmas.includes(sigma) ) {
return following === '' || (/^[\s–—,.·:;'’")\]|}\u0300-\u0345]/).test(following) ?
'ς' :
// Greek characters or hyphen... there are a few spacing accents and punctuation characters in here
(/^[-\u0370-\u03FF\u1F00-\u1FFF]/).test(following) ?
'σ' :
sigma;
}
return sigma;
};
const insertInTextbox = function (textBox, str) {
const {
selectionStart: start,
selectionEnd: end,
value: content
} = textBox;
const afterCursor = content.slice(end);
// Handle final sigma.
str = handleSigma(str, afterCursor.charAt(0));
const beforeCursor = start === 0 ?
'' :
start === 1 ?
handleSigma(content.charAt(start - 1), str) :
content.slice(0, start - 1) + handleSigma(content.charAt(start - 1), str); // Technically this works when start === 1...
setTextAndCursor(textBox, beforeCursor + str, afterCursor);
};
// Microsoft Edge doesn't emit the keypress event on backspace, Firefox does.
textBox.keydown(function handleBackspace (event) {
// Backspace has several options.
if ( event.which === 8 ) {
const { selectionStart: start, selectionEnd: end } = this;
// Go ahead with default action if characters are selected, or for ctrl + backspace.
if ( start !== end || event.ctrlKey ) {
return;
}
event.preventDefault();
const content = this.value;
let beforeCursor, afterCursor;
// Shift-backspace to delete a whole sequence of diacritics.
if ( event.shiftKey ) {
beforeCursor = content.slice(0, start).normalize('NFD').replace(/(?:[\u0300-\u0345]+|.)$/, ''),
afterCursor = content.slice(end);
// If backspaceToggle is checked, delete a single diacritic or character.
} else if ( backspaceToggle.prop('checked') ) {
if ( composedCharsToggle.prop('checked') ) {
beforeCursor = start === 0 ?
'' :
content.slice(0, start - 1)
+ content.charAt(start - 1).normalize('NFD').slice(0, -1); // decompose, remove last character
afterCursor = content.slice(end);
} else { // Assuming there are no composed characters if composedCharsToggle is not checked.
beforeCursor = start === 0 ? '' : content.slice(0, start - 1);
afterCursor = content.slice(end);
}
// Otherwise, delete a single character with all of its diacritics (if any).
} else {
beforeCursor = start === 0 ?
'' :
content.slice(0, start).replace(/[^\u0300-\u0345][\u0300-\u0345]*$/, '');
afterCursor = content.slice(end);
}
beforeCursor = beforeCursor.slice(0, -1) + handleSigma(beforeCursor.slice(-1), afterCursor.charAt(0));
setTextAndCursor(this, beforeCursor, afterCursor);
}
// console.log(`keycode: ${event.which}; shift: ${event.shiftKey}; alt: ${event.altKey}; ctrl: ${event.ctrlKey}`);
// console.log(event);
});
// This handles the keys that can type text.
textBox.keypress(function typeGreek (event) {
const keyCode = event.which;
if ( !(keyCode === 8 || event.ctrlKey || event.altKey || event.metaKey) ) {
if ( convert[keyCode] ) {
event.preventDefault();
insertInTextbox(this, convert[keyCode]);
} else {
// If one character long, event.key is the actual typed value?
if ( event.key.length === 1 ) {
const {
selectionStart: start,
selectionEnd: end,
value: content
} = this;
setTextAndCursor(
this,
start === 0 ?
'' :
start === 1 ?
handleSigma(content.charAt(start - 1), event.key) :
content.slice(0, start - 1)
+ handleSigma(content.charAt(start - 1), event.key),
content.slice(end));
}
}
}
// console.log(`keycode: ${event.which}; shift: ${event.shiftKey}; alt: ${event.altKey}; ctrl: ${event.ctrlKey}`);
// console.log(event);
});
};