feat(widget): simplify search and user card widgets

This commit is contained in:
kate522 committed 2026-08-15 16:35:47 +08:00
1 parent 8a23a3d526
commit 1067b9717b
3 files changed
+118 -171

No files matched your search

+61
View File
@@ -402,6 +402,67 @@
getSearchType: getSearchType,
searchConfig: searchConfig,
});
initWidgetKeywordRefresh();
}
function initWidgetKeywordRefresh() {
document.querySelectorAll("[data-aksrt-search-keywords]").forEach(function (list) {
if (list.dataset.aksrtSearchKeywordsBound === "1") return;
list.dataset.aksrtSearchKeywordsBound = "1";
var keywords;
try {
keywords = JSON.parse(list.getAttribute("data-keywords") || "[]");
} catch (e) {
keywords = [];
}
if (!Array.isArray(keywords) || !keywords.length) return;
keywords = keywords
.map(function (keyword) { return String(keyword || "").trim(); })
.filter(function (keyword, index, values) { return keyword && values.indexOf(keyword) === index; });
var limit = Math.max(1, parseInt(list.getAttribute("data-keyword-limit") || "12", 10) || 12);
var searchType = list.getAttribute("data-search-type") || "post";
var searchUrl = list.getAttribute("data-search-url") || window.location.href;
var refreshButton = list.parentElement
? list.parentElement.querySelector("[data-aksrt-search-keyword-refresh]")
: null;
function shuffledKeywords() {
var shuffled = keywords.slice();
for (var i = shuffled.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var current = shuffled[i];
shuffled[i] = shuffled[j];
shuffled[j] = current;
}
return shuffled.slice(0, limit);
}
function renderKeywords() {
list.replaceChildren();
shuffledKeywords().forEach(function (keyword) {
var link = document.createElement("a");
var url = new URL(searchUrl, window.location.href);
url.searchParams.set("s", keyword);
url.searchParams.set("aksrt_search_type", searchType);
link.className = "aksrt-search-widget__kw";
link.href = url.toString();
link.textContent = keyword;
list.appendChild(link);
});
}
if (refreshButton) {
refreshButton.addEventListener("click", function () {
refreshButton.classList.add("is-refreshing");
renderKeywords();
window.setTimeout(function () {
refreshButton.classList.remove("is-refreshing");
}, 200);
});
}
});
}
function initWidgetSearchHistory(ctx) {