feat: ✨ 菜单搜索添加历史记录
This commit is contained in:
@@ -28,27 +28,68 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<div class="search-result">
|
<div class="search-result">
|
||||||
<ul v-if="displayResults.length > 0">
|
<!-- 搜索历史 -->
|
||||||
<li
|
<template v-if="searchKeyword === '' && searchHistory.length > 0">
|
||||||
v-for="(item, index) in displayResults"
|
<div class="search-history">
|
||||||
:key="item.path"
|
<div class="search-history__title">
|
||||||
:class="[
|
搜索历史
|
||||||
'search-result__item',
|
<el-button
|
||||||
{
|
type="primary"
|
||||||
'search-result__item--active': index === activeIndex,
|
text
|
||||||
},
|
size="small"
|
||||||
]"
|
class="search-history__clear"
|
||||||
@click="navigateToRoute(item)"
|
@click="clearHistory"
|
||||||
>
|
>
|
||||||
<el-icon v-if="item.icon && item.icon.startsWith('el-icon')">
|
<el-icon><Delete /></el-icon>
|
||||||
<component :is="item.icon.replace('el-icon-', '')" />
|
</el-button>
|
||||||
</el-icon>
|
</div>
|
||||||
<div v-else-if="item.icon" :class="`i-svg:${item.icon}`" />
|
<ul class="search-history__list">
|
||||||
<div v-else class="i-svg:menu" />
|
<li
|
||||||
<span class="ml-2">{{ item.title }}</span>
|
v-for="(item, index) in searchHistory"
|
||||||
</li>
|
:key="index"
|
||||||
</ul>
|
class="search-history__item"
|
||||||
<el-empty v-else description="暂无数据" />
|
@click="navigateToRoute(item)"
|
||||||
|
>
|
||||||
|
<div class="search-history__icon">
|
||||||
|
<el-icon><Clock /></el-icon>
|
||||||
|
</div>
|
||||||
|
<span class="search-history__name">{{ item.title }}</span>
|
||||||
|
<div class="search-history__action">
|
||||||
|
<el-icon @click.stop="removeHistoryItem(index)"><Close /></el-icon>
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- 搜索结果 -->
|
||||||
|
<template v-else>
|
||||||
|
<ul v-if="displayResults.length > 0">
|
||||||
|
<li
|
||||||
|
v-for="(item, index) in displayResults"
|
||||||
|
:key="item.path"
|
||||||
|
:class="[
|
||||||
|
'search-result__item',
|
||||||
|
{
|
||||||
|
'search-result__item--active': index === activeIndex,
|
||||||
|
},
|
||||||
|
]"
|
||||||
|
@click="navigateToRoute(item)"
|
||||||
|
>
|
||||||
|
<el-icon v-if="item.icon && item.icon.startsWith('el-icon')">
|
||||||
|
<component :is="item.icon.replace('el-icon-', '')" />
|
||||||
|
</el-icon>
|
||||||
|
<div v-else-if="item.icon" :class="`i-svg:${item.icon}`" />
|
||||||
|
<div v-else class="i-svg:menu" />
|
||||||
|
<span class="ml-2">{{ item.title }}</span>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- 无搜索历史显示 -->
|
||||||
|
<div v-if="searchKeyword === '' && searchHistory.length === 0" class="no-history">
|
||||||
|
<p class="no-history__text">没有搜索历史</p>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<template #footer>
|
<template #footer>
|
||||||
@@ -83,10 +124,15 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import { ref, computed, onMounted, onBeforeUnmount } from "vue";
|
||||||
import router from "@/router";
|
import router from "@/router";
|
||||||
import { usePermissionStore } from "@/store";
|
import { usePermissionStore } from "@/store";
|
||||||
import { isExternal } from "@/utils";
|
import { isExternal } from "@/utils";
|
||||||
import { RouteRecordRaw } from "vue-router";
|
import { RouteRecordRaw } from "vue-router";
|
||||||
|
import { Clock, Close, Delete } from "@element-plus/icons-vue";
|
||||||
|
|
||||||
|
const HISTORY_KEY = "menu_search_history";
|
||||||
|
const MAX_HISTORY = 5;
|
||||||
|
|
||||||
const permissionStore = usePermissionStore();
|
const permissionStore = usePermissionStore();
|
||||||
const isModalVisible = ref(false);
|
const isModalVisible = ref(false);
|
||||||
@@ -96,6 +142,7 @@ const excludedRoutes = ref(["/redirect", "/login", "/401", "/404"]);
|
|||||||
const menuItems = ref<SearchItem[]>([]);
|
const menuItems = ref<SearchItem[]>([]);
|
||||||
const searchResults = ref<SearchItem[]>([]);
|
const searchResults = ref<SearchItem[]>([]);
|
||||||
const activeIndex = ref(-1);
|
const activeIndex = ref(-1);
|
||||||
|
const searchHistory = ref<SearchItem[]>([]);
|
||||||
|
|
||||||
interface SearchItem {
|
interface SearchItem {
|
||||||
title: string;
|
title: string;
|
||||||
@@ -105,6 +152,57 @@ interface SearchItem {
|
|||||||
redirect?: string;
|
redirect?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 从本地存储加载搜索历史
|
||||||
|
function loadSearchHistory() {
|
||||||
|
const historyStr = localStorage.getItem(HISTORY_KEY);
|
||||||
|
if (historyStr) {
|
||||||
|
try {
|
||||||
|
searchHistory.value = JSON.parse(historyStr);
|
||||||
|
} catch {
|
||||||
|
searchHistory.value = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存搜索历史到本地存储
|
||||||
|
function saveSearchHistory() {
|
||||||
|
localStorage.setItem(HISTORY_KEY, JSON.stringify(searchHistory.value));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加项目到搜索历史
|
||||||
|
function addToHistory(item: SearchItem) {
|
||||||
|
// 检查是否已存在
|
||||||
|
const index = searchHistory.value.findIndex((i) => i.path === item.path);
|
||||||
|
|
||||||
|
// 如果存在则移除
|
||||||
|
if (index !== -1) {
|
||||||
|
searchHistory.value.splice(index, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加到历史开头
|
||||||
|
searchHistory.value.unshift(item);
|
||||||
|
|
||||||
|
// 限制历史记录数量
|
||||||
|
if (searchHistory.value.length > MAX_HISTORY) {
|
||||||
|
searchHistory.value = searchHistory.value.slice(0, MAX_HISTORY);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存到本地存储
|
||||||
|
saveSearchHistory();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 移除历史记录项
|
||||||
|
function removeHistoryItem(index: number) {
|
||||||
|
searchHistory.value.splice(index, 1);
|
||||||
|
saveSearchHistory();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清空历史记录
|
||||||
|
function clearHistory() {
|
||||||
|
searchHistory.value = [];
|
||||||
|
localStorage.removeItem(HISTORY_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
// 注册全局快捷键
|
// 注册全局快捷键
|
||||||
function handleKeyDown(e: KeyboardEvent) {
|
function handleKeyDown(e: KeyboardEvent) {
|
||||||
// 判断是否为Ctrl+K组合键
|
// 判断是否为Ctrl+K组合键
|
||||||
@@ -117,6 +215,7 @@ function handleKeyDown(e: KeyboardEvent) {
|
|||||||
// 添加键盘事件监听
|
// 添加键盘事件监听
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
loadRoutes(permissionStore.routes);
|
loadRoutes(permissionStore.routes);
|
||||||
|
loadSearchHistory();
|
||||||
document.addEventListener("keydown", handleKeyDown);
|
document.addEventListener("keydown", handleKeyDown);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -179,6 +278,9 @@ function navigateResults(direction: string) {
|
|||||||
// 跳转到
|
// 跳转到
|
||||||
function navigateToRoute(item: SearchItem) {
|
function navigateToRoute(item: SearchItem) {
|
||||||
closeSearchModal();
|
closeSearchModal();
|
||||||
|
// 添加到历史记录
|
||||||
|
addToHistory(item);
|
||||||
|
|
||||||
if (isExternal(item.path)) {
|
if (isExternal(item.path)) {
|
||||||
window.open(item.path, "_blank");
|
window.open(item.path, "_blank");
|
||||||
} else {
|
} else {
|
||||||
@@ -232,6 +334,92 @@ function loadRoutes(routes: RouteRecordRaw[], parentPath = "") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 搜索历史样式 */
|
||||||
|
.search-history {
|
||||||
|
&__title {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
padding: 0 12px;
|
||||||
|
font-size: 12px;
|
||||||
|
line-height: 34px;
|
||||||
|
color: var(--el-text-color-secondary);
|
||||||
|
}
|
||||||
|
|
||||||
|
&__clear {
|
||||||
|
padding: 2px;
|
||||||
|
font-size: 12px;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
color: var(--el-color-danger);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
&__list {
|
||||||
|
padding: 0;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
&__icon {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
margin-right: 10px;
|
||||||
|
font-size: 16px;
|
||||||
|
color: var(--el-text-color-secondary);
|
||||||
|
}
|
||||||
|
|
||||||
|
&__name {
|
||||||
|
flex: 1;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
font-size: 14px;
|
||||||
|
color: var(--el-text-color-primary);
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
&__action {
|
||||||
|
padding: 4px;
|
||||||
|
color: var(--el-text-color-secondary);
|
||||||
|
border-radius: 4px;
|
||||||
|
opacity: 0;
|
||||||
|
transition: opacity 0.2s;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
color: var(--el-color-danger);
|
||||||
|
background-color: var(--el-fill-color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
&__item {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
height: 40px;
|
||||||
|
padding: 0 12px;
|
||||||
|
cursor: pointer;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
background-color: var(--el-fill-color-light);
|
||||||
|
|
||||||
|
.search-history__action {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 没有搜索历史时的样式 */
|
||||||
|
.no-history {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
height: 100px;
|
||||||
|
|
||||||
|
&__text {
|
||||||
|
font-size: 14px;
|
||||||
|
color: var(--el-text-color-secondary);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.dialog-footer {
|
.dialog-footer {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
@@ -272,13 +460,13 @@ function loadRoutes(routes: RouteRecordRaw[], parentPath = "") {
|
|||||||
height: 20px;
|
height: 20px;
|
||||||
padding: 0 4px;
|
padding: 0 4px;
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
color: #606266;
|
color: var(--el-text-color-regular);
|
||||||
background-color: white;
|
background-color: var(--el-fill-color-blank);
|
||||||
border: 1px solid #cdcde6;
|
border: 1px solid var(--el-border-color);
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
box-shadow:
|
box-shadow:
|
||||||
inset 0 -2px 0 0 #cdcde6,
|
inset 0 -2px 0 0 var(--el-border-color),
|
||||||
inset 0 0 1px 1px #fff,
|
inset 0 0 1px 1px var(--el-color-white),
|
||||||
0 1px 2px rgba(30, 35, 90, 0.2);
|
0 1px 2px rgba(30, 35, 90, 0.2);
|
||||||
|
|
||||||
&::before {
|
&::before {
|
||||||
@@ -301,7 +489,7 @@ function loadRoutes(routes: RouteRecordRaw[], parentPath = "") {
|
|||||||
|
|
||||||
.key-text {
|
.key-text {
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
color: #909399;
|
color: var(--el-text-color-secondary);
|
||||||
}
|
}
|
||||||
|
|
||||||
.ctrl-k-hint {
|
.ctrl-k-hint {
|
||||||
@@ -311,7 +499,7 @@ function loadRoutes(routes: RouteRecordRaw[], parentPath = "") {
|
|||||||
|
|
||||||
.ctrl-k-text {
|
.ctrl-k-text {
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
color: #909399;
|
color: var(--el-text-color-secondary);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 适配Element Plus对话框
|
// 适配Element Plus对话框
|
||||||
@@ -320,4 +508,11 @@ function loadRoutes(routes: RouteRecordRaw[], parentPath = "") {
|
|||||||
padding-top: 10px;
|
padding-top: 10px;
|
||||||
text-align: right;
|
text-align: right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 暗黑模式适配
|
||||||
|
html.dark {
|
||||||
|
.key-btn::before {
|
||||||
|
background: linear-gradient(to bottom, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
Reference in New Issue
Block a user