-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a141c6
commit f42271c
Showing
10 changed files
with
193 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<template> | ||
<div class="me-search-menu pointer" @click="showSearch = true"> | ||
<mel-icon-search class="icon"></mel-icon-search> | ||
<el-dialog | ||
v-model="showSearch" | ||
:show-close="false" | ||
@keydown.down="handleDown() && srollToIndex()" | ||
@keyup.up="handleUp() && srollToIndex()" | ||
@keyup.enter="handleEnter();close();" | ||
> | ||
<div class="search"> | ||
<el-input | ||
v-model="searchText" | ||
size="large" | ||
:placeholder="$t('搜索')+$t(' ')+$t('菜单')" | ||
:prefix-icon="Search" | ||
clearable | ||
@input="search(searchText)" | ||
> | ||
</el-input> | ||
<div v-if="filteredMenu.length" ref="listRef" class="list"> | ||
<el-scrollbar max-height="300px"> | ||
<a | ||
v-for="(item, index) in filteredMenu" | ||
:key="index" | ||
class="item" | ||
:class="{ active: activeIndex === index }" | ||
@click.stop="jump(item);close()" | ||
@mouseenter="activeIndex = index" | ||
> | ||
{{ item.meta.title }} | ||
</a> | ||
</el-scrollbar> | ||
</div> | ||
<el-empty v-else :image-size="80" /> | ||
</div> | ||
</el-dialog> | ||
</div> | ||
</template> | ||
<script setup lang="ts" name="MeSearchMenu"> | ||
import { useSearchMenu } from './useSearchMenu'; | ||
import { Search } from '@element-plus/icons-vue'; | ||
import { jump } from '@/router'; | ||
const showSearch = ref(false); | ||
const searchText = ref(''); | ||
const listRef = ref<HTMLElement>(); | ||
const { filteredMenu, search, activeIndex, handleDown, handleUp, handleEnter } = useSearchMenu(); | ||
const srollToIndex = () => { | ||
listRef.value!.querySelectorAll('.item')[activeIndex.value].scrollIntoView({ | ||
behavior: 'smooth', | ||
}); | ||
}; | ||
const close = ()=>{ | ||
showSearch.value = false; | ||
searchText.value = ''; | ||
filteredMenu.value = []; | ||
} | ||
</script> | ||
<style lang="scss" scoped> | ||
.me-search-menu { | ||
padding: 0 10px; | ||
display: flex; | ||
align-items: center; | ||
.icon { | ||
font-size: 1.3em; | ||
} | ||
:deep(.el-dialog__header) { | ||
display: none; | ||
} | ||
:deep(.el-dialog) { | ||
min-width: 300px; | ||
} | ||
} | ||
.search { | ||
.list { | ||
padding: 10px 0; | ||
.item { | ||
display: block; | ||
padding: 10px 32px 10px 20px; | ||
line-height: 1.2em; | ||
} | ||
.active { | ||
color: var(--el-color-primary); | ||
background-color: var(--el-fill-color-light); | ||
} | ||
} | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { useGlobalStore, useRouteStore } from '@/store'; | ||
import { RouteRecordRaw } from 'vue-router'; | ||
import { resolvePath, jump } from '@/router'; | ||
|
||
import { debounce } from 'lodash-es'; | ||
const menuList = [] as { path: string; isLink?: boolean; title: string[] }[]; | ||
|
||
const createMenuList = (routes: RouteRecordRaw[], baseTitle: string[] = [], basePath = '') => { | ||
routes.forEach((item) => { | ||
if (item.meta?.title) { | ||
const path = resolvePath(item.path, basePath); | ||
const title = [...baseTitle, item.meta.title]; | ||
if (!item.meta.hideMenu && (item.redirect || !item.children?.length)) { | ||
menuList.push({ | ||
path, | ||
title, | ||
isLink: item.meta.isLink, | ||
}); | ||
} | ||
if (item.children) { | ||
createMenuList(item.children, title, path); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
export const useSearchMenu = (debounceTime = 500) => { | ||
const { i18n } = useGlobalStore(); | ||
const { routes } = useRouteStore(); | ||
!menuList.length && createMenuList(routes); | ||
const filteredMenu = ref<{ path: string; meta: { isLink?: boolean; title: string } }[]>([]); | ||
const activeIndex = ref(0); | ||
const search = debounce((searchText: string) => { | ||
filteredMenu.value = []; | ||
activeIndex.value = 0; | ||
searchText && menuList.forEach((item) => { | ||
const title = item.title.map((v) => i18n.t(v)).join(' > '); | ||
if (title.search(searchText) > -1) { | ||
filteredMenu.value.push({ | ||
path: item.path, | ||
meta: { | ||
title, | ||
isLink: item.isLink, | ||
}, | ||
}); | ||
} | ||
return filteredMenu.value; | ||
}); | ||
}, debounceTime); | ||
|
||
// Arrow key up | ||
function handleUp() { | ||
if (!filteredMenu.value.length) return false; | ||
activeIndex.value--; | ||
if (activeIndex.value < 0) { | ||
activeIndex.value = filteredMenu.value.length - 1; | ||
} | ||
return true; | ||
} | ||
|
||
// Arrow key down | ||
function handleDown() { | ||
if (!filteredMenu.value.length) return false; | ||
activeIndex.value++; | ||
if (activeIndex.value > filteredMenu.value.length - 1) { | ||
activeIndex.value = 0; | ||
} | ||
return true; | ||
} | ||
|
||
// enter keyboard event | ||
async function handleEnter() { | ||
if (!filteredMenu.value[activeIndex.value]) return false; | ||
jump(filteredMenu.value[activeIndex.value]); | ||
return true; | ||
} | ||
|
||
return { | ||
search, | ||
filteredMenu, | ||
activeIndex, | ||
handleUp, | ||
handleDown, | ||
handleEnter | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/layout/components/header/components/topBar/components/right/index.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters