Skip to content

Commit

Permalink
refactor: extract customShortcutDialog component
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Aug 14, 2024
1 parent 2e72628 commit d3e10cb
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 84 deletions.
54 changes: 54 additions & 0 deletions apps/client/components/CustomShortcutDialog.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<template>
<UModal
v-model="showModal"
@close="handleCloseDialog"
>
<UContainer>
<h3 class="mb-4 text-center text-base font-bold">
请先按下单键/组合键,通过回车键(Enter ⏎)来设置
</h3>
<div class="h-8 rounded border border-solid text-center leading-8">
{{ shortcutKeyStr }}
</div>
<div class="mt-2 flex h-8 justify-center gap-0.5 text-center">
<div v-if="shortcutKeyTip">
<UKbd v-for="key in parseShortcutKeys(shortcutKeyTip)">
{{ key }}
</UKbd>
</div>
</div>
<div
v-if="hasSameShortcutKey"
class="mt-4 text-center text-xs"
:class="'text-[rgba(136,136,136,1)]'"
>
已有相同的按键绑定,请重新设置
</div>
</UContainer>
</UModal>
</template>

<script setup lang="ts">
import { onMounted, onUnmounted } from "vue";
import { useShortcutKeyMode } from "~/composables/user/shortcutKey";
import { parseShortcutKeys } from "~/utils/keyboardShortcuts";
const {
showModal,
shortcutKeyStr,
shortcutKeyTip,
hasSameShortcutKey,
handleCloseDialog,
handleKeydown,
} = useShortcutKeyMode();
onMounted(() => {
document.addEventListener("keydown", handleKeydown);
});
onUnmounted(() => {
document.removeEventListener("keydown", handleKeydown);
});
</script>

<style scoped></style>
2 changes: 1 addition & 1 deletion apps/client/components/UserMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
:name="item.icon"
class="mr-3 h-7 w-7"
></UIcon>
<span class="text-lg font-medium">{{ item.title }}</span>
<span class="font-medium">{{ item.title }}</span>
</button>
</div>

Expand Down
50 changes: 28 additions & 22 deletions apps/client/composables/user/shortcutKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,30 @@ export function convertMacKey(key: string) {
}

// 自定义快捷键
export function useShortcutKeyMode() {
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 shortcutKeyTip = computed(() => {
return shortcutKeyStr.value.replace(/\+/g, "+");
});
const hasSameShortcutKey = ref(false);

// 初始化快捷键
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));
}
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 = "";
Expand Down Expand Up @@ -121,6 +121,12 @@ export function useShortcutKeyMode() {
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)) {
Expand Down
63 changes: 2 additions & 61 deletions apps/client/pages/User/Setting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -173,51 +173,10 @@
</table>
</section>
</div>

<dialog
class="modal mt-[-8vh]"
:open="showModal"
>
<div
ref="dialogBoxRef"
class="modal-box min-h-[156px] max-w-[48rem]"
>
<h3 class="mb-4 text-center text-base font-bold text-fuchsia-500">
请先按下单键/组合键,通过回车键(Enter ⏎)来设置
</h3>
<div class="h-8 rounded border border-solid border-fuchsia-500 text-center leading-8">
{{ shortcutKeyStr }}
</div>
<div
v-if="shortcutKeyTip"
class="mt-2 flex justify-center gap-0.5 text-center"
>
<UKbd v-for="key in parseShortcutKeys(shortcutKeyTip)">
{{ key }}
</UKbd>
</div>
<div
v-if="hasSameShortcutKey"
class="mt-4 text-center text-xs"
:class="'text-[rgba(136,136,136,1)]'"
>
已有相同的按键绑定,请重新设置
</div>
</div>

<!-- click outside to close -->
<form
method="dialog"
class="modal-backdrop"
>
<button @click="handleCloseDialog"></button>
</form>
</dialog>
<CustomShortcutDialog />
</template>

<script setup lang="ts">
import { onMounted, onUnmounted, ref } from "vue";
import { useAutoNextQuestion } from "~/composables/user/autoNext";
import { useErrorTip } from "~/composables/user/errorTip";
import { GameMode, useGameMode } from "~/composables/user/gameMode";
Expand All @@ -232,8 +191,6 @@ import { useSpaceSubmitAnswer } from "~/composables/user/submitKey";
import { useShowWordsWidth } from "~/composables/user/words";
import { parseShortcutKeys } from "~/utils/keyboardShortcuts";
const dialogBoxRef = ref<HTMLElement | null>(null);
const { autoNextQuestion, toggleAutoQuestion } = useAutoNextQuestion();
const { keyboardSound, toggleKeyboardSound } = useKeyboardSound();
const { autoPlaySound, toggleAutoPlaySound } = useAutoPronunciation();
Expand All @@ -247,16 +204,7 @@ const {
const { showWordsWidth, toggleAutoWordsWidth } = useShowWordsWidth();
const { useSpace, toggleUseSpaceSubmitAnswer } = useSpaceSubmitAnswer();
const { showErrorTip, toggleShowErrorTip } = useErrorTip();
const {
showModal,
shortcutKeys,
shortcutKeyStr,
shortcutKeyTip,
hasSameShortcutKey,
handleEdit,
handleCloseDialog,
handleKeydown,
} = useShortcutKeyMode();
const { shortcutKeys, handleEdit } = useShortcutKeyMode();
const { getGameModeOptions, currentGameMode, toggleGameMode } = useGameMode();
Expand Down Expand Up @@ -286,13 +234,6 @@ const shortcutKeyBindList = [
type: SHORTCUT_KEY_TYPES.PAUSE,
},
];
onMounted(() => {
document.addEventListener("keydown", handleKeydown);
});
onUnmounted(() => {
document.removeEventListener("keydown", handleKeydown);
});
</script>

<style scoped>
Expand Down

0 comments on commit d3e10cb

Please sign in to comment.