Skip to content

Commit 6c8967d

Browse files
committed
feat(shortcut): 增加提示,设置完快捷键后 blur 输入框
template 部分没怎么改,套了一层 n-popover(加提示),改了一下 placeholder 的提示文字 onKeyDown 部分优化了体验,在设置完成快捷键后会自动 blur 掉输入框,删除快捷键和快捷键相同时增加了提示 ~~不会就我不知道怎么取消设置快捷键,按 Esc 没法取消,看了代码才知道是 Backspace 吧?(~~
1 parent 2268036 commit 6c8967d

1 file changed

Lines changed: 67 additions & 33 deletions

File tree

src/components/Setting/components/ShortcutRecorder.vue

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
11
<template>
22
<n-flex :wrap="false" align="center" style="width: 460px; justify-content: flex-end">
33
<!-- 本地/页面内快捷键 -->
4-
<n-input
5-
:value="shortcutItem.shortcut"
6-
placeholder="快捷键为空"
7-
readonly
8-
class="shortcut-input"
9-
@focus="onFocus(false)"
10-
@blur="onBlur"
11-
@keydown.stop="onKeyDown"
12-
@keyup="keyHandled = ''"
13-
/>
4+
<n-popover trigger="focus">
5+
<template #trigger>
6+
<n-input
7+
:value="shortcutItem.shortcut"
8+
placeholder="未设置"
9+
readonly
10+
class="shortcut-input"
11+
@focus="onFocus(false)"
12+
@blur="onBlur"
13+
@keydown.stop="onKeyDown"
14+
@keyup="keyHandled = ''"
15+
/>
16+
</template>
17+
<n-text>正在设置快捷键,按 Backspace 删除快捷键</n-text>
18+
</n-popover>
1419
<!-- 全局快捷键 -->
15-
<n-input
16-
v-if="allowGlobal"
17-
:value="shortcutItem.globalShortcut"
18-
:disabled="!shortcutStore.globalOpen"
19-
:status="shortcutItem.globalShortcut && shortcutItem.isRegistered ? 'error' : undefined"
20-
placeholder="快捷键为空"
21-
readonly
22-
class="shortcut-input"
23-
@focus="onFocus(true)"
24-
@blur="onBlur"
25-
@keydown.stop="onKeyDown"
26-
@keyup="keyHandled = ''"
27-
>
28-
<template #prefix>
29-
<n-text :depth="3">全局</n-text>
20+
<n-popover trigger="focus" v-if="allowGlobal">
21+
<template #trigger>
22+
<n-input
23+
:value="shortcutItem.globalShortcut"
24+
:disabled="!shortcutStore.globalOpen"
25+
:status="shortcutItem.globalShortcut && shortcutItem.isRegistered ? 'error' : undefined"
26+
placeholder="未设置"
27+
readonly
28+
class="shortcut-input"
29+
@focus="onFocus(true)"
30+
@blur="onBlur"
31+
@keydown.stop="onKeyDown"
32+
@keyup="keyHandled = ''"
33+
>
34+
<template #prefix>
35+
<n-text :depth="3">全局</n-text>
36+
</template>
37+
</n-input>
3038
</template>
31-
</n-input>
39+
<n-text>正在设置快捷键,按 Backspace 删除快捷键</n-text>
40+
</n-popover>
3241
</n-flex>
3342
</template>
3443

@@ -187,6 +196,11 @@ const changeShortcut = async (shortcut: string) => {
187196
shortcutStore.shortcutList[props.shortcutKey][targetKey] = shortcut;
188197
};
189198
199+
const isSameShortcut = (shortcut: string) => {
200+
const targetKey = isGlobalFocus.value ? "globalShortcut" : "shortcut";
201+
return shortcutStore.shortcutList[props.shortcutKey][targetKey] === shortcut;
202+
};
203+
190204
const onKeyDown = async (e: KeyboardEvent) => {
191205
e.preventDefault();
192206
e.stopPropagation();
@@ -195,8 +209,16 @@ const onKeyDown = async (e: KeyboardEvent) => {
195209
if (e.code === keyHandled.value) return;
196210
keyHandled.value = e.code;
197211
212+
const blur = () => {
213+
keyHandled.value = "";
214+
const target = e.target;
215+
if (target instanceof HTMLElement) target.blur();
216+
};
217+
198218
if (e.code === "Backspace") {
199219
changeShortcut("");
220+
window.$message.success("快捷键已删除");
221+
blur();
200222
return;
201223
}
202224
@@ -211,28 +233,40 @@ const onKeyDown = async (e: KeyboardEvent) => {
211233
.filter(Boolean)
212234
.join("+");
213235
214-
if (isRepeat(shortcut)) {
236+
const formattedShortcut = !isGlobalFocus.value
237+
? shortcut
238+
: (() => {
239+
const key = isCtrl || isShift || isAlt ? shortcut : "CmdOrCtrl+Shift+" + keyCode;
240+
return formatForGlobalShortcut(key);
241+
})();
242+
243+
if (isSameShortcut(formattedShortcut)) {
244+
window.$message.info("快捷键相同");
245+
blur();
246+
return;
247+
}
248+
249+
if (isRepeat(formattedShortcut)) {
215250
window.$message.warning("快捷键设置冲突");
251+
blur();
216252
return;
217253
}
218254
219255
if (isGlobalFocus.value) {
220-
const key = isCtrl || isShift || isAlt ? shortcut : "CmdOrCtrl+Shift+" + keyCode;
221-
const globalShortcut = formatForGlobalShortcut(key);
222-
if (!globalShortcut) return;
223-
224256
// 检查占用
225-
const isRegistered = await checkRegistered(globalShortcut);
257+
const isRegistered = await checkRegistered(formattedShortcut);
226258
if (isRegistered) {
227259
window.$message.warning("快捷键已被占用");
228260
} else {
229261
window.$message.success("快捷键设置成功");
230262
}
231-
changeShortcut(globalShortcut);
263+
changeShortcut(formattedShortcut);
232264
} else {
233265
changeShortcut(shortcut);
234266
window.$message.success("快捷键设置成功");
235267
}
268+
269+
blur();
236270
};
237271
</script>
238272

0 commit comments

Comments
 (0)