Line data Source code
1 : import { toRaw } from 'vue'
2 :
3 : import { RESERVED_BY_BROWSER, RESERVED_BY_TEXT_INPUT } from './reserved'
4 : import type { KeyCombo } from './types'
5 :
6 2356 : const MODIFIER_KEY_LABELS: Record<string, string> = {
7 : Control: 'Ctrl',
8 : Meta: 'Ctrl',
9 : Alt: 'Alt',
10 : Shift: 'Shift'
11 : }
12 :
13 2356 : export class KeyComboImpl implements KeyCombo {
14 : key: string
15 : ctrl: boolean
16 : alt: boolean
17 : shift: boolean
18 :
19 : constructor(obj: KeyCombo) {
20 84349 : this.key = obj.key
21 84349 : this.ctrl = obj.ctrl ?? false
22 84349 : this.alt = obj.alt ?? false
23 84349 : this.shift = obj.shift ?? false
24 : }
25 :
26 : static fromEvent(event: KeyboardEvent) {
27 1854 : return new KeyComboImpl({
28 : key: event.key,
29 : ctrl: event.ctrlKey || event.metaKey,
30 : alt: event.altKey,
31 : shift: event.shiftKey
32 : })
33 : }
34 :
35 : equals(other: unknown): boolean {
36 29 : const raw = toRaw(other)
37 :
38 29 : return raw instanceof KeyComboImpl
39 : ? this.key.toUpperCase() === raw.key.toUpperCase() &&
40 : this.ctrl === raw.ctrl &&
41 : this.alt === raw.alt &&
42 : this.shift === raw.shift
43 : : false
44 : }
45 :
46 : serialize(): string {
47 169741 : return `${this.key.toUpperCase()}:${this.ctrl}:${this.alt}:${this.shift}`
48 : }
49 :
50 : toString(): string {
51 13124 : return this.getKeySequences().join(' + ')
52 : }
53 :
54 : get hasModifier(): boolean {
55 737 : return this.ctrl || this.alt || this.shift
56 : }
57 :
58 : get isModifier(): boolean {
59 21420 : return this.key in MODIFIER_KEY_LABELS
60 : }
61 :
62 : get modifierCount(): number {
63 : const modifiers = [this.ctrl, this.alt, this.shift]
64 22 : return modifiers.reduce((acc, cur) => acc + Number(cur), 0)
65 : }
66 :
67 : get isShiftOnly(): boolean {
68 213 : return this.shift && this.modifierCount === 1
69 : }
70 :
71 : get isBrowserReserved(): boolean {
72 1145 : return RESERVED_BY_BROWSER.has(toNormalizedString(this))
73 : }
74 :
75 : get isReservedByTextInput(): boolean {
76 702 : return (
77 : !this.hasModifier ||
78 : this.isShiftOnly ||
79 : RESERVED_BY_TEXT_INPUT.has(toNormalizedString(this))
80 : )
81 : }
82 :
83 : getKeySequences(): string[] {
84 18760 : const sequences = getModifierSequences(this)
85 :
86 18760 : if (!this.isModifier || sequences.length === 0) {
87 18749 : sequences.push(getKeyLabel(this.key))
88 : }
89 :
90 18760 : return sequences
91 : }
92 : }
93 :
94 1350 : function toNormalizedString(combo: KeyComboImpl): string {
95 1350 : const sequences = getModifierSequences(combo)
96 :
97 1350 : if (!combo.isModifier || sequences.length === 0) {
98 1317 : sequences.push(getKeyLabel(combo.key, true))
99 : }
100 :
101 1350 : return sequences.join(' + ')
102 : }
103 :
104 20110 : function getModifierSequences(combo: KeyComboImpl): string[] {
105 20110 : const sequences: string[] = []
106 20110 : if (combo.ctrl) sequences.push('Ctrl')
107 20110 : if (combo.alt) sequences.push('Alt')
108 20110 : if (combo.shift) sequences.push('Shift')
109 20110 : return sequences
110 : }
111 :
112 20066 : function getKeyLabel(key: string, normalizeSingleCharacter = false): string {
113 20066 : const label = MODIFIER_KEY_LABELS[key] ?? key
114 20066 : return normalizeSingleCharacter && label.length === 1
115 : ? label.toLowerCase()
116 : : label
117 : }
|