import { useEffect, useRef, useState, type ReactNode, type RefObject, type KeyboardEvent as ReactKeyboardEvent, type MouseEvent as ReactMouseEvent, } from "react"; import { describeAt, type Strings } from "#ui/i18n.ts"; import { Icon } from "#ui/icons.tsx"; import { useFloating } from "#ui/shell/float.ts"; import type { Locate, PageState, ZoomState } from "#ui/viewers/types.ts"; /* ---------------------------------------------------------------- Menu */ /** ZCode dropdown: closes on Escape and on any pointer press outside it. */ export function Menu({ className, onClose, children, label, }: { className: string; onClose(): void; children: ReactNode; label: string; }) { const ref = useRef(null); useEffect(() => { const onDown = (event: PointerEvent) => { const target = event.target as Element; if (!ref.current?.contains(target) && !target.closest?.("[data-menu-trigger]")) onClose(); }; const onKey = (event: KeyboardEvent) => { if (event.key !== "Escape") return; event.stopPropagation(); onClose(); }; document.addEventListener("pointerdown", onDown, true); document.addEventListener("keydown", onKey, true); ref.current?.querySelector("[role^=menuitem]")?.focus(); return () => { document.removeEventListener("pointerdown", onDown, true); document.removeEventListener("keydown", onKey, true); }; }, [onClose]); const move = (event: ReactKeyboardEvent) => { if (event.key !== "ArrowDown" && event.key !== "ArrowUp") return; event.preventDefault(); const items = [...(ref.current?.querySelectorAll("[role^=menuitem]") ?? [])]; const index = items.indexOf(document.activeElement as HTMLElement); items[(index + (event.key === "ArrowDown" ? 1 : -1) + items.length) % items.length]?.focus(); }; return (
{children}
); } export function MenuItem({ icon, checked, children, hint, onSelect, }: { icon?: ReactNode; checked?: boolean; children: ReactNode; hint?: ReactNode; onSelect(): void; }) { return ( ); } /* ---------------------------------------------------------------- Find */ export function FindBar({ t, query, total, index, onQuery, onStep, onClose, }: { t: Strings; query: string; total: number | null; index: number; onQuery(query: string): void; onStep(direction: 1 | -1): void; onClose(): void; }) { const input = useRef(null); useEffect(() => { input.current?.focus(); input.current?.select(); }, []); const count = !query.trim() || total === null ? "" : total ? t.findCount(index + 1, total) : t.noResults; return (
); } /* ---------------------------------------------------------------- Page / zoom pill */ const IDLE_MS = 1500; /** * Floating page and zoom control. Shown while scrolling, while the pointer is near the bottom edge, * while focused or hovered; fades out after 1.5 s of stillness. */ export function PagePill({ t, frame, page, zoom, onPage, onZoom, onFit, }: { t: Strings; frame: RefObject; page: PageState; zoom: ZoomState | null; onPage(page: number): void; onZoom(direction: 1 | -1): void; onFit(): void; }) { const ref = useRef(null); const [visible, setVisible] = useState(true); const [draft, setDraft] = useState(null); const hold = useRef(false); useEffect(() => { const container = frame.current; if (!container) return; let timer = 0; const idle = () => { clearTimeout(timer); timer = window.setTimeout(() => { if (!hold.current && !ref.current?.contains(document.activeElement)) setVisible(false); }, IDLE_MS); }; const wake = () => { setVisible(true); idle(); }; const near = (event: PointerEvent) => { const bounds = container.getBoundingClientRect(); if (event.clientY > bounds.bottom - 80) wake(); }; container.addEventListener("scroll", wake, { capture: true, passive: true }); container.addEventListener("pointermove", near, { passive: true }); idle(); return () => { clearTimeout(timer); container.removeEventListener("scroll", wake, { capture: true }); container.removeEventListener("pointermove", near); }; }, [frame]); const go = () => { const value = Number(draft); setDraft(null); if (Number.isInteger(value) && value >= 1) onPage(Math.min(value, page.count)); }; return (
{ hold.current = true; setVisible(true); }} onPointerLeave={() => { hold.current = false; }} onFocus={() => setVisible(true)} > { setDraft(String(page.current)); event.target.select(); }} onChange={(event) => setDraft(event.target.value.replace(/\D/g, ""))} onBlur={() => setDraft(null)} onKeyDown={(event) => { if (event.key === "Enter") { go(); event.currentTarget.blur(); } else if (event.key === "Escape") { event.stopPropagation(); setDraft(null); event.currentTarget.blur(); } }} /> {t.ofPages(page.count)} {zoom ? ( <> ) : null}
); } /* ---------------------------------------------------------------- Selection bar and Ask */ export type BarMode = "bar" | "ask" | "added"; /** Keep the document's text selection when a press lands on the bar. */ const keepSelection = (event: ReactMouseEvent) => { if (!(event.target as Element).closest("textarea")) event.preventDefault(); }; export function SelectionBar({ t, frame, locate, text, label, quote, short, mode, canAsk, copied, onAdd, onAsk, onCopy, onCancelAsk, onSend, }: { t: Strings; frame: RefObject; locate: Locate; /** A text selection (offers Copy) rather than a block, page or cell range (shows its label). */ text: boolean; label: string; quote: string; /** Use the short "Add" label (spreadsheets, where the range label takes the room). */ short: boolean; mode: BarMode; canAsk: boolean; copied: boolean; onAdd(): void; onAsk(): void; onCopy(): void; onCancelAsk(): void; onSend(question: string): Promise; }) { const asking = mode === "ask"; const ref = useFloating(frame, locate, { side: asking ? "bottom" : "top", dock: true, }); const [question, setQuestion] = useState(""); const [sending, setSending] = useState(false); const send = async () => { if (!question.trim() || sending) return; setSending(true); const ok = await onSend(question.trim()); setSending(false); if (ok) setQuestion(""); }; if (asking) return (
{label} {quote ? “{quote}” : null}