Skip to content

Commit d3e10cb

Browse files
committed
refactor: extract customShortcutDialog component
1 parent 2e72628 commit d3e10cb

File tree

4 files changed

+85
-84
lines changed

4 files changed

+85
-84
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<template>
2+
<UModal
3+
v-model="showModal"
4+
@close="handleCloseDialog"
5+
>
6+
<UContainer>
7+
<h3 class="mb-4 text-center text-base font-bold">
8+
请先按下单键/组合键,通过回车键(Enter ⏎)来设置
9+
</h3>
10+
<div class="h-8 rounded border border-solid text-center leading-8">
11+
{{ shortcutKeyStr }}
12+
</div>
13+
<div class="mt-2 flex h-8 justify-center gap-0.5 text-center">
14+
<div v-if="shortcutKeyTip">
15+
<UKbd v-for="key in parseShortcutKeys(shortcutKeyTip)">
16+
{{ key }}
17+
</UKbd>
18+
</div>
19+
</div>
20+
<div
21+
v-if="hasSameShortcutKey"
22+
class="mt-4 text-center text-xs"
23+
:class="'text-[rgba(136,136,136,1)]'"
24+
>
25+
已有相同的按键绑定,请重新设置
26+
</div>
27+
</UContainer>
28+
</UModal>
29+
</template>
30+
31+
<script setup lang="ts">
32+
import { onMounted, onUnmounted } from "vue";
33+
34+
import { useShortcutKeyMode } from "~/composables/user/shortcutKey";
35+
import { parseShortcutKeys } from "~/utils/keyboardShortcuts";
36+
37+
const {
38+
showModal,
39+
shortcutKeyStr,
40+
shortcutKeyTip,
41+
hasSameShortcutKey,
42+
handleCloseDialog,
43+
handleKeydown,
44+
} = useShortcutKeyMode();
45+
46+
onMounted(() => {
47+
document.addEventListener("keydown", handleKeydown);
48+
});
49+
onUnmounted(() => {
50+
document.removeEventListener("keydown", handleKeydown);
51+
});
52+
</script>
53+
54+
<style scoped></style>

apps/client/components/UserMenu.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
:name="item.icon"
4949
class="mr-3 h-7 w-7"
5050
></UIcon>
51-
<span class="text-lg font-medium">{{ item.title }}</span>
51+
<span class="font-medium">{{ item.title }}</span>
5252
</button>
5353
</div>
5454

apps/client/composables/user/shortcutKey.ts

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,30 @@ export function convertMacKey(key: string) {
5151
}
5252

5353
// 自定义快捷键
54-
export function useShortcutKeyMode() {
55-
const showModal = ref<boolean>(false);
56-
const currentKeyType = ref<SHORTCUT_KEY_TYPES | "">("");
57-
const shortcutKeyStr = ref<string>("");
58-
const shortcutKeys = ref<{ [key: string]: any }>({
59-
...DEFAULT_SHORTCUT_KEYS,
60-
});
61-
const shortcutKeyTip = computed(() => {
62-
return shortcutKeyStr.value.replace(/\+/g, "+");
63-
});
64-
const hasSameShortcutKey = ref(false);
65-
66-
// 初始化快捷键
67-
setShortcutKeys();
68-
69-
function setShortcutKeys() {
70-
const localKeys = localStorage.getItem(SHORTCUT_KEYS);
71-
if (localKeys) {
72-
shortcutKeys.value = { ...shortcutKeys.value, ...JSON.parse(localKeys) };
73-
} else {
74-
localStorage.setItem(SHORTCUT_KEYS, JSON.stringify(shortcutKeys.value));
75-
}
54+
const showModal = ref<boolean>(false);
55+
const currentKeyType = ref<SHORTCUT_KEY_TYPES | "">("");
56+
const shortcutKeyStr = ref<string>("");
57+
const shortcutKeys = ref<{ [key: string]: any }>({
58+
...DEFAULT_SHORTCUT_KEYS,
59+
});
60+
const hasSameShortcutKey = ref(false);
61+
const shortcutKeyTip = computed(() => {
62+
return shortcutKeyStr.value.replace(/\+/g, "+");
63+
});
64+
65+
// 初始化快捷键
66+
setShortcutKeys();
67+
68+
function setShortcutKeys() {
69+
const localKeys = localStorage.getItem(SHORTCUT_KEYS);
70+
if (localKeys) {
71+
shortcutKeys.value = { ...shortcutKeys.value, ...JSON.parse(localKeys) };
72+
} else {
73+
localStorage.setItem(SHORTCUT_KEYS, JSON.stringify(shortcutKeys.value));
7674
}
75+
}
7776

77+
export function useShortcutKeyMode() {
7878
function handleEdit(type: SHORTCUT_KEY_TYPES) {
7979
showModal.value = true;
8080
shortcutKeyStr.value = "";
@@ -121,6 +121,12 @@ export function useShortcutKeyMode() {
121121
if (!showModal.value) return;
122122

123123
e.preventDefault();
124+
125+
if (e.key === "Escape") {
126+
handleCloseDialog();
127+
return;
128+
}
129+
124130
const mainKey = getKeyModifier(e);
125131
if (!mainKey && isEnterKey(e.key)) {
126132
if (checkSameShortcutKey(shortcutKeyStr.value)) {

apps/client/pages/User/Setting.vue

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -173,51 +173,10 @@
173173
</table>
174174
</section>
175175
</div>
176-
177-
<dialog
178-
class="modal mt-[-8vh]"
179-
:open="showModal"
180-
>
181-
<div
182-
ref="dialogBoxRef"
183-
class="modal-box min-h-[156px] max-w-[48rem]"
184-
>
185-
<h3 class="mb-4 text-center text-base font-bold text-fuchsia-500">
186-
请先按下单键/组合键,通过回车键(Enter ⏎)来设置
187-
</h3>
188-
<div class="h-8 rounded border border-solid border-fuchsia-500 text-center leading-8">
189-
{{ shortcutKeyStr }}
190-
</div>
191-
<div
192-
v-if="shortcutKeyTip"
193-
class="mt-2 flex justify-center gap-0.5 text-center"
194-
>
195-
<UKbd v-for="key in parseShortcutKeys(shortcutKeyTip)">
196-
{{ key }}
197-
</UKbd>
198-
</div>
199-
<div
200-
v-if="hasSameShortcutKey"
201-
class="mt-4 text-center text-xs"
202-
:class="'text-[rgba(136,136,136,1)]'"
203-
>
204-
已有相同的按键绑定,请重新设置
205-
</div>
206-
</div>
207-
208-
<!-- click outside to close -->
209-
<form
210-
method="dialog"
211-
class="modal-backdrop"
212-
>
213-
<button @click="handleCloseDialog"></button>
214-
</form>
215-
</dialog>
176+
<CustomShortcutDialog />
216177
</template>
217178

218179
<script setup lang="ts">
219-
import { onMounted, onUnmounted, ref } from "vue";
220-
221180
import { useAutoNextQuestion } from "~/composables/user/autoNext";
222181
import { useErrorTip } from "~/composables/user/errorTip";
223182
import { GameMode, useGameMode } from "~/composables/user/gameMode";
@@ -232,8 +191,6 @@ import { useSpaceSubmitAnswer } from "~/composables/user/submitKey";
232191
import { useShowWordsWidth } from "~/composables/user/words";
233192
import { parseShortcutKeys } from "~/utils/keyboardShortcuts";
234193
235-
const dialogBoxRef = ref<HTMLElement | null>(null);
236-
237194
const { autoNextQuestion, toggleAutoQuestion } = useAutoNextQuestion();
238195
const { keyboardSound, toggleKeyboardSound } = useKeyboardSound();
239196
const { autoPlaySound, toggleAutoPlaySound } = useAutoPronunciation();
@@ -247,16 +204,7 @@ const {
247204
const { showWordsWidth, toggleAutoWordsWidth } = useShowWordsWidth();
248205
const { useSpace, toggleUseSpaceSubmitAnswer } = useSpaceSubmitAnswer();
249206
const { showErrorTip, toggleShowErrorTip } = useErrorTip();
250-
const {
251-
showModal,
252-
shortcutKeys,
253-
shortcutKeyStr,
254-
shortcutKeyTip,
255-
hasSameShortcutKey,
256-
handleEdit,
257-
handleCloseDialog,
258-
handleKeydown,
259-
} = useShortcutKeyMode();
207+
const { shortcutKeys, handleEdit } = useShortcutKeyMode();
260208
261209
const { getGameModeOptions, currentGameMode, toggleGameMode } = useGameMode();
262210
@@ -286,13 +234,6 @@ const shortcutKeyBindList = [
286234
type: SHORTCUT_KEY_TYPES.PAUSE,
287235
},
288236
];
289-
290-
onMounted(() => {
291-
document.addEventListener("keydown", handleKeydown);
292-
});
293-
onUnmounted(() => {
294-
document.removeEventListener("keydown", handleKeydown);
295-
});
296237
</script>
297238

298239
<style scoped>

0 commit comments

Comments
 (0)