feat(search): Multi-Wort + Ranking + Keyboard-Nav; fix LinkList-Spalten
- Search: AND-Match über Tokens, Feld-gewichtetes Ranking, ↑↓⏎-Nav, Match-Highlight, Trefferzahl + aria-live - LinkListBlock: breitenbasierte Spalten (auto-fill minmax) statt fixer 4-Zeilen-Grid — verhinderte Zerbröseln bei vielen langen Labels Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { slide } from "svelte/transition";
|
import { slide } from "svelte/transition";
|
||||||
import { tick } from "svelte";
|
import { tick } from "svelte";
|
||||||
|
import { goto } from "$app/navigation";
|
||||||
import Icon from "@iconify/svelte";
|
import Icon from "@iconify/svelte";
|
||||||
import "$lib/iconify-offline";
|
import "$lib/iconify-offline";
|
||||||
|
|
||||||
@@ -25,18 +26,72 @@
|
|||||||
let loadError = $state<string | null>(null);
|
let loadError = $state<string | null>(null);
|
||||||
let inputEl = $state<HTMLInputElement | null>(null);
|
let inputEl = $state<HTMLInputElement | null>(null);
|
||||||
|
|
||||||
|
const MAX_RESULTS = 40;
|
||||||
|
|
||||||
const normalizedQuery = $derived(query.trim().toLowerCase());
|
const normalizedQuery = $derived(query.trim().toLowerCase());
|
||||||
|
const tokens = $derived(normalizedQuery.split(/\s+/).filter((t) => t.length > 0));
|
||||||
|
|
||||||
|
// All tokens must match (AND); score by field weight so title hits rank first.
|
||||||
const results = $derived.by<SearchHit[]>(() => {
|
const results = $derived.by<SearchHit[]>(() => {
|
||||||
if (!allHits || normalizedQuery.length < 2) return [];
|
if (!allHits || normalizedQuery.length < 2) return [];
|
||||||
const q = normalizedQuery;
|
const scored: { hit: SearchHit; score: number }[] = [];
|
||||||
return allHits
|
for (const h of allHits) {
|
||||||
.filter((h) => {
|
const title = h.title.toLowerCase();
|
||||||
const hay = `${h.title} ${h.subtitle} ${h.excerpt} ${h.slug}`.toLowerCase();
|
const subtitle = h.subtitle.toLowerCase();
|
||||||
return hay.includes(q);
|
const excerpt = h.excerpt.toLowerCase();
|
||||||
})
|
const slug = h.slug.toLowerCase();
|
||||||
.slice(0, 40);
|
let score = 0;
|
||||||
|
let allMatch = true;
|
||||||
|
for (const tok of tokens) {
|
||||||
|
const inTitle = title.includes(tok);
|
||||||
|
const inSub = subtitle.includes(tok);
|
||||||
|
const inExcerpt = excerpt.includes(tok);
|
||||||
|
const inSlug = slug.includes(tok);
|
||||||
|
if (!(inTitle || inSub || inExcerpt || inSlug)) {
|
||||||
|
allMatch = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (inTitle) score += title.startsWith(tok) ? 12 : 8;
|
||||||
|
if (inSub) score += 4;
|
||||||
|
if (inExcerpt) score += 2;
|
||||||
|
if (inSlug) score += 1;
|
||||||
|
}
|
||||||
|
if (allMatch) scored.push({ hit: h, score });
|
||||||
|
}
|
||||||
|
scored.sort((a, b) => b.score - a.score);
|
||||||
|
return scored.slice(0, MAX_RESULTS).map((s) => s.hit);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let activeIndex = $state(-1);
|
||||||
|
let itemEls = $state<(HTMLAnchorElement | null)[]>([]);
|
||||||
|
// Reset cursor whenever the result set changes.
|
||||||
|
$effect(() => {
|
||||||
|
void results;
|
||||||
|
activeIndex = -1;
|
||||||
|
});
|
||||||
|
// Keep the keyboard-selected item visible.
|
||||||
|
$effect(() => {
|
||||||
|
if (activeIndex >= 0) itemEls[activeIndex]?.scrollIntoView({ block: "nearest" });
|
||||||
|
});
|
||||||
|
|
||||||
|
function escapeHtml(s: string): string {
|
||||||
|
return s
|
||||||
|
.replace(/&/g, "&")
|
||||||
|
.replace(/</g, "<")
|
||||||
|
.replace(/>/g, ">");
|
||||||
|
}
|
||||||
|
function highlight(text: string): string {
|
||||||
|
const safe = escapeHtml(text);
|
||||||
|
if (tokens.length === 0) return safe;
|
||||||
|
const pattern = tokens
|
||||||
|
.map((t) => t.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
|
||||||
|
.join("|");
|
||||||
|
return safe.replace(
|
||||||
|
new RegExp(`(${pattern})`, "gi"),
|
||||||
|
'<mark class="bg-wald-100 text-inherit rounded-xs px-0.5">$1</mark>',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
async function loadIndex() {
|
async function loadIndex() {
|
||||||
if (allHits || loading) return;
|
if (allHits || loading) return;
|
||||||
loading = true;
|
loading = true;
|
||||||
@@ -70,7 +125,26 @@
|
|||||||
void loadIndex();
|
void loadIndex();
|
||||||
tick().then(() => inputEl?.focus());
|
tick().then(() => inputEl?.focus());
|
||||||
const onKey = (e: KeyboardEvent) => {
|
const onKey = (e: KeyboardEvent) => {
|
||||||
if (e.key === "Escape") closeSearch();
|
if (e.key === "Escape") {
|
||||||
|
closeSearch();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const n = results.length;
|
||||||
|
if (n === 0) return;
|
||||||
|
if (e.key === "ArrowDown") {
|
||||||
|
e.preventDefault();
|
||||||
|
activeIndex = (activeIndex + 1) % n;
|
||||||
|
} else if (e.key === "ArrowUp") {
|
||||||
|
e.preventDefault();
|
||||||
|
activeIndex = (activeIndex - 1 + n) % n;
|
||||||
|
} else if (e.key === "Enter" && activeIndex >= 0) {
|
||||||
|
e.preventDefault();
|
||||||
|
const href = results[activeIndex]?.href;
|
||||||
|
if (href) {
|
||||||
|
closeSearch();
|
||||||
|
void goto(href);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
window.addEventListener("keydown", onKey);
|
window.addEventListener("keydown", onKey);
|
||||||
return () => window.removeEventListener("keydown", onKey);
|
return () => window.removeEventListener("keydown", onKey);
|
||||||
@@ -112,6 +186,11 @@
|
|||||||
</label>
|
</label>
|
||||||
|
|
||||||
<div class="max-w-xl mx-auto mt-4 text-sm">
|
<div class="max-w-xl mx-auto mt-4 text-sm">
|
||||||
|
<p class="sr-only" role="status" aria-live="polite">
|
||||||
|
{#if normalizedQuery.length >= 2 && !loading && !loadError}
|
||||||
|
{results.length === 0 ? "Keine Treffer." : `${results.length} Treffer.`}
|
||||||
|
{/if}
|
||||||
|
</p>
|
||||||
{#if loading}
|
{#if loading}
|
||||||
<p class="text-stein-500 text-center py-2">Lade Suchindex…</p>
|
<p class="text-stein-500 text-center py-2">Lade Suchindex…</p>
|
||||||
{:else if loadError}
|
{:else if loadError}
|
||||||
@@ -121,13 +200,19 @@
|
|||||||
{:else if results.length === 0}
|
{:else if results.length === 0}
|
||||||
<p class="text-stein-500 text-center py-2">Keine Treffer.</p>
|
<p class="text-stein-500 text-center py-2">Keine Treffer.</p>
|
||||||
{:else}
|
{:else}
|
||||||
|
<p class="text-stein-400 text-xs mb-2">
|
||||||
|
{results.length === MAX_RESULTS ? `${MAX_RESULTS}+ Treffer` : `${results.length} Treffer`} · ↑↓ zum Wählen, ⏎ zum Öffnen
|
||||||
|
</p>
|
||||||
<ul class="divide-y divide-stein-200">
|
<ul class="divide-y divide-stein-200">
|
||||||
{#each results as hit}
|
{#each results as hit, i}
|
||||||
<li>
|
<li>
|
||||||
<a
|
<a
|
||||||
|
bind:this={itemEls[i]}
|
||||||
href={hit.href}
|
href={hit.href}
|
||||||
onclick={closeSearch}
|
onclick={closeSearch}
|
||||||
class="flex items-start gap-3 py-3 hover:bg-stein-50 rounded-xs px-2 -mx-2 no-underline"
|
onmouseenter={() => (activeIndex = i)}
|
||||||
|
aria-current={i === activeIndex ? "true" : undefined}
|
||||||
|
class="flex items-start gap-3 py-3 rounded-xs px-2 -mx-2 no-underline {i === activeIndex ? 'bg-stein-100' : 'hover:bg-stein-50'}"
|
||||||
>
|
>
|
||||||
{#if hit.image}
|
{#if hit.image}
|
||||||
<img
|
<img
|
||||||
@@ -150,9 +235,9 @@
|
|||||||
<span>{formatDate(hit.created)}</span>
|
<span>{formatDate(hit.created)}</span>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
<div class="font-medium text-stein-900 truncate">{hit.title}</div>
|
<div class="font-medium text-stein-900 truncate">{@html highlight(hit.title)}</div>
|
||||||
{#if hit.excerpt}
|
{#if hit.excerpt}
|
||||||
<div class="text-xs text-stein-600 line-clamp-2">{hit.excerpt}</div>
|
<div class="text-xs text-stein-600 line-clamp-2">{@html highlight(hit.excerpt)}</div>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</a>
|
</a>
|
||||||
|
|||||||
@@ -36,7 +36,7 @@
|
|||||||
class={iconOnlyAll
|
class={iconOnlyAll
|
||||||
? "flex flex-wrap gap-3"
|
? "flex flex-wrap gap-3"
|
||||||
: links.length > 4
|
: links.length > 4
|
||||||
? "grid grid-flow-row gap-x-8 gap-y-1.5 sm:grid-flow-col sm:grid-rows-4"
|
? "grid grid-cols-[repeat(auto-fill,minmax(13rem,1fr))] gap-x-8 gap-y-1.5"
|
||||||
: "flex flex-col gap-1.5"}
|
: "flex flex-col gap-1.5"}
|
||||||
>
|
>
|
||||||
{#each links as link}
|
{#each links as link}
|
||||||
|
|||||||
Reference in New Issue
Block a user