import { importSelectedFile } from "#ui/rpc.ts"; import { useEffect, useRef, useState, type RefObject } from "react"; import { DOC_KINDS, MAX_FILE_BYTES, type DocKind, type DocumentInfo } from "#office/contract.ts"; import { type Strings } from "#ui/i18n.ts"; import { Icon } from "#ui/icons.tsx"; import type { PageState, ViewerHandle } from "#ui/viewers/types.ts"; // 面板的文档导航与状态界面(抽屉、文件选择、空态、骨架、错误、文件变更提示),从 parts.tsx 拆出以控制单文件规模。 /* ---------------------------------------------------------------- Drawer */ export type DrawerTab = "pages" | "outline"; function Thumbnail({ handle, page, width, ratio, current, onPick, root, }: { handle: ViewerHandle; page: number; width: number; ratio: number; current: boolean; onPick(page: number): void; root: RefObject; }) { const slot = useRef(null); useEffect(() => { const element = slot.current; if (!element || !handle.thumbnail) return; let done = false; const observer = new IntersectionObserver( (entries) => { if (done || !entries.some((entry) => entry.isIntersecting)) return; done = true; observer.disconnect(); void handle.thumbnail!(page, width).then( (thumb) => { element.replaceChildren(thumb); element.style.height = ""; }, () => undefined, ); }, { root: root.current, rootMargin: "300px 0px" }, ); observer.observe(element); return () => observer.disconnect(); }, [handle, page, width, root]); return ( ); } const outlinePage = (at: string) => /^(?:page|slide):(\d+)/.exec(at)?.[1] ?? ""; export function Drawer({ t, info, handle, page, tab, docked, onTab, onPage, onOutline, }: { t: Strings; info: DocumentInfo; handle: ViewerHandle | null; page: PageState | null; tab: DrawerTab; docked: boolean; onTab(tab: DrawerTab): void; onPage(page: number): void; onOutline(at: string): void; }) { const scroller = useRef(null); const hasPages = Boolean(handle?.thumbnail && page); const hasOutline = info.outline.length > 0; const shown: DrawerTab = hasPages && (tab === "pages" || !hasOutline) ? "pages" : "outline"; const ratio = info.kind === "pptx" ? 9 / 16 : 1.294; // Bring the current page into the drawer's view when it opens or switches tab. useEffect(() => { const current = scroller.current?.querySelector("[aria-current=page]"); if (current && scroller.current) scroller.current.scrollTop = current.offsetTop - scroller.current.clientHeight / 3; }, [shown]); const currentOutline = (() => { if (!page) return -1; let found = -1; info.outline.forEach((item, i) => { const n = Number(outlinePage(item.at)); if (n && n <= page.current) found = i; }); return found; })(); return ( ); } /* ---------------------------------------------------------------- Empty, picker, skeleton, error */ export function EmptyState({ t, onBrowse, importing, }: { t: Strings; onBrowse(): void; importing: boolean; }) { return (

{t.emptyTitle}

{t.emptyDescription}

{t.emptyCaption}

); } const SKELETON_LINES = [ "title", "subtitle", "line", "line", "short", "heading", "line", "line", "line", "mid", "figure", ]; export function Skeleton({ kind }: { kind: DocKind | null }) { if (kind === "xlsx") return (
{Array.from({ length: 18 }, (_, i) => (
))}
); const slide = kind === "pptx"; return (
{[0, 1].map((n) => (
{n === 0 && !slide ? SKELETON_LINES.map((line, i) =>
) : null} {n === 0 && slide ? ( <>
) : null}
))}
); } export function ErrorState({ t, title, detail, onOpenAnother, }: { t: Strings; title: string; detail: string; onOpenAnother(): void; }) { return (

{title}

{detail ?

{detail}

: null}
); } export function ChangedBanner({ t, onReload, onDismiss, }: { t: Strings; onReload(): void; onDismiss(): void; }) { return (
{t.changed}
); } // 使用系统文件选择器,取消时不改变当前文档;选中的内容通过受限导入接口进入工作区。 export function NativeFileInput({ inputRef, t, importing, onImporting, onOpen, onError, }: { inputRef: RefObject; t: Strings; importing: boolean; onImporting(value: boolean): void; onOpen(path: string): void; onError(error: unknown, name: string): void; }) { return ( `.${kind}`).join(",")} disabled={importing} onChange={async (event) => { const file = event.currentTarget.files?.[0]; event.currentTarget.value = ""; if (!file) return; onImporting(true); try { if (file.size > MAX_FILE_BYTES) throw new Error(t.importTooLarge); onOpen(await importSelectedFile(file)); } catch (error) { onError(error, file.name); } finally { onImporting(false); } }} /> ); } export function useNativeFileInput( t: Strings, onOpen: (path: string) => void, onError: (error: unknown, name: string) => void, ) { const inputRef = useRef(null); const [importing, onImporting] = useState(false); return { importing, browse: () => inputRef.current?.click(), input: , }; }