droboticflow/src/components/TimelinePageNavigation.tsx

132 lines
4.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useState } from "react";
import { nextMatchingItem, pageContaining, timelinePage } from "../lib/annotation-list-navigation.js";
type Entry = { id: string; start: number; end: number };
const pageSize = 4;
export function useTimelinePage<T extends Entry>(
items: T[],
selectedId: string | undefined,
scope: string,
revealRevision?: number,
target?: { scope: string; revision: number; id?: string; start: number },
) {
const selectedPage = pageContaining(items, selectedId, pageSize);
const selectionKey = `${scope}:${selectedId}:${selectedPage}:${revealRevision}`;
const [state, setState] = useState({ page: selectedPage ?? 0, scope, selectionKey, revealedTarget: "" });
// Derive a new window before rendering its entries. Separate selection and
// clamping effects can race when a parked editor moves between long/short lists.
const targetKey = target ? `${target.scope}:${target.revision}` : "";
const revealTarget = target?.scope === scope && targetKey !== state.revealedTarget;
const targetItem = revealTarget
? (items.find((item) => item.id === target.id) ??
items.find((item) => item.start <= target.start && item.end > target.start) ??
items.find((item) => item.start >= target.start) ??
items.at(-1))
: undefined;
const requestedPage = revealTarget
? (pageContaining(items, targetItem?.id, pageSize) ?? 0)
: state.selectionKey !== selectionKey
? (selectedPage ?? (state.scope === scope ? state.page : 0))
: state.page;
const window = timelinePage(items, requestedPage, pageSize);
const revealedTarget = revealTarget ? targetKey : state.revealedTarget;
if (state.selectionKey !== selectionKey || state.page !== window.page || revealTarget) {
setState({ page: window.page, scope, selectionKey, revealedTarget });
}
const setPage = (page: number) => setState({ page, scope, selectionKey, revealedTarget });
return { ...window, setPage, reveal: () => setPage(selectedPage ?? window.page) };
}
export function TimelinePageNavigation<T extends Entry>({
label,
items,
selectedId,
navigation,
textFor,
onSelect,
disabled,
}: {
label: string;
items: T[];
selectedId?: string;
navigation: ReturnType<typeof useTimelinePage<T>>;
textFor: (item: T) => string;
onSelect: (item: T) => void;
disabled?: boolean;
}) {
const [query, setQuery] = useState("");
const [message, setMessage] = useState("");
const locate = () => {
if (disabled) return;
const found = nextMatchingItem(items, query, selectedId, textFor);
if (!found) {
setMessage("无匹配项");
return;
}
setMessage("");
navigation.setPage(pageContaining(items, found.id, pageSize) ?? 0);
onSelect(found);
};
return (
<nav
className="timeline-page-navigation"
aria-label={`${label}分页`}
onPointerDown={(event) => event.stopPropagation()}
onMouseMove={(event) => event.stopPropagation()}
>
<button
type="button"
aria-label={`${label}上一页`}
disabled={disabled || navigation.page === 0}
onClick={() => navigation.setPage(navigation.page - 1)}
>
‹
</button>
<span aria-label={`${label}页码`}>
{navigation.page + 1}/{navigation.count} · {items.length ? navigation.offset + 1 : 0}–
{navigation.offset + navigation.items.length}/{items.length}
</span>
<button
type="button"
aria-label={`${label}下一页`}
disabled={disabled || navigation.page + 1 >= navigation.count}
onClick={() => navigation.setPage(navigation.page + 1)}
>
›
</button>
<input
type="search"
aria-label={`查找${label}`}
placeholder={`${label === "描述" ? "描述文本" : "切片名称"} / #序号`}
value={query}
onChange={(event) => {
setQuery(event.target.value);
setMessage("");
}}
onKeyDown={(event) => {
if (event.key === "Enter") {
event.preventDefault();
locate();
}
}}
/>
<button type="button" aria-label={`查找下一${label}`} disabled={disabled || !query.trim()} onClick={locate}>
查找
</button>
<button
type="button"
aria-label={`定位当前${label}`}
disabled={disabled || !selectedId}
onClick={() => {
navigation.reveal();
setMessage("");
}}
>
当前{selectedId ? ` #${items.findIndex((item) => item.id === selectedId) + 1}` : ""}
</button>
{message && <span role="status">{message}</span>}
</nav>
);
}