feat(auto): 搜索建议

This commit is contained in:
irony 2025-12-06 21:02:17 +08:00
parent 8af87cd9f4
commit e67479489a
2 changed files with 32 additions and 7 deletions

View File

@ -383,10 +383,7 @@
/* 搜索建议 */
.search-suggestions {
position: absolute;
top: calc(var(--header-height) + 1px);
left: 0;
right: 0;
position: relative;
background: var(--bg-primary);
border-bottom: 1px solid var(--border-color);
max-height: 60vh;

View File

@ -177,6 +177,8 @@ const App = (function() {
$('#search-clear').addClass('hidden');
$('#search-results').html('<div class="search-hint">输入关键词搜索小说</div>');
$('#search-suggestions').html('').addClass('hidden');
// 清空时重新显示搜索历史和标签
renderSearchTags();
}
});
@ -419,6 +421,27 @@ const App = (function() {
$(this).text($text.hasClass('text-clamp-3') ? '展开' : '收起');
});
// 点击搜索建议项
$(document).on('click', '.suggestion-item', function(e) {
e.preventDefault();
e.stopPropagation();
const novelId = $(this).data('novel-id');
const author = $(this).data('author');
if (novelId) {
// 跳转到小说详情
hideSearch();
setTimeout(() => {
navigate('#/novel/' + novelId);
}, 100);
} else if (author) {
// 按作者搜索
$('#search-input').val(author);
$('#search-suggestions').html('').addClass('hidden');
doSearch(author);
}
});
// 加载更多按钮使用事件委托在pages.js中也有绑定这里作为备用
$(document).on('click', '.load-more-btn', function() {
if (Pages.hasMore()) {
@ -447,23 +470,27 @@ const App = (function() {
}
try {
console.log('正在加载搜索建议,关键词:', keyword);
const suggestions = await API.getSearchSuggestions(keyword, 8);
if (suggestions.length === 0) {
console.log('搜索建议结果:', suggestions);
if (!suggestions || suggestions.length === 0) {
$('#search-suggestions').html('').addClass('hidden');
return;
}
let html = '<div class="suggestions-list">';
suggestions.forEach(suggestion => {
if (!suggestion || !suggestion.text) return;
const highlightText = highlightKeyword(suggestion.text, keyword);
if (suggestion.novelId) {
html += `<div class="suggestion-item" data-novel-id="${suggestion.novelId}">
<span class="suggestion-type">${suggestion.type}</span>
<span class="suggestion-type">${suggestion.type || '书名'}</span>
<span class="suggestion-text">${highlightText}</span>
</div>`;
} else {
html += `<div class="suggestion-item" data-author="${suggestion.text}">
<span class="suggestion-type">${suggestion.type}</span>
<span class="suggestion-type">${suggestion.type || '作者'}</span>
<span class="suggestion-text">${highlightText}</span>
</div>`;
}
@ -471,6 +498,7 @@ const App = (function() {
html += '</div>';
$('#search-suggestions').html(html).removeClass('hidden');
console.log('搜索建议已显示HTML长度:', html.length);
} catch (error) {
console.error('加载搜索建议失败:', error);
$('#search-suggestions').html('').addClass('hidden');