feat: 搜索建议自动补全
- 搜索框输入时显示下拉建议列表(最多6条) - 建议项显示首字母图标+标题+分类名 - 支持高亮匹配文字 - 点击建议项直接打开网址 - blur延迟隐藏,防止点击丢失
This commit is contained in:
@@ -15,12 +15,28 @@
|
||||
v-model="search"
|
||||
placeholder="搜索网址..."
|
||||
class="search-box"
|
||||
@input="filterLinks"
|
||||
@input="onSearchInput"
|
||||
@focus="showSuggestions = true"
|
||||
@blur="hideSuggestions"
|
||||
>
|
||||
<template #prefix>
|
||||
<el-icon><Search /></el-icon>
|
||||
</template>
|
||||
</el-input>
|
||||
<div v-if="showSuggestions && suggestions.length > 0" class="suggestions-dropdown">
|
||||
<div
|
||||
v-for="s in suggestions"
|
||||
:key="s.id"
|
||||
class="suggestion-item"
|
||||
@mousedown.prevent="jumpTo(s.url)"
|
||||
>
|
||||
<span class="sug-icon">{{ s.title[0] }}</span>
|
||||
<div class="sug-info">
|
||||
<span class="sug-title" v-html="highlight(s.title, search)"></span>
|
||||
<span class="sug-cat">{{ s.categoryName }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -83,6 +99,9 @@ const search = ref('')
|
||||
const filteredLinks = ref<any[]>([])
|
||||
const searchTimer = ref<ReturnType<typeof setTimeout>>()
|
||||
const collapsedCats = ref<Set<number>>(new Set())
|
||||
const showSuggestions = ref(false)
|
||||
const suggestions = ref<any[]>([])
|
||||
let suggestTimer: ReturnType<typeof setTimeout>
|
||||
|
||||
function toggleCat(id: number) {
|
||||
const s = new Set(collapsedCats.value)
|
||||
@@ -101,13 +120,30 @@ function linksInCat(catId: number) {
|
||||
}
|
||||
|
||||
function filterLinks() {
|
||||
if (searchTimer.value) clearTimeout(searchTimer.value)
|
||||
searchTimer.value = setTimeout(() => {
|
||||
const q = search.value.trim().toLowerCase()
|
||||
filteredLinks.value = q
|
||||
? data.links.filter((l: any) => l.title.toLowerCase().includes(q) || l.description.toLowerCase().includes(q))
|
||||
: [...data.links]
|
||||
}, 200)
|
||||
}
|
||||
|
||||
function onSearchInput() {
|
||||
filterLinks()
|
||||
clearTimeout(suggestTimer)
|
||||
const q = search.value.trim().toLowerCase()
|
||||
if (q.length < 1) { suggestions.value = []; return }
|
||||
suggestTimer = setTimeout(() => {
|
||||
suggestions.value = data.links
|
||||
.filter((l: any) => l.title.toLowerCase().includes(q) || l.description.toLowerCase().includes(q))
|
||||
.slice(0, 6)
|
||||
}, 100)
|
||||
}
|
||||
|
||||
function hideSuggestions() {
|
||||
setTimeout(() => { showSuggestions.value = false }, 200)
|
||||
}
|
||||
|
||||
function jumpTo(url: string) {
|
||||
window.open(url, '_blank')
|
||||
}
|
||||
|
||||
function highlight(text: string, keyword: string) {
|
||||
@@ -163,6 +199,7 @@ loadData()
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 24px;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.brand {
|
||||
@@ -178,6 +215,48 @@ loadData()
|
||||
|
||||
.search-box { width: 320px; }
|
||||
|
||||
.suggestions-dropdown {
|
||||
position: absolute;
|
||||
top: 100%;
|
||||
right: 0;
|
||||
width: 320px;
|
||||
background: white;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 8px 24px rgba(0,0,0,0.12);
|
||||
z-index: 20;
|
||||
overflow: hidden;
|
||||
margin-top: 4px;
|
||||
}
|
||||
.suggestion-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
padding: 10px 14px;
|
||||
cursor: pointer;
|
||||
transition: background 0.15s;
|
||||
}
|
||||
.suggestion-item:hover { background: #f5f7fa; }
|
||||
.sug-icon {
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border-radius: 6px;
|
||||
background: linear-gradient(135deg, #6366f1, #818cf8);
|
||||
color: white;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.sug-info { display: flex; flex-direction: column; min-width: 0; }
|
||||
.sug-title { font-size: 14px; font-weight: 500; color: #333; }
|
||||
.sug-cat { font-size: 11px; color: #888; margin-top: 2px; }
|
||||
html.dark .suggestions-dropdown { background: var(--bg-card); }
|
||||
html.dark .suggestion-item:hover { background: var(--hover-bg); }
|
||||
html.dark .sug-title { color: var(--text-primary); }
|
||||
html.dark .sug-cat { color: var(--text-secondary); }
|
||||
|
||||
.admin-link {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
|
||||
Reference in New Issue
Block a user