diff --git a/README.md b/README.md index 4ff4bb6eb64cc..065617cc2009c 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ This is a fork of T3 Code that integrates a few additional features, including: - **Compact when idle for Claude threads:** a choice in Settings → Providers → Advanced → Compact idle threads. Besides compacting before the prompt cache expires, it compacts a large idle Claude thread (default 400K tokens or more) a set time (default 15 minutes) after the last message or answer from a user or a bot, so the compaction runs while no one waits for a reply. The token counts and the wait are set right under the choice. - **Prune idle worker threads:** `t3 prune-worker-threads` deletes the messages, turn items, nodes, plans and runtime requests of idle worker threads. A worker thread is idle when it has no open run and, in the last 7 days, was not created, had no run requested or ended, and had no turn item or message updated. A worker thread is a subagent thread that an agent started. Subagent threads that a bot starts are workers too. Threads a person opens, bot threads themselves and a person's forks are never workers. An idle worker keeps its rows while recovery still needs the worker, or while a thread that keeps its own rows names the worker as parent or fork source. Pruned threads keep their thread row, runs, parent link and pull request links, so they still show up and open with no messages. The command runs only when you call it, so run it daily to keep worker transcripts from piling up. Other threads still grow the database. With `--dry-run`, the command only prints how many threads it would empty and how many rows it would delete. You can run the command while the server runs. - **One way to start subagents in Claude and Codex threads:** T3 Code starts Claude and Codex sessions without the provider's own subagent tools: Claude's `Agent`, `Workflow`, `SendMessage` and `ListAgents`, and Codex's `spawn_agent` with its other collaboration tools. The agent starts every subagent, same provider and model included, with T3 Code's `delegate_task` tool, so each child is a T3 Code thread that you can open and follow. -- **Files remember where you stopped reading:** a code file or a rendered Markdown file in the file viewer opens where you left it when you switch back to its tab, close and reopen it, or reload the app; so does a PDF in the desktop app. Positions stay in this browser for 30 days. A link to a line still jumps to that line. Large text files, tables and HTML pages still open at the top. +- **Files remember where you stopped reading:** a file in the right panel opens where you left it when you switch back to its tab, close and reopen it, or reload the app. This covers code, Markdown, PDFs, large text files, CSV and TSV tables, HTML pages, Word and PowerPoint files, the first sheet of an Excel or ODS spreadsheet, notebooks, Mermaid diagrams and chat attachments; video and audio come back paused at the time you left. If the file changed, it opens at the same place, or at its start or end when that place is gone. Places stay in this browser for 30 days. A link to a line still jumps to that line once. PDFs in the right panel open in T3 Code's own PDF viewer, also in Chrome and Edge on the desktop. Image zoom, fonts, archives, SQLite files, ODT, ODP, RTF and EPUB files, HTML attachments and the other sheets of a spreadsheet still open at the top. A place in a notebook cell after the first 20 opens at the end of cell 20. - **Reflink worktrees on Linux:** Settings → Worktrees → Reflink worktrees (on by default) clones new worktrees copy-on-write from a cache of recent checkouts on the worktree location's drive, so worktrees share disk space until files change. Drives that cannot clone, such as ext4, use Git's normal checkout. - **FYI messages between agents:** an agent can send another thread an FYI, a note of at most 16,000 characters that needs no action and starts no turn. The receiving agent gets it with its next turn; a running Claude Code thread gets it at its next tool call. An FYI counts as delivered only when the turn that carries it finishes: a turn that fails, is stopped, restarts or is reverted gives it back, so the agent may see it twice. When a thread has an FYI waiting and has been idle for 6 hours, T3 Code wakes it to deliver its FYIs, unless the thread is settled, archived, waiting for your answer or holds queued messages; a snoozed thread waits until its snooze ends. The 6 hours count from the thread's last turn, or from the reset of a usage limit that stopped it on the provider it still uses (or from the end of a snooze that ends after that reset), so an FYI to a thread that has been idle longer wakes it within minutes. A thread whose usage limit has no known reset time, or whose provider fails every turn, gets a failed wake every 6 hours while an FYI waits. A wake carries up to 16,000 characters of FYIs. The rest, and any that a wake gives back because it failed or you stopped it, get another wake after 6 more idle hours. A wake is a new turn, so **Resume** no longer continues a turn that ended before it. Settle or archive a thread to stop its wakes. The timeline marks these messages "FYI · waiting", "FYI · sent" while the turn that carries them runs, and "FYI" once it finished. - **Open server files by their path:** in a browser signed in to T3 Code at its own address, open `/` (such as `https://t3.example.com/home/ana/work/report.html`) to see that file read-only, for files under the server user's home folder and temp folder. A session needs the `filesystem:read` permission, and a signed-out browser gets a sign-in page. Text and code of any type show as plain text, and images, PDFs, audio and video show as themselves. A folder shows its `index.html`, or else a list of its files. Links ending in `:line` or `:line:col` open the file. Names that start with `.`, and T3 Code's own data folder, are not served. An HTML page runs in a sandboxed frame at the same address: its scripts run but cannot act as you in T3, and it loads files from its own folder and below for an hour, though its scripts cannot fetch them. A page directly in the home or temp folder loads no other files. An HTML page linked from another site, or any HTML page over plain HTTP, first shows an Open link. diff --git a/apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx b/apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx index 078e36f6f3611..45354f124ddbc 100644 --- a/apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx +++ b/apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx @@ -1493,6 +1493,9 @@ it("toggles a Mermaid attachment between diagram and source through the lazy pre it("keeps a workspace PDF open across URL renewals and reloads it when the file changes", async () => { const { WorkspaceBrowserPreview } = await import("./FilePreviewPanel"); using _native = vi.spyOn(navigator, "pdfViewerEnabled", "get").mockReturnValue(true); + using fetches = vi.spyOn(window, "fetch"); + const fetched = () => + fetches.mock.calls.map(([input]) => String(input)).filter((url) => url.startsWith("blob:")); const original = URL.createObjectURL(new Blob(["%PDF-1.4"], { type: "application/pdf" })); const edited = URL.createObjectURL(new Blob(["%PDF-1.4 edited"], { type: "application/pdf" })); const preview = (mutation: string) => ( @@ -1514,8 +1517,15 @@ it("keeps a workspace PDF open across URL renewals and reloads it when the file }); try { await using screen = await render(preview("first")); - const opened = screen.container.querySelector("iframe"); - expect(opened?.src.startsWith(original)).toBe(true); + // A right-panel PDF keeps its place, so it opens in PDF.js even where the browser has a viewer. + const viewer = () => + screen.container.querySelector('[role="region"][aria-label="Planning PDF"]'); + // PDF.js loads lazily, which can take longer than waitFor's default on a busy machine. + await vi.waitFor(() => expect(viewer()).not.toBeNull(), { timeout: 10_000 }); + const opened = viewer(); + expect(screen.container.querySelector("iframe")).toBeNull(); + await vi.waitFor(() => expect(fetched().at(-1)?.startsWith(original)).toBe(true)); + const loads = fetched().length; // An agent command renews the signed URL but leaves the file alone. assetUrlState.mockReturnValue({ @@ -1526,7 +1536,8 @@ it("keeps a workspace PDF open across URL renewals and reloads it when the file }); await screen.rerender(preview("second")); expect(assetUrlState).toHaveBeenLastCalledWith(expect.anything(), expect.anything(), "second"); - expect(screen.container.querySelector("iframe")).toBe(opened); + expect(viewer()).toBe(opened); + expect(fetched()).toHaveLength(loads); assetUrlState.mockReturnValue({ _tag: "Success", @@ -1535,9 +1546,9 @@ it("keeps a workspace PDF open across URL renewals and reloads it when the file fileRevision: "two", }); await screen.rerender(preview("third")); - const reloaded = screen.container.querySelector("iframe"); - expect(reloaded).not.toBe(opened); - expect(reloaded?.src.startsWith(edited)).toBe(true); + await vi.waitFor(() => expect(viewer()).not.toBeNull()); + expect(viewer()).not.toBe(opened); + await vi.waitFor(() => expect(fetched().at(-1)?.startsWith(edited)).toBe(true)); // Servers without revisions hold the preview back until a fresh grant arrives. assetUrlState.mockReturnValue({ @@ -1547,10 +1558,11 @@ it("keeps a workspace PDF open across URL renewals and reloads it when the file waiting: true, }); await screen.rerender(preview("fourth")); - expect(screen.container.querySelector("iframe")).toBeNull(); + expect(viewer()).toBeNull(); assetUrlState.mockReturnValue({ _tag: "Success", url: original, expiresAt: 0 }); await screen.rerender(preview("fourth")); - expect(screen.container.querySelector("iframe")?.src.startsWith(original)).toBe(true); + await vi.waitFor(() => expect(viewer()).not.toBeNull()); + await vi.waitFor(() => expect(fetched().at(-1)?.startsWith(original)).toBe(true)); } finally { URL.revokeObjectURL(original); URL.revokeObjectURL(edited); diff --git a/apps/web/src/components/files/AttachmentFilePreview.tsx b/apps/web/src/components/files/AttachmentFilePreview.tsx index bbc476f2b893a..7fd61bc5d722c 100644 --- a/apps/web/src/components/files/AttachmentFilePreview.tsx +++ b/apps/web/src/components/files/AttachmentFilePreview.tsx @@ -41,6 +41,7 @@ import { PreviewImage } from "./PreviewImage"; import { AudioPreview } from "./AudioPreview"; import { BrowserDocumentFrame } from "./BrowserDocumentFrame"; import { DelimitedTablePreview } from "./DelimitedTablePreview"; +import type { FileReadingKey } from "./fileReadingStore"; import { FILE_SURFACE_SUBHEADER_CLASS, FileSurfaceAction, @@ -113,6 +114,10 @@ export function AttachmentFilePreview(props: { origin?: string; onRemove?: () => void; onClose?: () => void; + /** Keeps the PDF reading place across visits. */ + readingKey?: FileReadingKey | undefined; + /** The row a table (true) or raw text (false) reveals, which keeps the reader's place. */ + rowLine?: ((table: boolean) => number | null) | undefined; }) { const kind = filePreviewKind(props); const officeKind = officePreviewKind(props); @@ -134,19 +139,19 @@ export function AttachmentFilePreview(props: { const { copyToClipboard, isCopied } = useCopyToClipboard({ target: namedClipboardTarget(props.name), }); - // useAssetUrlRefresh keys its callback on these objects, so they must stay cached. + // useAssetUrlRefresh keys its callback on these objects, so they must stay cached. A parent may + // rebuild `asset` on any render, so the attachment's own resource depends only on its fields. + const attachmentResource = attachmentId + ? { + _tag: "attachment" as const, + attachmentId, + fileName: props.name, + mimeType: props.mimeType, + disposition: "inline" as const, + } + : null; const resource = - props.asset && "resource" in props.asset - ? props.asset.resource - : attachmentId - ? { - _tag: "attachment" as const, - attachmentId, - fileName: props.name, - mimeType: props.mimeType, - disposition: "inline" as const, - } - : null; + props.asset && "resource" in props.asset ? props.asset.resource : attachmentResource; const downloadResource = resource ? resource._tag === "draft-workspace-file" ? resource @@ -303,13 +308,19 @@ export function AttachmentFilePreview(props: { delimiter={rendered ? delimiter : null} copyContents={copyContents} wordWrap={wordWrap} + revealLine={props.rowLine?.(rendered && delimiter !== null) ?? null} /> ) : !url || (needsText && !content) ? ( ) : needsText && content ? ( delimiter && rendered ? ( - + ) : kind === "mermaid" && rendered ? ( }> @@ -344,6 +355,7 @@ export function AttachmentFilePreview(props: { name={props.name} kind={officeKind} sizeBytes={props.sizeBytes} + revealLine={props.rowLine?.(true) ?? null} /> ) : kind === "pdf" || kind === "html" ? ( @@ -353,6 +365,7 @@ export function AttachmentFilePreview(props: { pdf={kind === "pdf"} htmlRender={props.htmlRender === true} threadRef={props.threadRef} + readingKey={kind === "pdf" ? props.readingKey : undefined} // Each mount signs or creates a new URL, so the URL cannot identify the file across visits. // A draft keeps its Blob key after upload, so finishing the upload does not reload the PDF. documentKey={ diff --git a/apps/web/src/components/files/BrowserDocumentFrame.tsx b/apps/web/src/components/files/BrowserDocumentFrame.tsx index a29ed3594e27d..3405bdcd1073b 100644 --- a/apps/web/src/components/files/BrowserDocumentFrame.tsx +++ b/apps/web/src/components/files/BrowserDocumentFrame.tsx @@ -7,7 +7,6 @@ import { THREAD_JUMP_KEYBINDING_COMMANDS, } from "@t3tools/contracts"; import { - type FilePreviewScroll, filePreviewRestoreScrollMessage, isFilePreviewScrollRestored, readFilePreviewScroll, @@ -38,7 +37,8 @@ import { useState, } from "react"; import { isElectron } from "~/env"; -import { type FileReadingKey, usePdfReadingView } from "./fileReadingSave"; +import { FRAME_RETRY_MS, type FrameAction, FrameReading } from "./fileReadingFrame"; +import { type FileReadingKey, useFramePlace, usePdfReadingView } from "./fileReadingSave"; import { FileSurfaceLoading } from "./fileSurfaceChrome"; import type { PdfViewState } from "./PdfViewer"; @@ -95,7 +95,7 @@ function PdfDocument(props: { key={props.documentKey} src={props.src} title={props.title} - positionKey={props.documentKey} + positionKey={props.readingKey === undefined ? props.documentKey : undefined} initialView={view} onViewChange={setView} actions={props.actions} @@ -107,7 +107,8 @@ function PdfDocument(props: { function PdfDocumentRevision(props: { src: string; title: string; - positionKey: string; + /** Undefined when the reading store keeps the place, which the browser's own viewer hides. */ + positionKey: string | undefined; initialView: PdfViewState; onViewChange: (view: PdfViewState) => void; actions?: ReactNode; @@ -116,6 +117,7 @@ function PdfDocumentRevision(props: { // The Electron shell does not enable PDF plugins. WebKit embeds on iOS // expose only the first page, even when pdfViewerEnabled reports support. const native = + props.positionKey !== undefined && !isElectron && navigator.pdfViewerEnabled && !/iPad|iPhone|iPod/.test(navigator.userAgent) && @@ -157,7 +159,7 @@ export function BrowserDocumentFrame(props: { readonly documentId?: string | undefined; /** Toolbar actions for the in-app PDF viewer; Chromium's own viewer brings its own. */ readonly pdfActions?: ReactNode; - /** Saves the PDF.js reading position across reloads. */ + /** Saves the reader's place in a PDF.js view or an HTML page across reloads. */ readonly readingKey?: FileReadingKey | undefined; }) { const key = props.documentKey ?? props.src; @@ -184,6 +186,7 @@ export function BrowserDocumentFrame(props: { documentKey={key} src={props.src} title={props.title} + readingKey={props.readingKey} /> ); } @@ -214,6 +217,7 @@ function FileDocument(props: { readonly documentKey: string; readonly src: string; readonly title: string; + readonly readingKey: FileReadingKey | undefined; }) { const [frames, setFrames] = useState(() => ({ shown: { key: props.documentKey, src: props.src }, @@ -230,21 +234,37 @@ function FileDocument(props: { const shownFrame = useRef(null); const nextFrame = useRef(null); // The page reports its scroll position (see FILE_PREVIEW_SCROLL_SCRIPT); the next revision - // scrolls there before it is uncovered. - const scroll = useRef(null); + // scrolls there before it is uncovered, and a reading key keeps it across visits. + const place = useFramePlace(props.readingKey); + const [reading] = useState(() => new FrameReading(place.seed)); // Two frames let the browser paint the incoming page before it is uncovered. - const reveal = (revision: FileDocumentRevision) => - requestAnimationFrame(() => requestAnimationFrame(() => setFrames(showRevision(revision)))); + const reveal = (frame: Window, revision: FileDocumentRevision) => + requestAnimationFrame(() => + requestAnimationFrame(() => run(frame, reading.swap(frame), revision)), + ); + const run = (frame: Window, actions: FrameAction[], revision: FileDocumentRevision | null) => { + for (const action of actions) { + if (action.type === "restore") { + frame.postMessage(filePreviewRestoreScrollMessage(action.scroll), "*"); + } else if (action.type === "save") place.save(action.scroll); + else if (action.type === "retry") { + setTimeout(() => run(frame, reading.retry(frame), null), FRAME_RETRY_MS); + } else if (revision !== null) { + if (action.type === "reveal") reveal(frame, revision); + else setFrames(showRevision(revision)); + } + } + }; const onFrameMessage = useEffectEvent((event: MessageEvent) => { + const shown = shownFrame.current?.contentWindow; + const incoming = nextFrame.current?.contentWindow; + const frame = event.source === shown ? shown : event.source === incoming ? incoming : null; + if (!frame) return; + const role = frame === shown ? "shown" : "next"; const position = readFilePreviewScroll(event.data); - if (position !== undefined) { - if (event.source === shownFrame.current?.contentWindow) scroll.current = position; - } else if ( - next !== null && - event.source === nextFrame.current?.contentWindow && - isFilePreviewScrollRestored(event.data) - ) { - reveal(next); + if (position !== undefined) run(frame, reading.report(frame, role, position), next); + else if (isFilePreviewScrollRestored(event.data)) { + run(frame, reading.restored(frame, role), next); } }); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- listens for the frames' scroll reports @@ -253,10 +273,14 @@ function FileDocument(props: { window.addEventListener("message", onMessage); return () => window.removeEventListener("message", onMessage); }, []); + const onSwapTimeout = useEffectEvent((revision: FileDocumentRevision) => { + reading.swapTimedOut(nextFrame.current?.contentWindow ?? null); + setFrames(showRevision(revision)); + }); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- swaps in a revision whose load event never arrives useEffect(() => { if (next === null) return; - const timer = setTimeout(() => setFrames(showRevision(next)), FILE_DOCUMENT_SWAP_TIMEOUT_MS); + const timer = setTimeout(() => onSwapTimeout(next), FILE_DOCUMENT_SWAP_TIMEOUT_MS); return () => clearTimeout(timer); }, [next]); // The shown frame stays last so it paints on top and is never moved, which would reload it. @@ -274,20 +298,12 @@ function FileDocument(props: { sandbox="allow-scripts allow-forms allow-popups allow-downloads" inert={revision === next} aria-hidden={revision === next ? true : undefined} - onLoad={ - revision === next - ? (event) => { - if (scroll.current === null) { - reveal(revision); - return; - } - event.currentTarget.contentWindow?.postMessage( - filePreviewRestoreScrollMessage(scroll.current), - "*", - ); - } - : undefined - } + onLoad={(event) => { + const content = event.currentTarget.contentWindow; + if (content) { + run(content, reading.load(content, revision === next ? "next" : "shown"), revision); + } + }} /> ), )} diff --git a/apps/web/src/components/files/DelimitedTablePreview.tsx b/apps/web/src/components/files/DelimitedTablePreview.tsx index 4d7701380d0e3..06948cd4ad53d 100644 --- a/apps/web/src/components/files/DelimitedTablePreview.tsx +++ b/apps/web/src/components/files/DelimitedTablePreview.tsx @@ -6,6 +6,7 @@ export function DelimitedTablePreview(props: { readonly name: string; readonly text: string; readonly delimiter: "," | "\t"; + readonly revealLine?: number | null; }) { return ( }> diff --git a/apps/web/src/components/files/FilePreviewPanel.tsx b/apps/web/src/components/files/FilePreviewPanel.tsx index b756a2d7117b6..0079850570712 100644 --- a/apps/web/src/components/files/FilePreviewPanel.tsx +++ b/apps/web/src/components/files/FilePreviewPanel.tsx @@ -121,7 +121,7 @@ import { FileSurfaceLoading, } from "./fileSurfaceChrome"; import SourceFilePreview from "./ReadOnlySourcePreview"; -import { useFileReadingPosition, useRenderedReadingPosition } from "./fileReadingPosition"; +import { pathReadingKey, useFileReadingPosition } from "./fileReadingPosition"; import { resolveCenteredFileLineScrollTop } from "./fileLineReveal"; import { DiffCommentAnnotation } from "../diffs/DiffCommentAnnotation"; import { projectFileCacheKey } from "./fileContentRevision"; @@ -305,7 +305,7 @@ export function WorkspaceBrowserPreview(props: { name={props.title} /> } - readingKey={{ environmentId: props.environmentId, absolutePath: props.absolutePath }} + readingKey={pathReadingKey(props.environmentId, props.absolutePath)} /> ); } @@ -472,6 +472,7 @@ function WorkspaceDocumentPreview(props: { readonly name: string; readonly workspaceMutationId: string | null; readonly refreshRevision: number; + readonly rowLine: (table: boolean) => number | null; }) { const resource = props.draft && props.document @@ -517,6 +518,7 @@ function WorkspaceDocumentPreview(props: { sizeBytes={0} asset={{ environmentId: props.environmentId, resource }} origin={gt("File", { $context: "label for a file opened from the workspace" })} + rowLine={props.rowLine} /> ); } @@ -1176,14 +1178,9 @@ function RenderedMarkdownSurface({ }) { const saveTarget = { environmentId, cwd, relativePath }; useFileSaveSession(saveTarget, onPendingChange); - const scrollRef = useRenderedReadingPosition(environmentId, cwd, relativePath); return ( - +
{mayOpenEditor ? ( @@ -1697,6 +1696,8 @@ export default function FilePreviewPanel({ asset={{ environmentId, attachmentId: attachment.id }} htmlRender={attachment.htmlRender === true} threadRef={threadRef} + readingKey={reading.file} + rowLine={reading.rowLine} /> ) : relativePath && isVideo && absolutePath ? ( @@ -1776,6 +1777,7 @@ export default function FilePreviewPanel({ name={relativePath.split(/[\\/]/).at(-1) || relativePath} workspaceMutationId={workspaceMutationId} refreshRevision={fileRefreshRevision} + rowLine={reading.rowLine} /> ) : relativePath && file.error && file.data === null ? (
) : file.data.truncated || isHostFile || !canWriteFiles ? ( }> diff --git a/apps/web/src/components/files/OfficeViewer.tsx b/apps/web/src/components/files/OfficeViewer.tsx index e35cd9cf63fcc..b0741274b508f 100644 --- a/apps/web/src/components/files/OfficeViewer.tsx +++ b/apps/web/src/components/files/OfficeViewer.tsx @@ -58,6 +58,8 @@ export default function OfficeViewer(props: { readonly name: string; readonly kind: OfficePreviewKind; readonly sizeBytes?: number; + /** The row the workbook's opening sheet reveals. */ + readonly revealLine?: number | null; }) { if (props.kind === "docx" || props.kind === "xlsx" || props.kind === "pptx") return ( @@ -73,6 +75,7 @@ function ExtendedOfficeViewer(props: { readonly name: string; readonly kind: OfficePreviewKind; readonly sizeBytes?: number; + readonly revealLine?: number | null; }) { const gt = useGT(); const messages = useMessages(); @@ -352,6 +355,8 @@ function ExtendedOfficeViewer(props: { name={`${props.name}: ${sheetName(page)}`} count={sheet.rowCount} columns={sheet.columns} + sheet={page} + revealLine={page === 0 ? (props.revealLine ?? null) : null} getRows={(index) => (index === 0 ? sheet.rows : sheetPages.get(`${page}:${index}`))} loadPage={(index) => { const key = `${page}:${index}`; diff --git a/apps/web/src/components/files/PdfViewer.tsx b/apps/web/src/components/files/PdfViewer.tsx index c3e9025eccfd7..8a2f33b952fe5 100644 --- a/apps/web/src/components/files/PdfViewer.tsx +++ b/apps/web/src/components/files/PdfViewer.tsx @@ -47,6 +47,7 @@ export type PdfViewState = { offset: number; zoom: number | null; left: number; + width?: number; }; function PdfPage(props: { @@ -142,7 +143,7 @@ function PdfPage(props: { export default function PdfViewer(props: { src: string; title: string; - positionKey?: string; + positionKey?: string | undefined; initialView?: PdfViewState; onViewChange?: (view: PdfViewState) => void; actions?: ReactNode; @@ -170,6 +171,7 @@ export default function PdfViewer(props: { const scroll = useRef(null); const find = useRef(null); const zoomAnchor = useRef<{ page: number; offset: number; width: number } | null>(null); + const restored = useRef<{ top: number; left: number } | null>(null); const initialPage = initialView.page; // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Own the PDF fetch, load timeout, and pdf.js document task across source changes, retries, and unmounts. useEffect(() => { @@ -256,6 +258,7 @@ export default function PdfViewer(props: { offset: Math.max(0, node.scrollTop - (offsets[page]! - 16)), zoom, left: node.scrollLeft, + width, }; if (notify) props.onViewChange?.(view); if (props.positionKey !== undefined) { @@ -269,11 +272,12 @@ export default function PdfViewer(props: { const node = scroll.current; if (!pdf || !node) return; node.scrollTo({ - top: pdfRestoredScrollTop(ratios, width, initialView, savedPosition?.width), + top: pdfRestoredScrollTop(ratios, width, initialView, initialView.width), left: initialView.left, }); + restored.current = { top: node.scrollTop, left: node.scrollLeft }; setScrollTop(node.scrollTop); - saveView(node); + saveView(node, false); }); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Restore the saved PDF scroll position after the loaded document's page layout commits. useLayoutEffect(() => { @@ -461,7 +465,13 @@ export default function PdfViewer(props: { onScroll={(event) => { const top = event.currentTarget.scrollTop; setScrollTop(top); - saveView(event.currentTarget); + // The restore's own scroll event lands where the restore put the view. + const echo = restored.current; + restored.current = null; + saveView( + event.currentTarget, + echo?.top !== top || echo.left !== event.currentTarget.scrollLeft, + ); }} onKeyDown={(event) => { if ((event.ctrlKey || event.metaKey) && event.key === "f") { diff --git a/apps/web/src/components/files/VirtualTablePreview.tsx b/apps/web/src/components/files/VirtualTablePreview.tsx index 5154153807df1..c7c302189cfd9 100644 --- a/apps/web/src/components/files/VirtualTablePreview.tsx +++ b/apps/web/src/components/files/VirtualTablePreview.tsx @@ -3,6 +3,7 @@ import { useEffect, useEffectEvent, useLayoutEffect, useRef, useState } from "re import { observeResize } from "~/lib/observeResize"; import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip"; import { PREVIEW_PAGE_ROWS } from "./largeTextProtocol"; +import { announceViewer } from "./fileReadingPlace"; export default function VirtualTablePreview(props: { readonly name: string; @@ -15,6 +16,8 @@ export default function VirtualTablePreview(props: { readonly wrap?: boolean; readonly revealLine?: number | null; readonly revealRequestId?: number; + /** The index of the workbook sheet the table shows. */ + readonly sheet?: number; }) { const ref = useRef(null); const [left, setLeft] = useState(0); @@ -124,9 +127,11 @@ export default function VirtualTablePreview(props: { ) : null} {width > 0 && (!props.header || props.columns > 30 || measured) ? (
report
'; + +/** Revisions of one HTML file in the right panel's frame, with the pages' messages recorded. */ +function htmlPages(name: string) { + const readingKey = { + kind: "path", + environmentId, + absolutePath: `/reading/${name}/page.html`, + } as const; + const urls: string[] = []; + const messages: Array<{ source: MessageEventSource | null; data: unknown }> = []; + const onMessage = (event: MessageEvent) => { + messages.push({ source: event.source, data: event.data }); + }; + window.addEventListener("message", onMessage); + const reports = (source: MessageEventSource | null) => + messages.flatMap((message) => { + const scroll = message.source === source ? readFilePreviewScroll(message.data) : undefined; + return scroll === undefined ? [] : [scroll.y]; + }); + return { + readingKey, + saved: () => readings.readPosition(readingKey, "offset")?.top, + stored: () => + localStorage.getItem( + `t3code.fileReading.v1:${JSON.stringify([environmentId, readingKey.absolutePath, "offset"])}`, + ), + /** A page that posts `loaded:` on load; `bridge` adds the scroll script. */ + url(name: string, body = TALL_PAGE, options = { bridge: true }) { + const loaded = ``; + const html = body + loaded + (options.bridge ? FILE_PREVIEW_SCROLL_SCRIPT : ""); + const url = URL.createObjectURL(new Blob([html], { type: "text/html" })); + urls.push(url); + return url; + }, + page: (revision: string, src: string) => ( +
+ +
+ ), + loaded: (name: string) => messages.some((message) => message.data === `loaded:${name}`), + scrollPage: (frame: HTMLIFrameElement, y: number) => + frame.contentWindow?.postMessage({ scrollTo: y }, "*"), + reports, + lastY: (source: MessageEventSource | null) => reports(source).at(-1), + /** The frames' URLs, the page on screen last. */ + sources: (root: HTMLElement) => + Array.from(root.querySelectorAll("iframe"), (frame) => frame.src), + shown: (root: HTMLElement) => Array.from(root.querySelectorAll("iframe")).at(-1)!, + [Symbol.dispose]() { + window.removeEventListener("message", onMessage); + for (const url of urls) URL.revokeObjectURL(url); + for (const key of Object.keys(localStorage)) { + if (key.startsWith("t3code.fileReading.v1:")) localStorage.removeItem(key); + } + }, + }; +} + +/** Keeps the frame load event for `src` from the app, as a subresource that never finishes does. */ +function stallLoad(src: string) { + let stalled: HTMLIFrameElement | null = null; + // A frame's load event reaches the document but not the window. + const hold = (event: Event) => { + if (stalled !== null || !(event.target instanceof HTMLIFrameElement)) return; + if (event.target.src !== src) return; + stalled = event.target; + event.stopPropagation(); + }; + document.addEventListener("load", hold, true); + const stop = () => document.removeEventListener("load", hold, true); + return { + release() { + stop(); + stalled?.dispatchEvent(new Event("load")); + }, + [Symbol.dispose]: stop, + }; +} + +describe("an HTML file's frame keeps the reader's place", () => { + it("returns an HTML page to the reader's place, and a page shorter at load keeps that place", async () => { + using pages = htmlPages("html"); + const first = pages.url("first"); + // This page is short when it loads, then grows without scrolling, as late content does. + const second = pages.url( + "second", + `
report
`, + ); + await using screen = await render(pages.page("1", first)); + await vi.waitFor(() => expect(pages.loaded("first")).toBe(true)); + await userEvent.wheel(pages.shown(screen.container), { delta: { y: 3000 } }); + await vi.waitFor(() => expect(pages.saved()).toBeGreaterThan(2000)); + + await screen.rerender(<>); + const stored = pages.stored(); + const saved = pages.saved(); + await screen.rerender(pages.page("2", second)); + await vi.waitFor(() => { + expect(pages.lastY(pages.shown(screen.container).contentWindow)).toBe(saved); + }); + expect(pages.stored()).toBe(stored); + }); + + it("saves an HTML page once a later revision takes the place a stalled first page never took", async () => { + using pages = htmlPages("html-stalled"); + readings.writePosition(pages.readingKey, { view: "offset", top: 1500, left: 0 }); + const [first, second] = [pages.url("first"), pages.url("second")]; + using _stall = stallLoad(first); + await using screen = await render(pages.page("1", first)); + await vi.waitFor(() => expect(pages.loaded("first")).toBe(true)); + + await screen.rerender(pages.page("2", second)); + await vi.waitFor(() => expect(pages.sources(screen.container)).toEqual([second])); + const shown = pages.shown(screen.container); + await vi.waitFor(() => expect(pages.lastY(shown.contentWindow)).toBe(1500)); + // The page scrolls itself: a wheel sent as the frame is revealed can miss it. + pages.scrollPage(shown, 3500); + await vi.waitFor(() => expect(pages.saved()).toBe(3500)); + }); + + it("keeps an HTML revision where the reader scrolled it after a stalled swap showed it, in a later revision too", async () => { + using pages = htmlPages("html-early"); + readings.writePosition(pages.readingKey, { view: "offset", top: 1500, left: 0 }); + const [first, second, third] = [pages.url("first"), pages.url("second"), pages.url("third")]; + await using screen = await render(pages.page("1", first)); + await vi.waitFor(() => expect(pages.loaded("first")).toBe(true)); + using stall = stallLoad(second); + using _timers = fakeTimers(); + await screen.rerender(pages.page("2", second)); + await vi.waitFor(() => expect(pages.loaded("second")).toBe(true)); + await act(() => vi.advanceTimersByTime(5_000)); + await vi.waitFor(() => expect(pages.sources(screen.container)).toEqual([second])); + const content = pages.shown(screen.container).contentWindow; + // The reader scrolls the page on screen, which the parent sees only through its reports. + pages.scrollPage(pages.shown(screen.container), 3000); + await vi.waitFor(() => expect(pages.lastY(content)).toBeGreaterThan(2500)); + const place = pages.lastY(content); + expect(pages.saved()).toBe(1500); + + stall.release(); + expect(pages.saved()).toBe(place); + await screen.rerender(pages.page("3", third)); + await vi.waitFor(() => expect(pages.sources(screen.container)).toEqual([third])); + await vi.waitFor(() => { + expect(pages.lastY(pages.shown(screen.container).contentWindow)).toBe(place); + }); + expect(pages.reports(content).every((y) => y !== 1500)).toBe(true); + }); + + it("stalls once for an HTML page without the scroll script, then shows later revisions on load", async () => { + using pages = htmlPages("html-plain"); + readings.writePosition(pages.readingKey, { view: "offset", top: 1500, left: 0 }); + const [first, second, third] = ["first", "second", "third"].map((name) => + pages.url(name, undefined, { bridge: false }), + ) as [string, string, string]; + await using screen = await render(pages.page("1", first)); + await vi.waitFor(() => expect(pages.loaded("first")).toBe(true)); + using _timers = fakeTimers(); + + await screen.rerender(pages.page("2", second)); + await vi.waitFor(() => expect(pages.loaded("second")).toBe(true)); + expect(pages.sources(screen.container)).toEqual([second, first]); + act(() => vi.advanceTimersByTime(5_000)); + await vi.waitFor(() => expect(pages.sources(screen.container)).toEqual([second])); + // Each check advances the fake clock by its interval, far short of another 5 s stall. + await screen.rerender(pages.page("3", third)); + await vi.waitFor(() => expect(pages.sources(screen.container)).toEqual([third])); + }); + + it("saves nothing from a page the HTML frame navigates to", async () => { + using pages = htmlPages("html-away"); + const elsewhere = `data:text/html,${encodeURIComponent( + `
elsewhere
${FILE_PREVIEW_SCROLL_SCRIPT}`, + )}`; + const first = pages.url( + "first", + `${TALL_PAGE}`, + ); + await using screen = await render(pages.page("1", first)); + await vi.waitFor(() => expect(pages.loaded("first")).toBe(true)); + const content = pages.shown(screen.container).contentWindow!; + await userEvent.wheel(pages.shown(screen.container), { delta: { y: 1000 } }); + await vi.waitFor(() => expect(pages.saved()).toBeGreaterThan(500)); + const place = pages.saved(); + + content.postMessage("leave", "*"); + await vi.waitFor(() => expect(pages.lastY(content)).toBe(4321)); + expect(pages.saved()).toBe(place); + }); +}); + +describe("PDF reading view", () => { + function PdfView(props: { + readonly file: FileReadingKey; + readonly onView: (page: number) => void; + }) { + const [view, setView] = usePdfReadingView(props.file); + props.onView(view.page); + return ( + + ); + } + + it("seeds a new PDF.js mount with the page saved by an earlier one", async () => { + const file = { kind: "path", environmentId, absolutePath: "/reading/pdf/report.pdf" } as const; + const pages: number[] = []; + try { + { + await using screen = await render( + pages.push(page)} />, + ); + await userEvent.click(screen.getByRole("button", { name: "Go to page 3" })); + await vi.waitFor(() => expect(pages.at(-1)).toBe(2)); + } + pages.length = 0; + await using screen = await render( + pages.push(page)} />, + ); + await expect.element(screen.getByRole("button")).toBeVisible(); + expect(pages[0]).toBe(2); + } finally { + for (const key of Object.keys(localStorage)) { + if (key.startsWith("t3code.fileReading.v1:")) localStorage.removeItem(key); + } + } + }); + + it("keeps the browser's own viewer for a PDF without a reading key, on its first URL until the file changes", async () => { + using _native = vi.spyOn(navigator, "pdfViewerEnabled", "get").mockReturnValue(true); + const [original, renewed, edited] = ["%PDF-1.4", "%PDF-1.4", "%PDF-1.4 edited"].map((text) => + URL.createObjectURL(new Blob([text], { type: "application/pdf" })), + ) as [string, string, string]; + const draft = (src: string, revision: string) => ( + + ); + try { + await using screen = await render(draft(original, "one")); + const opened = screen.container.querySelector("iframe"); + expect(opened?.src.startsWith(original)).toBe(true); + + // A renewed grant for the same revision keeps the frame and the URL it opened with. + await screen.rerender(draft(renewed, "one")); + expect(screen.container.querySelector("iframe")).toBe(opened); + expect(opened?.src.startsWith(original)).toBe(true); + + await screen.rerender(draft(edited, "two")); + const reloaded = screen.container.querySelector("iframe"); + expect(reloaded).not.toBe(opened); + expect(reloaded?.src.startsWith(edited)).toBe(true); + } finally { + for (const url of [original, renewed, edited]) URL.revokeObjectURL(url); + } + }); +}); diff --git a/apps/web/src/components/files/fileReadingFrame.test.ts b/apps/web/src/components/files/fileReadingFrame.test.ts new file mode 100644 index 0000000000000..ea1766db69934 --- /dev/null +++ b/apps/web/src/components/files/fileReadingFrame.test.ts @@ -0,0 +1,135 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { FrameReading } from "./fileReadingFrame"; + +const at = (y: number) => ({ x: 0, y }); +const restore = (y: number) => ({ type: "restore", scroll: at(y) }) as const; +const save = (y: number) => ({ type: "save", scroll: at(y) }) as const; +const RETRY = { type: "retry" } as const; +const REVEAL = { type: "reveal" } as const; +const SHOW = { type: "show" } as const; + +/** Retries until the hold gives up, and every action they returned. */ +function retryAll(reading: FrameReading, frame: object) { + const actions = []; + for (let step = 0; step < 20; step += 1) { + const next = reading.retry(frame); + if (next.length === 0) break; + actions.push(...next); + } + return actions; +} + +describe("FrameReading", () => { + it("keeps the restore target, not a short report from the hold, as the place", () => { + const reading = new FrameReading(at(5000)); + const [shown, next] = [{}, {}]; + // A page that scrolls itself before its load is restored all the same. + expect(reading.report(shown, "shown", at(9000))).toEqual([]); + expect(reading.load(shown, "shown")).toEqual([restore(5000)]); + expect(reading.restored(shown, "shown")).toEqual([RETRY]); + expect(reading.report(shown, "shown", at(2000))).toEqual([restore(5000)]); + + expect(reading.load(next, "next")).toEqual([restore(5000)]); + expect(reading.restored(next, "next")).toEqual([RETRY, REVEAL]); + expect(reading.swap(next)).toEqual([SHOW]); + }); + + it("sends the next revision the place the reader moved to while it restored, before showing it", () => { + const reading = new FrameReading(null); + const [shown, next] = [{}, {}]; + reading.load(shown, "shown"); + expect(reading.report(shown, "shown", at(1200))).toEqual([save(1200)]); + expect(reading.load(next, "next")).toEqual([restore(1200)]); + expect(reading.restored(next, "next")).toEqual([RETRY, REVEAL]); + expect(reading.report(shown, "shown", at(1600))).toEqual([save(1600)]); + + expect(reading.swap(next)).toEqual([restore(1600)]); + // The retry its first restore started is still due, so one chain holds it from here. + expect(reading.restored(next, "next")).toEqual([REVEAL]); + // Once that chain ends, a restore after the reader moves again starts another. + retryAll(reading, next); + expect(reading.report(shown, "shown", at(2000))).toEqual([save(2000)]); + expect(reading.swap(next)).toEqual([restore(2000)]); + expect(reading.restored(next, "next")).toEqual([RETRY, REVEAL]); + expect(reading.swap(next)).toEqual([SHOW]); + + // On a first visit, the revision loads before any place; the reader scrolls before it shows. + const first = new FrameReading(null); + const [page, edited] = [{}, {}]; + first.load(page, "shown"); + expect(first.load(edited, "next")).toEqual([REVEAL]); + expect(first.report(page, "shown", at(900))).toEqual([save(900)]); + expect(first.swap(edited)).toEqual([restore(900)]); + expect(first.restored(edited, "next")).toEqual([RETRY, REVEAL]); + expect(first.swap(edited)).toEqual([SHOW]); + }); + + it("asks a page that grows without scrolling again, then lets it be after the hold", () => { + const reading = new FrameReading(at(3000)); + const page = {}; + reading.load(page, "shown"); + reading.restored(page, "shown"); + expect(reading.retry(page)).toEqual([restore(3000), RETRY]); + expect(reading.report(page, "shown", at(700))).toEqual([restore(3000)]); + const held = retryAll(reading, page); + expect(held.filter((action) => action.type === "restore").length).toBeGreaterThan(5); + expect(reading.retry(page)).toEqual([]); + expect(reading.report(page, "shown", at(650))).toEqual([save(650)]); + + const grown = new FrameReading(at(3000)); + grown.load(page, "shown"); + grown.restored(page, "shown"); + expect(grown.report(page, "shown", at(3000))).toEqual([]); + expect(grown.retry(page)).toEqual([]); + expect(grown.report(page, "shown", at(3400))).toEqual([save(3400)]); + + // A wider panel leaves no room to the side; the page reached its place all the same. + const wider = new FrameReading({ x: 300, y: 3000 }); + const narrow = {}; + wider.load(narrow, "shown"); + wider.restored(narrow, "shown"); + expect(wider.report(narrow, "shown", at(3000))).toEqual([]); + expect(wider.retry(narrow)).toEqual([]); + }); + + it("keeps the save of one page from a later page that was shown before it loaded", () => { + const reading = new FrameReading(null); + const [first, late, plain] = [{}, {}, {}]; + reading.load(first, "shown"); + expect(reading.report(first, "shown", at(1200))).toEqual([save(1200)]); + + reading.swapTimedOut(late); + expect(reading.report(late, "shown", at(40))).toEqual([]); + expect(reading.load(late, "shown")).toEqual([save(40)]); + expect(reading.report(late, "shown", at(90))).toEqual([save(90)]); + + // A page that did not move before its late load is restored to the place. + reading.swapTimedOut(plain); + expect(reading.load(plain, "shown")).toEqual([restore(90)]); + }); + + it("saves nothing from a document the frame navigated to", () => { + const reading = new FrameReading(null); + const frame = {}; + reading.load(frame, "shown"); + expect(reading.report(frame, "shown", at(300))).toEqual([save(300)]); + expect(reading.load(frame, "shown")).toEqual([]); + expect(reading.report(frame, "shown", at(4321))).toEqual([]); + }); + + it("waits once for a revision without the scroll script, then shows later ones on load", () => { + const reading = new FrameReading(at(1500)); + const [first, second, third] = [{}, {}, {}]; + reading.load(first, "shown"); + expect(reading.load(second, "next")).toEqual([restore(1500)]); + reading.swapTimedOut(second); + // The reader follows a link to a page with the scroll script, which says nothing for the file. + reading.load(second, "shown"); + expect(reading.report(second, "shown", at(300))).toEqual([]); + expect(reading.restored(second, "shown")).toEqual([]); + expect(reading.load(third, "next")).toEqual([restore(1500), REVEAL]); + reading.restored(third, "next"); + expect(reading.load({}, "next")).toEqual([restore(1500)]); + }); +}); diff --git a/apps/web/src/components/files/fileReadingFrame.ts b/apps/web/src/components/files/fileReadingFrame.ts new file mode 100644 index 0000000000000..c904fdfe37cb2 --- /dev/null +++ b/apps/web/src/components/files/fileReadingFrame.ts @@ -0,0 +1,186 @@ +// How an HTML file's frame finds and keeps the reader's place. Each document a frame shows has +// its own phase; the frame component posts, saves and reveals what these steps return. +import type { FilePreviewScroll } from "@t3tools/shared/htmlRender"; + +/** How often a restored page that is still short of its place is asked again. */ +export const FRAME_RETRY_MS = 100; +// A page may still grow for a while after it takes a restore; then the reader's scrolls count. +const HOLD_RETRIES = 10; + +type Phase = + /** Before its load; its reports wait for the load to decide. */ + | { readonly name: "opening" } + | { readonly name: "restoring"; readonly target: FilePreviewScroll } + /** Took the restore; a report or check short of the target asks again until the hold ends. */ + | { readonly name: "holding"; readonly target: FilePreviewScroll; readonly retries: number } + | { readonly name: "saving" } + /** The frame loaded another document, such as a linked page; nothing it reports is kept. */ + | { readonly name: "away" }; + +interface FrameDocument { + phase: Phase; + loads: number; + /** The document's last reported scroll. */ + at: FilePreviewScroll | null; + /** The place last sent to it. */ + sent: FilePreviewScroll | null; + /** A timed-out swap showed it before its load. */ + early: boolean; + /** A `retry` is due; one chain of them holds the document, however often it is restored. */ + retrying: boolean; +} + +/** The revision on screen, or the one loading beneath it. */ +export type FrameRole = "shown" | "next"; + +export type FrameAction = + | { readonly type: "restore"; readonly scroll: FilePreviewScroll } + /** Paint the next revision for two frames, then call `swap`. */ + | { readonly type: "reveal" } + /** Show the next revision in place of the one on screen. */ + | { readonly type: "show" } + | { readonly type: "save"; readonly scroll: FilePreviewScroll } + /** Call `retry` for this document after `FRAME_RETRY_MS`. */ + | { readonly type: "retry" }; + +const ORIGIN: FilePreviewScroll = { x: 0, y: 0 }; +const SAVING: Phase = { name: "saving" }; +const REVEAL: FrameAction = { type: "reveal" }; +const SHOW: FrameAction = { type: "show" }; +const RETRY: FrameAction = { type: "retry" }; + +// A page grows down after it loads; one with no room to the side never reaches a sideways place. +const short = (scroll: FilePreviewScroll, target: FilePreviewScroll) => scroll.y < target.y - 1; +const same = (left: FilePreviewScroll, right: FilePreviewScroll) => + left.x === right.x && left.y === right.y; + +/** The reader's place in one HTML file across the frame's documents, keyed by their windows. */ +export class FrameReading { + /** The saved place, then each report accepted from the document on screen. */ + private place: FilePreviewScroll | null; + /** A revision without the scroll script stalled a swap, so later ones show on load. */ + private noBridge = false; + private readonly documents = new WeakMap(); + + constructor(seed: FilePreviewScroll | null) { + this.place = seed; + } + + load(frame: W, role: FrameRole): FrameAction[] { + const document = this.document(frame); + if (++document.loads > 1) { + document.phase = { name: "away" }; + return []; + } + if (document.phase.name !== "opening") return []; + if (this.place === null) { + document.phase = SAVING; + return role === "next" ? [REVEAL] : []; + } + if (role === "next") { + return [this.restore(document, this.place), ...(this.noBridge ? [REVEAL] : [])]; + } + // A page a timed-out swap showed before its load keeps where the reader took it meanwhile. + if (document.early && document.at !== null) return this.accept(document, document.at); + return [this.restore(document, this.place)]; + } + + report(frame: W, role: FrameRole, scroll: FilePreviewScroll): FrameAction[] { + const document = this.document(frame); + const { phase } = document; + if (phase.name === "away") return []; + this.noBridge = false; + document.at = scroll; + if (phase.name === "holding") { + if (short(scroll, phase.target)) return [{ type: "restore", scroll: phase.target }]; + document.phase = SAVING; + return []; + } + if (role === "shown" && (phase.name === "saving" || this.place === null)) { + return this.accept(document, scroll); + } + return []; + } + + /** The document scrolled to the place it was sent. */ + restored(frame: W, role: FrameRole): FrameAction[] { + const document = this.document(frame); + if (document.phase.name === "away") return []; + this.noBridge = false; + if (document.phase.name !== "restoring") return []; + document.phase = { name: "holding", target: document.phase.target, retries: 0 }; + const retry = document.retrying ? [] : [RETRY]; + document.retrying = true; + return role === "next" ? [...retry, REVEAL] : retry; + } + + retry(frame: W): FrameAction[] { + const document = this.documents.get(frame); + if (document === undefined) return []; + const { phase } = document; + if (phase.name === "holding" && phase.retries < HOLD_RETRIES) { + document.phase = { ...phase, retries: phase.retries + 1 }; + // A page that did not move sends no report. + return short(document.at ?? ORIGIN, phase.target) + ? [{ type: "restore", scroll: phase.target }, RETRY] + : [RETRY]; + } + if (phase.name === "holding") document.phase = SAVING; + document.retrying = false; + return []; + } + + /** Just before the next revision shows, it gets the place if it was never sent or moved since. */ + swap(frame: W): FrameAction[] { + const document = this.documents.get(frame); + if ( + document === undefined || + document.phase.name === "away" || + this.place === null || + (document.sent !== null && same(document.sent, this.place)) + ) { + return [SHOW]; + } + // A page that never took a restore cannot say it took this one either. + const waits = document.phase.name !== "restoring"; + return waits + ? [this.restore(document, this.place)] + : [this.restore(document, this.place), SHOW]; + } + + /** The next revision was shown before it took a restore. */ + swapTimedOut(frame: W | null): void { + const document = frame === null ? undefined : this.document(frame); + if (document !== undefined) document.early = true; + const phase = document?.phase.name; + if (phase === undefined || phase === "opening" || phase === "restoring") this.noBridge = true; + } + + private document(frame: W): FrameDocument { + let document = this.documents.get(frame); + if (document === undefined) { + document = { + phase: { name: "opening" }, + loads: 0, + at: null, + sent: null, + early: false, + retrying: false, + }; + this.documents.set(frame, document); + } + return document; + } + + private restore(document: FrameDocument, target: FilePreviewScroll): FrameAction { + document.phase = { name: "restoring", target }; + document.sent = target; + return { type: "restore", scroll: target }; + } + + private accept(document: FrameDocument, scroll: FilePreviewScroll): FrameAction[] { + document.phase = SAVING; + this.place = scroll; + return [{ type: "save", scroll }]; + } +} diff --git a/apps/web/src/components/files/fileReadingPlace.browser.test.tsx b/apps/web/src/components/files/fileReadingPlace.browser.test.tsx new file mode 100644 index 0000000000000..e26935e6095b3 --- /dev/null +++ b/apps/web/src/components/files/fileReadingPlace.browser.test.tsx @@ -0,0 +1,253 @@ +import type { ReactNode } from "react"; +import { describe, expect, it, vi } from "vite-plus/test"; +import { userEvent } from "vite-plus/test/browser"; + +import { act, fakeTimers, render } from "../../../test/render"; +import appCss from "~/index.css?inline"; + +import { type Citation, useViewerPlace } from "./fileReadingPlace"; +import { readings } from "./fileReadingSave"; +import type { FileReadingKey } from "./fileReadingStore"; +import { PREVIEW_PAGE_ROWS } from "./largeTextProtocol"; +import VirtualTablePreview from "./VirtualTablePreview"; + +/** A workspace file of its own, so no case reads a place another case saved. */ +function placeFile(name: string) { + const file: FileReadingKey = { + kind: "path", + environmentId: "file-reading-place-test", + absolutePath: `/place/${name}`, + }; + return { + file, + [Symbol.dispose]() { + for (const key of Object.keys(localStorage)) { + if (key.startsWith("t3code.fileReading.v1:")) localStorage.removeItem(key); + } + }, + }; +} + +/** The app's styles, which lay out the table viewer's scroller. */ +function appStyles() { + const style = document.createElement("style"); + style.textContent = appCss; + document.head.append(style); + return { [Symbol.dispose]: () => style.remove() }; +} + +/** Rendered Markdown's shape: blocks in a scroll area viewport. */ +function Markdown(props: { readonly blocks: number }) { + return ( +
+
+ {Array.from({ length: props.blocks }, (_, index) => ( +

+ Block {index + 1} +

+ ))} +
+
+ ); +} + +/** A workspace file's code view, which keeps its own place. */ +function CodeView() { + return ( +
+
const a = 1;
+
+ ); +} + +/** The viewer container, with a control that switches the viewer, such as a mode toggle. */ +function Viewer(props: { readonly file: FileReadingKey; readonly children: ReactNode }) { + const { ref } = useViewerPlace({ + file: props.file, + views: ["rendered", "offset"], + citation: null, + }); + return ( +
+ + {props.children} +
+ ); +} + +const sheetRows = (page: number) => + Array.from({ length: PREVIEW_PAGE_ROWS }, (_, row) => [ + `row ${page * PREVIEW_PAGE_ROWS + row + 1}`, + String(row), + ]); + +/** + * A table the viewer replaces inside a wrapper that stays, as a workbook's sheet switch does. + * `indexed` holds the reveal back, as the large text viewer does while it indexes. + */ +function Sheet(props: { + readonly file: FileReadingKey; + readonly mount: number; + readonly citation?: Citation; + readonly indexed?: boolean; +}) { + const { ref, rowLine } = useViewerPlace({ + file: props.file, + views: ["rendered", "offset"], + citation: props.citation ?? null, + }); + return ( +
+
+ +
+
+ ); +} + +/** The table's scroller and the data index of the first row it shows. */ +function tableView(container: HTMLElement) { + const scroller = () => container.querySelector("[aria-rowcount] > [tabindex]"); + return { + scroller, + topRow() { + const element = scroller(); + if (element === null) return null; + const top = element.getBoundingClientRect().top; + const row = [...element.querySelectorAll("[data-preview-row]")].find( + (candidate) => candidate.getBoundingClientRect().bottom > top + 0.5, + ); + return row === undefined ? null : Number(row.dataset.previewRow); + }, + }; +} + +/** The observers that watch the whole viewer for a scroller to place, and the ones disconnected. */ +function viewerLooks() { + const observe = vi.spyOn(MutationObserver.prototype, "observe"); + const disconnect = vi.spyOn(MutationObserver.prototype, "disconnect"); + return { + started: (root: Element | null) => + observe.mock.calls.flatMap(([target, options], index) => + target === root && options?.subtree === true ? [observe.mock.contexts[index]] : [], + ), + stopped: (look: unknown) => disconnect.mock.contexts.includes(look as MutationObserver), + [Symbol.dispose]() { + observe.mockRestore(); + disconnect.mockRestore(); + }, + }; +} + +const press = (element: Element) => + element.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true })); +const typeIn = (element: Element) => + element.dispatchEvent(new KeyboardEvent("keydown", { key: "a", bubbles: true })); + +describe("the viewer container's look for a scroller", () => { + it("watches nothing for a file without a saved place, whatever the reader does", async () => { + using notes = placeFile("fresh.md"); + using looks = viewerLooks(); + await using screen = await render( + + + , + { router: false }, + ); + const root = screen.container.firstElementChild; + await screen.rerender( + + + , + ); + press(root!.querySelector("p")!); + typeIn(root!.querySelector("p")!); + press(screen.getByRole("button").element()); + expect(looks.started(root)).toEqual([]); + }); + + it("stops once it places the saved block or meets the code view, and only a control looks again, briefly", async () => { + using notes = placeFile("read.md"); + using looks = viewerLooks(); + readings.writePosition(notes.file, { view: "rendered", block: 20, fraction: 0 }); + await using screen = await render( + + + , + { router: false }, + ); + const root = screen.container.firstElementChild; + const viewport = root!.querySelector('[data-slot="scroll-area-viewport"]')!; + await vi.waitFor(() => expect(viewport.scrollTop).toBe(800)); + const [opening] = looks.started(root); + expect(looks.stopped(opening)).toBe(true); + + // The source view replaces the rendered one; it keeps its own place, so the look ends there. + await screen.rerender( + + + , + ); + const [, swapped] = looks.started(root); + await vi.waitFor(() => expect(looks.stopped(swapped)).toBe(true)); + + using _timers = fakeTimers(); + const code = root!.querySelector(".file-preview-virtualizer pre")!; + press(code); + typeIn(code); + expect(looks.started(root)).toHaveLength(2); + press(screen.getByRole("button").element()); + const [, , clicked] = looks.started(root); + expect(clicked).toBeDefined(); + expect(looks.stopped(clicked)).toBe(false); + await act(() => vi.advanceTimersByTime(2_000)); + expect(looks.stopped(clicked)).toBe(true); + }); +}); + +describe("the viewer container's table rows", () => { + it("returns a table the viewer mounts again inside it to the reader's row, long after any click", async () => { + using _styles = appStyles(); + using sheet = placeFile("book.ods"); + readings.writePosition(sheet.file, { view: "rendered", block: 80, fraction: 0 }); + await using screen = await render(, { router: false }); + const table = tableView(screen.container); + await vi.waitFor(() => expect(table.topRow()).toBe(80)); + await userEvent.wheel(table.scroller()!, { delta: { y: 1500 } }); + await vi.waitFor(() => + expect(readings.readPosition(sheet.file, "rendered")?.block).toBeGreaterThan(110), + ); + const read = readings.readPosition(sheet.file, "rendered")!.block; + + await screen.rerender(); + await vi.waitFor(() => expect(table.topRow()).toBe(read)); + }); + + it("leaves a table where the reader scrolled it while a cited line waits for the raw text", async () => { + using _styles = appStyles(); + using sheet = placeFile("data.csv"); + readings.writePosition(sheet.file, { view: "rendered", block: 80, fraction: 0 }); + const citation: Citation = { id: 1, line: 300, pending: () => true, shown: () => {} }; + const sheetAt = (indexed: boolean) => ( + + ); + await using screen = await render(sheetAt(false), { router: false }); + const table = tableView(screen.container); + await vi.waitFor(() => expect(table.topRow()).toBe(0)); + await userEvent.wheel(table.scroller()!, { delta: { y: 1000 } }); + await vi.waitFor(() => + expect(readings.readPosition(sheet.file, "rendered")?.block).toBeGreaterThan(20), + ); + const top = table.topRow(); + + await screen.rerender(sheetAt(true)); + expect(table.topRow()).toBe(top); + }); +}); diff --git a/apps/web/src/components/files/fileReadingPlace.ts b/apps/web/src/components/files/fileReadingPlace.ts new file mode 100644 index 0000000000000..03fcd37589e77 --- /dev/null +++ b/apps/web/src/components/files/fileReadingPlace.ts @@ -0,0 +1,785 @@ +// The container keeps the place of the DOM viewer it holds: by item for lists of blocks, else by +// offset; table rows restore through the viewer's own reveal (`rowLine`), media by playback time. +// A frame or a canvas viewer with its own state takes an adapter instead: `FrameReading` for +// HTML pages, `usePdfReadingView` for PDF.js, the source tracker for Pierre's code view. +import { useState, useSyncExternalStore } from "react"; + +import { observeResize } from "~/lib/observeResize"; + +import { PendingPosition, readings } from "./fileReadingSave"; +import { + fileReadingId, + type FileReadingKey, + type ReadingPosition, + type ReadingPositionOf, + type ReadingView, +} from "./fileReadingStore"; + +const HOLD_MS = 1000; +/** How long a viewer that loads may take to show the scroller of a saved place. */ +const LOOK_MS = 30_000; +/** A control may switch the viewer inside the container, such as a sheet or a mode. */ +const INPUT_LOOK_MS = 2000; +/** Sheet tabs, sheet pickers and mode toggles are buttons; a key or click in the content is not. */ +const VIEWER_SWITCH = "button"; +const CAPTURE_ATTEMPTS = 8; +/** Playback this close to where it started has not moved; a seek may land a little off. */ +const SAME_TIME_S = 0.01; + +type SourcePosition = ReadingPositionOf<"source">; +type RenderedPosition = ReadingPositionOf<"rendered">; +type OffsetPosition = ReadingPositionOf<"offset">; + +export interface ScrollTarget { + readonly top: number; + readonly left?: number; +} + +export interface ViewGeometry

{ + /** + * Where the scroller shows `position`, or null until the content can place it. `settled` turns + * true when the first second of a restore has passed. + */ + target(position: P, settled: boolean): ScrollTarget | null; + /** The position at the viewport top, or null while the content cannot tell. */ + capture(): P | null; +} + +/** A viewer's main scroller and how to read and place its position. */ +export interface Binding

{ + readonly element: HTMLElement; + readonly geometry: ViewGeometry

; +} + +interface Restore { + readonly element: HTMLElement; + readonly target: (settled: boolean) => ScrollTarget | null; + readonly holdUntil: number; + readonly until: number; + stopResize: (() => void) | null; + timer: ReturnType | null; +} + +/** + * Restores and saves the position of the scrollers `bind` accepts under `root`. Only scrolls that + * follow reader input on `root` are saved, so restores, reveals and layout resets never overwrite + * a saved position. + */ +export class ReadingTracker { + private state: "idle" | "holding" | "armed" = "idle"; + private frame: number | null = null; + private restoring: Restore | null = null; + /** A scrollbar drag can pause longer than the save delay without a new pointerdown. */ + private pointerDown = false; + private readonly pending: PendingPosition; + /** The newest position restored, captured or recorded. */ + anchor: ReadingPosition | null = null; + + constructor( + private readonly root: HTMLElement, + file: FileReadingKey, + private readonly bind: (scrolled: HTMLElement) => Binding | null, + private readonly onInput?: (event: Event) => void, + ) { + this.pending = new PendingPosition(file, () => { + if (this.state === "armed" && !this.pointerDown) this.state = "idle"; + }); + root.addEventListener("wheel", this.arm, { passive: true, capture: true }); + root.addEventListener("touchstart", this.arm, { passive: true, capture: true }); + root.addEventListener("pointerdown", this.press, { passive: true, capture: true }); + root.addEventListener("keydown", this.arm, { capture: true }); + // Scroll does not bubble, but its capture phase reaches every ancestor. + root.addEventListener("scroll", this.onScroll, { passive: true, capture: true }); + window.addEventListener("pointerup", this.release, true); + window.addEventListener("pointercancel", this.release, true); + window.addEventListener("pagehide", this.pending.flush); + } + + /** + * Places `position` every frame for a second, then on resizes of the scroller and its content + * until `until`. Reader input ends it. + */ + restore

(binding: Binding

, position: P, until = 0): void { + this.endRestore(); + this.anchor = position; + this.state = "holding"; + const holdUntil = performance.now() + HOLD_MS; + this.restoring = { + element: binding.element, + target: (settled) => binding.geometry.target(position, settled), + holdUntil, + until: Math.max(until, holdUntil), + stopResize: null, + timer: null, + }; + this.place(); + this.frame = requestAnimationFrame(this.hold); + } + + /** Hands the scroller to a reveal; a position the reader already scrolled to is kept. */ + stop(): void { + this.endRestore(); + this.pending.flush(); + this.state = "idle"; + } + + /** Saves where a landed reveal put the reader, without reading the half-rendered DOM. */ + record(position: ReadingPosition): void { + this.stop(); + this.anchor = position; + this.pending.write(position); + } + + dispose(): void { + this.stop(); + this.root.removeEventListener("wheel", this.arm, { capture: true }); + this.root.removeEventListener("touchstart", this.arm, { capture: true }); + this.root.removeEventListener("pointerdown", this.press, { capture: true }); + this.root.removeEventListener("keydown", this.arm, { capture: true }); + this.root.removeEventListener("scroll", this.onScroll, { capture: true }); + window.removeEventListener("pointerup", this.release, true); + window.removeEventListener("pointercancel", this.release, true); + window.removeEventListener("pagehide", this.pending.flush); + } + + private readonly press = (event: Event) => { + this.pointerDown = true; + this.arm(event); + }; + + private readonly release = () => { + this.pointerDown = false; + if (this.state === "armed" && !this.pending.waiting && this.frame === null) this.state = "idle"; + }; + + private readonly arm = (event: Event) => { + if (this.state === "holding") this.endRestore(); + this.state = "armed"; + // Input that scrolls nothing (a key in the editor, a wheel at the end) disarms after the delay. + this.pending.touch(); + this.onInput?.(event); + }; + + private readonly onScroll = (event: Event) => { + if (this.state !== "armed" || this.frame !== null || !(event.target instanceof HTMLElement)) { + return; + } + const binding = this.bind(event.target); + if (binding !== null) this.capture(binding, CAPTURE_ATTEMPTS); + }; + + private capture(binding: Binding, attempts: number): void { + this.frame = requestAnimationFrame(() => { + this.frame = null; + if (this.state !== "armed") return; + const position = binding.geometry.capture(); + if (position === null) { + // Pierre moves its rendered window a frame or two after a long jump. + if (attempts > 1) this.capture(binding, attempts - 1); + return; + } + this.anchor = position; + this.pending.hold(position); + }); + } + + private readonly hold = (time: number) => { + this.frame = null; + const restoring = this.restoring; + if (this.state !== "holding" || restoring === null) return; + if (time < restoring.holdUntil) { + this.place(); + this.frame = requestAnimationFrame(this.hold); + return; + } + this.place(); + const remaining = restoring.until - performance.now(); + if (remaining <= 0) { + this.endRestore(); + this.state = "idle"; + return; + } + const { element } = restoring; + const content = element.firstElementChild; + restoring.stopResize = observeResize(content === null ? element : [element, content], () => + this.place(), + ); + restoring.timer = setTimeout(() => { + this.endRestore(); + if (this.state === "holding") this.state = "idle"; + }, remaining); + }; + + private place(): void { + const restoring = this.restoring; + if (restoring === null) return; + const { element } = restoring; + const target = restoring.target(performance.now() >= restoring.holdUntil); + if (target === null) return; + const top = clamp(target.top, element.scrollHeight - element.clientHeight); + if (Math.abs(element.scrollTop - top) > 1) element.scrollTop = top; + if (target.left === undefined) return; + const left = clamp(target.left, element.scrollWidth - element.clientWidth); + if (Math.abs(element.scrollLeft - left) > 1) element.scrollLeft = left; + } + + private endRestore(): void { + if (this.frame !== null) cancelAnimationFrame(this.frame); + this.frame = null; + const restoring = this.restoring; + this.restoring = null; + restoring?.stopResize?.(); + if (restoring?.timer != null) clearTimeout(restoring.timer); + } +} + +const clamp = (value: number, max: number) => Math.min(Math.max(0, value), Math.max(0, max)); + +/** Table or large text rows, which restore through the viewer's own reveal. */ +interface RowsBinding extends Binding { + readonly restores: "rows"; + readonly raw: boolean; +} + +/** A viewer's main scroller and the view it restores from. */ +type ViewerBinding = + | (Binding & { readonly restores: "rendered" }) + | (Binding & { readonly restores: "offset" }) + | (Binding & { readonly restores: "source" }) + | RowsBinding; + +type Shape = (root: HTMLElement, file: FileReadingKey) => ViewerBinding | null; + +/** + * Only a workbook's first visible sheet keeps a place; `data-sheet` is the sheet's position among + * the visible ones, and a viewer that is no sheet has none. + */ +const laterSheet = (element: HTMLElement) => + element.dataset.sheet !== undefined && element.dataset.sheet !== "0"; + +/** Items in order, such as pages or cells; the viewport top's item and fraction is the place. */ +function blockGeometry( + scroller: HTMLElement, + items: () => ArrayLike | undefined, + /** Items mount in batches, so an index past the end waits for the hold before it caps. */ + batched: boolean, +): ViewGeometry { + return { + target(position, settled) { + // The first item's top margin belongs to the top of the file. + if (position.block === 0 && position.fraction === 0) return { top: 0 }; + const list = items(); + if (list === undefined || list.length === 0) return null; + const item = list[position.block]; + if (item === undefined) return batched && !settled ? null : { top: scroller.scrollHeight }; + const rect = item.getBoundingClientRect(); + return { + top: + scroller.scrollTop + + rect.top - + scroller.getBoundingClientRect().top + + position.fraction * rect.height, + }; + }, + capture() { + const list = items(); + if (list === undefined || list.length === 0) return null; + const top = scroller.getBoundingClientRect().top; + let low = 0; + let high = list.length - 1; + while (low < high) { + const middle = (low + high) >> 1; + if (list[middle]!.getBoundingClientRect().bottom > top) high = middle; + else low = middle + 1; + } + const rect = list[low]!.getBoundingClientRect(); + const fraction = rect.height > 0 ? (top - rect.top) / rect.height : 0; + return { view: "rendered", block: low, fraction: Math.min(Math.max(0, fraction), 0.999) }; + }, + }; +} + +function blocks( + scroller: HTMLElement | null | undefined, + items: (scroller: HTMLElement) => ArrayLike | undefined, + batched: boolean, +): ViewerBinding | null { + if (!scroller) return null; + return { + element: scroller, + restores: "rendered", + geometry: blockGeometry(scroller, () => items(scroller), batched), + }; +} + +function offsetBinding(scroller: Element | null | undefined): ViewerBinding | null { + if (!(scroller instanceof HTMLElement) || laterSheet(scroller)) return null; + return { + element: scroller, + restores: "offset", + geometry: { + target: (position) => ({ top: position.top, left: position.left }), + capture: () => ({ + view: "offset", + top: Math.max(0, scroller.scrollTop), + left: Math.max(0, scroller.scrollLeft), + }), + }, + }; +} + +/** Table rows keep their index and a raw text row its line; the viewer restores them itself. */ +function rowsGeometry( + scroller: HTMLElement, + raw: boolean, +): ViewGeometry { + return { + target: () => null, + capture() { + const top = scroller.getBoundingClientRect().top; + for (const row of scroller.querySelectorAll("[data-preview-row]")) { + const rect = row.getBoundingClientRect(); + if (rect.bottom <= top) continue; + const index = Number(row.dataset.previewRow); + if (raw) return { view: "source", line: index + 1, offset: 0 }; + const fraction = rect.height > 0 ? (top - rect.top) / rect.height : 0; + return { view: "rendered", block: index, fraction: Math.min(Math.max(0, fraction), 0.999) }; + } + return null; + }, + }; +} + +/** VirtualTablePreview: a table, or a raw text region, whose rows scroll in its focusable child. */ +export const ROWS_SELECTOR = '[aria-rowcount]:is([role="table"], [role="region"])'; +const GRID_SELECTOR = '[role="grid"][aria-rowcount]'; +const VIEWER_MOUNTED = "t3code-viewer-mounted"; + +/** + * A viewer that mounts deep in the container, such as a sheet's rows or an Excel grid, asks for + * its place; a direct-child swap or an input look may not see it. + */ +export function announceViewer(viewer: HTMLElement | null): void { + viewer?.dispatchEvent(new Event(VIEWER_MOUNTED, { bubbles: true })); +} + +const announcedBinding = (viewer: HTMLElement): ViewerBinding | null => + viewer.matches(ROWS_SELECTOR) + ? rowsOf(viewer) + : viewer.matches(GRID_SELECTOR) + ? offsetBinding(viewer) + : null; + +function rowsOf(rows: HTMLElement | null): RowsBinding | null { + const element = rows?.querySelector(":scope > [tabindex]"); + if (!rows || !element || laterSheet(rows)) return null; + const raw = rows.getAttribute("role") === "region"; + return { element, restores: "rows", raw, geometry: rowsGeometry(element, raw) }; +} + +/** Viewer families by their markup, tried in order; the first that shows owns the container. */ +const SHAPES: ReadonlyArray = [ + (root) => rowsOf(root.querySelector(ROWS_SELECTOR)), + (root) => + blocks( + root.querySelector("[data-office-pages]"), + (scroller) => scroller.querySelectorAll(':scope > div > div > [role="group"]'), + true, + ), + // NotebookPreview: its cells sit two levels below its scroller. + (root) => + blocks( + root.querySelector("section[aria-label]:not(.chat-markdown section)")?.parentElement + ?.parentElement, + (scroller) => scroller.querySelectorAll(":scope > div > section[aria-label]"), + false, + ), + (root) => + blocks( + root + .querySelector(".chat-markdown") + ?.closest('[data-slot="scroll-area-viewport"]'), + (scroller) => scroller.querySelector(".chat-markdown")?.children, + false, + ), + // An Excel sheet's grid. + (root) => offsetBinding(root.querySelector(GRID_SELECTOR)), + (root) => offsetBinding(root.querySelector("[data-markdown-mermaid]")?.parentElement), + // An attachment's source view, whose lines only Pierre knows: the pixels below line 1. A + // workspace file's source view has its own tracker. + (root, file) => { + const element = + file.kind === "attachment" + ? root.querySelector(".file-preview-virtualizer") + : null; + if (!element) return null; + return { + element, + restores: "source", + geometry: { + target: (position) => (position.line === 1 ? { top: position.offset } : null), + capture: () => ({ view: "source", line: 1, offset: Math.max(0, element.scrollTop) }), + }, + }; + }, +]; + +const findBinding: Shape = (root, file) => { + for (const shape of SHAPES) { + const binding = shape(root, file); + if (binding !== null) return binding; + } + return null; +}; + +/** A workspace file's code view or editor, which keeps its own place (`trackSourceView`). */ +const ownPlace = (root: HTMLElement, file: FileReadingKey) => + file.kind === "path" ? root.querySelector(".file-preview-virtualizer") : null; + +/** + * Saves the playback time of video and audio under `root` once the reader used the element's + * controls, and restores it paused when the element loads, so a first-frame seek is never saved. + */ +function keepMediaTime(root: HTMLElement, file: FileReadingKey): () => void { + const pending = new PendingPosition(file); + let used: HTMLMediaElement | null = null; + let heldTime = -1; + // A video embedded in rendered Markdown is not the file's own playback. + const mediaOf = (event: Event) => + event.target instanceof HTMLMediaElement && event.target.closest(".chat-markdown") === null + ? event.target + : null; + const hold = (media: HTMLMediaElement) => { + if (!Number.isFinite(media.currentTime)) return; + heldTime = media.currentTime; + pending.hold({ view: "media", time: Math.max(0, media.currentTime) }); + }; + // A source still loading, such as a changed file, reports zero until `restore` returns it to + // the reader's place, so it has no time to save. + const moved = (media: HTMLMediaElement) => + media.readyState < HTMLMediaElement.HAVE_METADATA ? 0 : Math.abs(media.currentTime - heldTime); + const use = (event: Event) => { + used = mediaOf(event) ?? used; + }; + const save = (event: Event) => { + const media = mediaOf(event); + if (media === null || media !== used) return; + if (moved(media) < (event.type === "timeupdate" ? 1 : SAME_TIME_S)) return; + hold(media); + }; + const restore = (event: Event) => { + const media = mediaOf(event); + if (media === null || media.played.length > 0) return; + const saved = readings.readPosition(file, "media"); + if (saved !== null && saved.time > 0) { + media.currentTime = Number.isFinite(media.duration) + ? Math.min(saved.time, media.duration) + : saved.time; + } + // Added in the capture phase, this runs after the player's own handler, which may seek to + // the first frame; the source starts there, so `exit` saves it only once playback moves. + media.addEventListener( + "loadedmetadata", + () => { + heldTime = media.currentTime; + }, + { once: true }, + ); + }; + // The player may pause an element it already removed, so its last events miss the container. + // Unmoved playback keeps its capture time, so a newer sample from another tab still wins. + const exit = () => { + if (used !== null && moved(used) >= SAME_TIME_S) hold(used); + pending.flush(); + }; + // Media events do not bubble, but their capture phase reaches every ancestor. + const listeners: ReadonlyArray void]> = [ + ["pointerdown", use], + ["keydown", use], + ["pause", save], + ["seeked", save], + ["timeupdate", save], + ["loadedmetadata", restore], + ]; + for (const [type, listener] of listeners) root.addEventListener(type, listener, true); + window.addEventListener("pagehide", exit); + return () => { + for (const [type, listener] of listeners) root.removeEventListener(type, listener, true); + window.removeEventListener("pagehide", exit); + exit(); + }; +} + +/** A pending citation's line in the file, and the reveal ledger's word on it. */ +export interface Citation { + /** Unique to the tab request that cited the line. */ + readonly id: number; + readonly line: number; + readonly pending: () => boolean; + readonly shown: () => void; +} + +/** The line a large text viewer (`source`) or a table (`table`) reveals when it is ready. */ +interface RowLines { + readonly source: number | null; + readonly table: number | null; +} + +const NO_LINES: RowLines = { source: null, table: null }; +const REVEALED_ROW = '[data-preview-reveal="true"]'; + +/** + * One file shown in the container, from when it opens until another file or a new citation + * replaces it. The panel reads its row lines as a store; the rest belongs to its event handlers. + */ +class Visit { + private lines: RowLines; + private readonly listeners = new Set<() => void>(); + /** The rows scroller of the viewer mount the lines belong to. */ + private rows: HTMLElement | null = null; + private dropped = false; + private stopReveal: (() => void) | null = null; + + constructor( + readonly fileId: string, + private readonly file: FileReadingKey | undefined, + private readonly views: ReadonlyArray, + private readonly citation: Citation | null, + ) { + this.lines = this.restoreLines(); + } + + get citationId(): number | null { + return this.citation?.id ?? null; + } + + readonly subscribe = (listener: () => void) => { + this.listeners.add(listener); + return () => { + this.listeners.delete(listener); + }; + }; + + readonly snapshot = () => this.lines; + + readonly attach = (root: HTMLElement | null) => { + const file = this.file; + if (root === null || file === undefined || this.views.length === 0) return; + const handled = new WeakSet(); + const bound = new WeakMap(); + /** The scroller placed or scrolled last; changes inside it bring no new viewer. */ + let latest: Element | null = null; + const take = (element: Element) => { + handled.add(element); + latest = element; + }; + const bind = (element: HTMLElement) => { + let binding = bound.get(element); + if (binding === undefined) { + const found = findBinding(root, file); + binding = found?.element === element ? found : null; + bound.set(element, binding); + // The reader scrolled it, so a later look must not move it. + if (binding !== null) take(element); + } + if (binding?.restores === "rows") this.drop(file, binding); + return binding; + }; + const tracker = new ReadingTracker(root, file, bind, (event) => { + if ( + (event.type === "pointerdown" || event.type === "keydown") && + event.target instanceof Element && + event.target.closest(VIEWER_SWITCH) !== null + ) { + startLook(INPUT_LOOK_MS); + } + }); + let observer: MutationObserver | null = null; + let frame: number | null = null; + let timer: ReturnType | null = null; + let until = 0; + const restore =

(binding: Binding

, saved: P | null) => { + if (saved !== null) tracker.restore(binding, saved, until); + }; + const stopLook = () => { + observer?.disconnect(); + observer = null; + if (frame !== null) cancelAnimationFrame(frame); + frame = null; + if (timer !== null) clearTimeout(timer); + timer = null; + }; + /** Places a viewer seen for the first time; the look has found what it watched for. */ + const settle = (element: Element, binding: ViewerBinding | null) => { + take(element); + stopLook(); + if (binding === null) return; + if (binding.restores === "rows") this.mountRows(file, binding); + else if (binding.restores === "rendered") { + restore(binding, readings.readPosition(file, "rendered")); + } else if (binding.restores === "offset") { + // A grid a cell link opened shows that cell; the reader's later scrolls still save. + if (binding.element.dataset.linkedCell === undefined) { + restore(binding, readings.readPosition(file, "offset")); + } + } else restore(binding, readings.readPosition(file, "source")); + }; + const check = () => { + frame = null; + if (!this.awaitsViewer(file)) return stopLook(); + const binding = findBinding(root, file); + const element = binding?.element ?? ownPlace(root, file); + if (element && !handled.has(element)) settle(element, binding); + }; + /** Watches the viewer for a scroller it has not placed yet, then places it once. */ + const startLook = (ms: number) => { + if (observer === null && !this.awaitsViewer(file)) return; + const end = performance.now() + ms; + if (end <= until && observer !== null) return; + until = end; + if (timer !== null) clearTimeout(timer); + timer = setTimeout(stopLook, ms); + if (observer !== null) return; + observer = new MutationObserver((records) => { + if (records.every((record) => latest?.contains(record.target) === true)) return; + frame ??= requestAnimationFrame(check); + }); + observer.observe(root, { childList: true, subtree: true }); + check(); + }; + // A new viewer, such as rendered and source Markdown or a changed file, replaces the children. + const swaps = new MutationObserver(() => startLook(LOOK_MS)); + swaps.observe(root, { childList: true }); + startLook(LOOK_MS); + const onMounted = (event: Event) => { + if (!(event.target instanceof HTMLElement)) return; + if (!this.awaitsViewer(file) && !this.hasRowPlace()) return; + const binding = announcedBinding(event.target); + if (binding !== null && !handled.has(binding.element)) settle(binding.element, binding); + }; + root.addEventListener(VIEWER_MOUNTED, onMounted); + const stopMedia = keepMediaTime(root, file); + return () => { + swaps.disconnect(); + root.removeEventListener(VIEWER_MOUNTED, onMounted); + stopLook(); + this.stopReveal?.(); + this.rows = null; + tracker.dispose(); + stopMedia(); + }; + }; + + private citedLine(): number | null { + const citation = this.citation; + return citation?.pending() === true ? citation.line : null; + } + + /** A citation not yet shown, or a saved place in a view this container restores. */ + private awaitsViewer(file: FileReadingKey): boolean { + return ( + this.citedLine() !== null || + this.views.some((view) => readings.readPosition(file, view) !== null) + ); + } + + private restoreLines(): RowLines { + const file = this.file; + if (file === undefined) return NO_LINES; + const rendered = readings.readPosition(file, "rendered"); + return { + source: this.citedLine() ?? readings.readPosition(file, "source")?.line ?? null, + table: rendered === null ? null : rendered.block + 1, + }; + } + + private hasRowPlace(): boolean { + const lines = this.restoreLines(); + return lines.source !== null || lines.table !== null; + } + + private publish(lines: RowLines): void { + if (lines.source === this.lines.source && lines.table === this.lines.table) return; + this.lines = lines; + for (const listener of this.listeners) listener(); + } + + /** A rows viewer mounted: it reveals a citation not yet shown, else the newest place. */ + private mountRows(file: FileReadingKey, rows: RowsBinding): void { + if (rows.element === this.rows) return; + this.stopReveal?.(); + this.rows = rows.element; + this.dropped = false; + this.publish(this.restoreLines()); + if (rows.raw && this.citedLine() !== null) this.awaitReveal(file, rows.element); + } + + /** + * The reader scrolled the rows, so finished indexing must not move them. Raw rows still reveal + * a pending citation; a table never shows one. + */ + private drop(file: FileReadingKey, rows: RowsBinding): void { + this.mountRows(file, rows); + if (this.dropped || (rows.raw && this.citedLine() !== null)) return; + this.dropped = true; + this.publish(NO_LINES); + } + + /** Once the rows show the cited line, it becomes the file's place and the citation is shown. */ + private awaitReveal(file: FileReadingKey, rows: HTMLElement): void { + const citation = this.citation; + if (citation === null) return; + const observer = new MutationObserver(() => check()); + const stop = () => { + observer.disconnect(); + if (this.stopReveal === stop) this.stopReveal = null; + }; + const check = () => { + if (!citation.pending()) return stop(); + if (rows.querySelector(REVEALED_ROW) === null) return; + stop(); + readings.writePosition(file, { view: "source", line: citation.line, offset: 0 }); + citation.shown(); + }; + this.stopReveal = stop; + observer.observe(rows, { + childList: true, + subtree: true, + attributeFilter: ["data-preview-reveal"], + }); + check(); + } +} + +export interface ViewerPlace { + /** The ref for the element that holds the file's viewer. */ + readonly ref: (root: HTMLElement | null) => (() => void) | undefined; + /** + * The line a table (`table`) or raw large text viewer reveals: a pending citation's line, or + * the reader's row; null once the reader scrolls the mounted rows. + */ + readonly rowLine: (table: boolean) => number | null; +} + +/** Keeps the place of whatever viewer the container holds for `file`. */ +export function useViewerPlace(input: { + readonly file: FileReadingKey | undefined; + /** The saved views the container restores; none when a frame or PDF.js keeps the place. */ + readonly views: ReadonlyArray; + /** A citation still pending when it opens the file; a new one starts a new visit. */ + readonly citation: Citation | null; +}): ViewerPlace { + const fileId = input.file === undefined ? "" : fileReadingId(input.file); + const create = () => new Visit(fileId, input.file, input.views, input.citation); + const [stored, setStored] = useState(create); + let visit = stored; + if ( + stored.fileId !== fileId || + (input.citation !== null && input.citation.id !== stored.citationId) + ) { + visit = create(); + setStored(visit); + } + const lines = useSyncExternalStore(visit.subscribe, visit.snapshot); + return { ref: visit.attach, rowLine: (table) => (table ? lines.table : lines.source) }; +} diff --git a/apps/web/src/components/files/fileReadingPosition.browser.test.tsx b/apps/web/src/components/files/fileReadingPosition.browser.test.tsx index 48b3ef3dcf223..6ebb2d514f522 100644 --- a/apps/web/src/components/files/fileReadingPosition.browser.test.tsx +++ b/apps/web/src/components/files/fileReadingPosition.browser.test.tsx @@ -1,238 +1,69 @@ -import { DEFAULT_THEMES, preloadHighlighter } from "@pierre/diffs"; -import { scopeThreadRef } from "@t3tools/client-runtime/environment"; -import { - EnvironmentId, - ThreadId, - type ProjectReadFileResult, - type ScopedThreadRef, -} from "@t3tools/contracts"; -import { AsyncResult } from "effect/reactivity"; -import type { ReactNode } from "react"; +import type { ChatFileAttachment } from "@t3tools/contracts"; import { describe, expect, it, vi } from "vite-plus/test"; import { userEvent } from "vite-plus/test/browser"; + import { act, render } from "../../../test/render"; -import appCss from "~/index.css?inline"; import { useClosedViewStore } from "~/closedViewStore"; -import { selectActiveRightPanelSurface, useRightPanelStore } from "~/rightPanelStore"; - -const state = vi.hoisted(() => ({ - file: null as ProjectReadFileResult | null, - files: {} as Record, - optimistic: null as ProjectReadFileResult | null, - writeFile: vi.fn(), -})); - -vi.mock(import("@t3tools/client-runtime/state/runtime"), async (original) => ({ - ...(await original()), - runAtomCommand: ((_registry: unknown, _command: unknown, request: unknown) => - state.writeFile(request)) as never, -})); -vi.mock(import("~/state/session"), async (original) => ({ - ...(await original()), - readEnvironmentScope: () => true, - useEnvironmentScope: () => true, -})); -vi.mock(import("~/state/filesystem"), async (original) => ({ - ...(await original()), - useFilesystemReadAccess: () => ({ canReadFiles: true, isPending: false, error: null }), -})); -vi.mock(import("~/state/use-atom-command"), async (original) => ({ - ...(await original()), - useAtomCommand: (() => state.writeFile) as never, -})); -vi.mock(import("~/state/server"), async (original) => { - const actual = await original(); - const { Atom } = await import("effect/reactivity"); - const noConfig = Atom.make(null); - return { - ...actual, - serverEnvironment: { ...actual.serverEnvironment, configValueAtom: (() => noConfig) as never }, - }; -}); -vi.mock(import("~/remoteOpen"), async (original) => ({ - ...(await original()), - useRemoteOpenState: () => ({ mode: "local-exec" }) as never, - useRemoteOpenResolution: () => ({ state: { mode: "local-exec" }, isResolved: true }) as never, -})); -vi.mock(import("~/hooks/useTheme"), async (original) => ({ - ...(await original()), - useTheme: () => ({ resolvedTheme: "dark" }) as never, -})); -vi.mock(import("~/hooks/useWorkspaceMutationRefresh"), () => ({ - useWorkspaceMutationRefresh: vi.fn(), -})); -vi.mock(import("./FileBreadcrumbs"), () => ({ FileBreadcrumbs: () => <> })); -vi.mock(import("./FileBrowserPanel"), () => ({ default: () =>

})); -vi.mock(import("../DiffWorkerPoolProvider"), () => ({ - DiffWorkerPoolProvider: ({ children }: { children?: ReactNode }) => <>{children}, -})); -vi.mock(import("./projectFilesQueryState"), async (original) => ({ - ...(await original()), - useProjectFileQuery: (_environmentId: EnvironmentId, _cwd: string, path: string | null) => ({ - data: (path === null ? undefined : state.files[path]) ?? state.file, - error: null, - isPending: false, - readError: null, - isNotFile: false, - isFile: true, - refresh: vi.fn(), - }), - getOptimisticProjectFileQueryData: () => state.optimistic, - getUnsavedProjectFileQueryData: () => null, - setProjectFileQueryData: ( - _environmentId: EnvironmentId, - _cwd: string, - relativePath: string, - contents: string, - ) => { - state.optimistic = { relativePath, contents, truncated: false, byteLength: contents.length }; - }, - confirmProjectFileQueryData: () => true, -})); - -import FilePreviewPanel from "./FilePreviewPanel"; -import { type FileReadingKey, usePdfReadingView } from "./fileReadingSave"; +import { useRightPanelStore } from "~/rightPanelStore"; + +vi.mock(import("~/assets/assetUrls"), async (original) => + (await import("./fileReadingTestMocks")).mockAssetUrls(await original()), +); +vi.mock(import("@t3tools/client-runtime/state/runtime"), async (original) => + (await import("./fileReadingTestMocks")).mockRuntime(await original()), +); +vi.mock(import("~/state/session"), async (original) => + (await import("./fileReadingTestMocks")).mockSession(await original()), +); +vi.mock(import("~/state/filesystem"), async (original) => + (await import("./fileReadingTestMocks")).mockFilesystem(await original()), +); +vi.mock(import("~/state/use-atom-command"), async (original) => + (await import("./fileReadingTestMocks")).mockAtomCommand(await original()), +); +vi.mock(import("~/state/server"), async (original) => + (await import("./fileReadingTestMocks")).mockServer(await original()), +); +vi.mock(import("~/remoteOpen"), async (original) => + (await import("./fileReadingTestMocks")).mockRemoteOpen(await original()), +); +vi.mock(import("~/hooks/useTheme"), async (original) => + (await import("./fileReadingTestMocks")).mockTheme(await original()), +); +vi.mock(import("./projectFilesQueryState"), async (original) => + (await import("./fileReadingTestMocks")).mockProjectFiles(await original()), +); +vi.mock(import("~/hooks/useWorkspaceMutationRefresh"), async () => + (await import("./fileReadingTestMocks")).mockPanelParts.mutationRefresh(), +); +vi.mock(import("./FileBreadcrumbs"), async () => + (await import("./fileReadingTestMocks")).mockPanelParts.breadcrumbs(), +); +vi.mock(import("./FileBrowserPanel"), async () => + (await import("./fileReadingTestMocks")).mockPanelParts.browser(), +); +vi.mock(import("../DiffWorkerPoolProvider"), async () => + (await import("./fileReadingTestMocks")).mockPanelParts.diffWorkers(), +); + +import { readings } from "./fileReadingSave"; +import { + AttachmentPanel, + COLD_WORKER, + csv, + environmentId, + holdIndexing, + largeText, + lines, + ReadingPanel, + readingSetup, + rowsView, + sourceFile, + sourceView, + state, +} from "./fileReadingTestKit"; import { FILE_LINK_REVEAL_ATTRIBUTE } from "./fileSurfaceChrome"; -const environmentId = EnvironmentId.make("file-reading-position-test"); - -function setup() { - const keys = ["t3code.fileExplorerOpen", "t3code.renderMarkdown"]; - const previous = keys.map((key) => localStorage.getItem(key)); - localStorage.setItem(keys[0]!, "false"); - localStorage.setItem(keys[1]!, "true"); - state.file = null; - state.files = {}; - state.optimistic = null; - state.writeFile.mockReset().mockResolvedValue(AsyncResult.success(undefined)); - return { - [Symbol.dispose]() { - for (const [index, key] of keys.entries()) { - const value = previous[index]; - if (value == null) localStorage.removeItem(key); - else localStorage.setItem(key, value); - } - }, - }; -} - -const noop = () => {}; - -function sourceFile(relativePath: string, word: string, lines: number): ProjectReadFileResult { - const contents = Array.from( - { length: lines }, - (_, index) => `const ${word}_${index + 1} = ${index + 1};`, - ).join("\n"); - return { relativePath, contents, byteLength: contents.length, truncated: false }; -} - -async function readingSetup(name: string) { - await preloadHighlighter({ themes: Object.values(DEFAULT_THEMES), langs: ["typescript"] }); - const settings = setup(); - const style = document.createElement("style"); - style.textContent = appCss; - document.head.append(style); - const doc = Array.from({ length: 80 }, (_, index) => `Paragraph ${index + 1}.`).join("\n\n"); - state.files = { - "a.ts": sourceFile("a.ts", "alpha", 300), - "b.ts": sourceFile("b.ts", "beta", 300), - "c.ts": sourceFile("c.ts", "gamma", 10), - "doc.md": { relativePath: "doc.md", contents: doc, byteLength: doc.length, truncated: false }, - }; - const ref = scopeThreadRef(environmentId, ThreadId.make(`reading-${name}`)); - const cwd = `/reading/${name}`; - return { - ref, - cwd, - panel: useRightPanelStore.getState(), - saved: (path: string, view: "source" | "rendered") => - JSON.parse( - localStorage.getItem( - `t3code.fileReading.v1:${JSON.stringify([environmentId, `${cwd}/${path}`, view])}`, - ) ?? "null", - ) as { line?: number; block?: number; at: number } | null, - [Symbol.dispose]() { - useRightPanelStore.getState().removeThread(ref); - for (const key of Object.keys(localStorage)) { - if (key.startsWith("t3code.fileReading.v1:") || key.startsWith("t3code.fileReveal.v1:")) { - localStorage.removeItem(key); - } - } - style.remove(); - settings[Symbol.dispose](); - }, - }; -} - -/** Renders the active file tab the way the right panel does: one panel per workspace. */ -function ReadingPanel(props: { readonly threadRef: ScopedThreadRef; readonly cwd: string }) { - const surface = useRightPanelStore((store) => - selectActiveRightPanelSurface(store.byThreadKey, props.threadRef), - ); - return ( -
- {surface?.kind === "file" ? ( - - ) : null} -
- ); -} - -function sourceView(container: HTMLElement) { - const scroller = () => container.querySelector(".file-preview-virtualizer")!; - const line = (lineNumber: number) => - container - .querySelector("diffs-container") - ?.shadowRoot?.querySelector(`[data-line="${lineNumber}"]`) ?? null; - return { - scroller, - line, - /** The first row the viewport shows, partly or fully, and its top relative to the viewport. */ - topLine() { - const top = scroller().getBoundingClientRect().top; - const rows = container - .querySelector("diffs-container") - ?.shadowRoot?.querySelectorAll("[data-line]"); - const row = [...(rows ?? [])] - .filter((element) => element.getBoundingClientRect().bottom > top + 0.5) - .sort( - (left, right) => - Number(left.getAttribute("data-line")) - Number(right.getAttribute("data-line")), - )[0]; - return { - line: Number(row?.getAttribute("data-line")), - offset: Math.round(row!.getBoundingClientRect().top - top), - }; - }, - expectLineInView(lineNumber: number, text: string) { - const element = line(lineNumber); - expect(element?.textContent).toContain(text); - const view = scroller().getBoundingClientRect(); - const rect = element!.getBoundingClientRect(); - expect(rect.top).toBeGreaterThanOrEqual(view.top - 1); - expect(rect.bottom).toBeLessThanOrEqual(view.bottom + 1); - }, - }; -} - describe("file reading position", () => { it("returns a source file to the reader's line after a tab switch and a panel remount", async () => { using reading = await readingSetup("switch"); @@ -442,41 +273,266 @@ describe("file reading position", () => { }); }); -describe("PDF reading view", () => { - function PdfView(props: { - readonly file: FileReadingKey; - readonly onView: (page: number) => void; - }) { - const [view, setView] = usePdfReadingView(props.file); - props.onView(view.page); - return ( - +describe("large text and table rows keep the reader's place", () => { + it("returns a CSV table to the reader's row after a tab switch, capped at the end when shorter", async () => { + using reading = await readingSetup("csv"); + const { panel, ref } = reading; + const table = (rows: number) => { + const contents = csv(rows); + return { relativePath: "table.csv", contents, byteLength: contents.length, truncated: false }; + }; + state.files["table.csv"] = table(300); + panel.openFile(ref, "b.ts"); + panel.openFile(ref, "table.csv"); + await using screen = await render(); + const rows = rowsView(screen.container); + const view = sourceView(screen.container); + await vi.waitFor(rows.indexed, COLD_WORKER); + await rows.wheel(2900); + await vi.waitFor(() => + expect(reading.saved("table.csv", "rendered")?.block).toBeGreaterThan(50), ); - } - - it("seeds a new PDF.js mount with the page saved by an earlier one", async () => { - const file = { environmentId, absolutePath: "/reading/pdf/report.pdf" }; - const pages: number[] = []; - try { - { - await using screen = await render( - pages.push(page)} />, - ); - await userEvent.click(screen.getByRole("button", { name: "Go to page 3" })); - await vi.waitFor(() => expect(pages.at(-1)).toBe(2)); - } - pages.length = 0; - await using screen = await render( - pages.push(page)} />, + const before = rows.topRow(); + expect(before).toBe(reading.saved("table.csv", "rendered")?.block); + + await act(() => panel.activateSurface(ref, "file:b.ts")); + await vi.waitFor(() => view.expectLineInView(1, "beta_1")); + await act(() => panel.activateSurface(ref, "file:table.csv")); + await vi.waitFor(() => expect(rows.topRow()).toBe(before), COLD_WORKER); + + await screen.rerender(<>); + await screen.rerender(); + await vi.waitFor(() => expect(rows.topRow()).toBe(before), COLD_WORKER); + + await act(() => panel.activateSurface(ref, "file:b.ts")); + await vi.waitFor(() => view.expectLineInView(1, "beta_1")); + const saved = reading.saved("table.csv", "rendered"); + state.files["table.csv"] = table(40); + await act(() => panel.activateSurface(ref, "file:table.csv")); + await vi.waitFor(() => { + rows.indexed(); + const scroller = rows.scroller()!; + expect(scroller.scrollHeight).toBeGreaterThan(scroller.clientHeight); + expect(scroller.scrollTop).toBeGreaterThanOrEqual( + scroller.scrollHeight - scroller.clientHeight - 1, ); - await expect.element(screen.getByRole("button")).toBeVisible(); - expect(pages[0]).toBe(2); - } finally { - for (const key of Object.keys(localStorage)) { - if (key.startsWith("t3code.fileReading.v1:")) localStorage.removeItem(key); - } - } + }, COLD_WORKER); + expect(reading.saved("table.csv", "rendered")).toEqual(saved); + }); + + it("keeps a text file's line when it grows past the 1 MB preview and when it shrinks back", async () => { + using reading = await readingSetup("crossing"); + const { panel, ref } = reading; + const contents = lines(400); + const small = { + relativePath: "app.log", + contents, + byteLength: contents.length, + truncated: false, + }; + state.files["app.log"] = small; + panel.openFile(ref, "b.ts"); + panel.openFile(ref, "app.log"); + await using screen = await render(); + const view = sourceView(screen.container); + const rows = rowsView(screen.container); + await vi.waitFor(() => view.expectLineInView(1, "log line 1")); + await userEvent.wheel(view.scroller(), { delta: { y: 2000 } }); + await vi.waitFor(() => expect(reading.saved("app.log", "source")?.line).toBeGreaterThan(50)); + const read = reading.saved("app.log", "source")!.line!; + + await act(() => panel.activateSurface(ref, "file:b.ts")); + await vi.waitFor(() => view.expectLineInView(1, "beta_1")); + reading.serve("app.log", contents); + state.files["app.log"] = { ...small, byteLength: 2_000_000, truncated: true }; + await act(() => panel.activateSurface(ref, "file:app.log")); + await vi.waitFor(() => { + rows.indexed(); + expect(rows.topRow()).toBe(read - 1); + }, COLD_WORKER); + + await rows.wheel(2000); + await vi.waitFor(() => + expect(reading.saved("app.log", "source")?.line).toBeGreaterThan(read + 30), + ); + const later = reading.saved("app.log", "source")!.line!; + expect(rows.topRow()).toBe(later - 1); + await act(() => panel.activateSurface(ref, "file:b.ts")); + await vi.waitFor(() => view.expectLineInView(1, "beta_1")); + state.files["app.log"] = small; + await act(() => panel.activateSurface(ref, "file:app.log")); + await vi.waitFor(() => expect(view.topLine()).toEqual({ line: later, offset: 0 })); + }); + + it("reveals a cited line of a large text file once, even when the reader scrolls and leaves while it indexes, then returns to the reader's place", async () => { + using reading = await readingSetup("large-cited"); + using indexing = holdIndexing(); + const { panel, ref } = reading; + largeText(reading, "big.log", lines(600)); + panel.openFile(ref, "b.ts"); + panel.openFile(ref, "big.log", 40); + await using screen = await render(); + const rows = rowsView(screen.container); + const view = sourceView(screen.container); + await vi.waitFor(() => expect(rows.topRow()).toBe(0), COLD_WORKER); + await rows.wheel(600); + await vi.waitFor(() => expect(reading.saved("big.log", "source")?.line).toBeGreaterThan(10)); + // The reader leaves before indexing ends, so the viewer has yet to show the citation. + await act(() => panel.activateSurface(ref, "file:b.ts")); + await vi.waitFor(() => view.expectLineInView(1, "beta_1")); + await act(() => panel.activateSurface(ref, "file:big.log")); + await vi.waitFor(() => expect(rows.topRow()).toBe(0), COLD_WORKER); + indexing.release(); + await vi.waitFor( + () => + expect( + screen.container.querySelector('[data-preview-reveal="true"]')?.textContent, + ).toContain("log line 40"), + COLD_WORKER, + ); + expect(reading.saved("big.log", "source")?.line).toBe(40); + + await rows.wheel(3000); + await vi.waitFor(() => expect(reading.saved("big.log", "source")?.line).toBeGreaterThan(100)); + const before = rows.topRow(); + await act(() => panel.activateSurface(ref, "file:b.ts")); + await vi.waitFor(() => view.expectLineInView(1, "beta_1")); + await act(() => panel.activateSurface(ref, "file:big.log")); + await vi.waitFor(() => { + rows.indexed(); + expect(rows.topRow()).toBe(before); + }, COLD_WORKER); + }); + + it("leaves a large text file where the reader scrolled it while it indexed, and a click there keeps the restore", async () => { + using reading = await readingSetup("indexing"); + using indexing = holdIndexing(); + const { panel, ref } = reading; + largeText(reading, "big.log", `${lines(600)}\n`); + readings.writePosition( + { kind: "path", environmentId, absolutePath: `${reading.cwd}/big.log` }, + { view: "source", line: 40, offset: 0 }, + ); + panel.openFile(ref, "b.ts"); + panel.openFile(ref, "big.log"); + await using screen = await render(); + const rows = rowsView(screen.container); + const view = sourceView(screen.container); + const status = () => screen.container.querySelector('[role="status"]')?.textContent ?? ""; + await vi.waitFor(() => expect(status()).toMatch(/600 lines.*Indexing/), COLD_WORKER); + await rows.wheel(3000); + await vi.waitFor(() => expect(rows.topRow()).toBeGreaterThan(80)); + const top = rows.scroller()!.scrollTop; + + indexing.release(); + await vi.waitFor(rows.indexed); + expect(Math.abs(rows.scroller()!.scrollTop - top)).toBeLessThan(29); + await vi.waitFor(() => expect(reading.saved("big.log", "source")?.line).toBeGreaterThan(80)); + const line = reading.saved("big.log", "source")!.line!; + // Opening the file again from the tree keeps the rows where the reader has them. + await act(() => panel.openFile(ref, "big.log")); + expect(screen.container.querySelector('[data-preview-reveal="true"]')).toBeNull(); + expect(Math.abs(rows.scroller()!.scrollTop - top)).toBeLessThan(29); + + await act(() => panel.activateSurface(ref, "file:b.ts")); + await vi.waitFor(() => view.expectLineInView(1, "beta_1")); + indexing.hold(); + await act(() => panel.activateSurface(ref, "file:big.log")); + // A click that scrolls nothing while it indexes leaves the restore to land. + await vi.waitFor(() => expect(status()).toMatch(/600 lines.*Indexing/), COLD_WORKER); + await userEvent.click(rows.scroller()!); + indexing.release(); + await vi.waitFor(() => { + rows.indexed(); + expect(rows.topRow()).toBe(line - 1); + }, COLD_WORKER); + }); + + it("returns a large text file that changed while open to the reader's line", async () => { + using reading = await readingSetup("changed"); + const { panel, ref } = reading; + largeText(reading, "app.log", `${lines(600)}\n`); + panel.openFile(ref, "b.ts"); + panel.openFile(ref, "app.log"); + await using screen = await render( + , + ); + const rows = rowsView(screen.container); + await vi.waitFor(rows.indexed, COLD_WORKER); + await rows.wheel(3000); + await vi.waitFor(() => expect(reading.saved("app.log", "source")?.line).toBeGreaterThan(50)); + const line = reading.saved("app.log", "source")!.line!; + + // An agent appends to the log, and the viewer loads the new bytes. + largeText(reading, "app.log", `${lines(700)}\n`); + await screen.rerender( + , + ); + await vi.waitFor(() => { + rows.indexed(); + expect(screen.container.querySelector('[role="status"]')?.textContent).toContain("700 lines"); + expect(rows.topRow()).toBe(line - 1); + }, COLD_WORKER); + }); + + it("returns a large CSV attachment's table and its raw text each to its own line", async () => { + using reading = await readingSetup("csv-attachment"); + const data: ChatFileAttachment = { + type: "file", + id: "attachment-data.csv" as ChatFileAttachment["id"], + name: "data.csv", + mimeType: "text/csv", + sizeBytes: 2_000_000, + }; + reading.attach(data, csv(400)); + const key = { kind: "attachment", environmentId, attachmentId: data.id } as const; + readings.writePosition(key, { view: "rendered", block: 80, fraction: 0 }); + readings.writePosition(key, { view: "source", line: 200, offset: 0 }); + await using screen = await render( + , + ); + const rows = rowsView(screen.container); + await vi.waitFor(() => { + rows.indexed(); + expect(rows.topRow()).toBe(80); + }, COLD_WORKER); + + await userEvent.click(screen.getByRole("button", { name: "Show source" })); + await vi.waitFor(() => { + rows.indexed(); + expect(screen.container.querySelector('[role="region"][aria-rowcount]')).not.toBeNull(); + expect(rows.topRow()).toBe(199); + }, COLD_WORKER); + }); + + it("scrolls a CSV attachment's table on past its saved row", async () => { + using reading = await readingSetup("small-csv-attachment"); + const text = csv(400); + const data: ChatFileAttachment = { + type: "file", + id: "attachment-table.csv" as ChatFileAttachment["id"], + name: "table.csv", + mimeType: "text/csv", + sizeBytes: text.length, + }; + reading.attach(data, text); + const key = { kind: "attachment", environmentId, attachmentId: data.id } as const; + readings.writePosition(key, { view: "rendered", block: 80, fraction: 0 }); + await using screen = await render( + , + ); + const rows = rowsView(screen.container); + await vi.waitFor(() => { + rows.indexed(); + expect(rows.topRow()).toBe(80); + }, COLD_WORKER); + // The scroll re-renders the panel, which must not load the attachment and its table again. + const table = rows.scroller(); + await rows.wheel(1500); + await vi.waitFor(() => + expect(reading.attachmentSaved(data.id, "rendered")?.block).toBeGreaterThan(110), + ); + expect(rows.scroller()).toBe(table); + expect(rows.topRow()).toBe(reading.attachmentSaved(data.id, "rendered")?.block); }); }); diff --git a/apps/web/src/components/files/fileReadingPosition.ts b/apps/web/src/components/files/fileReadingPosition.ts index 32de34b21a25a..3853d638bfc83 100644 --- a/apps/web/src/components/files/fileReadingPosition.ts +++ b/apps/web/src/components/files/fileReadingPosition.ts @@ -2,179 +2,58 @@ import { VirtualizedFile, type File as FileInstance, type PostRenderPhase } from import { scopedThreadKey } from "@t3tools/client-runtime/environment"; import type { EnvironmentId, ScopedThreadRef } from "@t3tools/contracts"; import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks"; +import { filePreviewKind } from "@t3tools/shared/filePreview"; import { useState } from "react"; +import { isBrowserPreviewFile } from "~/browser/openFileInPreview"; import { useClosedViewStore } from "~/closedViewStore"; import { useRightPanelStore } from "~/rightPanelStore"; import { createRevealLedger, + fileReadingId, liveFileSurface, type FileReadingKey, - type ReadingPosition, type ReadingPositionOf, + type ReadingView, type RevealVerdict, } from "./fileReadingStore"; -import { PendingPosition, readings } from "./fileReadingSave"; - -const HOLD_MS = 1000; -const CAPTURE_ATTEMPTS = 8; +import { + type Binding, + ReadingTracker, + type ScrollTarget, + useViewerPlace, +} from "./fileReadingPlace"; +import { readings } from "./fileReadingSave"; const ledger = createRevealLedger(readings, useRightPanelStore, useClosedViewStore); + +export const pathReadingKey = (environmentId: string, absolutePath: string): FileReadingKey => ({ + kind: "path", + environmentId, + absolutePath, +}); ledger.start(); +const NO_VIEWS: ReadonlyArray = []; +// A workspace file's code view has its own tracker; an attachment's source view is the container's. +const PATH_VIEWS: ReadonlyArray = ["rendered", "offset"]; +const ATTACHMENT_VIEWS: ReadonlyArray = ["rendered", "offset", "source"]; + type FilePostRender = ( fileContainer: HTMLElement, instance: FileInstance, phase: PostRenderPhase, ) => void; type SourcePosition = ReadingPositionOf<"source">; -type RenderedPosition = ReadingPositionOf<"rendered">; - -interface ViewGeometry

{ - /** The scroll top that puts `position` at the viewport top, or null until the content can place it. */ - target(position: P): number | null; - /** The position at the viewport top, or null while the content cannot tell. */ - capture(): P | null; -} - -/** - * Restores and saves one scroll container's position. Only scrolls that follow reader input on the - * container are saved, so restores, reveals and layout resets never overwrite a saved position. - */ -class ReadingTracker

{ - private state: "idle" | "holding" | "armed" = "idle"; - private frame: number | null = null; - private holdUntil = 0; - /** A scrollbar drag can pause longer than the save delay without a new pointerdown. */ - private pointerDown = false; - private readonly pending: PendingPosition; - /** The restored or last captured position. */ - anchor: P | null = null; - - /** `input` receives reader input; it differs from `element` when a custom scrollbar sits beside it. */ - constructor( - private readonly element: HTMLElement, - file: FileReadingKey, - private readonly geometry: ViewGeometry

, - private readonly input: HTMLElement = element, - ) { - this.pending = new PendingPosition(file, () => { - if (this.state === "armed" && !this.pointerDown) this.state = "idle"; - }); - input.addEventListener("wheel", this.arm, { passive: true }); - input.addEventListener("touchstart", this.arm, { passive: true }); - input.addEventListener("pointerdown", this.press, { passive: true, capture: true }); - input.addEventListener("keydown", this.arm, { capture: true }); - element.addEventListener("scroll", this.onScroll, { passive: true }); - window.addEventListener("pointerup", this.release, true); - window.addEventListener("pointercancel", this.release, true); - window.addEventListener("pagehide", this.pending.flush); - } - - restore(position: P): void { - this.cancelFrame(); - this.anchor = position; - this.state = "holding"; - this.holdUntil = performance.now() + HOLD_MS; - this.place(position); - this.frame = requestAnimationFrame(this.hold); - } - - /** Hands the container to a reveal; a position the reader already scrolled to is kept. */ - stop(): void { - this.cancelFrame(); - this.pending.flush(); - this.state = "idle"; - } - - /** Saves where a landed reveal put the reader, without reading the half-rendered DOM. */ - record(position: P): void { - this.stop(); - this.anchor = position; - this.pending.write(position); - } - - dispose(): void { - this.stop(); - this.input.removeEventListener("wheel", this.arm); - this.input.removeEventListener("touchstart", this.arm); - this.input.removeEventListener("pointerdown", this.press, { capture: true }); - this.input.removeEventListener("keydown", this.arm, { capture: true }); - this.element.removeEventListener("scroll", this.onScroll); - window.removeEventListener("pointerup", this.release, true); - window.removeEventListener("pointercancel", this.release, true); - window.removeEventListener("pagehide", this.pending.flush); - } - - private readonly press = () => { - this.pointerDown = true; - this.arm(); - }; - - private readonly release = () => { - this.pointerDown = false; - if (this.state === "armed" && !this.pending.waiting && this.frame === null) this.state = "idle"; - }; - - private readonly arm = () => { - if (this.state === "holding") this.cancelFrame(); - this.state = "armed"; - // Input that scrolls nothing (a key in the editor, a wheel at the end) disarms after the delay. - this.pending.touch(); - }; - - private readonly onScroll = () => { - if (this.state === "armed" && this.frame === null) this.capture(CAPTURE_ATTEMPTS); - }; - - private capture(attempts: number): void { - this.frame = requestAnimationFrame(() => { - this.frame = null; - if (this.state !== "armed") return; - const position = this.geometry.capture(); - if (position === null) { - // Pierre moves its rendered window a frame or two after a long jump. - if (attempts > 1) this.capture(attempts - 1); - return; - } - this.anchor = position; - this.pending.hold(position); - }); - } - - private readonly hold = (time: number) => { - this.frame = null; - if (this.state !== "holding" || this.anchor === null) return; - if (time >= this.holdUntil) { - this.state = "idle"; - return; - } - this.place(this.anchor); - this.frame = requestAnimationFrame(this.hold); - }; - - private place(position: P): void { - const target = this.geometry.target(position); - if (target === null) return; - const max = Math.max(0, this.element.scrollHeight - this.element.clientHeight); - const top = Math.min(Math.max(0, target), max); - if (Math.abs(this.element.scrollTop - top) > 1) this.element.scrollTop = top; - } - - private cancelFrame(): void { - if (this.frame !== null) cancelAnimationFrame(this.frame); - this.frame = null; - } -} type SourceLines = Pick< VirtualizedFile, "getLinePosition" | "getNumericScrollAnchor" >; -class SourceGeometry implements ViewGeometry { +class SourceGeometry { constructor( private readonly element: HTMLElement, private fileContainer: HTMLElement, @@ -186,9 +65,9 @@ class SourceGeometry implements ViewGeometry { this.lines = lines; } - target(position: SourcePosition): number | null { + target(position: SourcePosition): ScrollTarget | null { const line = this.lines.getLinePosition(position.line); - return line === undefined ? null : this.fileTop() + line.top + position.offset; + return line === undefined ? null : { top: this.fileTop() + line.top + position.offset }; } capture(): SourcePosition | null { @@ -213,55 +92,14 @@ class SourceGeometry implements ViewGeometry { } } -class RenderedGeometry implements ViewGeometry { - constructor(private readonly viewport: HTMLElement) {} - - target(position: RenderedPosition): number | null { - // The first block's top margin belongs to the top of the file. - if (position.block === 0 && position.fraction === 0) return 0; - const blocks = this.blocks(); - if (!blocks || blocks.length === 0) return null; - // A block past the end of shorter contents clamps to the bottom. - const block = blocks.item(position.block); - if (!block) return this.viewport.scrollHeight; - const rect = block.getBoundingClientRect(); - return ( - this.viewport.scrollTop + - rect.top - - this.viewport.getBoundingClientRect().top + - position.fraction * rect.height - ); - } - - capture(): RenderedPosition | null { - const blocks = this.blocks(); - if (!blocks || blocks.length === 0) return null; - const top = this.viewport.getBoundingClientRect().top; - let low = 0; - let high = blocks.length - 1; - while (low < high) { - const middle = (low + high) >> 1; - if (blocks[middle]!.getBoundingClientRect().bottom > top) high = middle; - else low = middle + 1; - } - const rect = blocks[low]!.getBoundingClientRect(); - const fraction = rect.height > 0 ? (top - rect.top) / rect.height : 0; - return { view: "rendered", block: low, fraction: Math.min(Math.max(0, fraction), 0.999) }; - } - - private blocks(): HTMLCollection | undefined { - return this.viewport.querySelector(".chat-markdown")?.children; - } -} - interface SourceBinding { - readonly tracker: ReadingTracker; + readonly tracker: ReadingTracker; readonly geometry: SourceGeometry; + readonly binding: Binding; } // Keyed by Pierre's file container: its unmount phase also fires when the file turns into a placeholder. const sourceBindings = new WeakMap(); -const sourceTrackerByFile = new Map>(); -const fileId = (file: FileReadingKey) => JSON.stringify([file.environmentId, file.absolutePath]); +const sourceTrackerByFile = new Map(); function trackSourceView( fileContainer: HTMLElement, @@ -274,8 +112,11 @@ function trackSourceView( if (phase === "unmount") { existing?.tracker.dispose(); sourceBindings.delete(fileContainer); - if (existing !== undefined && sourceTrackerByFile.get(fileId(file)) === existing.tracker) { - sourceTrackerByFile.delete(fileId(file)); + if ( + existing !== undefined && + sourceTrackerByFile.get(fileReadingId(file)) === existing.tracker + ) { + sourceTrackerByFile.delete(fileReadingId(file)); } return; } @@ -287,12 +128,15 @@ function trackSourceView( const element = fileContainer.closest(".file-preview-virtualizer"); if (element === null) return; const geometry = new SourceGeometry(element, fileContainer, instance); - const tracker = new ReadingTracker(element, file, geometry); - sourceBindings.set(fileContainer, { tracker, geometry }); - sourceTrackerByFile.set(fileId(file), tracker); + const binding = { element, geometry }; + const tracker = new ReadingTracker(element, file, (scrolled) => + scrolled === element ? binding : null, + ); + sourceBindings.set(fileContainer, { tracker, geometry, binding }); + sourceTrackerByFile.set(fileReadingId(file), tracker); if (verdict === "pending") return; const saved = readings.readPosition(file, "source"); - if (saved !== null) tracker.restore(saved); + if (saved !== null) tracker.restore(binding, saved); } /** @@ -307,6 +151,10 @@ export function useFileReadingPosition(input: { readonly cwd: string; /** The workspace file shown, or null for an attachment. */ readonly relativePath: string | null; + /** The chat attachment shown, which is kept by its id. */ + readonly attachment?: + | { readonly id: string; readonly name: string; readonly mimeType: string } + | undefined; readonly revealLine: number | null; }) { const threadKey = scopedThreadKey(input.threadRef); @@ -317,22 +165,52 @@ export function useFileReadingPosition(input: { const file = input.relativePath === null ? null + : pathReadingKey(input.environmentId, resolvePathLinkTarget(input.relativePath, input.cwd)); + const viewMount = () => { + const newest = + verdict === "shown" && file !== null + ? readings.newestView(file, ["source", "rendered", "offset"]) + : null; + return { surface, renderedAllowed: newest !== null && newest !== "source" }; + }; + // A reveal that lands while this file's view is mounted must not flip it to rendered. + const [mounted, setMounted] = useState(viewMount); + if (mounted.surface !== surface) setMounted(viewMount()); + const placed: FileReadingKey | undefined = + input.attachment === undefined + ? (file ?? undefined) : { + kind: "attachment", environmentId: input.environmentId, - absolutePath: resolvePathLinkTarget(input.relativePath, input.cwd), + attachmentId: input.attachment.id, }; - const viewMount = () => ({ - surface, - renderedAllowed: - verdict === "shown" && - file !== null && - readings.newestView(file, ["source", "rendered"]) === "rendered", + // AttachmentFilePreview shows an attachment by its content type, the panel a file by its name. + const kind = input.attachment === undefined ? null : filePreviewKind(input.attachment); + const framed = + kind === null + ? isBrowserPreviewFile(input.relativePath ?? "") + : kind === "pdf" || kind === "html"; + const place = useViewerPlace({ + file: placed, + // A frame or PDF.js keeps its own place. + views: framed ? NO_VIEWS : input.attachment === undefined ? PATH_VIEWS : ATTACHMENT_VIEWS, + citation: + verdict === "pending" && input.revealLine !== null && surface !== undefined + ? { + id: ledger.generation(surface), + line: input.revealLine, + pending: () => ledger.verdict(surface) === "pending", + shown: () => ledger.markShown(threadKey, surface), + } + : null, }); - // A reveal that lands while this file's view is mounted must not flip it to rendered. - const [mounted, setMounted] = useState(viewMount); - if (mounted.surface !== surface) setMounted(viewMount()); return { + /** The reading key of the file or attachment shown. */ + file: placed, + /** The ref for the element that holds the viewer, which keeps the place of most viewers. */ + placeRef: place.ref, + rowLine: place.rowLine, scrollLine: verdict === "pending" ? input.revealLine : null, revealKey: ledger.generation(surface), renderedAllowed: mounted.renderedAllowed, @@ -342,7 +220,7 @@ export function useFileReadingPosition(input: { const saved = readings.readPosition(file, "source"); const binding = sourceBindings.get(fileContainer); if (saved === null || binding === undefined) return true; - binding.tracker.restore(saved); + binding.tracker.restore(binding.binding, saved); return false; }, /** A saved source position restores the code view, so the panel's own offset stays out. */ @@ -360,32 +238,13 @@ export function useFileReadingPosition(input: { // A landed reveal does not re-render the panel, so ask the ledger now. trackSourceView(fileContainer, instance, phase, file, ledger.verdict(surface)); }, - caretLine: (): number | "first-visible" => - (ledger.verdict(surface) === "pending" ? input.revealLine : null) ?? - (file === null ? undefined : sourceTrackerByFile.get(fileId(file))?.anchor?.line) ?? - "first-visible", + caretLine: (): number | "first-visible" => { + const anchor = file === null ? null : sourceTrackerByFile.get(fileReadingId(file))?.anchor; + return ( + (ledger.verdict(surface) === "pending" ? input.revealLine : null) ?? + (anchor?.view === "source" ? anchor.line : undefined) ?? + "first-visible" + ); + }, }; } - -/** A stable ref for a rendered Markdown `ScrollArea` root, restoring and saving its top block. */ -export function useRenderedReadingPosition( - environmentId: EnvironmentId, - cwd: string, - relativePath: string, -) { - const [ref] = useState(() => (root: HTMLElement | null) => { - const viewport = root?.querySelector('[data-slot="scroll-area-viewport"]'); - if (!viewport) return; - const file = { environmentId, absolutePath: resolvePathLinkTarget(relativePath, cwd) }; - const tracker = new ReadingTracker( - viewport, - file, - new RenderedGeometry(viewport), - root ?? viewport, - ); - const saved = readings.readPosition(file, "rendered"); - if (saved !== null) tracker.restore(saved); - return () => tracker.dispose(); - }); - return ref; -} diff --git a/apps/web/src/components/files/fileReadingSave.ts b/apps/web/src/components/files/fileReadingSave.ts index b85a3557d3d1d..6f5f891d16bc7 100644 --- a/apps/web/src/components/files/fileReadingSave.ts +++ b/apps/web/src/components/files/fileReadingSave.ts @@ -1,9 +1,11 @@ +import type { FilePreviewScroll } from "@t3tools/shared/htmlRender"; import { useEffect, useState } from "react"; import type { PdfViewState } from "./PdfViewer"; import { createFileReadingStore, type FileReadingKey, + type HeldPosition, type ReadingPosition, } from "./fileReadingStore"; @@ -19,16 +21,17 @@ export const readings = createFileReadingStore({ /** A position waiting out the save delay, written early on unmount and pagehide. */ export class PendingPosition { - private position: ReadingPosition | null = null; + private held: HeldPosition | null = null; private timer: ReturnType | null = null; constructor( - private readonly file: FileReadingKey, + readonly file: FileReadingKey, private readonly onSettle?: () => void, ) {} hold(position: ReadingPosition): void { - this.position = position; + if (this.held !== null && this.held.position.view !== position.view) this.held.flush(); + this.held = readings.holdPosition(this.file, position); this.clearTimer(); this.schedule(); } @@ -39,7 +42,7 @@ export class PendingPosition { } write(position: ReadingPosition): void { - this.position = position; + this.hold(position); this.flush(); } @@ -49,8 +52,8 @@ export class PendingPosition { readonly flush = (): void => { this.clearTimer(); - if (this.position !== null) readings.writePosition(this.file, this.position); - this.position = null; + this.held?.flush(); + this.held = null; }; private schedule(): void { @@ -66,18 +69,9 @@ export class PendingPosition { } } -const FIRST_PAGE: PdfViewState = { page: 0, offset: 0, zoom: null, left: 0 }; - -/** PDF.js view state seeded from and saved to the reading store when `file` is given. */ -export function usePdfReadingView(file: FileReadingKey | undefined) { - const [pending] = useState(() => (file === undefined ? null : new PendingPosition(file))); - const [view, setView] = useState(() => { - const saved = file === undefined ? null : readings.readPosition(file, "pdf"); - return saved === null - ? FIRST_PAGE - : { page: saved.page, offset: saved.offset, zoom: saved.zoom, left: saved.left }; - }); - // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- writes the debounced PDF position to localStorage when the page hides or the document unmounts +/** Flushes `pending` when the page hides or the caller unmounts. */ +function useFlushOnExit(pending: PendingPosition | null) { + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- writes the debounced position to localStorage when the page hides or the view unmounts useEffect(() => { if (pending === null) return; window.addEventListener("pagehide", pending.flush); @@ -86,18 +80,57 @@ export function usePdfReadingView(file: FileReadingKey | undefined) { pending.flush(); }; }, [pending]); +} + +const FIRST_PAGE: PdfViewState = { page: 0, offset: 0, zoom: null, left: 0 }; + +/** Whether two PDF views show the same spot; an offset measured at another width is scaled. */ +function samePdfView(next: PdfViewState, view: PdfViewState) { + const scale = next.width !== undefined && view.width !== undefined ? next.width / view.width : 1; + return ( + next.page === view.page && + next.zoom === view.zoom && + next.left === view.left && + Math.abs(next.offset - view.offset * scale) <= 1 + ); +} + +/** PDF.js view state seeded from and saved to the reading store when `file` is given. */ +export function usePdfReadingView(file: FileReadingKey | undefined) { + const [pending] = useState(() => (file === undefined ? null : new PendingPosition(file))); + const [view, setView] = useState(() => { + const saved = file === undefined ? null : readings.readPosition(file, "pdf"); + if (saved === null) return FIRST_PAGE; + const { view: _view, ...state } = saved; + return state; + }); + useFlushOnExit(pending); const changeView = (next: PdfViewState) => { - // PdfViewer reports its own restore; saving that again would renew the entry on every open. - if ( - next.page === view.page && - next.offset === view.offset && - next.zoom === view.zoom && - next.left === view.left - ) { - return; - } + if (samePdfView(next, view)) return; setView(next); pending?.hold({ view: "pdf", ...next }); }; return [view, changeView] as const; } + +/** + * An HTML page's saved place: `seed` starts the frame's restore, and `save` keeps a scroll the + * page reported, debounced and skipped when unchanged. `FrameReading` decides what to save. + */ +export function useFramePlace(file: FileReadingKey | undefined) { + const [pending] = useState(() => (file === undefined ? null : new PendingPosition(file))); + const [place] = useState(() => { + const saved = pending === null ? null : readings.readPosition(pending.file, "offset"); + let last: FilePreviewScroll | null = saved === null ? null : { x: saved.left, y: saved.top }; + return { + seed: last, + save(scroll: FilePreviewScroll) { + if (pending === null || (last?.x === scroll.x && last.y === scroll.y)) return; + last = scroll; + pending.hold({ view: "offset", top: Math.max(0, scroll.y), left: Math.max(0, scroll.x) }); + }, + }; + }); + useFlushOnExit(pending); + return place; +} diff --git a/apps/web/src/components/files/fileReadingStore.test.ts b/apps/web/src/components/files/fileReadingStore.test.ts index 860ffacc45380..3a7173bb63b05 100644 --- a/apps/web/src/components/files/fileReadingStore.test.ts +++ b/apps/web/src/components/files/fileReadingStore.test.ts @@ -13,7 +13,7 @@ import { } from "./fileReadingStore"; const DAY = 24 * 60 * 60 * 1000; -const file = { environmentId: "env-1", absolutePath: "/work/a.ts" }; +const file = { kind: "path", environmentId: "env-1", absolutePath: "/work/a.ts" } as const; const sourceKey = 't3code.fileReading.v1:["env-1","/work/a.ts","source"]'; function memoryStorage(options: { quota?: number } = {}) { @@ -82,6 +82,30 @@ describe("file reading positions", () => { line: 12, offset: -4, }); + + const attachment = { + kind: "attachment", + environmentId: "env-1", + attachmentId: "/work/a.ts", + } as const; + readings.writePosition(attachment, { view: "source", line: 30, offset: 0 }); + expect(items.has('t3code.fileReading.v1:["env-1",{"attachment":"/work/a.ts"},"source"]')).toBe( + true, + ); + expect(readings.readPosition(file, "source")?.line).toBe(12); + expect(readings.readPosition(attachment, "source")?.line).toBe(30); + + const places = [ + { view: "rendered", block: 4, fraction: 0.5 }, + { view: "pdf", page: 2, offset: 120, zoom: null, left: 0, width: 640 }, + { view: "offset", top: 900, left: 12 }, + { view: "media", time: 42.5 }, + ] as const; + for (const place of places) readings.writePosition(attachment, place); + const reloaded = setup(storage).readings; + for (const place of places) { + expect(reloaded.readPosition(attachment, place.view)).toEqual(place); + } }); it("names the newest of a file's saved views", () => { @@ -145,6 +169,27 @@ describe("file reading positions", () => { 't3code.fileReading.v1:["env-1","/fraction","rendered"]', JSON.stringify({ v: 1, at: 1_000_000, view: "rendered", block: 0, fraction: 1 }), ); + items.set( + 't3code.fileReading.v1:["env-1","/top","offset"]', + JSON.stringify({ v: 1, at: 1_000_000, view: "offset", top: -1, left: 0 }), + ); + items.set( + 't3code.fileReading.v1:["env-1","/time","media"]', + JSON.stringify({ v: 1, at: 1_000_000, view: "media", time: -2 }), + ); + items.set( + 't3code.fileReading.v1:["env-1","/width","pdf"]', + JSON.stringify({ + v: 1, + at: 1_000_000, + view: "pdf", + page: 0, + offset: 0, + zoom: null, + left: 0, + width: 0, + }), + ); items.set( key("/old"), JSON.stringify({ v: 1, at: 1_000_000 - 31 * DAY, view: "source", line: 1, offset: 0 }), @@ -164,6 +209,46 @@ describe("file reading positions", () => { expect(readings.readPosition({ ...file, absolutePath: "/good" }, "source")?.line).toBe(7); }); + it("keeps the newest sample: a held one is read at once, and a flush never replaces a later one", () => { + const { items, storage } = memoryStorage(); + const { clock, readings } = setup(storage); + const held = readings.holdPosition(file, { view: "rendered", block: 7, fraction: 0 }); + expect(items.size).toBe(0); + expect(readings.readPosition(file, "rendered")?.block).toBe(7); + expect(readings.newestView(file, ["source", "rendered"])).toBe("rendered"); + + clock.now += 50; + readings.writePosition(file, { view: "rendered", block: 9, fraction: 0 }); + clock.now += 300; + held.flush(); + expect(readings.readPosition(file, "rendered")?.block).toBe(9); + expect(setup(storage).readings.readPosition(file, "rendered")?.block).toBe(9); + + const first = readings.holdPosition(file, { view: "source", line: 3, offset: 0 }); + clock.now += 10; + const second = readings.holdPosition(file, { view: "source", line: 5, offset: 0 }); + first.flush(); + expect(readings.readPosition(file, "source")?.line).toBe(5); + second.flush(); + expect(setup(storage).readings.readPosition(file, "source")?.line).toBe(5); + + // Two samples in one millisecond keep the order they were captured in. + const earlier = readings.holdPosition(file, { view: "source", line: 7, offset: 0 }); + const newer = readings.holdPosition(file, { view: "source", line: 8, offset: 0 }); + newer.flush(); + earlier.flush(); + expect(readings.readPosition(file, "source")?.line).toBe(8); + expect(setup(storage).readings.readPosition(file, "source")?.line).toBe(8); + + // The clock steps back, as after a VM resume; this page's newer sample still wins. + clock.now -= 5_000; + const stepped = readings.holdPosition(file, { view: "source", line: 9, offset: 0 }); + expect(readings.readPosition(file, "source")?.line).toBe(9); + stepped.flush(); + expect(readings.readPosition(file, "source")?.line).toBe(9); + expect(setup(storage).readings.readPosition(file, "source")?.line).toBe(9); + }); + it("frees the oldest half on a full quota, retries once, then keeps writes in memory", () => { const full = memoryStorage({ quota: 1_200 }); const { clock, readings } = setup(full.storage); diff --git a/apps/web/src/components/files/fileReadingStore.ts b/apps/web/src/components/files/fileReadingStore.ts index 0adb6faa85e55..b7abea6b5a35e 100644 --- a/apps/web/src/components/files/fileReadingStore.ts +++ b/apps/web/src/components/files/fileReadingStore.ts @@ -18,11 +18,13 @@ const RECEIPT_CAP = 200; const PositiveInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(1)); const Index = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)); +const NonNegative = Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)); +const Positive = Schema.Finite.check(Schema.isGreaterThan(0)); const ReadingPosition = Schema.Union([ - /** `offset` is the viewport top minus the line top, so it is zero or negative. */ + /** `offset` is the viewport top minus the line's top; an attachment's source view keeps line 1. */ Schema.Struct({ view: Schema.Literal("source"), line: PositiveInt, offset: Schema.Finite }), - /** `block` is a top-level Markdown block; `fraction` is how far the viewport top is into it. */ + /** `block` is an item: a Markdown block, table row, page or notebook cell; `fraction` is how far the viewport top is into it. */ Schema.Struct({ view: Schema.Literal("rendered"), block: Index, @@ -30,13 +32,23 @@ const ReadingPosition = Schema.Union([ Schema.isBetween({ minimum: 0, maximum: 1, exclusiveMaximum: true }), ), }), + /** `width` is the page width `offset` was measured at. */ Schema.Struct({ view: Schema.Literal("pdf"), page: Index, offset: Schema.Finite, - zoom: Schema.NullOr(Schema.Finite.check(Schema.isGreaterThan(0))), + zoom: Schema.NullOr(Positive), left: Schema.Finite, + width: Schema.optionalKey(Positive), }), + /** CSS pixels of a scroller with no item structure; the browser caps them. */ + Schema.Struct({ + view: Schema.Literal("offset"), + top: NonNegative, + left: NonNegative, + }), + /** Seconds into a video or audio file. */ + Schema.Struct({ view: Schema.Literal("media"), time: NonNegative }), ]); export type ReadingPosition = typeof ReadingPosition.Type; export type ReadingView = ReadingPosition["view"]; @@ -54,9 +66,16 @@ const EntryHeader = Schema.Struct({ v: Schema.Literal(1), at: Schema.Finite }); const decodeHeader = Schema.decodeUnknownOption(EntryHeader); const decodeReceiptKey = Schema.decodeUnknownOption(Schema.Tuple([Schema.String, Schema.String])); -export interface FileReadingKey { - readonly environmentId: string; - readonly absolutePath: string; +/** A workspace or host file by absolute path, or a chat attachment by id. */ +export type FileReadingKey = + | { readonly kind: "path"; readonly environmentId: string; readonly absolutePath: string } + | { readonly kind: "attachment"; readonly environmentId: string; readonly attachmentId: string }; + +/** A sample a debounced save holds; reads see it before it is written. */ +export interface HeldPosition { + readonly position: ReadingPosition; + /** Writes the sample with its capture time, unless a later one is stored, and forgets it. */ + flush(): void; } export interface StorageLike { @@ -70,8 +89,15 @@ export interface StorageLike { interface Entry { readonly at: number; readonly value: A; + /** Orders this page's own samples, whatever its clock does; absent for stored entries. */ + readonly order?: number; } +const later = (entry: Entry, than: Entry) => + entry.order !== undefined && than.order !== undefined + ? entry.order > than.order + : entry.at > than.at || (entry.at === than.at && entry.order !== undefined); + /** One key per entry under `prefix`; `index` holds every live storage key, oldest first. */ interface Table { readonly prefix: string; @@ -79,6 +105,7 @@ interface Table { readonly decode: (input: unknown) => Option.Option; readonly index: Map; readonly written: Map>; + readonly held: Map>; } function table( @@ -86,11 +113,16 @@ function table( cap: number, decode: (input: unknown) => Option.Option, ): Table { - return { prefix, cap, decode, index: new Map(), written: new Map() }; + return { prefix, cap, decode, index: new Map(), written: new Map(), held: new Map() }; } +// A path keeps its first key, so places saved before attachment keys existed survive. +const documentOf = (file: FileReadingKey) => + file.kind === "path" ? file.absolutePath : { attachment: file.attachmentId }; +export const fileReadingId = (file: FileReadingKey) => + JSON.stringify([file.environmentId, documentOf(file)]); const positionKey = (file: FileReadingKey, view: ReadingView) => - POSITION_PREFIX + JSON.stringify([file.environmentId, file.absolutePath, view]); + POSITION_PREFIX + JSON.stringify([file.environmentId, documentOf(file), view]); const receiptKey = (threadKey: string, relativePath: string) => RECEIPT_PREFIX + JSON.stringify([threadKey, relativePath]); @@ -108,6 +140,7 @@ export function createFileReadingStore(options: { const receipts = table(RECEIPT_PREFIX, RECEIPT_CAP, Schema.decodeUnknownOption(RevealReceipt)); const tables: ReadonlyArray> = [positions, receipts]; let storage: StorageLike | null | undefined; + let captured = 0; let scanned = false; let writable = true; @@ -200,20 +233,35 @@ export function createFileReadingStore(options: { } }; + const newest = (entries: ReadonlyArray | undefined>, now: number) => { + let found: Entry | undefined; + for (const entry of entries) { + if (entry === undefined || !fresh(entry.at, now)) continue; + if (found === undefined || later(entry, found)) found = entry; + } + return found; + }; + + /** This page's last write and the stored entry, unless the stored one is that same write. */ + const lastWrites = (target: Table, key: string, now: number) => { + const written = target.written.get(key); + const stored = readStored(target, key, now); + return [written, stored?.at === written?.at ? undefined : stored]; + }; + const read = (target: Table, key: string): Entry | undefined => { scan(); const now = options.now(); - const written = target.written.get(key); - const mine = written !== undefined && fresh(written.at, now) ? written : undefined; - const stored = readStored(target, key, now); - return stored !== undefined && (mine === undefined || stored.at > mine.at) ? stored : mine; + return newest([target.held.get(key), ...lastWrites(target, key, now)], now); }; - const write = (target: Table, key: string, value: A) => { + const write = (target: Table, key: string, entry: Entry) => { scan(); - const at = options.now(); + const { at, value } = entry; + const current = newest(lastWrites(target, key, at), at); + if (current !== undefined && later(current, entry)) return; target.written.delete(key); - target.written.set(key, { at, value }); + target.written.set(key, entry); for (const writtenKey of target.written.keys()) { if (target.written.size <= target.cap) break; target.written.delete(writtenKey); @@ -250,14 +298,31 @@ export function createFileReadingStore(options: { return value?.view === view ? (value as ReadingPositionOf) : null; }, writePosition(file: FileReadingKey, position: ReadingPosition): void { - write(positions, positionKey(file, position.view), position); + write(positions, positionKey(file, position.view), { + at: options.now(), + value: position, + order: ++captured, + }); + }, + /** Holds a sample captured now; the newest sample of a file's view wins, held or stored. */ + holdPosition(file: FileReadingKey, position: ReadingPosition): HeldPosition { + const key = positionKey(file, position.view); + const entry = { at: options.now(), value: position, order: ++captured }; + positions.held.set(key, entry); + return { + position, + flush: () => { + if (positions.held.get(key) === entry) positions.held.delete(key); + write(positions, key, entry); + }, + }; }, /** The view among `views` whose saved entry is newest. */ newestView(file: FileReadingKey, views: ReadonlyArray): ReadingView | null { let newest: Entry | undefined; for (const view of views) { const entry = read(positions, positionKey(file, view)); - if (entry?.value.view === view && (newest === undefined || entry.at > newest.at)) { + if (entry?.value.view === view && (newest === undefined || later(entry, newest))) { newest = entry; } } @@ -267,7 +332,7 @@ export function createFileReadingStore(options: { return read(receipts, receiptKey(threadKey, relativePath))?.value ?? null; }, writeReceipt(threadKey: string, relativePath: string, receipt: RevealReceipt): void { - write(receipts, receiptKey(threadKey, relativePath), receipt); + write(receipts, receiptKey(threadKey, relativePath), { at: options.now(), value: receipt }); }, receiptTabs(): ReadonlyArray<{ readonly threadKey: string; readonly relativePath: string }> { scan(); diff --git a/apps/web/src/components/files/fileReadingTestKit.tsx b/apps/web/src/components/files/fileReadingTestKit.tsx new file mode 100644 index 0000000000000..92f72db6377f3 --- /dev/null +++ b/apps/web/src/components/files/fileReadingTestKit.tsx @@ -0,0 +1,400 @@ +// Shared setup for the right panel's reading tests. A test file mocks the panel's modules with +// `fileReadingTestMocks` before it imports this kit. +import { DEFAULT_THEMES, preloadHighlighter } from "@pierre/diffs"; +import { scopeThreadRef } from "@t3tools/client-runtime/environment"; +import { + type ChatFileAttachment, + EnvironmentId, + type ProjectReadFileResult, + type ScopedThreadRef, + ThreadId, +} from "@t3tools/contracts"; +import { expect, vi } from "vite-plus/test"; +import { userEvent } from "vite-plus/test/browser"; + +import appCss from "~/index.css?inline"; +import { selectActiveRightPanelSurface, useRightPanelStore } from "~/rightPanelStore"; + +import FilePreviewPanel from "./FilePreviewPanel"; +import { resetState, state } from "./fileReadingTestMocks"; + +export { state }; + +export const environmentId = EnvironmentId.make("file-reading-position-test"); +// Each mount of the text viewer starts its indexing worker cold, slow on a busy machine. +export const COLD_WORKER = { timeout: 10_000 }; + +type SavedView = "source" | "rendered" | "pdf" | "offset" | "media"; + +export function sourceFile( + relativePath: string, + word: string, + lines: number, +): ProjectReadFileResult { + const contents = Array.from( + { length: lines }, + (_, index) => `const ${word}_${index + 1} = ${index + 1};`, + ).join("\n"); + return { relativePath, contents, byteLength: contents.length, truncated: false }; +} + +/** A thread whose right panel reads files under `/reading/`, and its saved places. */ +export async function readingSetup(name: string) { + await preloadHighlighter({ themes: Object.values(DEFAULT_THEMES), langs: ["typescript"] }); + const settings = ["t3code.fileExplorerOpen", "t3code.renderMarkdown"]; + const previous = settings.map((key) => localStorage.getItem(key)); + localStorage.setItem(settings[0]!, "false"); + localStorage.setItem(settings[1]!, "true"); + resetState(); + const style = document.createElement("style"); + style.textContent = appCss; + document.head.append(style); + const doc = Array.from({ length: 80 }, (_, index) => `Paragraph ${index + 1}.`).join("\n\n"); + state.files = { + "a.ts": sourceFile("a.ts", "alpha", 300), + "b.ts": sourceFile("b.ts", "beta", 300), + "c.ts": sourceFile("c.ts", "gamma", 10), + "doc.md": { relativePath: "doc.md", contents: doc, byteLength: doc.length, truncated: false }, + }; + const ref = scopeThreadRef(environmentId, ThreadId.make(`reading-${name}`)); + const cwd = `/reading/${name}`; + const urls: string[] = []; + const stored = (key: string) => + JSON.parse(localStorage.getItem(`t3code.fileReading.v1:${key}`) ?? "null") as { + line?: number; + offset?: number; + block?: number; + page?: number; + top?: number; + left?: number; + time?: number; + at: number; + } | null; + return { + ref, + cwd, + panel: useRightPanelStore.getState(), + saved: (path: string, view: SavedView) => + stored(JSON.stringify([environmentId, `${cwd}/${path}`, view])), + attachmentSaved: (id: string, view: SavedView) => + stored(JSON.stringify([environmentId, { attachment: id }, view])), + /** Serves `contents` at the file's asset URL; the fragment keeps the panel's query off the blob. */ + serve(path: string, contents: Blob | string) { + const url = URL.createObjectURL(contents instanceof Blob ? contents : new Blob([contents])); + urls.push(url); + state.assets[`${cwd}/${path}`] = `${url}#asset`; + }, + /** Serves an attachment by its id. */ + attach(attachment: ChatFileAttachment, contents: Blob | string) { + const url = URL.createObjectURL(contents instanceof Blob ? contents : new Blob([contents])); + urls.push(url); + state.assets[attachment.id] = url; + }, + [Symbol.dispose]() { + for (const url of urls) URL.revokeObjectURL(url); + useRightPanelStore.getState().removeThread(ref); + for (const key of Object.keys(localStorage)) { + if (key.startsWith("t3code.fileReading.v1:") || key.startsWith("t3code.fileReveal.v1:")) { + localStorage.removeItem(key); + } + } + style.remove(); + for (const [index, key] of settings.entries()) { + const value = previous[index]; + if (value == null) localStorage.removeItem(key); + else localStorage.setItem(key, value); + } + }, + }; +} + +const noop = () => {}; + +function Panel(props: { + readonly threadRef: ScopedThreadRef; + readonly cwd: string; + readonly relativePath: string; + readonly attachment?: ChatFileAttachment; + readonly revealLine: number | null; + readonly revealRequestId: number; + readonly width: number | undefined; + readonly workspaceMutationId: string | null; +}) { + return ( +

+ ); +} + +/** Renders the active file tab the way the right panel does: one panel per workspace. */ +export function ReadingPanel(props: { + readonly threadRef: ScopedThreadRef; + readonly cwd: string; + readonly width?: number; + readonly workspaceMutationId?: string; +}) { + const surface = useRightPanelStore((store) => + selectActiveRightPanelSurface(store.byThreadKey, props.threadRef), + ); + return surface?.kind === "file" ? ( + + ) : null; +} + +/** A chat attachment open in the right panel. */ +export function AttachmentPanel(props: { + readonly threadRef: ScopedThreadRef; + readonly cwd: string; + readonly attachment: ChatFileAttachment; +}) { + return ( + + ); +} + +export function sourceView(container: HTMLElement) { + const scroller = () => container.querySelector(".file-preview-virtualizer")!; + const line = (lineNumber: number) => + container + .querySelector("diffs-container") + ?.shadowRoot?.querySelector(`[data-line="${lineNumber}"]`) ?? null; + return { + scroller, + line, + /** The first row the viewport shows, partly or fully, and its top relative to the viewport. */ + topLine() { + const top = scroller().getBoundingClientRect().top; + const rows = container + .querySelector("diffs-container") + ?.shadowRoot?.querySelectorAll("[data-line]"); + const row = [...(rows ?? [])] + .filter((element) => element.getBoundingClientRect().bottom > top + 0.5) + .sort( + (left, right) => + Number(left.getAttribute("data-line")) - Number(right.getAttribute("data-line")), + )[0]; + return { + line: Number(row?.getAttribute("data-line")), + offset: Math.round(row!.getBoundingClientRect().top - top), + }; + }, + expectLineInView(lineNumber: number, text: string) { + const element = line(lineNumber); + expect(element?.textContent).toContain(text); + const view = scroller().getBoundingClientRect(); + const rect = element!.getBoundingClientRect(); + expect(rect.top).toBeGreaterThanOrEqual(view.top - 1); + expect(rect.bottom).toBeLessThanOrEqual(view.bottom + 1); + }, + }; +} + +/** Rows of the large text and table viewer, by their data index. */ +export function rowsView(container: HTMLElement) { + const scroller = () => container.querySelector("[aria-rowcount] > [tabindex]"); + return { + scroller, + /** The data index of the first row the viewport shows. */ + topRow() { + const element = scroller(); + if (element === null) return null; + const top = element.getBoundingClientRect().top; + const row = [...element.querySelectorAll("[data-preview-row]")].find( + (candidate) => candidate.getBoundingClientRect().bottom > top + 0.5, + ); + return row === undefined ? null : Number(row.dataset.previewRow); + }, + indexed() { + const status = container.querySelector('[role="status"]')?.textContent ?? ""; + expect(status).toMatch(/lines|rows/); + expect(status).not.toContain("Indexing"); + }, + /** userEvent finds the scroller again by its text, so rows the worker has not filled yet would lose it. */ + async wheel(y: number) { + await vi.waitFor(() => { + const shown = [...scroller()!.querySelectorAll("[data-preview-row]")]; + expect(shown.length).toBeGreaterThan(0); + expect(shown.filter((row) => /^\d+$/.test(row.textContent ?? ""))).toEqual([]); + }, COLD_WORKER); + await userEvent.wheel(scroller()!, { delta: { y } }); + }, + }; +} + +/** The first item a scroller shows, and how far its top sits above the scroller's top. */ +export function firstShown(scroller: HTMLElement, items: string) { + const top = scroller.getBoundingClientRect().top; + const elements = [...scroller.querySelectorAll(items)]; + const index = elements.findIndex((item) => item.getBoundingClientRect().bottom > top + 0.5); + return { + index, + offset: Math.round(top - (elements[index]?.getBoundingClientRect().top ?? top)), + }; +} + +export function csv(rows: number) { + return ["name,value", ...Array.from({ length: rows }, (_, index) => `row ${index + 1},${index}`)] + .join("\n") + .concat("\n"); +} + +export function lines(count: number) { + return Array.from({ length: count }, (_, index) => `log line ${index + 1}`).join("\n"); +} + +/** A large text file the panel streams from its asset URL. */ +export function largeText( + reading: { readonly serve: (path: string, contents: string) => void }, + path: string, + contents: string, +) { + reading.serve(path, contents); + state.files[path] = { relativePath: path, contents, byteLength: 2_000_000, truncated: true }; +} + +/** + * Holds back the large text worker's finished index until `release`, as a long file does; rows + * the worker has read still show and scroll. `hold` holds the next workers' indexes again. + */ +export function holdIndexing() { + const Native = window.Worker; + const held: Array<() => void> = []; + let holding = true; + class HoldingWorker extends Native { + constructor(url: string | URL, options?: WorkerOptions) { + super(url, options); + // The first listener, so it can keep the finished index from the viewer's own. + this.addEventListener("message", (event) => { + const data: unknown = event.data; + if (!holding || !isFinishedIndex(data)) return; + event.stopImmediatePropagation(); + held.push(() => this.dispatchEvent(new MessageEvent("message", { data }))); + }); + } + } + window.Worker = HoldingWorker; + return { + hold() { + holding = true; + }, + release() { + holding = false; + for (const deliver of held.splice(0)) deliver(); + }, + [Symbol.dispose]() { + window.Worker = Native; + }, + }; +} + +const isFinishedIndex = (data: unknown) => + typeof data === "object" && + data !== null && + "type" in data && + data.type === "meta" && + "complete" in data && + data.complete === true; + +/** A PDF of letter pages, each saying which page it is. */ +export function letterPdf(pages: number) { + const font = 3 + pages * 2; + const objects = [ + "<< /Type /Catalog /Pages 2 0 R >>", + `<< /Type /Pages /Count ${pages} /Kids [${Array.from({ length: pages }, (_, index) => `${3 + index * 2} 0 R`).join(" ")}] >>`, + ...Array.from({ length: pages }, (_, index) => { + const text = `BT /F1 24 Tf 72 700 Td (Page ${index + 1}) Tj ET`; + return [ + `<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 ${font} 0 R >> >> /Contents ${4 + index * 2} 0 R >>`, + `<< /Length ${text.length} >>\nstream\n${text}\nendstream`, + ]; + }).flat(), + "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>", + ]; + let source = "%PDF-1.4\n"; + const offsets: number[] = []; + for (const [index, object] of objects.entries()) { + offsets.push(source.length); + source += `${index + 1} 0 obj\n${object}\nendobj\n`; + } + const xref = source.length; + source += `xref\n0 ${objects.length + 1}\n0000000000 65535 f \n${offsets + .map((offset) => `${String(offset).padStart(10, "0")} 00000 n \n`) + .join("")}trailer\n<< /Size ${objects.length + 1} /Root 1 0 R >>\nstartxref\n${xref}\n%%EOF`; + return new Blob([source], { type: "application/pdf" }); +} + +/** Ten seconds of 8 kHz silence, which audio and video elements load with a known duration. */ +export function silentWav() { + const rate = 8000; + const samples = rate * 10; + const bytes = new Uint8Array(44 + samples).fill(128); + const view = new DataView(bytes.buffer); + const text = (offset: number, value: string) => + [...value].forEach((char, index) => view.setUint8(offset + index, char.charCodeAt(0))); + text(0, "RIFF"); + view.setUint32(4, 36 + samples, true); + text(8, "WAVEfmt "); + view.setUint32(16, 16, true); + view.setUint16(20, 1, true); + view.setUint16(22, 1, true); + view.setUint32(24, rate, true); + view.setUint32(28, rate, true); + view.setUint16(32, 1, true); + view.setUint16(34, 8, true); + text(36, "data"); + view.setUint32(40, samples, true); + return new Blob([bytes], { type: "audio/wav" }); +} + +/** Marks a media element used, as a reader pressing its native controls does. */ +export function pressMedia(media: HTMLMediaElement) { + media.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true })); + media.dispatchEvent(new PointerEvent("pointerup", { bubbles: true })); +} + +/** Seeks a media element as a reader pressing its native controls would. */ +export async function seekAsReader(media: HTMLMediaElement, time: number) { + pressMedia(media); + const seeked = new Promise((resolve) => + media.addEventListener("seeked", resolve, { once: true }), + ); + media.currentTime = time; + await seeked; +} diff --git a/apps/web/src/components/files/fileReadingTestMocks.tsx b/apps/web/src/components/files/fileReadingTestMocks.tsx new file mode 100644 index 0000000000000..68e179c7cb3fd --- /dev/null +++ b/apps/web/src/components/files/fileReadingTestMocks.tsx @@ -0,0 +1,164 @@ +// Module stand-ins for the right panel's reading tests. Each test file's `vi.mock` factories call +// these; this module imports nothing they replace, so a factory can load it while they load. +import type { AssetResource, EnvironmentId, ProjectReadFileResult } from "@t3tools/contracts"; +import { AsyncResult, Atom } from "effect/reactivity"; +import type { ReactNode } from "react"; +import { vi } from "vite-plus/test"; + +/** What the stand-ins read; `readingSetup` resets it for each test. */ +export const state = { + file: null as ProjectReadFileResult | null, + files: {} as Record, + optimistic: null as ProjectReadFileResult | null, + writeFile: vi.fn(), + /** Asset URLs by absolute path or attachment id. */ + assets: {} as Record, +}; + +export function resetState() { + state.file = null; + state.files = {}; + state.assets = {}; + state.optimistic = null; + state.writeFile.mockReset().mockResolvedValue(AsyncResult.success(undefined)); +} + +const keyOf = (resource: AssetResource | null): string => + (resource !== null && "path" in resource + ? resource.path + : resource !== null && "attachmentId" in resource + ? resource.attachmentId + : undefined) ?? ""; +const refreshers = new WeakMap Promise>(); +const noResource = async () => null; + +export const mockAssetUrls = ( + actual: typeof import("~/assets/assetUrls"), +): typeof import("~/assets/assetUrls") => ({ + ...actual, + useAssetUrlState: (_environmentId, resource) => { + const url = state.assets[keyOf(resource)]; + return url === undefined + ? { _tag: "Loading" } + : { _tag: "Success", url, expiresAt: Number.MAX_SAFE_INTEGER }; + }, + // The real hook keeps its function only while it gets the same resource object. + useAssetUrlRefresh: (_environmentId, resource) => { + if (resource === null) return noResource; + let refresh = refreshers.get(resource); + if (refresh === undefined) { + const key = keyOf(resource); + refresh = async () => state.assets[key] ?? null; + refreshers.set(resource, refresh); + } + return refresh; + }, +}); + +/** Vite's worker transform adds a large inline source map; a blob of the raw file is the same code. */ +export async function mockPdfWorkerUrl() { + const { default: source } = await import("pdfjs-dist/build/pdf.worker.min.mjs?raw"); + return { default: URL.createObjectURL(new Blob([source], { type: "text/javascript" })) }; +} + +/** + * The Word engine compiles about 30 MB of WebAssembly per document. The stand-in lays out 40 + * blank pages in the real page list, which is what keeps a Word or PowerPoint reader's place. + */ +export async function mockWordDocument() { + const { OfficePages } = await import("./office/OfficePages"); + const pages = Array.from({ length: 40 }, () => ({ width: 600, height: 800 })); + const paint = async () => {}; + const ignore = () => {}; + return { + default: () => , + }; +} + +export const mockRuntime = (actual: T) => ({ + ...actual, + runAtomCommand: ((_registry: unknown, _command: unknown, request: unknown) => + state.writeFile(request)) as never, +}); + +export const mockSession = (actual: T) => ({ + ...actual, + readEnvironmentScope: () => true, + useEnvironmentScope: () => true, +}); + +export const mockFilesystem = (actual: T) => ({ + ...actual, + useFilesystemReadAccess: () => ({ canReadFiles: true, isPending: false, error: null }), +}); + +export const mockAtomCommand = (actual: T) => ({ + ...actual, + useAtomCommand: (() => state.writeFile) as never, +}); + +const noConfig = Atom.make(null); +export const mockServer = ( + actual: typeof import("~/state/server"), +): typeof import("~/state/server") => ({ + ...actual, + serverEnvironment: { ...actual.serverEnvironment, configValueAtom: (() => noConfig) as never }, +}); + +export const mockRemoteOpen = (actual: T) => ({ + ...actual, + useRemoteOpenState: () => ({ mode: "local-exec" }) as never, + useRemoteOpenResolution: () => ({ state: { mode: "local-exec" }, isResolved: true }) as never, +}); + +// Without the diff worker pool, Pierre highlights on the page, which has only the dark theme. +export const mockTheme = (actual: T) => ({ + ...actual, + useTheme: () => ({ resolvedTheme: "dark" }) as never, + useRenderedAppearance: () => "dark" as never, +}); + +export const mockPanelParts = { + mutationRefresh: () => ({ useWorkspaceMutationRefresh: vi.fn() }), + breadcrumbs: () => ({ FileBreadcrumbs: () => <> }), + browser: () => ({ default: () =>
}), + diffWorkers: () => ({ + DiffWorkerPoolProvider: ({ children }: { children?: ReactNode }) => <>{children}, + }), +}; + +// The real query keeps its result while the file does not change, so memoized renders stay memoized. +const queries = new Map(); +const fileQuery = (data: ProjectReadFileResult | null) => { + let query = queries.get(data); + if (query === undefined) { + query = { + data, + error: null, + isPending: false, + readError: null, + isNotFile: false, + isFile: true, + refresh: vi.fn(), + }; + queries.set(data, query); + } + return query; +}; + +export const mockProjectFiles = (actual: T) => ({ + ...actual, + useProjectFileQuery: (_environmentId: EnvironmentId, _cwd: string, path: string | null) => + fileQuery((path === null ? undefined : state.files[path]) ?? state.file) as never, + getOptimisticProjectFileQueryData: () => state.optimistic, + getUnsavedProjectFileQueryData: () => null, + setProjectFileQueryData: ( + _environmentId: EnvironmentId, + _cwd: string, + relativePath: string, + contents: string, + ) => { + state.optimistic = { relativePath, contents, truncated: false, byteLength: contents.length }; + }, + confirmProjectFileQueryData: () => true, +}); diff --git a/apps/web/src/components/files/fileReadingViewers.browser.test.tsx b/apps/web/src/components/files/fileReadingViewers.browser.test.tsx new file mode 100644 index 0000000000000..cfd0a88bd34e1 --- /dev/null +++ b/apps/web/src/components/files/fileReadingViewers.browser.test.tsx @@ -0,0 +1,609 @@ +import type { ChatFileAttachment } from "@t3tools/contracts"; +import JSZip from "jszip"; +import { describe, expect, it, vi } from "vite-plus/test"; +import { userEvent } from "vite-plus/test/browser"; + +import { act, render } from "../../../test/render"; + +vi.mock(import("~/assets/assetUrls"), async (original) => + (await import("./fileReadingTestMocks")).mockAssetUrls(await original()), +); +vi.mock(import("@t3tools/client-runtime/state/runtime"), async (original) => + (await import("./fileReadingTestMocks")).mockRuntime(await original()), +); +vi.mock(import("~/state/session"), async (original) => + (await import("./fileReadingTestMocks")).mockSession(await original()), +); +vi.mock(import("~/state/filesystem"), async (original) => + (await import("./fileReadingTestMocks")).mockFilesystem(await original()), +); +vi.mock(import("~/state/use-atom-command"), async (original) => + (await import("./fileReadingTestMocks")).mockAtomCommand(await original()), +); +vi.mock(import("~/state/server"), async (original) => + (await import("./fileReadingTestMocks")).mockServer(await original()), +); +vi.mock(import("~/remoteOpen"), async (original) => + (await import("./fileReadingTestMocks")).mockRemoteOpen(await original()), +); +vi.mock(import("~/hooks/useTheme"), async (original) => + (await import("./fileReadingTestMocks")).mockTheme(await original()), +); +vi.mock(import("./projectFilesQueryState"), async (original) => + (await import("./fileReadingTestMocks")).mockProjectFiles(await original()), +); +vi.mock(import("~/hooks/useWorkspaceMutationRefresh"), async () => + (await import("./fileReadingTestMocks")).mockPanelParts.mutationRefresh(), +); +vi.mock(import("./FileBreadcrumbs"), async () => + (await import("./fileReadingTestMocks")).mockPanelParts.breadcrumbs(), +); +vi.mock(import("./FileBrowserPanel"), async () => + (await import("./fileReadingTestMocks")).mockPanelParts.browser(), +); +vi.mock(import("../DiffWorkerPoolProvider"), async () => + (await import("./fileReadingTestMocks")).mockPanelParts.diffWorkers(), +); +vi.mock(import("pdfjs-dist/build/pdf.worker.min.mjs?url"), async () => + (await import("./fileReadingTestMocks")).mockPdfWorkerUrl(), +); +vi.mock(import("./office/WordDocument"), async () => + (await import("./fileReadingTestMocks")).mockWordDocument(), +); + +import { ROWS_SELECTOR } from "./fileReadingPlace"; +import { readings } from "./fileReadingSave"; +import { + AttachmentPanel, + environmentId, + firstShown, + letterPdf, + pressMedia, + ReadingPanel, + readingSetup, + rowsView, + seekAsReader, + silentWav, + sourceView, + state, +} from "./fileReadingTestKit"; + +// PDF.js, Mermaid and the Office viewers load lazily and start their workers cold. +const COLD_VIEWER = { timeout: 10_000 }; + +function attachment(name: string, mimeType: string, text: string): ChatFileAttachment { + return { + type: "file", + id: `attachment-${name}` as ChatFileAttachment["id"], + name, + mimeType, + sizeBytes: text.length, + }; +} + +function flowchart(nodes: number) { + return `flowchart TD\n${Array.from({ length: nodes }, (_, index) => ` N${index} --> N${index + 1}`).join("\n")}`; +} + +/** + * A workbook of sheets of 300 numbered rows. Its first stored sheet is hidden, so it opens on + * "First", and cell A1 of "Second" links to row 280 of "First". + */ +function workbook() { + const sheets = [ + { name: "Hidden", state: ' state="hidden"', links: "" }, + { name: "First", state: "", links: "" }, + { + name: "Second", + state: "", + links: '', + }, + ]; + const archive = new JSZip(); + archive.file( + "xl/workbook.xml", + `${sheets + .map( + (sheet, index) => + ``, + ) + .join("")}`, + ); + archive.file( + "xl/_rels/workbook.xml.rels", + `${sheets + .map( + (_, index) => + ``, + ) + .join("")}`, + ); + const data = Array.from( + { length: 300 }, + (_, row) => `${row + 1}`, + ).join(""); + for (const [index, sheet] of sheets.entries()) { + archive.file( + `xl/worksheets/sheet${index + 1}.xml`, + `${data}${sheet.links}`, + ); + } + return archive.generateAsync({ type: "blob" }); +} + +/** An OpenDocument spreadsheet with two sheets of numbered rows; a sheet may have none. */ +function openDocumentSheet(north: number, south = 1) { + const rows = (count: number) => + Array.from( + { length: count }, + (_, index) => + `row ${index + 1}`, + ).join(""); + return new JSZip() + .file( + "content.xml", + `${rows(north)}${rows(south)}`, + ) + .generateAsync({ type: "blob" }); +} + +describe("every right-panel viewer keeps the reader's place", () => { + it("opens a PDF in PDF.js where the browser has its own viewer, at the reader's spot in a new width", async () => { + using reading = await readingSetup("pdf"); + using _native = vi.spyOn(navigator, "pdfViewerEnabled", "get").mockReturnValue(true); + // Each search of the viewer container for a scroller starts with the rows selector. + using queries = vi.spyOn(Element.prototype, "querySelector"); + const searches = () => queries.mock.calls.filter(([selector]) => selector === ROWS_SELECTOR); + const { panel, ref } = reading; + reading.serve("doc.pdf", letterPdf(6)); + panel.openFile(ref, "b.ts"); + panel.openFile(ref, "doc.pdf"); + await using screen = await render(); + const view = sourceView(screen.container); + const region = () => + screen.container.querySelector('[role="region"][aria-label="doc.pdf"]'); + /** How far down page 3 the viewport top is, as a share of the page's height. */ + const intoPage = () => { + const page = region()!.querySelector('canvas[aria-label="Page 3"]')!.getBoundingClientRect(); + return (region()!.getBoundingClientRect().top - page.top) / page.height; + }; + // PDF.js loads its worker and the document before the first page shows. + await vi.waitFor(() => expect(screen.container.textContent).toContain("1 / 6 pages"), { + timeout: 10_000, + }); + await userEvent.wheel(region()!, { delta: { y: 2000 } }); + await vi.waitFor(() => expect(reading.saved("doc.pdf", "pdf")?.page).toBe(2)); + const before = intoPage(); + expect(before).toBeGreaterThan(0.05); + + await act(() => panel.activateSurface(ref, "file:b.ts")); + await vi.waitFor(() => view.expectLineInView(1, "beta_1")); + await screen.rerender(); + await act(() => panel.activateSurface(ref, "file:doc.pdf")); + await vi.waitFor(() => expect(screen.container.textContent).toContain("3 / 6 pages"), { + timeout: 10_000, + }); + // PdfViewer scales the 16px gap above a page with the page; an unscaled offset misses by 0.1. + await vi.waitFor(() => expect(intoPage()).toBeCloseTo(before, 1)); + // PDF.js keeps its own place, so the container never searches it, even after a click. + await userEvent.click(region()!); + expect(searches()).toEqual([]); + }); + + it("returns a zoomed PDF to the reader's sideways pan", async () => { + using reading = await readingSetup("pdf-pan"); + using _native = vi.spyOn(navigator, "pdfViewerEnabled", "get").mockReturnValue(true); + const { panel, ref } = reading; + reading.serve("doc.pdf", letterPdf(2)); + readings.writePosition( + { kind: "path", environmentId, absolutePath: `${reading.cwd}/doc.pdf` }, + { view: "pdf", page: 0, offset: 0, zoom: 2, left: 0 }, + ); + panel.openFile(ref, "b.ts"); + panel.openFile(ref, "doc.pdf"); + await using screen = await render(); + const view = sourceView(screen.container); + const region = () => + screen.container.querySelector('[role="region"][aria-label="doc.pdf"]'); + await vi.waitFor(() => { + expect(region()!.scrollWidth).toBeGreaterThan(region()!.clientWidth + 300); + }, COLD_VIEWER); + await userEvent.wheel(region()!, { delta: { x: 200 } }); + await vi.waitFor(() => expect(reading.saved("doc.pdf", "pdf")?.left).toBeGreaterThan(100)); + const left = region()!.scrollLeft; + + await act(() => panel.activateSurface(ref, "file:b.ts")); + await vi.waitFor(() => view.expectLineInView(1, "beta_1")); + await act(() => panel.activateSurface(ref, "file:doc.pdf")); + await vi.waitFor(() => expect(region()?.scrollLeft).toBeCloseTo(left, 0), COLD_VIEWER); + }); + + it("returns a Word document to the reader's page after a tab switch", async () => { + using reading = await readingSetup("word"); + const { panel, ref } = reading; + const docx = await new JSZip() + .file( + "word/document.xml", + `Report`, + ) + .generateAsync({ type: "blob" }); + reading.serve("report.docx", docx); + panel.openFile(ref, "b.ts"); + panel.openFile(ref, "report.docx"); + await using screen = await render(); + const view = sourceView(screen.container); + const pages = () => screen.container.querySelector("[data-office-pages]"); + const shown = () => firstShown(pages()!, '[role="group"]'); + // The Office preparation worker starts cold. + await vi.waitFor(() => expect(pages()?.querySelectorAll('[role="group"]')).toHaveLength(40), { + timeout: 10_000, + }); + await userEvent.wheel(pages()!, { delta: { y: 9300 } }); + await vi.waitFor(() => + expect(reading.saved("report.docx", "rendered")?.block).toBeGreaterThan(5), + ); + const before = shown(); + + await act(() => panel.activateSurface(ref, "file:b.ts")); + await vi.waitFor(() => view.expectLineInView(1, "beta_1")); + await act(() => panel.activateSurface(ref, "file:report.docx")); + await vi.waitFor( + () => { + expect(shown().index).toBe(before.index); + expect(Math.abs(shown().offset - Math.max(0, before.offset))).toBeLessThanOrEqual(1); + }, + { timeout: 10_000 }, + ); + }); + + it("returns a notebook to the reader's cell after a tab switch", async () => { + using reading = await readingSetup("notebook"); + const { panel, ref } = reading; + const notebook = { + nbformat: 4, + metadata: { language_info: { name: "python" } }, + cells: Array.from({ length: 20 }, (_, cell) => ({ + cell_type: "markdown", + metadata: {}, + source: [`## Cell ${cell + 1}\n`, ...Array.from({ length: 12 }, () => "Some notes.\n\n")], + })), + }; + reading.serve("nb.ipynb", JSON.stringify(notebook)); + panel.openFile(ref, "b.ts"); + panel.openFile(ref, "nb.ipynb"); + await using screen = await render(); + const view = sourceView(screen.container); + const cells = () => + screen.container.querySelector("section[aria-label]")?.parentElement?.parentElement ?? null; + const shown = () => firstShown(cells()!, "section[aria-label]"); + // The notebook goes through the cold Office conversion worker. + await vi.waitFor(() => expect(cells()?.textContent).toContain("Cell 20"), { timeout: 10_000 }); + await userEvent.wheel(cells()!, { delta: { y: 4000 } }); + await vi.waitFor(() => expect(reading.saved("nb.ipynb", "rendered")?.block).toBeGreaterThan(3)); + const before = shown(); + + await act(() => panel.activateSurface(ref, "file:b.ts")); + await vi.waitFor(() => view.expectLineInView(1, "beta_1")); + await act(() => panel.activateSurface(ref, "file:nb.ipynb")); + await vi.waitFor( + () => { + expect(shown().index).toBe(before.index); + expect(Math.abs(shown().offset - Math.max(0, before.offset))).toBeLessThanOrEqual(1); + }, + { timeout: 10_000 }, + ); + }); + + it("returns a Mermaid diagram to the reader's offset after a tab switch", async () => { + using reading = await readingSetup("mermaid"); + const { panel, ref } = reading; + const diagram = `flowchart TD\n${Array.from({ length: 40 }, (_, index) => ` N${index} --> N${index + 1}`).join("\n")}`; + state.files["flow.mmd"] = { + relativePath: "flow.mmd", + contents: diagram, + byteLength: diagram.length, + truncated: false, + }; + panel.openFile(ref, "b.ts"); + panel.openFile(ref, "flow.mmd"); + await using screen = await render(); + const view = sourceView(screen.container); + const scroller = () => + screen.container.querySelector("[data-markdown-mermaid]")?.parentElement ?? null; + // The diagram renderer loads lazily. + await vi.waitFor( + () => { + const element = scroller(); + expect(element?.querySelector("svg")).toBeTruthy(); + expect(element!.scrollHeight).toBeGreaterThan(element!.clientHeight + 1500); + }, + { timeout: 10_000 }, + ); + await userEvent.wheel(scroller()!, { delta: { y: 1200 } }); + await vi.waitFor(() => expect(reading.saved("flow.mmd", "offset")?.top).toBeGreaterThan(1000)); + const before = scroller()!.scrollTop; + + await act(() => panel.activateSurface(ref, "file:b.ts")); + await vi.waitFor(() => view.expectLineInView(1, "beta_1")); + await act(() => panel.activateSurface(ref, "file:flow.mmd")); + await vi.waitFor(() => expect(scroller()?.scrollTop).toBeCloseTo(before, 0), { + timeout: 10_000, + }); + }); + + it("returns audio and video paused at the reader's time, and a video's first frame saves nothing", async () => { + using reading = await readingSetup("media"); + const { panel, ref } = reading; + reading.serve("talk.wav", silentWav()); + reading.serve("clip.mp4", silentWav()); + panel.openFile(ref, "b.ts"); + panel.openFile(ref, "clip.mp4"); + await using screen = await render(); + const view = sourceView(screen.container); + const media = (selector: string) => screen.container.querySelector(selector); + const clip = { kind: "path", environmentId, absolutePath: `${reading.cwd}/clip.mp4` } as const; + const saveInOtherTab = (name: string, at: number, time: number) => + localStorage.setItem( + `t3code.fileReading.v1:${JSON.stringify([environmentId, `${reading.cwd}/${name}`, "media"])}`, + JSON.stringify({ v: 1, at, view: "media", time }), + ); + + await vi.waitFor(() => expect(media("video")?.currentTime).toBeCloseTo(0.1, 2)); + await vi.waitFor(() => expect(media("video")?.seeking).toBe(false)); + expect(readings.readPosition(clip, "media")).toBeNull(); + // A time saved after the video loaded, as another tab saves one, stays when the reader + // presses the untouched video, such as to mute it. + readings.writePosition(clip, { view: "media", time: 5 }); + pressMedia(media("video")!); + await act(() => panel.activateSurface(ref, "file:b.ts")); + await vi.waitFor(() => view.expectLineInView(1, "beta_1")); + expect(reading.saved("clip.mp4", "media")?.time).toBe(5); + await act(() => panel.activateSurface(ref, "file:clip.mp4")); + await vi.waitFor(() => expect(media("video")?.currentTime).toBeCloseTo(5, 2)); + await seekAsReader(media("video")!, 3); + await vi.waitFor(() => expect(reading.saved("clip.mp4", "media")?.time).toBeCloseTo(3, 2)); + + // The reader's first press comes before the source loads, as a first tap on Play on iOS does; + // playback moves from there with no other press. + screen.container.addEventListener( + "loadstart", + (event) => pressMedia(event.target as HTMLMediaElement), + { capture: true, once: true }, + ); + await act(() => panel.openFile(ref, "talk.wav")); + await vi.waitFor(() => expect(media("audio")?.readyState).toBeGreaterThan(0)); + media("audio")!.currentTime = 4; + await vi.waitFor(() => expect(reading.saved("talk.wav", "media")?.time).toBeCloseTo(4, 2)); + // Playback moves on between saves, and the tab switch keeps where it got to. + const audio = media("audio")!; + audio.muted = true; + await audio.play(); + await vi.waitFor(() => expect(audio.currentTime).toBeGreaterThan(4.1)); + // A chat link opens the file that is already playing; the player stays and plays on. + await act(() => panel.openFile(ref, "talk.wav")); + expect(media("audio")).toBe(audio); + const reopened = audio.currentTime; + await vi.waitFor(() => expect(audio.currentTime).toBeGreaterThan(reopened + 0.3)); + const played = audio.currentTime; + + await act(() => panel.activateSurface(ref, "file:b.ts")); + await vi.waitFor(() => view.expectLineInView(1, "beta_1")); + const left = reading.saved("talk.wav", "media")!.time!; + expect(left).toBeGreaterThanOrEqual(played); + await act(() => panel.activateSurface(ref, "file:talk.wav")); + await vi.waitFor(() => { + expect(media("audio")?.currentTime).toBeCloseTo(left, 2); + expect(media("audio")?.paused).toBe(true); + }); + await seekAsReader(media("audio")!, 2); + await vi.waitFor(() => expect(reading.saved("talk.wav", "media")?.time).toBeCloseTo(2, 2)); + const replaced = media("audio")!; + await replaced.play(); + await vi.waitFor(() => expect(replaced.currentTime).toBeGreaterThan(2.3)); + replaced.pause(); + const paused = replaced.currentTime; + await vi.waitFor(() => expect(reading.saved("talk.wav", "media")?.time).toBeCloseTo(paused, 2)); + // The file changes while open; the player loads it into the same element, which resets it. + // The reader presses the player while the new source has no time yet. + replaced.addEventListener("emptied", () => pressMedia(replaced), { once: true }); + reading.serve("talk.wav", silentWav()); + await screen.rerender( + , + ); + await vi.waitFor(() => { + expect(media("audio")).toBe(replaced); + expect(replaced.src).toContain("edited"); + expect(replaced.readyState).toBeGreaterThan(0); + expect(replaced.currentTime).toBeCloseTo(paused, 2); + }); + expect(reading.saved("talk.wav", "media")?.time).toBeCloseTo(paused, 2); + // Another browser tab saves a newer time; leaving the paused player here keeps it. + saveInOtherTab("talk.wav", reading.saved("talk.wav", "media")!.at + 1, 7); + await act(() => panel.activateSurface(ref, "file:clip.mp4")); + await vi.waitFor(() => { + expect(media("video")?.currentTime).toBeCloseTo(3, 2); + expect(media("video")?.paused).toBe(true); + }); + const theirs = reading.saved("talk.wav", "media")!; + expect(theirs.time).toBe(7); + // Reopened at 7 s, a press that leaves playback there saves nothing over a newer time, also + // when the time reads a few milliseconds off, as an engine that seeks by frame may report it. + await act(() => panel.activateSurface(ref, "file:talk.wav")); + await vi.waitFor(() => expect(media("audio")?.currentTime).toBeCloseTo(7, 2)); + saveInOtherTab("talk.wav", theirs.at + 1, 8); + await seekAsReader(media("audio")!, 7.005); + await act(() => panel.activateSurface(ref, "file:clip.mp4")); + expect(reading.saved("talk.wav", "media")?.time).toBe(8); + }); + + it("returns a Markdown attachment, even one named like a PDF, to the reader's block after the panel remounts", async () => { + using reading = await readingSetup("attachment"); + const text = Array.from({ length: 80 }, (_, index) => `Paragraph ${index + 1}.`).join("\n\n"); + // The panel shows an attachment by its content type, whatever its name says. + const notes = attachment("report.pdf", "text/markdown", text); + reading.attach(notes, new Blob([text], { type: notes.mimeType })); + const panel = ; + await using screen = await render(panel); + const viewport = () => + screen.container + .querySelector(".chat-markdown") + ?.closest('[data-slot="scroll-area-viewport"]'); + await vi.waitFor(() => expect(viewport()?.textContent).toContain("Paragraph 80.")); + await userEvent.wheel(viewport()!, { delta: { y: 1500 } }); + await vi.waitFor(() => + expect(reading.attachmentSaved(notes.id, "rendered")?.block).toBeGreaterThan(10), + ); + const before = viewport()!.scrollTop; + + await screen.rerender(<>); + await screen.rerender( + , + ); + await vi.waitFor(() => expect(Math.abs(viewport()!.scrollTop - before)).toBeLessThanOrEqual(1)); + }); + + it("keeps a Mermaid attachment's diagram and source places apart", async () => { + using reading = await readingSetup("mermaid-attachment"); + const text = flowchart(40); + const flow = attachment("flow.mmd", "text/vnd.mermaid", text); + reading.attach(flow, new Blob([text], { type: flow.mimeType })); + const panel = () => ( + + ); + await using screen = await render(panel()); + const diagram = () => + screen.container.querySelector("[data-markdown-mermaid]")?.parentElement ?? null; + const source = () => screen.container.querySelector(".file-preview-virtualizer"); + // The diagram renderer loads lazily. + await vi.waitFor(() => { + const element = diagram(); + expect(element?.querySelector("svg")).toBeTruthy(); + expect(element!.scrollHeight).toBeGreaterThan(element!.clientHeight + 1500); + }, COLD_VIEWER); + await userEvent.wheel(diagram()!, { delta: { y: 1200 } }); + await vi.waitFor(() => + expect(reading.attachmentSaved(flow.id, "offset")?.top).toBeGreaterThan(1000), + ); + const before = diagram()!.scrollTop; + const saved = reading.attachmentSaved(flow.id, "offset"); + + await userEvent.click(screen.getByRole("button", { name: "Show Mermaid source" })); + await vi.waitFor(() => { + expect(source()!.scrollHeight).toBeGreaterThan(source()!.clientHeight + 300); + }); + await userEvent.wheel(source()!, { delta: { y: 400 } }); + await vi.waitFor(() => + expect(reading.attachmentSaved(flow.id, "source")?.offset).toBeGreaterThan(200), + ); + const sourceTop = source()!.scrollTop; + expect(reading.attachmentSaved(flow.id, "offset")).toEqual(saved); + + await screen.rerender(<>); + await screen.rerender(panel()); + await vi.waitFor(() => expect(diagram()?.scrollTop).toBeCloseTo(before, 0), COLD_VIEWER); + await userEvent.click(screen.getByRole("button", { name: "Show Mermaid source" })); + await vi.waitFor(() => expect(source()?.scrollTop).toBeCloseTo(sourceTop, 0)); + }); + + it("keeps a workbook's place on its first visible sheet, which another sheet and a cell link leave alone", async () => { + using reading = await readingSetup("excel"); + const { panel, ref } = reading; + const book = { kind: "path", environmentId, absolutePath: `${reading.cwd}/book.xlsx` } as const; + reading.serve("book.xlsx", await workbook()); + panel.openFile(ref, "b.ts"); + panel.openFile(ref, "book.xlsx"); + await using screen = await render(); + const grid = () => screen.container.querySelector('[role="grid"]'); + const showSheet = async (name: string) => { + await vi.waitFor(() => { + expect(grid()?.getAttribute("aria-label")).toBe(name); + expect(grid()!.scrollHeight).toBeGreaterThan(grid()!.clientHeight + 2000); + }, COLD_VIEWER); + }; + await showSheet("First"); + await userEvent.wheel(grid()!, { delta: { y: 1500 } }); + await vi.waitFor(() => expect(reading.saved("book.xlsx", "offset")?.top).toBeGreaterThan(1000)); + const before = grid()!.scrollTop; + const saved = readings.readPosition(book, "offset"); + + // A bare click presses nothing, so no look for a switched viewer runs from here. + const click = (name: string) => + act(() => + screen + .getByRole("tab", { name }) + .element() + .dispatchEvent(new MouseEvent("click", { bubbles: true })), + ); + await click("Second"); + await showSheet("Second"); + // A chat link opens the workbook again while it shows the second sheet. + await act(() => panel.openFile(ref, "book.xlsx")); + // Two wheels a frame apart, so a scroll the tracker took would be captured by now. + await userEvent.wheel(grid()!, { delta: { y: 700 } }); + await vi.waitFor(() => expect(grid()!.scrollTop).toBeGreaterThan(300)); + const second = grid()!.scrollTop; + await userEvent.wheel(grid()!, { delta: { y: 700 } }); + await vi.waitFor(() => expect(grid()!.scrollTop).toBeGreaterThan(second + 300)); + expect(readings.readPosition(book, "offset")).toEqual(saved); + + // The workbook mounts the first sheet's grid again, at the place the reader left it, also + // when the sheet shows after any look a press started has ended. + await click("First"); + await showSheet("First"); + await vi.waitFor(() => expect(grid()!.scrollTop).toBeCloseTo(before, 0)); + + // A link in the second sheet opens the first one at the linked row, not at the reader's place. + await userEvent.click(screen.getByRole("tab", { name: "Second" })); + await showSheet("Second"); + // The grid follows a link once it has laid out its cells, which is when it draws its headers. + await vi.waitFor(() => { + const canvas = grid()!.querySelector("canvas")!; + const [red, , , alpha] = canvas.getContext("2d")!.getImageData(1, 1, 1, 1).data; + expect(alpha === 255 && red! < 255).toBe(true); + }); + await userEvent.click(grid()!, { position: { x: 60, y: 30 } }); + await showSheet("First"); + await vi.waitFor(() => expect(grid()!.scrollTop).toBeGreaterThan(4000)); + await userEvent.wheel(grid()!, { delta: { y: 100 } }); + await vi.waitFor(() => expect(reading.saved("book.xlsx", "offset")?.top).toBeGreaterThan(4000)); + }); + + it("returns an OpenDocument spreadsheet's first sheet to the reader's row, which a later sheet leaves alone", async () => { + using reading = await readingSetup("ods"); + const { panel, ref } = reading; + reading.serve("book.ods", await openDocumentSheet(200)); + panel.openFile(ref, "b.ts"); + panel.openFile(ref, "book.ods"); + await using screen = await render(); + const view = sourceView(screen.container); + const rows = rowsView(screen.container); + await vi.waitFor(() => expect(rows.scroller()).toBeTruthy(), COLD_VIEWER); + await rows.wheel(2900); + await vi.waitFor(() => + expect(reading.saved("book.ods", "rendered")?.block).toBeGreaterThan(50), + ); + const before = rows.topRow(); + expect(before).toBe(reading.saved("book.ods", "rendered")?.block); + + await act(() => panel.activateSurface(ref, "file:b.ts")); + await vi.waitFor(() => view.expectLineInView(1, "beta_1")); + await act(() => panel.activateSurface(ref, "file:book.ods")); + await vi.waitFor(() => expect(rows.topRow()).toBe(before), COLD_VIEWER); + + // A revision empties the first sheet; reading the second one leaves the first sheet's row. + await act(() => panel.activateSurface(ref, "file:b.ts")); + await vi.waitFor(() => view.expectLineInView(1, "beta_1")); + const book = { kind: "path", environmentId, absolutePath: `${reading.cwd}/book.ods` } as const; + const saved = readings.readPosition(book, "rendered"); + reading.serve("book.ods", await openDocumentSheet(0, 200)); + await act(() => panel.activateSurface(ref, "file:book.ods")); + await expect.element(screen.getByText("This sheet is empty.")).toBeVisible(); + await userEvent.click(screen.getByRole("button", { name: "Next sheet" })); + await rows.wheel(700); + await vi.waitFor(() => expect(rows.topRow()).toBeGreaterThan(10)); + const second = rows.topRow()!; + await rows.wheel(700); + await vi.waitFor(() => expect(rows.topRow()).toBeGreaterThan(second + 10)); + expect(readings.readPosition(book, "rendered")).toEqual(saved); + }); +}); diff --git a/apps/web/src/components/files/office/Spreadsheet.tsx b/apps/web/src/components/files/office/Spreadsheet.tsx index 2b09e417dea5d..27b9fa1b68084 100644 --- a/apps/web/src/components/files/office/Spreadsheet.tsx +++ b/apps/web/src/components/files/office/Spreadsheet.tsx @@ -15,6 +15,7 @@ import { type Selection, type CellAddr, } from "@betteroffice/xlsx/headless"; +import { announceViewer } from "../fileReadingPlace"; import { FileSurfaceLoading, FileSurfaceNotice } from "../fileSurfaceChrome"; import type { WorkbookView } from "../officePreviewProtocol"; import { createWorkerClient, SupersededError } from "./worker-rpc"; @@ -115,6 +116,7 @@ export default function Spreadsheet(props: { key={opened.layout.info.activeSheet} engine={opened.engine} layout={opened.layout} + tabPosition={opened.tabs.findIndex((tab) => tab.index === opened.layout.info.activeSheet)} initialCell={opened.target} onNavigate={(name, cell) => { const tab = opened.tabs.find((tab) => tab.name.toLowerCase() === name.toLowerCase()); @@ -167,6 +169,7 @@ export default function Spreadsheet(props: { function SpreadsheetGrid(props: { engine: Client; layout: SheetLayout; + tabPosition: number; initialCell: CellAddr | undefined; onNavigate: (sheet: string, cell: CellAddr) => void; }) { @@ -691,6 +694,7 @@ function SpreadsheetGrid(props: { window.addEventListener("resize", schedule); resizeArea(); root.scrollTo({ left, top }); + announceViewer(root); if (initialCell) { selection = at(initialCell); reveal(); @@ -722,6 +726,8 @@ function SpreadsheetGrid(props: { tabIndex={0} role="grid" aria-label={layout.info.sheetNames[layout.info.activeSheet]} + data-sheet={props.tabPosition} + data-linked-cell={initialCell && `${columnName(initialCell.col)}${initialCell.row + 1}`} aria-readonly="true" aria-multiselectable="true" aria-rowcount={lastCell.row + 1} diff --git a/apps/web/src/components/files/pdfLayout.test.ts b/apps/web/src/components/files/pdfLayout.test.ts index d8bec95bc00b4..dd755d21c0b4d 100644 --- a/apps/web/src/components/files/pdfLayout.test.ts +++ b/apps/web/src/components/files/pdfLayout.test.ts @@ -79,9 +79,10 @@ describe("mixed-height page layout", () => { it("restores a saved spot scaled to the new width and kept inside pages that remain", () => { expect(pdfRestoredScrollTop(ratios, width, { page: 1, offset: 100 })).toBe(256); expect(pdfRestoredScrollTop(ratios, width * 2, { page: 1, offset: 100 }, width)).toBe(496); - // A page that shrank keeps the reader on its last pixel; a removed page falls back to the last. + // A page that shrank keeps the reader on its last pixel; a removed page restores to the end. expect(pdfRestoredScrollTop(ratios, width, { page: 2, offset: 600 })).toBe(964 + 305); - expect(pdfRestoredScrollTop(ratios.slice(0, 2), width, { page: 2, offset: 0 })).toBe(156); + expect(pdfRestoredScrollTop(ratios.slice(0, 2), width, { page: 2, offset: 0 })).toBe(980); + expect(pdfRestoredScrollTop(ratios.slice(0, 1), width, { page: 2, offset: 0 })).toBe(172); }); }); diff --git a/apps/web/src/components/files/pdfLayout.ts b/apps/web/src/components/files/pdfLayout.ts index 9292fcf583b46..613f99e8728e9 100644 --- a/apps/web/src/components/files/pdfLayout.ts +++ b/apps/web/src/components/files/pdfLayout.ts @@ -59,6 +59,7 @@ export function pdfRestoredScrollTop( ) { const page = Math.min(view.page, ratios.length - 1); const offsets = pdfPageOffsets(ratios, width); + if (view.page > page) return offsets[ratios.length]!; return ( offsets[page]! - PDF_PAGE_GAP +