2023-08-10 14:48:22 +08:00
|
|
|
|
<script lang="ts" setup>
|
|
|
|
|
import { computed, ref, shallowRef } from "vue"
|
|
|
|
|
import { type RouteRecordName, type RouteRecordRaw, useRouter } from "vue-router"
|
|
|
|
|
import { usePermissionStore } from "@/store/modules/permission"
|
|
|
|
|
import SearchResult from "./SearchResult.vue"
|
|
|
|
|
import SearchFooter from "./SearchFooter.vue"
|
2023-08-14 17:08:44 +08:00
|
|
|
|
import { ElMessage, ElScrollbar } from "element-plus"
|
2023-08-10 14:48:22 +08:00
|
|
|
|
import { cloneDeep, debounce } from "lodash-es"
|
2024-02-06 13:39:56 +08:00
|
|
|
|
import { useDevice } from "@/hooks/useDevice"
|
2023-08-22 16:06:49 +08:00
|
|
|
|
import { isExternal } from "@/utils/validate"
|
2023-08-10 14:48:22 +08:00
|
|
|
|
|
2024-02-04 17:14:24 +08:00
|
|
|
|
/** 控制 modal 显隐 */
|
|
|
|
|
const modelValue = defineModel<boolean>({ required: true })
|
2023-08-10 14:48:22 +08:00
|
|
|
|
|
|
|
|
|
const router = useRouter()
|
2024-02-06 13:39:56 +08:00
|
|
|
|
const { isMobile } = useDevice()
|
2023-08-10 14:48:22 +08:00
|
|
|
|
|
|
|
|
|
const inputRef = ref<HTMLInputElement | null>(null)
|
|
|
|
|
const scrollbarRef = ref<InstanceType<typeof ElScrollbar> | null>(null)
|
|
|
|
|
const searchResultRef = ref<InstanceType<typeof SearchResult> | null>(null)
|
|
|
|
|
|
2023-08-14 16:00:12 +08:00
|
|
|
|
const keyword = ref<string>("")
|
2023-08-10 14:48:22 +08:00
|
|
|
|
const resultList = shallowRef<RouteRecordRaw[]>([])
|
2023-08-15 13:36:54 +08:00
|
|
|
|
const activeRouteName = ref<RouteRecordName | undefined>(undefined)
|
2023-08-14 16:00:12 +08:00
|
|
|
|
/** 是否按下了上键或下键(用于解决和 mouseenter 事件的冲突) */
|
|
|
|
|
const isPressUpOrDown = ref<boolean>(false)
|
2023-08-10 14:48:22 +08:00
|
|
|
|
|
|
|
|
|
/** 控制搜索对话框宽度 */
|
2024-02-06 13:39:56 +08:00
|
|
|
|
const modalWidth = computed(() => (isMobile.value ? "80vw" : "40vw"))
|
2023-08-10 14:48:22 +08:00
|
|
|
|
/** 树形菜单 */
|
|
|
|
|
const menusData = computed(() => cloneDeep(usePermissionStore().routes))
|
|
|
|
|
|
|
|
|
|
/** 搜索(防抖) */
|
|
|
|
|
const handleSearch = debounce(() => {
|
|
|
|
|
const flatMenusData = flatTree(menusData.value)
|
|
|
|
|
resultList.value = flatMenusData.filter((menu) =>
|
|
|
|
|
keyword.value ? menu.meta?.title?.toLocaleLowerCase().includes(keyword.value.toLocaleLowerCase().trim()) : false
|
|
|
|
|
)
|
|
|
|
|
// 默认选中搜索结果的第一项
|
|
|
|
|
const length = resultList.value?.length
|
2023-08-15 13:36:54 +08:00
|
|
|
|
activeRouteName.value = length > 0 ? resultList.value[0].name : undefined
|
2023-08-10 14:48:22 +08:00
|
|
|
|
}, 500)
|
|
|
|
|
|
|
|
|
|
/** 将树形菜单扁平化为一维数组,用于菜单搜索 */
|
|
|
|
|
const flatTree = (arr: RouteRecordRaw[], result: RouteRecordRaw[] = []) => {
|
|
|
|
|
arr.forEach((item) => {
|
|
|
|
|
result.push(item)
|
|
|
|
|
item.children && flatTree(item.children, result)
|
|
|
|
|
})
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 关闭搜索对话框 */
|
|
|
|
|
const handleClose = () => {
|
2024-02-04 17:14:24 +08:00
|
|
|
|
modelValue.value = false
|
2023-08-10 14:48:22 +08:00
|
|
|
|
// 延时处理防止用户看到重置数据的操作
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
keyword.value = ""
|
|
|
|
|
resultList.value = []
|
|
|
|
|
}, 200)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 根据下标位置进行滚动 */
|
|
|
|
|
const scrollTo = (index: number) => {
|
2023-08-14 16:00:12 +08:00
|
|
|
|
if (!searchResultRef.value) return
|
|
|
|
|
const scrollTop = searchResultRef.value.getScrollTop(index)
|
2023-08-10 14:48:22 +08:00
|
|
|
|
// 手动控制 el-scrollbar 滚动条滚动,设置滚动条到顶部的距离
|
2023-08-14 16:00:12 +08:00
|
|
|
|
scrollbarRef.value?.setScrollTop(scrollTop)
|
2023-08-10 14:48:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 键盘上键 */
|
|
|
|
|
const handleUp = () => {
|
2023-08-14 16:00:12 +08:00
|
|
|
|
isPressUpOrDown.value = true
|
2023-08-10 14:48:22 +08:00
|
|
|
|
const { length } = resultList.value
|
|
|
|
|
if (length === 0) return
|
2023-08-15 13:36:54 +08:00
|
|
|
|
// 获取该 name 在菜单中第一次出现的位置
|
2023-08-10 14:48:22 +08:00
|
|
|
|
const index = resultList.value.findIndex((item) => item.name === activeRouteName.value)
|
2023-08-15 13:36:54 +08:00
|
|
|
|
// 如果已处在顶部
|
2023-08-10 14:48:22 +08:00
|
|
|
|
if (index === 0) {
|
2023-08-15 13:36:54 +08:00
|
|
|
|
const bottomName = resultList.value[length - 1].name
|
|
|
|
|
// 如果顶部和底部的 bottomName 相同,且长度大于 1,就再跳一个位置(可解决遇到首尾两个相同 name 导致的上键不能生效的问题)
|
|
|
|
|
if (activeRouteName.value === bottomName && length > 1) {
|
|
|
|
|
activeRouteName.value = resultList.value[length - 2].name
|
|
|
|
|
scrollTo(length - 2)
|
|
|
|
|
} else {
|
|
|
|
|
// 跳转到底部
|
|
|
|
|
activeRouteName.value = bottomName
|
|
|
|
|
scrollTo(length - 1)
|
|
|
|
|
}
|
2023-08-10 14:48:22 +08:00
|
|
|
|
} else {
|
2023-08-15 13:36:54 +08:00
|
|
|
|
activeRouteName.value = resultList.value[index - 1].name
|
2023-08-10 14:48:22 +08:00
|
|
|
|
scrollTo(index - 1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 键盘下键 */
|
|
|
|
|
const handleDown = () => {
|
2023-08-14 16:00:12 +08:00
|
|
|
|
isPressUpOrDown.value = true
|
2023-08-10 14:48:22 +08:00
|
|
|
|
const { length } = resultList.value
|
|
|
|
|
if (length === 0) return
|
2023-08-15 13:36:54 +08:00
|
|
|
|
// 获取该 name 在菜单中最后一次出现的位置(可解决遇到连续两个相同 name 导致的下键不能生效的问题)
|
|
|
|
|
const index = resultList.value.map((item) => item.name).lastIndexOf(activeRouteName.value)
|
|
|
|
|
// 如果已处在底部
|
2023-08-10 14:48:22 +08:00
|
|
|
|
if (index === length - 1) {
|
2023-08-15 13:36:54 +08:00
|
|
|
|
const topName = resultList.value[0].name
|
|
|
|
|
// 如果底部和顶部的 topName 相同,且长度大于 1,就再跳一个位置(可解决遇到首尾两个相同 name 导致的下键不能生效的问题)
|
|
|
|
|
if (activeRouteName.value === topName && length > 1) {
|
|
|
|
|
activeRouteName.value = resultList.value[1].name
|
|
|
|
|
scrollTo(1)
|
|
|
|
|
} else {
|
|
|
|
|
// 跳转到顶部
|
|
|
|
|
activeRouteName.value = topName
|
|
|
|
|
scrollTo(0)
|
|
|
|
|
}
|
2023-08-10 14:48:22 +08:00
|
|
|
|
} else {
|
2023-08-15 13:36:54 +08:00
|
|
|
|
activeRouteName.value = resultList.value[index + 1].name
|
2023-08-10 14:48:22 +08:00
|
|
|
|
scrollTo(index + 1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 键盘回车键 */
|
|
|
|
|
const handleEnter = () => {
|
|
|
|
|
const { length } = resultList.value
|
2023-08-14 17:08:44 +08:00
|
|
|
|
if (length === 0) return
|
2023-08-15 13:36:54 +08:00
|
|
|
|
const name = activeRouteName.value
|
2023-08-22 16:06:49 +08:00
|
|
|
|
const path = resultList.value.find((item) => item.name === name)?.path
|
|
|
|
|
if (path && isExternal(path)) {
|
|
|
|
|
window.open(path, "_blank", "noopener, noreferrer")
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-08-15 13:36:54 +08:00
|
|
|
|
if (!name) {
|
|
|
|
|
ElMessage.warning("无法通过搜索进入该菜单,请为对应的路由设置唯一的 Name")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
router.push({ name })
|
|
|
|
|
} catch {
|
|
|
|
|
ElMessage.error("该菜单有必填的动态参数,无法通过搜索进入")
|
2023-08-14 17:08:44 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-08-10 14:48:22 +08:00
|
|
|
|
handleClose()
|
|
|
|
|
}
|
2023-08-14 16:00:12 +08:00
|
|
|
|
|
|
|
|
|
/** 释放上键或下键 */
|
|
|
|
|
const handleReleaseUpOrDown = () => {
|
|
|
|
|
isPressUpOrDown.value = false
|
|
|
|
|
}
|
2023-08-10 14:48:22 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
|
|
|
|
<el-dialog
|
2024-02-04 17:14:24 +08:00
|
|
|
|
v-model="modelValue"
|
2023-08-10 14:48:22 +08:00
|
|
|
|
@opened="inputRef?.focus()"
|
|
|
|
|
@closed="inputRef?.blur()"
|
|
|
|
|
@keydown.up="handleUp"
|
|
|
|
|
@keydown.down="handleDown"
|
|
|
|
|
@keydown.enter="handleEnter"
|
2023-08-14 16:00:12 +08:00
|
|
|
|
@keyup.up.down="handleReleaseUpOrDown"
|
2023-08-10 14:48:22 +08:00
|
|
|
|
:before-close="handleClose"
|
|
|
|
|
:width="modalWidth"
|
|
|
|
|
top="5vh"
|
|
|
|
|
class="search-modal__private"
|
|
|
|
|
append-to-body
|
|
|
|
|
>
|
|
|
|
|
<el-input ref="inputRef" v-model="keyword" @input="handleSearch" placeholder="搜索菜单" size="large" clearable>
|
|
|
|
|
<template #prefix>
|
|
|
|
|
<SvgIcon name="search" />
|
|
|
|
|
</template>
|
|
|
|
|
</el-input>
|
|
|
|
|
<el-empty v-if="resultList.length === 0" description="暂无搜索结果" :image-size="100" />
|
|
|
|
|
<template v-else>
|
|
|
|
|
<p>搜索结果</p>
|
2023-08-14 16:00:12 +08:00
|
|
|
|
<el-scrollbar ref="scrollbarRef" max-height="40vh" always>
|
|
|
|
|
<SearchResult
|
|
|
|
|
ref="searchResultRef"
|
|
|
|
|
v-model="activeRouteName"
|
|
|
|
|
:list="resultList"
|
|
|
|
|
:isPressUpOrDown="isPressUpOrDown"
|
|
|
|
|
@click="handleEnter"
|
|
|
|
|
/>
|
2023-08-10 14:48:22 +08:00
|
|
|
|
</el-scrollbar>
|
|
|
|
|
</template>
|
|
|
|
|
<template #footer>
|
|
|
|
|
<SearchFooter :total="resultList.length" />
|
|
|
|
|
</template>
|
|
|
|
|
</el-dialog>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
|
.search-modal__private {
|
|
|
|
|
.svg-icon {
|
|
|
|
|
font-size: 18px;
|
|
|
|
|
}
|
|
|
|
|
.el-dialog__header {
|
|
|
|
|
display: none;
|
|
|
|
|
}
|
|
|
|
|
.el-dialog__footer {
|
|
|
|
|
border-top: 1px solid var(--el-border-color);
|
|
|
|
|
padding: var(--el-dialog-padding-primary);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</style>
|