-
Notifications
You must be signed in to change notification settings - Fork 531
Expand file tree
/
Copy pathparseHotkey.ts
More file actions
75 lines (65 loc) · 1.78 KB
/
parseHotkey.ts
File metadata and controls
75 lines (65 loc) · 1.78 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
73
74
75
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2026 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import type { Hotkey } from '@cloudbeaver/core-blocks';
const reservedModifierKeywords = ['shift', 'alt', 'meta', 'mod', 'ctrl'];
const mappedKeys: Record<string, string> = {
esc: 'escape',
return: 'enter',
'.': 'period',
',': 'comma',
'-': 'slash',
' ': 'space',
'`': 'backquote',
'#': 'backslash',
'+': 'bracketright',
ShiftLeft: 'shift',
ShiftRight: 'shift',
AltLeft: 'alt',
AltRight: 'alt',
MetaLeft: 'meta',
MetaRight: 'meta',
OSLeft: 'meta',
OSRight: 'meta',
ControlLeft: 'ctrl',
ControlRight: 'ctrl',
};
export function mapKey(key: string): string {
return (mappedKeys[key] || key)
.trim()
.toLowerCase()
.replace(/key|digit|numpad|arrow/, '');
}
export function isHotkeyModifier(key: string): boolean {
return reservedModifierKeywords.includes(key);
}
export function parseKeysHookInput(keys: string, splitKey = ','): string[] {
return keys.split(splitKey);
}
export function parseHotkey(hotkey: string, combinationKey = '+'): Hotkey {
const keys = hotkey
.toLocaleLowerCase()
.split(combinationKey)
.map(k => mapKey(k));
const modifiers: Record<string, boolean> = {
alt: keys.includes('alt'),
ctrl: keys.includes('ctrl') || keys.includes('control'),
shift: keys.includes('shift'),
meta: keys.includes('meta'),
mod: keys.includes('mod'),
};
const singleCharKeys = keys.filter(k => !reservedModifierKeywords.includes(k));
return {
...modifiers,
hotkey,
keys: singleCharKeys,
isSequence: false,
metadata: undefined,
useKey: true,
description: undefined,
};
}