-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclean_shortcuts.ts
More file actions
72 lines (68 loc) · 1.9 KB
/
clean_shortcuts.ts
File metadata and controls
72 lines (68 loc) · 1.9 KB
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
import type {
KbsDefinition,
KbsInternalShortcut,
KbsKeyDefinition,
} from '../types.ts';
import type { Modifiers } from './make_key.ts';
const defaultModifiers: Modifiers = {
ctrl: false,
shift: undefined,
alt: false,
};
export function cleanShortcuts(
inputs: ReadonlyArray<readonly KbsDefinition[]>,
): readonly KbsInternalShortcut[] {
const result: KbsInternalShortcut[] = [];
for (const input of inputs) {
for (const definition of input) {
const [main, ...aliases] = shortcutToObjects(definition.shortcut);
result.push({
shortcut: main as KbsKeyDefinition,
aliases,
handler: definition.handler,
meta: definition.meta,
maxFrequency: definition.maxFrequency ?? 0,
});
}
}
return result;
}
function shortcutToObjects(
shortcut:
| string
| KbsKeyDefinition
| ReadonlyArray<string | KbsKeyDefinition>,
): readonly KbsKeyDefinition[] {
if (typeof shortcut === 'string') {
return [{ ...defaultModifiers, key: shortcut.toLowerCase() }];
} else if (
// Cannot use `Array.isArray` because it does not narrow `ReadonlyArray` (<https://github.com/microsoft/TypeScript/issues/17002>)
'map' in shortcut
) {
return shortcut.map((shortcut) => {
if (typeof shortcut === 'string') {
return { ...defaultModifiers, key: shortcut.toLowerCase() };
} else if ('key' in shortcut) {
return {
...defaultModifiers,
...shortcut,
key: shortcut.key.toLowerCase(),
};
} else {
return {
...defaultModifiers,
...shortcut,
code: shortcut.code.toLowerCase(),
};
}
});
} else if ('key' in shortcut) {
return [
{ ...defaultModifiers, ...shortcut, key: shortcut.key.toLowerCase() },
];
} else {
return [
{ ...defaultModifiers, ...shortcut, code: shortcut.code.toLowerCase() },
];
}
}