perf(components): perf global-search

This commit is contained in:
Soybean 2024-03-11 22:57:37 +08:00
parent 779ba4e415
commit 72745229d6
4 changed files with 50 additions and 40 deletions

View File

@ -5,28 +5,32 @@ defineOptions({ name: 'SearchFooter' });
</script> </script>
<template> <template>
<div class="h-44px flex-y-center px-24px"> <div class="h-44px flex-y-center gap-14px px-24px">
<span class="mr-14px flex-y-center"> <span class="flex-y-center">
<icon-mdi-keyboard-return class="icon mr-6px p-2px text-20px" /> <icon-mdi-keyboard-return class="icon-shadow icon-item" />
<span>{{ $t('common.confirm') }}</span> <span>{{ $t('common.confirm') }}</span>
</span> </span>
<span class="mr-14px flex-y-center"> <span class="flex-y-center">
<icon-mdi-arrow-up-thin class="icon mr-5px p-2px text-20px" /> <icon-mdi-arrow-up-thin class="icon-shadow icon-item" />
<icon-mdi-arrow-down-thin class="icon mr-6px p-2px text-20px" /> <icon-mdi-arrow-down-thin class="icon-shadow icon-item" />
<span>{{ $t('common.switch') }}</span> <span>{{ $t('common.switch') }}</span>
</span> </span>
<span class="flex-y-center"> <span class="flex-y-center">
<icon-mdi-keyboard-esc class="icon mr-6px p-2px text-20px" /> <icon-mdi-keyboard-esc class="icon-shadow icon-item" />
<span>{{ $t('common.close') }}</span> <span>{{ $t('common.close') }}</span>
</span> </span>
</div> </div>
</template> </template>
<style lang="scss" scoped> <style lang="scss" scoped>
.icon { .icon-shadow {
box-shadow: box-shadow:
inset 0 -2px #cdcde6, inset 0 -2px #cdcde6,
inset 0 0 1px 1px #fff, inset 0 0 1px 1px #fff,
0 1px 2px 1px #1e235a66; 0 1px 2px 1px #1e235a66;
} }
.icon-item {
--at-apply: mr-6px p-2px text-20px;
}
</style> </style>

View File

@ -10,20 +10,20 @@ import SearchFooter from './search-footer.vue';
defineOptions({ name: 'SearchModal' }); defineOptions({ name: 'SearchModal' });
const appStore = useAppStore();
const isMobile = computed(() => appStore.isMobile);
const router = useRouter(); const router = useRouter();
const appStore = useAppStore();
const routeStore = useRouteStore(); const routeStore = useRouteStore();
const isMobile = computed(() => appStore.isMobile);
const keyword = ref(''); const keyword = ref('');
const activePath = ref(''); const activePath = ref('');
const resultOptions = shallowRef<App.Global.Menu[]>([]); const resultOptions = shallowRef<App.Global.Menu[]>([]);
const handleSearch = useDebounceFn(search, 300); const handleSearch = useDebounceFn(search, 300);
const modelShow = defineModel<boolean>('show', { required: true }); const visible = defineModel<boolean>('show', { required: true });
/** 查询 */
function search() { function search() {
resultOptions.value = routeStore.searchMenus.filter(menu => { resultOptions.value = routeStore.searchMenus.filter(menu => {
const trimKeyword = keyword.value.toLocaleLowerCase().trim(); const trimKeyword = keyword.value.toLocaleLowerCase().trim();
@ -34,8 +34,8 @@ function search() {
} }
function handleClose() { function handleClose() {
modelShow.value = false; visible.value = false;
/** 延时处理防止用户看到某些操作 */ // handle with setTimeout to prevent user from seeing some operations
setTimeout(() => { setTimeout(() => {
resultOptions.value = []; resultOptions.value = [];
keyword.value = ''; keyword.value = '';
@ -46,44 +46,54 @@ function handleClose() {
function handleUp() { function handleUp() {
const { length } = resultOptions.value; const { length } = resultOptions.value;
if (length === 0) return; if (length === 0) return;
const index = resultOptions.value.findIndex(item => item.routePath === activePath.value);
if (index === 0) { const index = getActivePathIndex();
activePath.value = resultOptions.value[length - 1].routePath; if (index === -1) return;
} else {
activePath.value = resultOptions.value[index - 1].routePath; const activeIndex = index === 0 ? length - 1 : index - 1;
}
activePath.value = resultOptions.value[activeIndex].routePath;
} }
/** key down */ /** key down */
function handleDown() { function handleDown() {
const { length } = resultOptions.value; const { length } = resultOptions.value;
if (length === 0) return; if (length === 0) return;
const index = resultOptions.value.findIndex(item => item.routePath === activePath.value);
if (index + 1 === length) { const index = getActivePathIndex();
activePath.value = resultOptions.value[0].routePath; if (index === -1) return;
} else {
activePath.value = resultOptions.value[index + 1].routePath; const activeIndex = index === length - 1 ? 0 : index + 1;
}
activePath.value = resultOptions.value[activeIndex].routePath;
}
function getActivePathIndex() {
return resultOptions.value.findIndex(item => item.routePath === activePath.value);
} }
/** key enter */ /** key enter */
function handleEnter(e: Event | undefined) { function handleEnter(e: Event | undefined) {
const { length } = resultOptions.value; if (resultOptions.value?.length === 0 || activePath.value === '') return;
if (length === 0 || activePath.value === '') return;
e?.preventDefault(); e?.preventDefault();
handleClose(); handleClose();
router.push(activePath.value); router.push(activePath.value);
} }
onKeyStroke('Escape', handleClose); function registerShortcut() {
onKeyStroke('Enter', handleEnter); onKeyStroke('Escape', handleClose);
onKeyStroke('ArrowUp', handleUp); onKeyStroke('Enter', handleEnter);
onKeyStroke('ArrowDown', handleDown); onKeyStroke('ArrowUp', handleUp);
onKeyStroke('ArrowDown', handleDown);
}
registerShortcut();
</script> </script>
<template> <template>
<NModal <NModal
v-model:show="modelShow" v-model:show="visible"
:segmented="{ footer: 'soft' }" :segmented="{ footer: 'soft' }"
:closable="false" :closable="false"
preset="card" preset="card"

View File

@ -20,8 +20,7 @@ const theme = useThemeStore();
const active = defineModel<string>('path', { required: true }); const active = defineModel<string>('path', { required: true });
/** 鼠标移入 */ async function handleMouseEnter(item: App.Global.Menu) {
async function handleMouse(item: App.Global.Menu) {
active.value = item.routePath; active.value = item.routePath;
} }
@ -41,7 +40,7 @@ function handleTo() {
color: item.routePath === active ? '#fff' : '' color: item.routePath === active ? '#fff' : ''
}" }"
@click="handleTo" @click="handleTo"
@mouseenter="handleMouse(item)" @mouseenter="handleMouseEnter(item)"
> >
<component :is="item.icon" /> <component :is="item.icon" />
<span class="ml-5px flex-1"> <span class="ml-5px flex-1">

View File

@ -6,13 +6,10 @@ import SearchModal from './components/search-modal.vue';
defineOptions({ name: 'GlobalSearch' }); defineOptions({ name: 'GlobalSearch' });
const { bool: show, toggle } = useBoolean(); const { bool: show, toggle } = useBoolean();
function handleSearch() {
toggle();
}
</script> </script>
<template> <template>
<ButtonIcon :tooltip-content="$t('common.search')" @click="handleSearch"> <ButtonIcon :tooltip-content="$t('common.search')" @click="toggle">
<icon-uil-search /> <icon-uil-search />
</ButtonIcon> </ButtonIcon>
<SearchModal v-model:show="show" /> <SearchModal v-model:show="show" />