@uyghurcommunity/keyboard 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +206 -2
- package/dist/uyghur-keyboard.js +644 -0
- package/docs/layouts.md +43 -0
- package/package.json +26 -4
- package/src/element.d.ts +1 -0
- package/src/element.js +3 -0
- package/src/index.d.ts +56 -0
- package/src/index.js +411 -0
- package/src/layouts.js +103 -0
- package/src/navigation.js +45 -0
- package/src/style.js +83 -0
|
@@ -0,0 +1,644 @@
|
|
|
1
|
+
// Generated by npm run build. Source available in src/.
|
|
2
|
+
const alkatipFontFace = `@font-face {
|
|
3
|
+
font-family: "ALKATIP Basma";
|
|
4
|
+
src: url("https://db.onlinewebfonts.com/t/3c16518f40da2fc2901f37666c0556a7.woff2") format("woff2"),
|
|
5
|
+
url("https://db.onlinewebfonts.com/t/3c16518f40da2fc2901f37666c0556a7.woff") format("woff"),
|
|
6
|
+
url("https://db.onlinewebfonts.com/t/3c16518f40da2fc2901f37666c0556a7.ttf") format("truetype");
|
|
7
|
+
font-weight: normal;
|
|
8
|
+
font-style: normal;
|
|
9
|
+
font-display: swap;
|
|
10
|
+
}`;
|
|
11
|
+
const alkatipHelperStyles = `.alkatip-basma {
|
|
12
|
+
font-family: "ALKATIP Basma", serif;
|
|
13
|
+
direction: rtl;
|
|
14
|
+
unicode-bidi: embed;
|
|
15
|
+
line-height: 2.2;
|
|
16
|
+
}`;
|
|
17
|
+
const keyboardStyles = `
|
|
18
|
+
${alkatipHelperStyles}
|
|
19
|
+
.ug-keyboard {
|
|
20
|
+
--_bg:#f1f3ef; --_key:#fff; --_ink:#17362f; --_line:#ccd7cf; --_hover:#e3ede5; --_focus:#087866;
|
|
21
|
+
box-sizing:border-box;
|
|
22
|
+
width:100%;
|
|
23
|
+
padding:12px;
|
|
24
|
+
border:1px solid var(--ug-line,var(--_line));
|
|
25
|
+
border-radius:16px;
|
|
26
|
+
background:var(--ug-bg,var(--_bg));
|
|
27
|
+
color:var(--ug-ink,var(--_ink));
|
|
28
|
+
color-scheme:light;
|
|
29
|
+
font-family:"ALKATIP Basma",Tahoma,Arial,sans-serif;
|
|
30
|
+
line-height:1.5;
|
|
31
|
+
}
|
|
32
|
+
.ug-keyboard[dir="ltr"] { direction:ltr; }
|
|
33
|
+
.ug-keyboard[data-theme="dark"] {
|
|
34
|
+
--_bg:#182722; --_key:#263c33; --_ink:#eef7f2; --_line:#527367; --_hover:#37564a; --_focus:#89e6ca;
|
|
35
|
+
color-scheme:dark;
|
|
36
|
+
}
|
|
37
|
+
.ug-keyboard * { box-sizing:border-box; }
|
|
38
|
+
.ug-keyboard__row {
|
|
39
|
+
display:flex;
|
|
40
|
+
flex-wrap:wrap;
|
|
41
|
+
justify-content:center;
|
|
42
|
+
gap:6px;
|
|
43
|
+
margin-block:6px;
|
|
44
|
+
}
|
|
45
|
+
.ug-keyboard__layout, .ug-keyboard__key {
|
|
46
|
+
min-width:44px;
|
|
47
|
+
min-height:44px;
|
|
48
|
+
border:1px solid var(--ug-line,var(--_line));
|
|
49
|
+
border-radius:8px;
|
|
50
|
+
background:var(--ug-key,var(--_key));
|
|
51
|
+
color:inherit;
|
|
52
|
+
cursor:pointer;
|
|
53
|
+
touch-action:manipulation;
|
|
54
|
+
}
|
|
55
|
+
.ug-keyboard__layout {
|
|
56
|
+
max-width:100%;
|
|
57
|
+
padding:4px 8px;
|
|
58
|
+
font:16px/1.5 "ALKATIP Basma",Tahoma,Arial,sans-serif;
|
|
59
|
+
}
|
|
60
|
+
.ug-keyboard__key {
|
|
61
|
+
display:inline-flex;
|
|
62
|
+
align-items:center;
|
|
63
|
+
justify-content:center;
|
|
64
|
+
flex:1 0 44px;
|
|
65
|
+
padding:4px 2px;
|
|
66
|
+
font:22px/1.5 "ALKATIP Basma",Tahoma,Arial,sans-serif;
|
|
67
|
+
}
|
|
68
|
+
.ug-keyboard__key:hover, .ug-keyboard__key[aria-pressed="true"] { background:var(--ug-hover,var(--_hover)); }
|
|
69
|
+
.ug-keyboard__key:focus-visible, .ug-keyboard__layout:focus-visible {
|
|
70
|
+
outline:2px solid var(--ug-focus,var(--_focus));
|
|
71
|
+
outline-offset:1px;
|
|
72
|
+
z-index:1;
|
|
73
|
+
}
|
|
74
|
+
.ug-keyboard__key[data-action] { font-size:16px; }
|
|
75
|
+
.ug-keyboard__key[data-action="space"] { flex-grow:4; }
|
|
76
|
+
.ug-keyboard[data-compact="true"] { padding:6px; }
|
|
77
|
+
.ug-keyboard[data-compact="true"] .ug-keyboard__row { gap:3px; margin-block:3px; }
|
|
78
|
+
.ug-keyboard[data-compact="true"] .ug-keyboard__key { padding:2px; font-size:18px; }
|
|
79
|
+
.ug-keyboard[hidden] { display:none; }
|
|
80
|
+
@media (max-width:420px) {
|
|
81
|
+
.ug-keyboard { padding:6px; }
|
|
82
|
+
.ug-keyboard__row { gap:4px; }
|
|
83
|
+
}
|
|
84
|
+
`;
|
|
85
|
+
|
|
86
|
+
/** The complete 32-letter alphabet is represented across these virtual rows. */
|
|
87
|
+
export const KEY_LAYOUT = Object.freeze([
|
|
88
|
+
Object.freeze(['چ', 'ۋ', 'ې', 'ر', 'ت', 'ي', 'ۇ', 'ڭ', 'و', 'پ']),
|
|
89
|
+
Object.freeze(['ھ', 'س', 'د', 'ا', 'ە', 'ى', 'ق', 'ك', 'ل', 'ژ']),
|
|
90
|
+
Object.freeze(['ز', 'ش', 'غ', 'ۈ', 'ب', 'ن', 'م', 'ۆ', 'گ', 'خ', 'ف', 'ج']),
|
|
91
|
+
Object.freeze(['ئ', 'ئا', 'ئە', 'ئې', 'ئى', 'ئو', 'ئۇ', 'ئۆ', 'ئۈ']),
|
|
92
|
+
]);
|
|
93
|
+
|
|
94
|
+
/** An explicit, optional phonetic map. This is not an operating-system layout. */
|
|
95
|
+
export const PHONETIC_MAP = Object.freeze({
|
|
96
|
+
a: 'ا', b: 'ب', c: 'چ', d: 'د', e: 'ە', f: 'ف', g: 'گ', h: 'ھ', i: 'ى', j: 'ج',
|
|
97
|
+
k: 'ك', l: 'ل', m: 'م', n: 'ن', o: 'و', p: 'پ', q: 'ق', r: 'ر', s: 'س', t: 'ت',
|
|
98
|
+
u: 'ۇ', v: 'ۆ', w: 'ۋ', x: 'خ', y: 'ي', z: 'ز',
|
|
99
|
+
Z: 'ژ', A: 'ئا', E: 'ئە', I: 'ئى', O: 'ئو', U: 'ئۇ', G: 'غ', N: 'ڭ', S: 'ش', V: 'ئۆ', W: 'ئۈ', C: 'ج', Y: 'ې',
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const arabicExtras = ['ژ', 'ف', 'گ', 'خ', 'ج', 'ۆ', '،', '؛', '؟', '«', '»'];
|
|
103
|
+
|
|
104
|
+
// Functional key positions verified against Microsoft Learn's Uyghur layout
|
|
105
|
+
// (KLID 00010480) and cross-checked against XKB cn(ug). The web keyboard
|
|
106
|
+
// displays key positions from left to right and edits text right to left.
|
|
107
|
+
// The Uyghur character inventory and behavior still need native-speaker review.
|
|
108
|
+
const desktopRows = [
|
|
109
|
+
['`', ...'1234567890', '-', '='],
|
|
110
|
+
[...'چۋېرتيۇڭوپ', ']', '[', '\\'],
|
|
111
|
+
[...'ھسداەىقكل', '؛', "'"],
|
|
112
|
+
[...'زشغۈبنم', '،', '.', 'ئ'],
|
|
113
|
+
];
|
|
114
|
+
const desktopShiftedRows = [
|
|
115
|
+
['~', ...'!@#$%^&*)(', '_', '+'],
|
|
116
|
+
[...'چۋېرتيۇڭوپ', '»', '«', '|'],
|
|
117
|
+
['ھ', 'س', 'ژ', 'ف', 'گ', 'خ', 'ج', 'ۆ', 'لا', ':', '"'],
|
|
118
|
+
[...'زشغۈبنم', '>', '<', '؟'],
|
|
119
|
+
];
|
|
120
|
+
const desktopKeys = [..."`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./"];
|
|
121
|
+
const desktopShiftKeys = [...'~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?'];
|
|
122
|
+
const desktopMap = {};
|
|
123
|
+
for (const [keys, values] of [[desktopKeys, desktopRows.flat()], [desktopShiftKeys, desktopShiftedRows.flat()]]) {
|
|
124
|
+
keys.forEach((key, index) => { desktopMap[key] = values[index]; });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const latinRows = [
|
|
128
|
+
[...'qwertyuiop'],
|
|
129
|
+
[...'asdfghjkl'],
|
|
130
|
+
[...'zxcvbnm', 'ë', 'ö', 'ü', "'"],
|
|
131
|
+
];
|
|
132
|
+
const cyrillicRows = [
|
|
133
|
+
[...'йцукенгшщзхъ'],
|
|
134
|
+
[...'фывапролджэ'],
|
|
135
|
+
[...'ячсмитьбюё'],
|
|
136
|
+
[...'әғҗқңөүһ'],
|
|
137
|
+
];
|
|
138
|
+
|
|
139
|
+
function shiftRows(rows) {
|
|
140
|
+
return rows.map((row) => row.map((key) => key.toUpperCase()));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function freezeLayout(layout) {
|
|
144
|
+
for (const rows of [layout.rows, layout.shiftedRows]) {
|
|
145
|
+
if (!rows) continue;
|
|
146
|
+
rows.forEach(Object.freeze);
|
|
147
|
+
Object.freeze(rows);
|
|
148
|
+
}
|
|
149
|
+
Object.freeze(layout.labels);
|
|
150
|
+
Object.freeze(layout.physicalMap);
|
|
151
|
+
Object.freeze(layout.secondary);
|
|
152
|
+
return Object.freeze(layout);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export const KEYBOARD_LAYOUTS = Object.freeze({
|
|
156
|
+
phonetic: freezeLayout({
|
|
157
|
+
id: 'phonetic', labels: { en: 'Phonetic', ug: 'ئاھاڭداش' }, lang: 'ug-Arab',
|
|
158
|
+
direction: 'rtl', rows: KEY_LAYOUT, physicalMap: PHONETIC_MAP, secondary: arabicExtras,
|
|
159
|
+
}),
|
|
160
|
+
'uey-desktop': freezeLayout({
|
|
161
|
+
id: 'uey-desktop', labels: { en: 'Arabic desktop (UEY)', ug: 'ئەرەب يېزىقى (UEY)' }, lang: 'ug-Arab',
|
|
162
|
+
direction: 'rtl', keyDirection: 'ltr', rows: desktopRows, shiftedRows: desktopShiftedRows,
|
|
163
|
+
physicalMap: desktopMap, secondary: arabicExtras, reviewStatus: 'draft',
|
|
164
|
+
}),
|
|
165
|
+
uly: freezeLayout({
|
|
166
|
+
id: 'uly', labels: { en: 'Latin (ULY)', ug: 'لاتىن يېزىقى (ULY)' }, lang: 'ug-Latn',
|
|
167
|
+
direction: 'ltr', rows: latinRows, shiftedRows: shiftRows(latinRows),
|
|
168
|
+
secondary: ['ch', 'gh', 'ng', 'sh', 'zh', '،', '؛', '؟', '«', '»'], reviewStatus: 'draft',
|
|
169
|
+
}),
|
|
170
|
+
usy: freezeLayout({
|
|
171
|
+
id: 'usy', labels: { en: 'Cyrillic (USY)', ug: 'كىرىل يېزىقى (USY)' }, lang: 'ug-Cyrl',
|
|
172
|
+
direction: 'ltr', rows: cyrillicRows, shiftedRows: shiftRows(cyrillicRows),
|
|
173
|
+
secondary: ['،', '؛', '؟', '«', '»'], reviewStatus: 'draft',
|
|
174
|
+
}),
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// New Uyghur translations and control labels need native-speaker review.
|
|
178
|
+
const keyboardLabels = {
|
|
179
|
+
en: {
|
|
180
|
+
keyboard: 'Uyghur keyboard', layout: 'Keyboard layout', space: 'Space', backspace: 'Backspace', shift: 'Shift',
|
|
181
|
+
punctuation: { '،': 'Comma', '؛': 'Semicolon', '؟': 'Question mark', '«': 'Left guillemet', '»': 'Right guillemet' },
|
|
182
|
+
},
|
|
183
|
+
ug: {
|
|
184
|
+
keyboard: 'ئۇيغۇرچە كۇنۇپكا تاختىسى', layout: 'كۇنۇپكا تاختىسىنىڭ ئورۇنلاشتۇرۇلۇشى',
|
|
185
|
+
space: 'بوشلۇق', backspace: 'ئالدىنقى ھەرپنى ئۆچۈرۈش', shift: 'چوڭ ھەرپ كۇنۇپكىسى',
|
|
186
|
+
punctuation: { '،': 'پەش', '؛': 'چېكىتلىك پەش', '؟': 'سوئال بەلگىسى', '«': 'سول تىرناق', '»': 'ئوڭ تىرناق' },
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const directions = new Set(['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown', 'Home', 'End']);
|
|
191
|
+
|
|
192
|
+
/** Locate the next key using the visual rows, including rows wrapped on phones. */
|
|
193
|
+
function nextKey(buttonRows, current, key, rtl) {
|
|
194
|
+
if (!directions.has(key)) return undefined;
|
|
195
|
+
const buttons = buttonRows.flat();
|
|
196
|
+
const currentRect = current.getBoundingClientRect?.();
|
|
197
|
+
const rows = [];
|
|
198
|
+
if (currentRect?.width) {
|
|
199
|
+
let top;
|
|
200
|
+
for (const button of buttons) {
|
|
201
|
+
const rect = button.getBoundingClientRect();
|
|
202
|
+
if (top !== rect.top) {
|
|
203
|
+
rows.push([]);
|
|
204
|
+
top = rect.top;
|
|
205
|
+
}
|
|
206
|
+
rows.at(-1).push(button);
|
|
207
|
+
}
|
|
208
|
+
} else {
|
|
209
|
+
rows.push(...buttonRows);
|
|
210
|
+
}
|
|
211
|
+
const rowIndex = rows.findIndex((row) => row.includes(current));
|
|
212
|
+
if (rowIndex < 0) return undefined;
|
|
213
|
+
const row = rows[rowIndex];
|
|
214
|
+
const index = row.indexOf(current);
|
|
215
|
+
if (key === 'Home') return row[0];
|
|
216
|
+
if (key === 'End') return row.at(-1);
|
|
217
|
+
if (key === 'ArrowLeft') return row[index + (rtl ? 1 : -1)] || current;
|
|
218
|
+
if (key === 'ArrowRight') return row[index + (rtl ? -1 : 1)] || current;
|
|
219
|
+
const nextRow = rows[rowIndex + (key === 'ArrowUp' ? -1 : 1)];
|
|
220
|
+
if (!nextRow) return current;
|
|
221
|
+
if (!currentRect?.width) return nextRow[Math.min(index, nextRow.length - 1)];
|
|
222
|
+
const center = currentRect.left + currentRect.width / 2;
|
|
223
|
+
let nearest = current;
|
|
224
|
+
let distance = Infinity;
|
|
225
|
+
for (const button of nextRow) {
|
|
226
|
+
const rect = button.getBoundingClientRect();
|
|
227
|
+
const difference = Math.abs(center - rect.left - rect.width / 2);
|
|
228
|
+
if (difference < distance) {
|
|
229
|
+
nearest = button;
|
|
230
|
+
distance = difference;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return nearest;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
const fontStyleId = 'uyghur-keyboard-alkatip-font';
|
|
238
|
+
const fontRegistrations = new WeakMap();
|
|
239
|
+
function acquireFont(document) {
|
|
240
|
+
let registration = fontRegistrations.get(document);
|
|
241
|
+
if (!registration) {
|
|
242
|
+
const parent = document.head || document.documentElement;
|
|
243
|
+
if (!parent) return () => {};
|
|
244
|
+
let element = document.getElementById(fontStyleId);
|
|
245
|
+
const owned = !element;
|
|
246
|
+
if (!element) {
|
|
247
|
+
element = document.createElement('style');
|
|
248
|
+
element.id = fontStyleId;
|
|
249
|
+
element.textContent = alkatipFontFace + '\n' + alkatipHelperStyles;
|
|
250
|
+
parent.appendChild(element);
|
|
251
|
+
}
|
|
252
|
+
registration = { element, owned, count: 0 };
|
|
253
|
+
fontRegistrations.set(document, registration);
|
|
254
|
+
}
|
|
255
|
+
registration.count++;
|
|
256
|
+
let released = false;
|
|
257
|
+
return () => {
|
|
258
|
+
if (released) return;
|
|
259
|
+
released = true;
|
|
260
|
+
if (--registration.count === 0) {
|
|
261
|
+
if (registration.owned) registration.element.remove();
|
|
262
|
+
fontRegistrations.delete(document);
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function selection(value, start, end) {
|
|
268
|
+
const length = value.length;
|
|
269
|
+
const first = Number.isFinite(start) ? Math.max(0, Math.min(length, Math.trunc(start))) : length;
|
|
270
|
+
const last = Number.isFinite(end) ? Math.max(0, Math.min(length, Math.trunc(end))) : first;
|
|
271
|
+
return [Math.min(first, last), Math.max(first, last)];
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** Pure UTF-16 selection replacement, using the same offsets as HTML inputs. */
|
|
275
|
+
export function replaceSelection(value, start, end, text) {
|
|
276
|
+
if (typeof value !== 'string' || typeof text !== 'string') throw new TypeError('value and text must be strings');
|
|
277
|
+
const [a, b] = selection(value, start, end);
|
|
278
|
+
return { value: value.slice(0, a) + text + value.slice(b), start: a + text.length, end: a + text.length };
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** Remove a selected range or the preceding complete grapheme. */
|
|
282
|
+
export function deleteSelectionBackward(value, start, end) {
|
|
283
|
+
if (typeof value !== 'string') throw new TypeError('value must be a string');
|
|
284
|
+
const [a, b] = selection(value, start, end);
|
|
285
|
+
if (a !== b) return replaceSelection(value, a, b, '');
|
|
286
|
+
if (!a) return { value, start: 0, end: 0 };
|
|
287
|
+
let previous = 0;
|
|
288
|
+
if (typeof Intl.Segmenter === 'function') {
|
|
289
|
+
for (const part of new Intl.Segmenter('ug', { granularity: 'grapheme' }).segment(value)) {
|
|
290
|
+
if (part.index >= a) break;
|
|
291
|
+
previous = part.index;
|
|
292
|
+
}
|
|
293
|
+
} else {
|
|
294
|
+
previous = a - 1;
|
|
295
|
+
const code = value.charCodeAt(previous);
|
|
296
|
+
if (previous && code >= 0xdc00 && code <= 0xdfff) previous--;
|
|
297
|
+
}
|
|
298
|
+
return replaceSelection(value, previous, a, '');
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function makeEvent(target, name, type, text, cancelable) {
|
|
302
|
+
const view = target.ownerDocument?.defaultView || globalThis;
|
|
303
|
+
if (typeof view.InputEvent === 'function') {
|
|
304
|
+
return new view.InputEvent(name, { bubbles: true, composed: true, cancelable, inputType: type, data: text });
|
|
305
|
+
}
|
|
306
|
+
const event = new view.Event(name, { bubbles: true, composed: true, cancelable });
|
|
307
|
+
Object.defineProperties(event, { inputType: { value: type }, data: { value: text } });
|
|
308
|
+
return event;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Mount a virtual keyboard next to a text input/textarea, or in options.container. */
|
|
312
|
+
export function createKeyboard(target, options = {}) {
|
|
313
|
+
if (!target || typeof target.value !== 'string' || typeof target.setSelectionRange !== 'function' || !target.ownerDocument) {
|
|
314
|
+
throw new TypeError('createKeyboard needs a text input or textarea');
|
|
315
|
+
}
|
|
316
|
+
if (target.tagName === 'INPUT' && !['text', 'search', 'tel', 'url', 'password'].includes(target.type || 'text')) {
|
|
317
|
+
throw new TypeError('The input type must support text selection');
|
|
318
|
+
}
|
|
319
|
+
const layouts = new Map(Object.entries(KEYBOARD_LAYOUTS));
|
|
320
|
+
for (const custom of options.layouts || []) {
|
|
321
|
+
if (!custom.id || layouts.has(custom.id) || !custom.rows?.some((row) => row.length) ||
|
|
322
|
+
!['rtl', 'ltr'].includes(custom.direction)) {
|
|
323
|
+
throw new TypeError('Custom layouts need a unique id, at least one key, and a valid direction');
|
|
324
|
+
}
|
|
325
|
+
layouts.set(custom.id, custom);
|
|
326
|
+
}
|
|
327
|
+
let layout = layouts.get(options.layout ?? 'phonetic');
|
|
328
|
+
if (!layout) throw new TypeError('Unknown keyboard layout: ' + options.layout);
|
|
329
|
+
const direction = options.direction;
|
|
330
|
+
if (direction && !['rtl', 'ltr', 'auto', 'preserve'].includes(direction)) {
|
|
331
|
+
throw new TypeError('direction must be rtl, ltr, auto, or preserve');
|
|
332
|
+
}
|
|
333
|
+
const originalDirection = target.getAttribute('dir');
|
|
334
|
+
let appliedDirection;
|
|
335
|
+
const doc = target.ownerDocument;
|
|
336
|
+
const locale = options.locale === 'en' ? 'en' : 'ug';
|
|
337
|
+
const labels = keyboardLabels[locale];
|
|
338
|
+
const root = doc.createElement('div');
|
|
339
|
+
root.className = 'ug-keyboard alkatip-basma';
|
|
340
|
+
root.lang = locale;
|
|
341
|
+
root.dir = locale === 'ug' ? 'rtl' : 'ltr';
|
|
342
|
+
root.setAttribute('role', 'group');
|
|
343
|
+
root.setAttribute('aria-label', options.label || labels.keyboard);
|
|
344
|
+
const style = doc.createElement('style');
|
|
345
|
+
style.textContent = keyboardStyles;
|
|
346
|
+
root.appendChild(style);
|
|
347
|
+
let physical = options.physical === true;
|
|
348
|
+
let autoHamza = options.autoHamza === true;
|
|
349
|
+
let secondary = options.secondaryKeys === true;
|
|
350
|
+
let destroyed = false;
|
|
351
|
+
let composing = false;
|
|
352
|
+
let shifted = false;
|
|
353
|
+
let buttonRows = [];
|
|
354
|
+
let activeButton;
|
|
355
|
+
|
|
356
|
+
const layoutSelect = doc.createElement('select');
|
|
357
|
+
layoutSelect.className = 'ug-keyboard__layout';
|
|
358
|
+
layoutSelect.setAttribute('aria-label', labels.layout);
|
|
359
|
+
for (const [id, item] of layouts) {
|
|
360
|
+
const option = doc.createElement('option');
|
|
361
|
+
option.value = id;
|
|
362
|
+
option.textContent = item.labels[locale] || item.labels.en;
|
|
363
|
+
layoutSelect.appendChild(option);
|
|
364
|
+
}
|
|
365
|
+
layoutSelect.value = layout.id;
|
|
366
|
+
root.appendChild(layoutSelect);
|
|
367
|
+
const keys = doc.createElement('div');
|
|
368
|
+
keys.className = 'ug-keyboard__keys';
|
|
369
|
+
root.appendChild(keys);
|
|
370
|
+
|
|
371
|
+
function setTargetDirection() {
|
|
372
|
+
const nextDirection = direction || layout.direction;
|
|
373
|
+
if (nextDirection === 'preserve') return;
|
|
374
|
+
if (appliedDirection !== undefined && target.getAttribute('dir') !== appliedDirection) return;
|
|
375
|
+
target.setAttribute('dir', nextDirection);
|
|
376
|
+
appliedDirection = nextDirection;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function keyButton(text, action) {
|
|
380
|
+
const button = doc.createElement('button');
|
|
381
|
+
button.type = 'button';
|
|
382
|
+
button.className = 'ug-keyboard__key';
|
|
383
|
+
button.textContent = text;
|
|
384
|
+
button.tabIndex = -1;
|
|
385
|
+
if (action) button.dataset.action = action;
|
|
386
|
+
else button.dataset.text = text;
|
|
387
|
+
const localized = labels[action] || labels.punctuation[text];
|
|
388
|
+
button.lang = localized ? locale : layout.lang;
|
|
389
|
+
button.setAttribute('aria-label', localized || text);
|
|
390
|
+
if (action === 'shift') button.setAttribute('aria-pressed', String(shifted));
|
|
391
|
+
return button;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function focusedElement() {
|
|
395
|
+
return root.getRootNode?.().activeElement || doc.activeElement;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function setActiveButton(button) {
|
|
399
|
+
if (activeButton) activeButton.tabIndex = -1;
|
|
400
|
+
activeButton = button;
|
|
401
|
+
activeButton.tabIndex = 0;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function renderKeys() {
|
|
405
|
+
const index = buttonRows.flat().indexOf(activeButton);
|
|
406
|
+
const restoreFocus = keys.contains(focusedElement());
|
|
407
|
+
keys.dir = layout.keyDirection || layout.direction;
|
|
408
|
+
const rows = (shifted ? layout.shiftedRows : layout.rows).map((row) => row.map((text) => keyButton(text)));
|
|
409
|
+
if (secondary && layout.secondary?.length) rows.push(layout.secondary.map((text) => keyButton(text)));
|
|
410
|
+
const actions = [keyButton(labels.space, 'space'), keyButton('⌫ ' + (locale === 'ug' ? 'ئۆچۈرۈش' : 'Delete'), 'backspace')];
|
|
411
|
+
if (layout.shiftedRows) actions.unshift(keyButton('⇧', 'shift'));
|
|
412
|
+
rows.push(actions);
|
|
413
|
+
buttonRows = rows;
|
|
414
|
+
keys.replaceChildren();
|
|
415
|
+
for (const buttons of rows) {
|
|
416
|
+
const row = doc.createElement('div');
|
|
417
|
+
row.className = 'ug-keyboard__row';
|
|
418
|
+
for (const button of buttons) row.appendChild(button);
|
|
419
|
+
keys.appendChild(row);
|
|
420
|
+
}
|
|
421
|
+
const buttons = rows.flat();
|
|
422
|
+
setActiveButton(buttons[Math.min(Math.max(index, 0), buttons.length - 1)]);
|
|
423
|
+
if (restoreFocus) activeButton.focus();
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function setLayout(id) {
|
|
427
|
+
const next = layouts.get(id);
|
|
428
|
+
if (!next) throw new TypeError('Unknown keyboard layout: ' + id);
|
|
429
|
+
if (destroyed || next === layout) return;
|
|
430
|
+
layout = next;
|
|
431
|
+
shifted = false;
|
|
432
|
+
layoutSelect.value = id;
|
|
433
|
+
setTargetDirection();
|
|
434
|
+
renderKeys();
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function setTheme(theme) {
|
|
438
|
+
if (!['light', 'dark'].includes(theme)) throw new TypeError('theme must be light or dark');
|
|
439
|
+
root.dataset.theme = theme;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function setCompact(enabled) {
|
|
443
|
+
root.dataset.compact = String(Boolean(enabled));
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
setTheme(options.theme ?? 'light');
|
|
447
|
+
setCompact(options.compact);
|
|
448
|
+
renderKeys();
|
|
449
|
+
|
|
450
|
+
if (options.container) options.container.appendChild(root);
|
|
451
|
+
else if (target.parentNode) target.parentNode.insertBefore(root, target.nextSibling);
|
|
452
|
+
else throw new Error('Attach the target to a document or provide a container');
|
|
453
|
+
// Font faces must be registered in the owner document, including when keys
|
|
454
|
+
// themselves live in a Web Component's shadow root.
|
|
455
|
+
const releaseFont = acquireFont(doc);
|
|
456
|
+
setTargetDirection();
|
|
457
|
+
|
|
458
|
+
function commit(result, inputType, text, focusTarget = true) {
|
|
459
|
+
if (destroyed || target.disabled || target.readOnly || composing) return false;
|
|
460
|
+
if (!target.dispatchEvent(makeEvent(target, 'beforeinput', inputType, text, true))) return false;
|
|
461
|
+
target.value = result.value;
|
|
462
|
+
if (focusTarget) target.focus({ preventScroll: true });
|
|
463
|
+
target.setSelectionRange(result.start, result.end);
|
|
464
|
+
target.dispatchEvent(makeEvent(target, 'input', inputType, text, false));
|
|
465
|
+
const view = doc.defaultView || globalThis;
|
|
466
|
+
if (typeof view.CustomEvent === 'function') {
|
|
467
|
+
root.dispatchEvent(new view.CustomEvent('uyghur-input', { bubbles: true, composed: true, detail: { value: target.value, inputType, text } }));
|
|
468
|
+
}
|
|
469
|
+
return true;
|
|
470
|
+
}
|
|
471
|
+
function insert(text) {
|
|
472
|
+
if (typeof text !== 'string') throw new TypeError('insert expects a string');
|
|
473
|
+
return insertText(text, true);
|
|
474
|
+
}
|
|
475
|
+
function insertText(text, focusTarget) {
|
|
476
|
+
const result = replaceSelection(target.value, target.selectionStart, target.selectionEnd, text);
|
|
477
|
+
return commit(result, 'insertText', text, focusTarget);
|
|
478
|
+
}
|
|
479
|
+
function deleteBackward(focusTarget) {
|
|
480
|
+
const result = deleteSelectionBackward(target.value, target.selectionStart, target.selectionEnd);
|
|
481
|
+
return commit(result, 'deleteContentBackward', null, focusTarget);
|
|
482
|
+
}
|
|
483
|
+
function withHamza(text) {
|
|
484
|
+
if (!autoHamza || layout.lang !== 'ug-Arab' || !/^[اەېىوۇۆۈ]$/u.test(text)) return text;
|
|
485
|
+
const [start] = selection(target.value, target.selectionStart, target.selectionEnd);
|
|
486
|
+
const insideWord = /[\p{L}\p{M}\p{N}\p{Pc}\u200c\u200d]$/u.test(target.value.slice(0, start));
|
|
487
|
+
return insideWord ? text : 'ئ' + text;
|
|
488
|
+
}
|
|
489
|
+
function onClick(event) {
|
|
490
|
+
const button = event.target.closest?.('button.ug-keyboard__key');
|
|
491
|
+
if (!button || !keys.contains(button)) return;
|
|
492
|
+
setActiveButton(button);
|
|
493
|
+
const action = button.dataset.action;
|
|
494
|
+
const focusTarget = event.detail !== 0 || focusedElement() !== button;
|
|
495
|
+
if (action === 'shift') {
|
|
496
|
+
shifted = !shifted;
|
|
497
|
+
renderKeys();
|
|
498
|
+
} else if (action === 'backspace') {
|
|
499
|
+
deleteBackward(focusTarget);
|
|
500
|
+
} else {
|
|
501
|
+
insertText(withHamza(action === 'space' ? ' ' : button.dataset.text), focusTarget);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
function onPointerDown(event) {
|
|
505
|
+
// Keep the selection in the target while clicking the virtual keys.
|
|
506
|
+
if (event.target.closest?.('button.ug-keyboard__key')) event.preventDefault();
|
|
507
|
+
}
|
|
508
|
+
function onVirtualKeyDown(event) {
|
|
509
|
+
if (event.altKey || event.ctrlKey || event.metaKey || event.defaultPrevented) return;
|
|
510
|
+
const button = nextKey(buttonRows, event.target, event.key, keys.dir === 'rtl');
|
|
511
|
+
if (!button) return;
|
|
512
|
+
event.preventDefault();
|
|
513
|
+
setActiveButton(button);
|
|
514
|
+
button.focus();
|
|
515
|
+
}
|
|
516
|
+
function onKeyDown(event) {
|
|
517
|
+
if (composing || event.isComposing || event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey) return;
|
|
518
|
+
const map = layout.physicalMap;
|
|
519
|
+
const mapped = physical && map && Object.hasOwn(map, event.key) ? map[event.key] : undefined;
|
|
520
|
+
const text = withHamza(mapped ?? event.key);
|
|
521
|
+
if (mapped !== undefined || text !== event.key) {
|
|
522
|
+
event.preventDefault();
|
|
523
|
+
insertText(text, true);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
const onLayoutChange = () => setLayout(layoutSelect.value);
|
|
527
|
+
const beginComposition = () => { composing = true; };
|
|
528
|
+
const endComposition = () => { composing = false; };
|
|
529
|
+
root.addEventListener('click', onClick);
|
|
530
|
+
root.addEventListener('pointerdown', onPointerDown);
|
|
531
|
+
keys.addEventListener('keydown', onVirtualKeyDown);
|
|
532
|
+
layoutSelect.addEventListener('change', onLayoutChange);
|
|
533
|
+
target.addEventListener('keydown', onKeyDown);
|
|
534
|
+
target.addEventListener('compositionstart', beginComposition);
|
|
535
|
+
target.addEventListener('compositionend', endComposition);
|
|
536
|
+
|
|
537
|
+
return {
|
|
538
|
+
element: root,
|
|
539
|
+
get layout() { return layout.id; },
|
|
540
|
+
insert,
|
|
541
|
+
backspace() { return deleteBackward(true); },
|
|
542
|
+
setLayout,
|
|
543
|
+
setTheme,
|
|
544
|
+
setCompact,
|
|
545
|
+
setAutoHamza(enabled) { autoHamza = Boolean(enabled); },
|
|
546
|
+
setSecondaryKeys(enabled) {
|
|
547
|
+
if (destroyed) return;
|
|
548
|
+
secondary = Boolean(enabled);
|
|
549
|
+
renderKeys();
|
|
550
|
+
},
|
|
551
|
+
setPhysicalMapping(enabled) { physical = Boolean(enabled); },
|
|
552
|
+
setVisible(visible) { root.hidden = !visible; },
|
|
553
|
+
destroy() {
|
|
554
|
+
if (destroyed) return;
|
|
555
|
+
destroyed = true;
|
|
556
|
+
root.removeEventListener('click', onClick);
|
|
557
|
+
root.removeEventListener('pointerdown', onPointerDown);
|
|
558
|
+
keys.removeEventListener('keydown', onVirtualKeyDown);
|
|
559
|
+
layoutSelect.removeEventListener('change', onLayoutChange);
|
|
560
|
+
target.removeEventListener('keydown', onKeyDown);
|
|
561
|
+
target.removeEventListener('compositionstart', beginComposition);
|
|
562
|
+
target.removeEventListener('compositionend', endComposition);
|
|
563
|
+
root.remove();
|
|
564
|
+
releaseFont();
|
|
565
|
+
if (appliedDirection !== undefined && target.getAttribute('dir') === appliedDirection) {
|
|
566
|
+
if (originalDirection === null) target.removeAttribute('dir');
|
|
567
|
+
else target.setAttribute('dir', originalDirection);
|
|
568
|
+
}
|
|
569
|
+
},
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/** Define the optional Web Component. Safe to import the package in Node/SSR. */
|
|
574
|
+
export function defineUyghurKeyboard(name = 'uyghur-keyboard', registry = globalThis.customElements) {
|
|
575
|
+
if (!registry || typeof globalThis.HTMLElement !== 'function') return null;
|
|
576
|
+
if (registry.get(name)) return registry.get(name);
|
|
577
|
+
class UyghurKeyboardElement extends HTMLElement {
|
|
578
|
+
static get observedAttributes() {
|
|
579
|
+
return ['for', 'physical', 'layout', 'auto-hamza', 'secondary-keys', 'compact', 'theme', 'lang'];
|
|
580
|
+
}
|
|
581
|
+
connectedCallback() {
|
|
582
|
+
if (!this.shadowRoot) this.attachShadow({ mode: 'open' });
|
|
583
|
+
this.mountKeyboard();
|
|
584
|
+
this.readyListener = () => this.mountKeyboard();
|
|
585
|
+
if (this.ownerDocument.readyState === 'loading') this.ownerDocument.addEventListener('DOMContentLoaded', this.readyListener, { once: true });
|
|
586
|
+
}
|
|
587
|
+
attributeChangedCallback(name) {
|
|
588
|
+
if (!this.isConnected) return;
|
|
589
|
+
if (!this.controller || name === 'for' || name === 'lang') {
|
|
590
|
+
this.mountKeyboard();
|
|
591
|
+
} else if (name === 'layout') {
|
|
592
|
+
this.controller.setLayout(this.requestedLayout);
|
|
593
|
+
} else if (name === 'theme') {
|
|
594
|
+
this.controller.setTheme(this.getAttribute('theme') === 'dark' ? 'dark' : 'light');
|
|
595
|
+
} else {
|
|
596
|
+
const setters = {
|
|
597
|
+
physical: 'setPhysicalMapping', 'auto-hamza': 'setAutoHamza',
|
|
598
|
+
'secondary-keys': 'setSecondaryKeys', compact: 'setCompact',
|
|
599
|
+
};
|
|
600
|
+
this.controller[setters[name]](this.hasAttribute(name));
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
disconnectedCallback() {
|
|
604
|
+
this.controller?.destroy();
|
|
605
|
+
this.controller = null;
|
|
606
|
+
if (this.readyListener) this.ownerDocument.removeEventListener('DOMContentLoaded', this.readyListener);
|
|
607
|
+
}
|
|
608
|
+
mountKeyboard() {
|
|
609
|
+
this.controller?.destroy();
|
|
610
|
+
this.controller = null;
|
|
611
|
+
this.shadowRoot?.replaceChildren();
|
|
612
|
+
const hostStyle = this.ownerDocument.createElement('style');
|
|
613
|
+
hostStyle.textContent = ':host { display: block; }';
|
|
614
|
+
this.shadowRoot?.appendChild(hostStyle);
|
|
615
|
+
const target = this.ownerDocument.getElementById(this.getAttribute('for') || '');
|
|
616
|
+
if (!target) return;
|
|
617
|
+
this.controller = createKeyboard(target, {
|
|
618
|
+
container: this.shadowRoot,
|
|
619
|
+
layout: this.requestedLayout,
|
|
620
|
+
physical: this.hasAttribute('physical'),
|
|
621
|
+
autoHamza: this.hasAttribute('auto-hamza'),
|
|
622
|
+
secondaryKeys: this.hasAttribute('secondary-keys'),
|
|
623
|
+
compact: this.hasAttribute('compact'),
|
|
624
|
+
theme: this.getAttribute('theme') === 'dark' ? 'dark' : 'light',
|
|
625
|
+
locale: this.getAttribute('lang')?.split('-')[0] === 'en' ? 'en' : 'ug',
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
get requestedLayout() {
|
|
629
|
+
const id = this.getAttribute('layout');
|
|
630
|
+
return Object.hasOwn(KEYBOARD_LAYOUTS, id) ? id : 'phonetic';
|
|
631
|
+
}
|
|
632
|
+
get layout() { return this.controller?.layout ?? this.requestedLayout; }
|
|
633
|
+
set layout(id) {
|
|
634
|
+
if (!Object.hasOwn(KEYBOARD_LAYOUTS, id)) throw new TypeError('Unknown keyboard layout: ' + id);
|
|
635
|
+
this.setAttribute('layout', id);
|
|
636
|
+
}
|
|
637
|
+
insert(text) { return this.controller?.insert(text) || false; }
|
|
638
|
+
backspace() { return this.controller?.backspace() || false; }
|
|
639
|
+
}
|
|
640
|
+
registry.define(name, UyghurKeyboardElement);
|
|
641
|
+
return UyghurKeyboardElement;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
defineUyghurKeyboard();
|
package/docs/layouts.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Keyboard layouts
|
|
2
|
+
|
|
3
|
+
| ID | Virtual keyboard | Optional physical mapping |
|
|
4
|
+
| --- | --- | --- |
|
|
5
|
+
| `phonetic` | The original Arabic-script rows and explicit hamza-vowel keys | The original `PHONETIC_MAP` |
|
|
6
|
+
| `uey-desktop` | Arabic-script arrangement in ANSI desktop key order, with a Shift layer | QWERTY key values mapped to Arabic-script characters |
|
|
7
|
+
| `uly` | Latin letters, including `ë`, `ö`, and `ü`, and an apostrophe | The browser or operating system provides the physical input |
|
|
8
|
+
| `usy` | Cyrillic letters, including the Uyghur-specific keys, with space for letters used in loanwords | The browser or operating system provides the physical input |
|
|
9
|
+
|
|
10
|
+
The `uey-desktop` implementation uses the base and Shift positions of the printable ANSI rows, including the number and punctuation rows. It leaves operating-system modifiers, Caps Lock, AltGr, the numeric keypad and shortcuts to the host browser. The physical mapping reads `KeyboardEvent.key` from a US QWERTY input source. The virtual Shift button stays active until pressed again or the user switches layouts. Sources for the key positions are [Microsoft Learn's Uyghur keyboard, KLID 00010480](https://learn.microsoft.com/en-us/globalization/keyboards/kbdughr1) and [the XKB `cn(ug)` layout](https://github.com/GalliumOS/xkeyboard-config/blob/master/symbols/cn). Where the sources differ in shifted punctuation, the desktop layout follows the Microsoft reference.
|
|
11
|
+
|
|
12
|
+
The Latin and Cyrillic layouts are original virtual arrangements. Their letter inventories and key placement, the desktop character assignments, the optional secondary keys and their order, the automatic hamza behavior, the new Uyghur accessibility and layout labels, and the new test examples need native-speaker review. The three new built-in layouts carry `reviewStatus: 'draft'`.
|
|
13
|
+
|
|
14
|
+
## Adding a layout
|
|
15
|
+
|
|
16
|
+
Pass a plain data object through `layouts` and set its ID through `layout` or `keyboard.setLayout(id)`. Custom layouts appear alongside built-in layouts in the selector.
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
const keyboard = createKeyboard(document.querySelector('textarea'), {
|
|
20
|
+
locale: 'en',
|
|
21
|
+
layout: 'symbols',
|
|
22
|
+
layouts: [{
|
|
23
|
+
id: 'symbols',
|
|
24
|
+
labels: { en: 'Symbols' },
|
|
25
|
+
lang: 'en',
|
|
26
|
+
direction: 'ltr',
|
|
27
|
+
rows: [['+', '-', '×', '÷']],
|
|
28
|
+
physicalMap: { '*': '×', '/': '÷' },
|
|
29
|
+
}],
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
- Give each layout a unique `id` and a label in English. An Uyghur label can be supplied as `labels.ug`.
|
|
34
|
+
- `lang` is the BCP 47 language or language-script tag for the keys. The text direction follows `direction`, unless the embedder supplies a `direction` option. `keyDirection` optionally overrides the visual key order.
|
|
35
|
+
- `rows` and optional `shiftedRows` contain button text. A button inserts the full string. A `shiftedRows` table adds a virtual Shift control.
|
|
36
|
+
- `physicalMap` maps exact key values to output strings when `physical` is enabled. The optional `secondary` row is displayed when `secondaryKeys` is enabled.
|
|
37
|
+
- Set `reviewStatus: 'draft'` on new Uyghur layouts that still need native-speaker review and document the source of the data.
|
|
38
|
+
|
|
39
|
+
## Keyboard and touch behavior
|
|
40
|
+
|
|
41
|
+
Tab moves to the layout selector and then the active key. The arrow keys move within and between visible rows. Home and End go to the first and last keys in the current row. Enter and Space activate a key and retain focus on it. Tab and Shift+Tab can leave the key group. Pointer activation returns focus to the editing target so typing can continue there.
|
|
42
|
+
|
|
43
|
+
Keys wrap in narrow containers and have a minimum width and height of 44 CSS pixels in both standard and compact mode. Controls use a minimum text size of 16 CSS pixels. For iOS Safari, set your own input or textarea font size to at least 16 CSS pixels too, as in the [README examples](../README.md#embed-with-one-script). Use a normal responsive viewport and leave pinch zoom available.
|