@uyghurcommunity/keyboard 0.2.0 → 0.3.1

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 CHANGED
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.1
4
+
5
+ - Uyghur wording corrections from editorial review:
6
+ - The phonetic layout is now labelled تاۋۇش بويىچە.
7
+ - Screen readers hear Shift كۇنۇپكىسى for the Shift key, and سول قوش تىرناق / ئوڭ قوش تىرناق for « and ».
8
+ - Demo page: clearer intro, key-legend guide and review note. Options renamed to كۇنۇپكا بەلگىلىرى and كۆرۈنۈش.
9
+
10
+ ## 0.3.0
11
+
12
+ - **The standard Uyghur computer layout (`uey-desktop`) is now the default**, for `createKeyboard()` and `<uyghur-keyboard>`. It is the layout built into Windows, macOS and Linux and taught by typing trainers, so the on-screen keys match a real Uyghur keyboard. It is listed first and labelled "Standard Uyghur (UEY)".
13
+ - To keep the previous behaviour, pass `layout: 'phonetic'` (or `layout="phonetic"` on the element).
14
+ - Export `DEFAULT_LAYOUT`. A new test pins every letter of the standard layout, base and Shift, to its key.
15
+ - **Key legends:** on the standard layout, each key shows its Shift letter (D: د and ژ) and its Latin key cap, like a printed Uyghur keyboard. Turn them off with `legends: false` or `setLegends(false)`.
16
+ - **Pressed keys light up:** pressing a key on a real keyboard highlights the matching on-screen key, by key position (`KeyboardEvent.code`) whatever layout the operating system uses, so the keyboard doubles as a layout guide.
17
+ - Shift and Delete show Uyghur UI icons instead of the ⇧ and ⌫ characters.
18
+ - Demo page: options and notes in Uyghur, a layout guide, an install command with a copy button, and Uyghur UI icons instead of arrow characters.
19
+
3
20
  ## 0.2.0
4
21
 
5
22
  - Add data-driven layouts with a layout selector and a controller API. Keep the original phonetic layout and exported mappings as the default. Add Arabic desktop (UEY), Latin (ULY) and Cyrillic (USY) layouts.
package/README.md CHANGED
@@ -16,7 +16,13 @@ A dependency-free virtual keyboard and Web Component for Uyghur Arabic, Latin an
16
16
 
17
17
  ## Package and source
18
18
 
19
- Package name **`@uyghurcommunity/keyboard`** · Version **0.2.0**. To build a local installable package, run:
19
+ Package name **`@uyghurcommunity/keyboard`** · Version **0.3.1**. Install it from npm:
20
+
21
+ ```sh
22
+ npm install @uyghurcommunity/keyboard
23
+ ```
24
+
25
+ To work on the source, run:
20
26
 
21
27
  ```sh
22
28
  npm test
@@ -82,7 +88,7 @@ import { createKeyboard } from '@uyghurcommunity/keyboard';
82
88
  const input = document.querySelector('textarea');
83
89
  const keyboard = createKeyboard(input, {
84
90
  physical: false,
85
- layout: 'phonetic',
91
+ layout: 'uey-desktop', // the default: the standard Uyghur computer layout
86
92
  direction: 'rtl',
87
93
  // container: document.querySelector('#keyboard'),
88
94
  });
@@ -90,7 +96,7 @@ const keyboard = createKeyboard(input, {
90
96
  keyboard.insert('سالام');
91
97
  keyboard.backspace();
92
98
  keyboard.setPhysicalMapping(true);
93
- keyboard.setLayout('uey-desktop');
99
+ keyboard.setLayout('phonetic');
94
100
  keyboard.setTheme('dark');
95
101
  keyboard.setVisible(false);
96
102
  keyboard.destroy();
@@ -104,11 +110,12 @@ Supported targets: `textarea` and text-selection-capable inputs (`text`, `search
104
110
 
105
111
  | Option | Default | Behavior |
106
112
  | --- | --- | --- |
107
- | `layout` | `phonetic` | Choose `phonetic`, `uey-desktop`, `uly`, `usy`, or a supplied custom layout ID. |
113
+ | `layout` | `uey-desktop` | The standard Uyghur computer layout (as on Windows, macOS and Linux). Or choose `phonetic`, `uly`, `usy`, or a supplied custom layout ID. |
108
114
  | `layouts` | — | Add plain data layouts to the built-ins. |
109
115
  | `physical` | `false` | Apply the active layout's physical map when it has one. |
110
116
  | `autoHamza` | `false` | Insert `ئ` before a single Arabic-script vowel typed at the beginning of a word. |
111
- | `secondaryKeys` | `false` | Show supplemental keys such as Arabic punctuation. |
117
+ | `secondaryKeys` | `false` | Show supplemental keys such as Uyghur punctuation (، ؛ ؟ « »). |
118
+ | `legends` | `true` | On the standard layout, show each key's Shift letter and Latin key cap. Pressing a real key always lights up its on-screen key. |
112
119
  | `compact` | `false` | Reduce padding and gaps while keeping 44 × 44 CSS-pixel minimum touch targets. |
113
120
  | `theme` | `light` | Choose `light` or `dark`. |
114
121
  | `locale` | `ug` | Use Uyghur or English (`en`) names for controls. |
@@ -137,7 +144,7 @@ Calling `defineUyghurKeyboard()` from the main module registers the component ma
137
144
 
138
145
  ## Phonetic map
139
146
 
140
- This optional convenience map applies to the default layout. Virtual keys provide all Uyghur letters, and uppercase physical mappings provide common additional forms.
147
+ This optional convenience map applies to the `phonetic` layout (`layout: 'phonetic'`). The default layout, `uey-desktop`, uses the standard Uyghur computer positions instead (A ھ, S س, D د, F ا …). Virtual keys provide all Uyghur letters, and uppercase physical mappings provide common additional forms.
141
148
 
142
149
  ```text
143
150
  Lowercase:
@@ -71,7 +71,18 @@ ${alkatipHelperStyles}
71
71
  outline-offset:1px;
72
72
  z-index:1;
73
73
  }
74
- .ug-keyboard__key[data-action] { font-size:16px; }
74
+ .ug-keyboard__key[data-action] { font-size:16px; gap:8px; }
75
+ .ug-keyboard__key { position:relative; }
76
+ /* Shift letter (top) and Latin key cap (bottom), as printed on a Uyghur keyboard. */
77
+ .ug-keyboard__shift-legend { position:absolute; top:1px; left:6px; font-size:13px; line-height:1.3; opacity:.72; pointer-events:none; }
78
+ .ug-keyboard__cap { position:absolute; bottom:2px; right:6px; font:10px/1 ui-monospace,SFMono-Regular,Menlo,Consolas,monospace; opacity:.45; pointer-events:none; }
79
+ /* A real key press lights up its virtual key. */
80
+ .ug-keyboard__key[data-pressed="true"] { background:var(--ug-focus,var(--_focus)); color:var(--ug-key,var(--_key)); border-color:var(--ug-focus,var(--_focus)); transform:translateY(1px); }
81
+ /* Shift and Delete icons are Uyghur UI drawings (arrow-up, arrow-left), drawn as masks so they follow the text colour. */
82
+ .ug-keyboard__icon { display:inline-block; width:18px; height:18px; flex:none; background-color:currentColor; -webkit-mask:center/contain no-repeat; mask:center/contain no-repeat; }
83
+ .ug-keyboard__icon--shift { -webkit-mask-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 19V5m-6 6 6-6 6 6'/%3E%3C/svg%3E"); mask-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 19V5m-6 6 6-6 6 6'/%3E%3C/svg%3E"); }
84
+ .ug-keyboard__icon--backspace { -webkit-mask-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M19 12H5m6-6-6 6 6 6'/%3E%3C/svg%3E"); mask-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M19 12H5m6-6-6 6 6 6'/%3E%3C/svg%3E"); }
85
+ .ug-keyboard[data-compact="true"] .ug-keyboard__cap { display:none; }
75
86
  .ug-keyboard__key[data-action="space"] { flex-grow:4; }
76
87
  .ug-keyboard[data-compact="true"] { padding:6px; }
77
88
  .ug-keyboard[data-compact="true"] .ug-keyboard__row { gap:3px; margin-block:3px; }
@@ -80,6 +91,9 @@ ${alkatipHelperStyles}
80
91
  @media (max-width:420px) {
81
92
  .ug-keyboard { padding:6px; }
82
93
  .ug-keyboard__row { gap:4px; }
94
+ .ug-keyboard__cap { display:none; }
95
+ .ug-keyboard__shift-legend { left:3px; font-size:11px; }
96
+ .ug-keyboard__key[data-action="backspace"] { font-size:13px; gap:4px; }
83
97
  }
84
98
  `;
85
99
 
@@ -119,6 +133,16 @@ const desktopShiftedRows = [
119
133
  ];
120
134
  const desktopKeys = [..."`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./"];
121
135
  const desktopShiftKeys = [...'~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?'];
136
+ // Physical key positions, row by row, as KeyboardEvent.code values, so a real key
137
+ // press can light up the matching virtual key whatever the operating-system layout.
138
+ const desktopCodes = [
139
+ ['Backquote', 'Digit1', 'Digit2', 'Digit3', 'Digit4', 'Digit5', 'Digit6', 'Digit7', 'Digit8', 'Digit9', 'Digit0', 'Minus', 'Equal'],
140
+ [...'QWERTYUIOP'].map((k) => 'Key' + k).concat(['BracketLeft', 'BracketRight', 'Backslash']),
141
+ [...'ASDFGHJKL'].map((k) => 'Key' + k).concat(['Semicolon', 'Quote']),
142
+ [...'ZXCVBNM'].map((k) => 'Key' + k).concat(['Comma', 'Period', 'Slash']),
143
+ ];
144
+ // The Latin key cap printed on each key, as on a Uyghur keyboard sticker.
145
+ const desktopCaps = desktopCodes.map((row) => row.map((code) => (code.startsWith('Key') ? code.slice(3) : '')));
122
146
  const desktopMap = {};
123
147
  for (const [keys, values] of [[desktopKeys, desktopRows.flat()], [desktopShiftKeys, desktopShiftedRows.flat()]]) {
124
148
  keys.forEach((key, index) => { desktopMap[key] = values[index]; });
@@ -141,7 +165,7 @@ function shiftRows(rows) {
141
165
  }
142
166
 
143
167
  function freezeLayout(layout) {
144
- for (const rows of [layout.rows, layout.shiftedRows]) {
168
+ for (const rows of [layout.rows, layout.shiftedRows, layout.keyCodes, layout.keyCaps]) {
145
169
  if (!rows) continue;
146
170
  rows.forEach(Object.freeze);
147
171
  Object.freeze(rows);
@@ -152,15 +176,23 @@ function freezeLayout(layout) {
152
176
  return Object.freeze(layout);
153
177
  }
154
178
 
179
+ /**
180
+ * The standard Uyghur computer layout is the default: the one built into Windows
181
+ * (KLID 00010480), Linux (XKB cn(ug)) and macOS, and taught by typing trainers
182
+ * such as Konupka. Its letters are checked key by key in the tests.
183
+ */
184
+ export const DEFAULT_LAYOUT = 'uey-desktop';
185
+
155
186
  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
187
  'uey-desktop': freezeLayout({
161
- id: 'uey-desktop', labels: { en: 'Arabic desktop (UEY)', ug: 'ئەرەب يېزىقى (UEY)' }, lang: 'ug-Arab',
188
+ id: 'uey-desktop', labels: { en: 'Standard Uyghur (UEY)', ug: 'ئۆلچەملىك ئۇيغۇرچە (UEY)' }, lang: 'ug-Arab',
162
189
  direction: 'rtl', keyDirection: 'ltr', rows: desktopRows, shiftedRows: desktopShiftedRows,
163
190
  physicalMap: desktopMap, secondary: arabicExtras, reviewStatus: 'draft',
191
+ keyCodes: desktopCodes, keyCaps: desktopCaps,
192
+ }),
193
+ phonetic: freezeLayout({
194
+ id: 'phonetic', labels: { en: 'Phonetic', ug: 'تاۋۇش بويىچە' }, lang: 'ug-Arab',
195
+ direction: 'rtl', rows: KEY_LAYOUT, physicalMap: PHONETIC_MAP, secondary: arabicExtras,
164
196
  }),
165
197
  uly: freezeLayout({
166
198
  id: 'uly', labels: { en: 'Latin (ULY)', ug: 'لاتىن يېزىقى (ULY)' }, lang: 'ug-Latn',
@@ -182,8 +214,8 @@ const keyboardLabels = {
182
214
  },
183
215
  ug: {
184
216
  keyboard: 'ئۇيغۇرچە كۇنۇپكا تاختىسى', layout: 'كۇنۇپكا تاختىسىنىڭ ئورۇنلاشتۇرۇلۇشى',
185
- space: 'بوشلۇق', backspace: 'ئالدىنقى ھەرپنى ئۆچۈرۈش', shift: 'چوڭ ھەرپ كۇنۇپكىسى',
186
- punctuation: { '،': 'پەش', '؛': 'چېكىتلىك پەش', '؟': 'سوئال بەلگىسى', '«': 'سول تىرناق', '»': 'ئوڭ تىرناق' },
217
+ space: 'بوشلۇق', backspace: 'ئالدىنقى ھەرپنى ئۆچۈرۈش', shift: 'Shift كۇنۇپكىسى',
218
+ punctuation: { '،': 'پەش', '؛': 'چېكىتلىك پەش', '؟': 'سوئال بەلگىسى', '«': 'سول قوش تىرناق', '»': 'ئوڭ قوش تىرناق' },
187
219
  },
188
220
  };
189
221
 
@@ -324,7 +356,7 @@ export function createKeyboard(target, options = {}) {
324
356
  }
325
357
  layouts.set(custom.id, custom);
326
358
  }
327
- let layout = layouts.get(options.layout ?? 'phonetic');
359
+ let layout = layouts.get(options.layout ?? DEFAULT_LAYOUT);
328
360
  if (!layout) throw new TypeError('Unknown keyboard layout: ' + options.layout);
329
361
  const direction = options.direction;
330
362
  if (direction && !['rtl', 'ltr', 'auto', 'preserve'].includes(direction)) {
@@ -347,6 +379,9 @@ export function createKeyboard(target, options = {}) {
347
379
  let physical = options.physical === true;
348
380
  let autoHamza = options.autoHamza === true;
349
381
  let secondary = options.secondaryKeys === true;
382
+ // Shift letters and Latin key caps on each key, where the layout has them.
383
+ let legends = options.legends !== false;
384
+ const pressTimers = new Map();
350
385
  let destroyed = false;
351
386
  let composing = false;
352
387
  let shifted = false;
@@ -376,7 +411,15 @@ export function createKeyboard(target, options = {}) {
376
411
  appliedDirection = nextDirection;
377
412
  }
378
413
 
379
- function keyButton(text, action) {
414
+ function legend(className, text) {
415
+ const span = doc.createElement('span');
416
+ span.className = className;
417
+ span.textContent = text;
418
+ span.setAttribute('aria-hidden', 'true');
419
+ return span;
420
+ }
421
+
422
+ function keyButton(text, action, { shiftText, cap, code } = {}) {
380
423
  const button = doc.createElement('button');
381
424
  button.type = 'button';
382
425
  button.className = 'ug-keyboard__key';
@@ -384,6 +427,10 @@ export function createKeyboard(target, options = {}) {
384
427
  button.tabIndex = -1;
385
428
  if (action) button.dataset.action = action;
386
429
  else button.dataset.text = text;
430
+ if (code) button.dataset.code = code;
431
+ if (legends && shiftText) button.appendChild(legend('ug-keyboard__shift-legend', shiftText));
432
+ if (legends && cap) button.appendChild(legend('ug-keyboard__cap', cap));
433
+ if (action === 'shift' || action === 'backspace') button.insertBefore(legend(`ug-keyboard__icon ug-keyboard__icon--${action}`, ''), button.firstChild ?? null);
387
434
  const localized = labels[action] || labels.punctuation[text];
388
435
  button.lang = localized ? locale : layout.lang;
389
436
  button.setAttribute('aria-label', localized || text);
@@ -405,10 +452,17 @@ export function createKeyboard(target, options = {}) {
405
452
  const index = buttonRows.flat().indexOf(activeButton);
406
453
  const restoreFocus = keys.contains(focusedElement());
407
454
  keys.dir = layout.keyDirection || layout.direction;
408
- const rows = (shifted ? layout.shiftedRows : layout.rows).map((row) => row.map((text) => keyButton(text)));
455
+ const current = shifted ? layout.shiftedRows : layout.rows;
456
+ const other = shifted ? layout.rows : layout.shiftedRows;
457
+ const rows = current.map((row, r) => row.map((text, k) => keyButton(text, undefined, {
458
+ // Show the Shift letter only where Shift gives a different letter (D: د and ژ).
459
+ shiftText: !shifted && other?.[r]?.[k] && other[r][k] !== text ? other[r][k] : undefined,
460
+ cap: layout.keyCaps?.[r]?.[k],
461
+ code: layout.keyCodes?.[r]?.[k],
462
+ })));
409
463
  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'));
464
+ const actions = [keyButton(labels.space, 'space'), keyButton(locale === 'ug' ? 'ئۆچۈرۈش' : 'Delete', 'backspace')];
465
+ if (layout.shiftedRows) actions.unshift(keyButton('', 'shift'));
412
466
  rows.push(actions);
413
467
  buttonRows = rows;
414
468
  keys.replaceChildren();
@@ -513,8 +567,40 @@ export function createKeyboard(target, options = {}) {
513
567
  setActiveButton(button);
514
568
  button.focus();
515
569
  }
570
+ /** The virtual key a physical key press corresponds to: by position first, then by character. */
571
+ function buttonFor(event) {
572
+ const all = buttonRows.flat();
573
+ if (event.code) {
574
+ const byCode = all.find((button) => button.dataset.code === event.code);
575
+ if (byCode) return byCode;
576
+ }
577
+ if (event.key === ' ') return all.find((button) => button.dataset.action === 'space');
578
+ if (event.key === 'Backspace') return all.find((button) => button.dataset.action === 'backspace');
579
+ if (event.key === 'Shift') return all.find((button) => button.dataset.action === 'shift');
580
+ const map = layout.physicalMap;
581
+ const char = map && Object.hasOwn(map, event.key) ? map[event.key] : event.key;
582
+ return all.find((button) => button.dataset.text === char || button.dataset.text === event.key);
583
+ }
584
+ function release(button) {
585
+ clearTimeout(pressTimers.get(button));
586
+ pressTimers.delete(button);
587
+ if (button.dataset) delete button.dataset.pressed;
588
+ }
589
+ function press(event) {
590
+ const button = buttonFor(event);
591
+ if (!button) return;
592
+ release(button);
593
+ button.dataset.pressed = 'true';
594
+ // Keyboards that never send keyup (some mobile IMEs) still clear the highlight.
595
+ pressTimers.set(button, setTimeout(() => release(button), 600));
596
+ }
597
+ function onKeyUp(event) {
598
+ const button = buttonFor(event);
599
+ if (button) release(button);
600
+ }
516
601
  function onKeyDown(event) {
517
602
  if (composing || event.isComposing || event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey) return;
603
+ press(event);
518
604
  const map = layout.physicalMap;
519
605
  const mapped = physical && map && Object.hasOwn(map, event.key) ? map[event.key] : undefined;
520
606
  const text = withHamza(mapped ?? event.key);
@@ -531,6 +617,7 @@ export function createKeyboard(target, options = {}) {
531
617
  keys.addEventListener('keydown', onVirtualKeyDown);
532
618
  layoutSelect.addEventListener('change', onLayoutChange);
533
619
  target.addEventListener('keydown', onKeyDown);
620
+ target.addEventListener('keyup', onKeyUp);
534
621
  target.addEventListener('compositionstart', beginComposition);
535
622
  target.addEventListener('compositionend', endComposition);
536
623
 
@@ -549,6 +636,11 @@ export function createKeyboard(target, options = {}) {
549
636
  renderKeys();
550
637
  },
551
638
  setPhysicalMapping(enabled) { physical = Boolean(enabled); },
639
+ setLegends(enabled) {
640
+ if (destroyed) return;
641
+ legends = Boolean(enabled);
642
+ renderKeys();
643
+ },
552
644
  setVisible(visible) { root.hidden = !visible; },
553
645
  destroy() {
554
646
  if (destroyed) return;
@@ -558,6 +650,8 @@ export function createKeyboard(target, options = {}) {
558
650
  keys.removeEventListener('keydown', onVirtualKeyDown);
559
651
  layoutSelect.removeEventListener('change', onLayoutChange);
560
652
  target.removeEventListener('keydown', onKeyDown);
653
+ target.removeEventListener('keyup', onKeyUp);
654
+ for (const button of [...pressTimers.keys()]) release(button);
561
655
  target.removeEventListener('compositionstart', beginComposition);
562
656
  target.removeEventListener('compositionend', endComposition);
563
657
  root.remove();
@@ -627,7 +721,7 @@ export function defineUyghurKeyboard(name = 'uyghur-keyboard', registry = global
627
721
  }
628
722
  get requestedLayout() {
629
723
  const id = this.getAttribute('layout');
630
- return Object.hasOwn(KEYBOARD_LAYOUTS, id) ? id : 'phonetic';
724
+ return Object.hasOwn(KEYBOARD_LAYOUTS, id) ? id : DEFAULT_LAYOUT;
631
725
  }
632
726
  get layout() { return this.controller?.layout ?? this.requestedLayout; }
633
727
  set layout(id) {
package/docs/layouts.md CHANGED
@@ -2,12 +2,12 @@
2
2
 
3
3
  | ID | Virtual keyboard | Optional physical mapping |
4
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 |
5
+ | `uey-desktop` (default) | **The standard Uyghur computer layout**, in ANSI desktop key order, with a Shift layer | QWERTY key values mapped to Arabic-script characters |
6
+ | `phonetic` | The original Arabic-script rows and explicit hamza-vowel keys; not an operating-system layout | The original `PHONETIC_MAP` |
7
7
  | `uly` | Latin letters, including `ë`, `ö`, and `ü`, and an apostrophe | The browser or operating system provides the physical input |
8
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
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.
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. Its letters also match the reference sheet used by the Konupka typing trainer key for key, and a test pins every letter position. Since 0.3.0 it is the default layout, so text typed on the virtual keyboard uses the same key positions as a Uyghur keyboard on a computer.
11
11
 
12
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
13
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uyghurcommunity/keyboard",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "Accessible, dependency-free Uyghur virtual keyboard with multiple layouts and optional physical mapping.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/index.d.ts CHANGED
@@ -13,6 +13,10 @@ export interface KeyboardLayout {
13
13
  readonly physicalMap?: Readonly<Record<string, string>>;
14
14
  readonly secondary?: readonly string[];
15
15
  readonly reviewStatus?: 'draft';
16
+ /** KeyboardEvent.code for each key, parallel to rows; lets real key presses light up virtual keys. */
17
+ readonly keyCodes?: readonly (readonly string[])[];
18
+ /** Latin key cap printed on each key, parallel to rows. */
19
+ readonly keyCaps?: readonly (readonly string[])[];
16
20
  }
17
21
  export interface KeyboardOptions {
18
22
  container?: HTMLElement | ShadowRoot;
@@ -24,6 +28,8 @@ export interface KeyboardOptions {
24
28
  theme?: KeyboardTheme;
25
29
  autoHamza?: boolean;
26
30
  secondaryKeys?: boolean;
31
+ /** Show each key's Shift letter and Latin key cap where the layout has them (default true). */
32
+ legends?: boolean;
27
33
  compact?: boolean;
28
34
  label?: string;
29
35
  }
@@ -35,6 +41,7 @@ export interface KeyboardController {
35
41
  setCompact(enabled: boolean): void;
36
42
  setAutoHamza(enabled: boolean): void;
37
43
  setSecondaryKeys(enabled: boolean): void;
44
+ setLegends(enabled: boolean): void;
38
45
  insert(text: string): boolean;
39
46
  backspace(): boolean;
40
47
  setPhysicalMapping(enabled: boolean): void;
@@ -46,6 +53,8 @@ export interface UyghurKeyboardElement extends HTMLElement {
46
53
  insert(text: string): boolean;
47
54
  backspace(): boolean;
48
55
  }
56
+ /** The standard Uyghur computer layout, used when no layout is given. */
57
+ export const DEFAULT_LAYOUT: 'uey-desktop';
49
58
  export const KEYBOARD_LAYOUTS: Readonly<Record<KeyboardLayoutId, KeyboardLayout>>;
50
59
  export const KEY_LAYOUT: readonly (readonly string[])[];
51
60
  export const PHONETIC_MAP: Readonly<Record<string, string>>;
package/src/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { alkatipFontFace, alkatipHelperStyles, keyboardStyles } from './style.js';
2
- import { KEYBOARD_LAYOUTS, keyboardLabels } from './layouts.js';
2
+ import { DEFAULT_LAYOUT, KEYBOARD_LAYOUTS, keyboardLabels } from './layouts.js';
3
3
  import { nextKey } from './navigation.js';
4
- export { KEY_LAYOUT, PHONETIC_MAP, KEYBOARD_LAYOUTS } from './layouts.js';
4
+ export { KEY_LAYOUT, PHONETIC_MAP, KEYBOARD_LAYOUTS, DEFAULT_LAYOUT } from './layouts.js';
5
5
 
6
6
  const fontStyleId = 'uyghur-keyboard-alkatip-font';
7
7
  const fontRegistrations = new WeakMap();
@@ -93,7 +93,7 @@ export function createKeyboard(target, options = {}) {
93
93
  }
94
94
  layouts.set(custom.id, custom);
95
95
  }
96
- let layout = layouts.get(options.layout ?? 'phonetic');
96
+ let layout = layouts.get(options.layout ?? DEFAULT_LAYOUT);
97
97
  if (!layout) throw new TypeError('Unknown keyboard layout: ' + options.layout);
98
98
  const direction = options.direction;
99
99
  if (direction && !['rtl', 'ltr', 'auto', 'preserve'].includes(direction)) {
@@ -116,6 +116,9 @@ export function createKeyboard(target, options = {}) {
116
116
  let physical = options.physical === true;
117
117
  let autoHamza = options.autoHamza === true;
118
118
  let secondary = options.secondaryKeys === true;
119
+ // Shift letters and Latin key caps on each key, where the layout has them.
120
+ let legends = options.legends !== false;
121
+ const pressTimers = new Map();
119
122
  let destroyed = false;
120
123
  let composing = false;
121
124
  let shifted = false;
@@ -145,7 +148,15 @@ export function createKeyboard(target, options = {}) {
145
148
  appliedDirection = nextDirection;
146
149
  }
147
150
 
148
- function keyButton(text, action) {
151
+ function legend(className, text) {
152
+ const span = doc.createElement('span');
153
+ span.className = className;
154
+ span.textContent = text;
155
+ span.setAttribute('aria-hidden', 'true');
156
+ return span;
157
+ }
158
+
159
+ function keyButton(text, action, { shiftText, cap, code } = {}) {
149
160
  const button = doc.createElement('button');
150
161
  button.type = 'button';
151
162
  button.className = 'ug-keyboard__key';
@@ -153,6 +164,10 @@ export function createKeyboard(target, options = {}) {
153
164
  button.tabIndex = -1;
154
165
  if (action) button.dataset.action = action;
155
166
  else button.dataset.text = text;
167
+ if (code) button.dataset.code = code;
168
+ if (legends && shiftText) button.appendChild(legend('ug-keyboard__shift-legend', shiftText));
169
+ if (legends && cap) button.appendChild(legend('ug-keyboard__cap', cap));
170
+ if (action === 'shift' || action === 'backspace') button.insertBefore(legend(`ug-keyboard__icon ug-keyboard__icon--${action}`, ''), button.firstChild ?? null);
156
171
  const localized = labels[action] || labels.punctuation[text];
157
172
  button.lang = localized ? locale : layout.lang;
158
173
  button.setAttribute('aria-label', localized || text);
@@ -174,10 +189,17 @@ export function createKeyboard(target, options = {}) {
174
189
  const index = buttonRows.flat().indexOf(activeButton);
175
190
  const restoreFocus = keys.contains(focusedElement());
176
191
  keys.dir = layout.keyDirection || layout.direction;
177
- const rows = (shifted ? layout.shiftedRows : layout.rows).map((row) => row.map((text) => keyButton(text)));
192
+ const current = shifted ? layout.shiftedRows : layout.rows;
193
+ const other = shifted ? layout.rows : layout.shiftedRows;
194
+ const rows = current.map((row, r) => row.map((text, k) => keyButton(text, undefined, {
195
+ // Show the Shift letter only where Shift gives a different letter (D: د and ژ).
196
+ shiftText: !shifted && other?.[r]?.[k] && other[r][k] !== text ? other[r][k] : undefined,
197
+ cap: layout.keyCaps?.[r]?.[k],
198
+ code: layout.keyCodes?.[r]?.[k],
199
+ })));
178
200
  if (secondary && layout.secondary?.length) rows.push(layout.secondary.map((text) => keyButton(text)));
179
- const actions = [keyButton(labels.space, 'space'), keyButton('⌫ ' + (locale === 'ug' ? 'ئۆچۈرۈش' : 'Delete'), 'backspace')];
180
- if (layout.shiftedRows) actions.unshift(keyButton('⇧', 'shift'));
201
+ const actions = [keyButton(labels.space, 'space'), keyButton(locale === 'ug' ? 'ئۆچۈرۈش' : 'Delete', 'backspace')];
202
+ if (layout.shiftedRows) actions.unshift(keyButton('', 'shift'));
181
203
  rows.push(actions);
182
204
  buttonRows = rows;
183
205
  keys.replaceChildren();
@@ -282,8 +304,40 @@ export function createKeyboard(target, options = {}) {
282
304
  setActiveButton(button);
283
305
  button.focus();
284
306
  }
307
+ /** The virtual key a physical key press corresponds to: by position first, then by character. */
308
+ function buttonFor(event) {
309
+ const all = buttonRows.flat();
310
+ if (event.code) {
311
+ const byCode = all.find((button) => button.dataset.code === event.code);
312
+ if (byCode) return byCode;
313
+ }
314
+ if (event.key === ' ') return all.find((button) => button.dataset.action === 'space');
315
+ if (event.key === 'Backspace') return all.find((button) => button.dataset.action === 'backspace');
316
+ if (event.key === 'Shift') return all.find((button) => button.dataset.action === 'shift');
317
+ const map = layout.physicalMap;
318
+ const char = map && Object.hasOwn(map, event.key) ? map[event.key] : event.key;
319
+ return all.find((button) => button.dataset.text === char || button.dataset.text === event.key);
320
+ }
321
+ function release(button) {
322
+ clearTimeout(pressTimers.get(button));
323
+ pressTimers.delete(button);
324
+ if (button.dataset) delete button.dataset.pressed;
325
+ }
326
+ function press(event) {
327
+ const button = buttonFor(event);
328
+ if (!button) return;
329
+ release(button);
330
+ button.dataset.pressed = 'true';
331
+ // Keyboards that never send keyup (some mobile IMEs) still clear the highlight.
332
+ pressTimers.set(button, setTimeout(() => release(button), 600));
333
+ }
334
+ function onKeyUp(event) {
335
+ const button = buttonFor(event);
336
+ if (button) release(button);
337
+ }
285
338
  function onKeyDown(event) {
286
339
  if (composing || event.isComposing || event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey) return;
340
+ press(event);
287
341
  const map = layout.physicalMap;
288
342
  const mapped = physical && map && Object.hasOwn(map, event.key) ? map[event.key] : undefined;
289
343
  const text = withHamza(mapped ?? event.key);
@@ -300,6 +354,7 @@ export function createKeyboard(target, options = {}) {
300
354
  keys.addEventListener('keydown', onVirtualKeyDown);
301
355
  layoutSelect.addEventListener('change', onLayoutChange);
302
356
  target.addEventListener('keydown', onKeyDown);
357
+ target.addEventListener('keyup', onKeyUp);
303
358
  target.addEventListener('compositionstart', beginComposition);
304
359
  target.addEventListener('compositionend', endComposition);
305
360
 
@@ -318,6 +373,11 @@ export function createKeyboard(target, options = {}) {
318
373
  renderKeys();
319
374
  },
320
375
  setPhysicalMapping(enabled) { physical = Boolean(enabled); },
376
+ setLegends(enabled) {
377
+ if (destroyed) return;
378
+ legends = Boolean(enabled);
379
+ renderKeys();
380
+ },
321
381
  setVisible(visible) { root.hidden = !visible; },
322
382
  destroy() {
323
383
  if (destroyed) return;
@@ -327,6 +387,8 @@ export function createKeyboard(target, options = {}) {
327
387
  keys.removeEventListener('keydown', onVirtualKeyDown);
328
388
  layoutSelect.removeEventListener('change', onLayoutChange);
329
389
  target.removeEventListener('keydown', onKeyDown);
390
+ target.removeEventListener('keyup', onKeyUp);
391
+ for (const button of [...pressTimers.keys()]) release(button);
330
392
  target.removeEventListener('compositionstart', beginComposition);
331
393
  target.removeEventListener('compositionend', endComposition);
332
394
  root.remove();
@@ -396,7 +458,7 @@ export function defineUyghurKeyboard(name = 'uyghur-keyboard', registry = global
396
458
  }
397
459
  get requestedLayout() {
398
460
  const id = this.getAttribute('layout');
399
- return Object.hasOwn(KEYBOARD_LAYOUTS, id) ? id : 'phonetic';
461
+ return Object.hasOwn(KEYBOARD_LAYOUTS, id) ? id : DEFAULT_LAYOUT;
400
462
  }
401
463
  get layout() { return this.controller?.layout ?? this.requestedLayout; }
402
464
  set layout(id) {
package/src/layouts.js CHANGED
@@ -34,6 +34,16 @@ const desktopShiftedRows = [
34
34
  ];
35
35
  const desktopKeys = [..."`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./"];
36
36
  const desktopShiftKeys = [...'~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?'];
37
+ // Physical key positions, row by row, as KeyboardEvent.code values, so a real key
38
+ // press can light up the matching virtual key whatever the operating-system layout.
39
+ const desktopCodes = [
40
+ ['Backquote', 'Digit1', 'Digit2', 'Digit3', 'Digit4', 'Digit5', 'Digit6', 'Digit7', 'Digit8', 'Digit9', 'Digit0', 'Minus', 'Equal'],
41
+ [...'QWERTYUIOP'].map((k) => 'Key' + k).concat(['BracketLeft', 'BracketRight', 'Backslash']),
42
+ [...'ASDFGHJKL'].map((k) => 'Key' + k).concat(['Semicolon', 'Quote']),
43
+ [...'ZXCVBNM'].map((k) => 'Key' + k).concat(['Comma', 'Period', 'Slash']),
44
+ ];
45
+ // The Latin key cap printed on each key, as on a Uyghur keyboard sticker.
46
+ const desktopCaps = desktopCodes.map((row) => row.map((code) => (code.startsWith('Key') ? code.slice(3) : '')));
37
47
  const desktopMap = {};
38
48
  for (const [keys, values] of [[desktopKeys, desktopRows.flat()], [desktopShiftKeys, desktopShiftedRows.flat()]]) {
39
49
  keys.forEach((key, index) => { desktopMap[key] = values[index]; });
@@ -56,7 +66,7 @@ function shiftRows(rows) {
56
66
  }
57
67
 
58
68
  function freezeLayout(layout) {
59
- for (const rows of [layout.rows, layout.shiftedRows]) {
69
+ for (const rows of [layout.rows, layout.shiftedRows, layout.keyCodes, layout.keyCaps]) {
60
70
  if (!rows) continue;
61
71
  rows.forEach(Object.freeze);
62
72
  Object.freeze(rows);
@@ -67,15 +77,23 @@ function freezeLayout(layout) {
67
77
  return Object.freeze(layout);
68
78
  }
69
79
 
80
+ /**
81
+ * The standard Uyghur computer layout is the default: the one built into Windows
82
+ * (KLID 00010480), Linux (XKB cn(ug)) and macOS, and taught by typing trainers
83
+ * such as Konupka. Its letters are checked key by key in the tests.
84
+ */
85
+ export const DEFAULT_LAYOUT = 'uey-desktop';
86
+
70
87
  export const KEYBOARD_LAYOUTS = Object.freeze({
71
- phonetic: freezeLayout({
72
- id: 'phonetic', labels: { en: 'Phonetic', ug: 'ئاھاڭداش' }, lang: 'ug-Arab',
73
- direction: 'rtl', rows: KEY_LAYOUT, physicalMap: PHONETIC_MAP, secondary: arabicExtras,
74
- }),
75
88
  'uey-desktop': freezeLayout({
76
- id: 'uey-desktop', labels: { en: 'Arabic desktop (UEY)', ug: 'ئەرەب يېزىقى (UEY)' }, lang: 'ug-Arab',
89
+ id: 'uey-desktop', labels: { en: 'Standard Uyghur (UEY)', ug: 'ئۆلچەملىك ئۇيغۇرچە (UEY)' }, lang: 'ug-Arab',
77
90
  direction: 'rtl', keyDirection: 'ltr', rows: desktopRows, shiftedRows: desktopShiftedRows,
78
91
  physicalMap: desktopMap, secondary: arabicExtras, reviewStatus: 'draft',
92
+ keyCodes: desktopCodes, keyCaps: desktopCaps,
93
+ }),
94
+ phonetic: freezeLayout({
95
+ id: 'phonetic', labels: { en: 'Phonetic', ug: 'تاۋۇش بويىچە' }, lang: 'ug-Arab',
96
+ direction: 'rtl', rows: KEY_LAYOUT, physicalMap: PHONETIC_MAP, secondary: arabicExtras,
79
97
  }),
80
98
  uly: freezeLayout({
81
99
  id: 'uly', labels: { en: 'Latin (ULY)', ug: 'لاتىن يېزىقى (ULY)' }, lang: 'ug-Latn',
@@ -97,7 +115,7 @@ export const keyboardLabels = {
97
115
  },
98
116
  ug: {
99
117
  keyboard: 'ئۇيغۇرچە كۇنۇپكا تاختىسى', layout: 'كۇنۇپكا تاختىسىنىڭ ئورۇنلاشتۇرۇلۇشى',
100
- space: 'بوشلۇق', backspace: 'ئالدىنقى ھەرپنى ئۆچۈرۈش', shift: 'چوڭ ھەرپ كۇنۇپكىسى',
101
- punctuation: { '،': 'پەش', '؛': 'چېكىتلىك پەش', '؟': 'سوئال بەلگىسى', '«': 'سول تىرناق', '»': 'ئوڭ تىرناق' },
118
+ space: 'بوشلۇق', backspace: 'ئالدىنقى ھەرپنى ئۆچۈرۈش', shift: 'Shift كۇنۇپكىسى',
119
+ punctuation: { '،': 'پەش', '؛': 'چېكىتلىك پەش', '؟': 'سوئال بەلگىسى', '«': 'سول قوش تىرناق', '»': 'ئوڭ قوش تىرناق' },
102
120
  },
103
121
  };
package/src/style.js CHANGED
@@ -70,7 +70,18 @@ ${alkatipHelperStyles}
70
70
  outline-offset:1px;
71
71
  z-index:1;
72
72
  }
73
- .ug-keyboard__key[data-action] { font-size:16px; }
73
+ .ug-keyboard__key[data-action] { font-size:16px; gap:8px; }
74
+ .ug-keyboard__key { position:relative; }
75
+ /* Shift letter (top) and Latin key cap (bottom), as printed on a Uyghur keyboard. */
76
+ .ug-keyboard__shift-legend { position:absolute; top:1px; left:6px; font-size:13px; line-height:1.3; opacity:.72; pointer-events:none; }
77
+ .ug-keyboard__cap { position:absolute; bottom:2px; right:6px; font:10px/1 ui-monospace,SFMono-Regular,Menlo,Consolas,monospace; opacity:.45; pointer-events:none; }
78
+ /* A real key press lights up its virtual key. */
79
+ .ug-keyboard__key[data-pressed="true"] { background:var(--ug-focus,var(--_focus)); color:var(--ug-key,var(--_key)); border-color:var(--ug-focus,var(--_focus)); transform:translateY(1px); }
80
+ /* Shift and Delete icons are Uyghur UI drawings (arrow-up, arrow-left), drawn as masks so they follow the text colour. */
81
+ .ug-keyboard__icon { display:inline-block; width:18px; height:18px; flex:none; background-color:currentColor; -webkit-mask:center/contain no-repeat; mask:center/contain no-repeat; }
82
+ .ug-keyboard__icon--shift { -webkit-mask-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 19V5m-6 6 6-6 6 6'/%3E%3C/svg%3E"); mask-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 19V5m-6 6 6-6 6 6'/%3E%3C/svg%3E"); }
83
+ .ug-keyboard__icon--backspace { -webkit-mask-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M19 12H5m6-6-6 6 6 6'/%3E%3C/svg%3E"); mask-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M19 12H5m6-6-6 6 6 6'/%3E%3C/svg%3E"); }
84
+ .ug-keyboard[data-compact="true"] .ug-keyboard__cap { display:none; }
74
85
  .ug-keyboard__key[data-action="space"] { flex-grow:4; }
75
86
  .ug-keyboard[data-compact="true"] { padding:6px; }
76
87
  .ug-keyboard[data-compact="true"] .ug-keyboard__row { gap:3px; margin-block:3px; }
@@ -79,5 +90,8 @@ ${alkatipHelperStyles}
79
90
  @media (max-width:420px) {
80
91
  .ug-keyboard { padding:6px; }
81
92
  .ug-keyboard__row { gap:4px; }
93
+ .ug-keyboard__cap { display:none; }
94
+ .ug-keyboard__shift-legend { left:3px; font-size:11px; }
95
+ .ug-keyboard__key[data-action="backspace"] { font-size:13px; gap:4px; }
82
96
  }
83
97
  `;