Merge pull request #29 from vbenjs/global-search

fix: prevent default searching in browser
pull/48/MERGE
Vben 2024-07-06 19:53:15 +08:00 committed by GitHub
commit f7a93a9bb3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 31 additions and 9 deletions

View File

@ -1,7 +1,7 @@
<script setup lang="ts">
import type { MenuRecordRaw } from '@vben/types';
import { ref } from 'vue';
import { onMounted, onUnmounted, ref, watch } from 'vue';
import { $t } from '@vben/locales';
import {
@ -46,15 +46,37 @@ function handleClose() {
keyword.value = '';
}
if (props.enableShortcutKey) {
const keys = useMagicKeys();
const cmd = isWindowsOs() ? keys['ctrl+k'] : keys['cmd+k'];
whenever(cmd, () => {
if (props.enableShortcutKey) {
open.value = true;
}
const keys = useMagicKeys();
const cmd = isWindowsOs() ? keys['ctrl+k'] : keys['cmd+k'];
whenever(cmd, () => {
if (props.enableShortcutKey) {
open.value = true;
}
});
const preventDefaultBrowserSearchHotKey = (event: KeyboardEvent) => {
if (event.key.toLowerCase() === 'k' && (event.metaKey || event.ctrlKey)) {
event.preventDefault();
}
};
const toggleKeydownListener = () => {
if (props.enableShortcutKey) {
window.addEventListener('keydown', preventDefaultBrowserSearchHotKey);
} else {
window.removeEventListener('keydown', preventDefaultBrowserSearchHotKey);
}
};
watch(() => props.enableShortcutKey, toggleKeydownListener);
onMounted(() => {
toggleKeydownListener();
onUnmounted(() => {
window.removeEventListener('keydown', preventDefaultBrowserSearchHotKey);
});
}
});
</script>
<template>