-
Notifications
You must be signed in to change notification settings - Fork 789
/
Copy pathshortcutKey.ts
178 lines (158 loc) · 4.43 KB
/
shortcutKey.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { computed, ref } from "vue";
export enum SHORTCUT_KEY_TYPES {
SOUND = "sound",
ANSWER = "answer",
SKIP = "skip",
PREVIOUS = "previous",
MASTERED = "mastered",
PAUSE = "pause",
}
export const SHORTCUT_KEYS = "shortcutKeys";
export const DEFAULT_SHORTCUT_KEYS = {
sound: "Ctrl+'",
answer: "Ctrl+;",
skip: "Ctrl+.",
previous: "Ctrl+,",
mastered: "Ctrl+m",
pause: "Ctrl+p",
};
export const KEYBOARD = {
ESC: "Esc",
ALT: "Alt",
CTRL: "Ctrl",
META: "Meta",
SHIFT: "Shift",
ENTER: "Enter",
COMMAND: "Command",
CONTROL: "Control",
};
export const SPECIAL_KEYS = new Map([
[KEYBOARD.ALT, true],
[KEYBOARD.CTRL, true],
[KEYBOARD.CONTROL, true],
[KEYBOARD.SHIFT, true],
[KEYBOARD.META, true],
[KEYBOARD.COMMAND, true],
]);
// 对于 Mac 特殊修饰键 Control 和 Meta 键转换处理
// Control → Ctrl
// Meta → Command
// 其他键照旧返回
export function convertMacKey(key: string) {
return (
{
[KEYBOARD.CONTROL]: KEYBOARD.CTRL,
[KEYBOARD.META]: KEYBOARD.COMMAND,
}[key] || key
);
}
export function parseShortcut(shortcut: string): string[] {
return shortcut
.split("+")
.map((key) => key.trim().charAt(0).toUpperCase() + key.slice(1).toLowerCase());
}
// 自定义快捷键
const showModal = ref<boolean>(false);
const currentKeyType = ref<SHORTCUT_KEY_TYPES | "">("");
const shortcutKeyStr = ref<string>("");
const shortcutKeys = ref<{ [key: string]: any }>({
...DEFAULT_SHORTCUT_KEYS,
});
const hasSameShortcutKey = ref(false);
const shortcutKeyTip = computed(() => {
return shortcutKeyStr.value.replace(/\+/g, "+");
});
// 初始化快捷键
setShortcutKeys();
function setShortcutKeys() {
const localKeys = localStorage.getItem(SHORTCUT_KEYS);
if (localKeys) {
shortcutKeys.value = { ...shortcutKeys.value, ...JSON.parse(localKeys) };
} else {
localStorage.setItem(SHORTCUT_KEYS, JSON.stringify(shortcutKeys.value));
}
}
export function useShortcutKeyMode() {
function handleEdit(type: SHORTCUT_KEY_TYPES) {
showModal.value = true;
shortcutKeyStr.value = "";
currentKeyType.value = type;
}
function handleCloseDialog() {
showModal.value = false;
hasSameShortcutKey.value = false;
}
function getKeyModifier(e: KeyboardEvent) {
if (e.altKey) return KEYBOARD.ALT;
if (e.shiftKey) return KEYBOARD.SHIFT;
if (e.ctrlKey) return KEYBOARD.CTRL;
if (e.metaKey) return KEYBOARD.COMMAND;
return "";
}
function saveShortcutKeys() {
const trimmedShortcutKeyStr = shortcutKeyStr.value.trim();
if (trimmedShortcutKeyStr) {
shortcutKeys.value[currentKeyType.value] = trimmedShortcutKeyStr;
localStorage.setItem(SHORTCUT_KEYS, JSON.stringify(shortcutKeys.value));
}
}
function isEnterKey(key: string) {
return key === KEYBOARD.ENTER;
}
function checkSameShortcutKey(key: string) {
const keys = Object.values(shortcutKeys.value);
const currentShortcutKey = shortcutKeys.value[currentKeyType.value];
return keys.some((x) => x === key && x !== currentShortcutKey);
}
/**
* 参考于 VSCode 快捷键
* 有待讨论,产品角度出发,快捷键应该只支持组合键形式
* 单键组合在使用过程中不方便写单词
*/
function handleKeydown(e: KeyboardEvent) {
if (!showModal.value) return;
e.preventDefault();
if (e.key === "Escape") {
handleCloseDialog();
return;
}
const mainKey = getKeyModifier(e);
if (!mainKey && isEnterKey(e.key)) {
if (checkSameShortcutKey(shortcutKeyStr.value)) {
hasSameShortcutKey.value = true;
} else {
saveShortcutKeys();
handleCloseDialog();
}
return;
}
const key = convertMacKey(e.key);
if (SPECIAL_KEYS.has(e.key) || !mainKey) {
// 单键
shortcutKeyStr.value = key;
} else {
// 组合键
shortcutKeyStr.value = `${mainKey}+${key}`;
}
}
function reset() {
showModal.value = false;
currentKeyType.value = "";
shortcutKeyStr.value = "";
shortcutKeys.value = { ...DEFAULT_SHORTCUT_KEYS };
hasSameShortcutKey.value = false;
localStorage.removeItem(SHORTCUT_KEYS);
}
return {
showModal, // 弹框对象
shortcutKeys, // 快捷键对象
shortcutKeyStr, // 单个修改的快捷键
shortcutKeyTip, // 快捷键输入框底部注释
hasSameShortcutKey, // 是否有相同的快捷键
setShortcutKeys,
handleKeydown,
handleEdit,
handleCloseDialog,
reset,
};
}