feat: 增加视频清单搜索,并在开发期缓存剧目清单

This commit is contained in:
Nixevol
2026-09-27 04:21:00 +08:00
parent cc930cd1ed
commit f010426f94
7 changed files with 268 additions and 21 deletions
+153 -8
View File
@@ -23,8 +23,16 @@ const expanded = ref<string[]>([]);
const selected = ref<Set<number>>(new Set());
const busy = ref(false);
const error = ref("");
const query = ref("");
const searchHint = ref("");
const flashKey = ref("");
let catalogAbort: AbortController | null = null;
let catalogGeneration = 0;
let searchCursor = 0;
let lastQuery = "";
let revealGeneration = 0;
const flashSerial = ref(0);
const scrollEl = ref<HTMLElement | null>(null);
function coverOf(image: string) {
if (!image) return "";
@@ -98,7 +106,7 @@ function applyPage(event: { key: string; name: string; note?: string; items?: Dr
if (event.done) section.loading = false;
}
async function reload() {
async function reload(refresh = false) {
catalogAbort?.abort();
const current = new AbortController();
catalogAbort = current;
@@ -109,7 +117,7 @@ async function reload() {
const response = await fetch("/api/catalog", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ token: props.token, device: props.device }),
body: JSON.stringify({ token: props.token, device: props.device, refresh }),
signal: current.signal,
});
if (!response.ok || !response.body) {
@@ -157,14 +165,124 @@ async function reload() {
}
}
function cardKey(sectionKey: string, id: number) {
return `${sectionKey}-${id}`;
}
function matches() {
const keyword = query.value.trim().toLowerCase();
const found: { sectionKey: string; id: number }[] = [];
if (!keyword) return found;
for (const section of sections.value) {
for (const item of section.items) {
if (item.title.toLowerCase().includes(keyword)) {
found.push({ sectionKey: section.key, id: item.id });
}
}
}
return found;
}
function clearSearch() {
searchHint.value = "";
flashKey.value = "";
lastQuery = "";
searchCursor = 0;
}
function finishFlash(key: string, serial: number, event: AnimationEvent) {
if (event.animationName !== "drama-flash") return;
if (flashKey.value === key && flashSerial.value === serial) flashKey.value = "";
}
function cardInView(node: HTMLElement, scroller: HTMLElement) {
const card = node.getBoundingClientRect();
const view = scroller.getBoundingClientRect();
return card.top >= view.top && card.bottom <= view.bottom;
}
function scrollCardIntoView(node: HTMLElement) {
const scroller = scrollEl.value;
return new Promise<void>((resolve) => {
if (!scroller) {
resolve();
return;
}
node.scrollIntoView({ block: "center", behavior: "smooth" });
let last = scroller.scrollTop;
let stable = 0;
let moved = false;
const started = performance.now();
const tick = () => {
const current = scroller.scrollTop;
if (Math.abs(current - last) >= 0.5) {
moved = true;
stable = 0;
} else {
stable += 1;
}
last = current;
const arrived = cardInView(node, scroller);
const stopped = moved ? stable >= 4 : stable >= 8;
if ((stopped && arrived) || performance.now() - started > 3000) {
resolve();
return;
}
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
});
}
async function find(keyword = query.value) {
const generation = ++revealGeneration;
const text = keyword.trim();
query.value = text;
if (!text) {
searchHint.value = "请输入名称";
return;
}
const found = matches();
if (!found.length) {
searchHint.value = "没有找到";
flashKey.value = "";
return;
}
if (text.toLowerCase() !== lastQuery) {
lastQuery = text.toLowerCase();
searchCursor = 0;
} else {
searchCursor = (searchCursor + 1) % found.length;
}
const hit = found[searchCursor];
const key = cardKey(hit.sectionKey, hit.id);
searchHint.value = found.length > 1 ? `${searchCursor + 1}/${found.length}` : "";
flashKey.value = "";
if (!isExpanded(hit.sectionKey)) {
expanded.value = [...expanded.value, hit.sectionKey];
}
await nextTick();
if (generation !== revealGeneration) return;
const node = scrollEl.value?.querySelector(`[data-card="${key}"]`);
if (node instanceof HTMLElement) await scrollCardIntoView(node);
if (generation !== revealGeneration) return;
if (node instanceof HTMLElement && scrollEl.value && !cardInView(node, scrollEl.value)) {
node.scrollIntoView({ block: "center", behavior: "auto" });
}
flashSerial.value += 1;
flashKey.value = "";
await nextTick();
if (generation !== revealGeneration) return;
flashKey.value = key;
}
onMounted(reload);
defineExpose({ reload, busy });
defineExpose({ reload, busy, find, searchHint, clearSearch });
</script>
<template>
<div class="download-page">
<div class="download-body">
<div class="download-scroll">
<div ref="scrollEl" class="download-scroll">
<n-alert v-if="error" class="catalog-error" type="error" :show-icon="false">{{ error }}</n-alert>
<section v-for="section in sections" :key="section.key" class="category">
<div class="category-bar">
@@ -189,7 +307,13 @@ defineExpose({ reload, busy });
<n-text v-if="section.note" depth="3">{{ section.note }}</n-text>
<n-text v-else-if="!section.loading && section.items.length === 0" depth="3">暂无剧集</n-text>
<div v-else class="drama-grid">
<article v-for="item in section.items" :key="item.id" class="drama-card" @click="toggleDrama(item.id, !selected.has(item.id))">
<article
v-for="item in section.items"
:key="item.id"
class="drama-card"
:data-card="cardKey(section.key, item.id)"
@click="toggleDrama(item.id, !selected.has(item.id))"
>
<div class="cover">
<img v-if="item.image" :src="coverOf(item.image)" :alt="item.title" />
</div>
@@ -197,6 +321,12 @@ defineExpose({ reload, busy });
<n-checkbox :checked="selected.has(item.id)" @update:checked="toggleDrama(item.id, $event)" />
</div>
<div class="drama-title">{{ item.title }}</div>
<div
v-if="flashKey === cardKey(section.key, item.id)"
:key="flashSerial"
class="drama-mask"
@animationend="finishFlash(cardKey(section.key, item.id), flashSerial, $event)"
></div>
</article>
</div>
</div>
@@ -210,10 +340,12 @@ defineExpose({ reload, busy });
</template>
<style>
.download-page { height: 100%; min-height: 0; display: flex; flex-direction: column; }
.download-body { position: relative; flex: 1; min-height: 0; }
.download-scroll { height: 100%; overflow: auto; padding: 0 0 12px; }
.download-page { height: 100%; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
.download-body { position: relative; flex: 1; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
.download-scroll { flex: 1; min-height: 0; overflow: auto; padding: 0 0 12px; }
.download-foot {
position: relative;
z-index: 2;
flex: none;
display: flex;
align-items: center;
@@ -278,5 +410,18 @@ defineExpose({ reload, busy });
border-radius: 6px;
background: rgba(0, 0, 0, 0.45);
}
.drama-mask {
position: absolute;
inset: 0;
z-index: 2;
pointer-events: none;
opacity: 0;
background: rgba(99, 226, 183, 0.42);
animation: drama-flash 0.45s ease-in-out 3;
}
@keyframes drama-flash {
0%, 100% { opacity: 0; }
50% { opacity: 1; }
}
.drama-title { padding: 8px; font-size: 13px; line-height: 1.35; }
</style>