diff --git a/e2e/notebookScroll.spec.ts b/e2e/notebookScroll.spec.ts new file mode 100644 index 0000000000..6f5b84e19c --- /dev/null +++ b/e2e/notebookScroll.spec.ts @@ -0,0 +1,128 @@ +import type { Page } from "@playwright/test"; +import { expect, openFromTree, stubDesktop, test } from "./fixtures"; + +const NOTEBOOK_PATH = "/dev/acme-api/scroll-repro.ipynb"; + +async function openNotebook(page: Page) { + await openFromTree(page, "README.md"); + await stubDesktop(page); + await page.evaluate(async (path) => { + const snapshot = { + id: "scroll-repro", + path, + version: 1, + dirty: false, + nbformat: 4, + nbformatMinor: 5, + metadata: {}, + canUndo: false, + canRedo: false, + cells: Array.from({ length: 100 }, (_, i) => ({ + id: `cell-${i + 1}`, + cellType: "code", + source: `# Cell ${i + 1}\nvalue_${i + 1} = ${i + 1}`, + metadata: {}, + attachments: {}, + executionCount: null, + outputs: [], + })), + }; + Object.assign(window.__ipc.handlers, { + notebook_open: () => snapshot, + notebook_get: () => snapshot, + notebook_cell_docs: () => [], + sdk_detect: () => [], + sdk_python_effective: () => null, + jupyter_servers: () => [], + }); + const module = "/src/workbench/contributions.ts"; + const { editorService } = await import(/* @vite-ignore */ module); + await editorService().openFile(path, { preview: false }); + }, NOTEBOOK_PATH); + await expect(page.getByRole("region", { name: "Cell 1: code", exact: true })).toBeVisible(); +} + +function notebookScroll(page: Page) { + return page + .getByRole("toolbar", { name: "Notebook toolbar" }) + .locator("..") + .locator(":scope > div") + .filter({ has: page.locator("[data-index]") }); +} + +async function selectCell36(page: Page) { + const scroll = notebookScroll(page); + const cell = page.getByRole("region", { name: "Cell 36: code", exact: true }); + // Visit the intervening cells so their measured heights differ from the estimates on remount. + await expect + .poll( + async () => { + if (await cell.count()) return true; + await scroll.evaluate((el) => (el.scrollTop += 300)); + return false; + }, + { intervals: [50], timeout: 15_000 }, + ) + .toBe(true); + await cell.locator(".cm-content").click(); + await expect(cell).toHaveClass(/selected/); + return cell; +} + +async function viewport(page: Page) { + return notebookScroll(page).evaluate((el) => { + const top = el.getBoundingClientRect().top; + const first = [...el.querySelectorAll("[data-index]")].find( + (cell) => cell.getBoundingClientRect().bottom > top, + )!; + return { + first: first.dataset.index, + offset: Math.round(first.getBoundingClientRect().top - top), + scrollTop: Math.round(el.scrollTop), + }; + }); +} + +for (const via of ["tab", "back"] as const) { + test(`notebook keeps its viewport and selected cell when returning via ${via}`, async ({ workbench: page }) => { + await openNotebook(page); + const cell = await selectCell36(page); + const before = await viewport(page); + const tabs = page.getByRole("tablist", { name: "Editor tabs" }); + + for (let round = 0; round < 2; round++) { + await tabs.getByRole("tab", { name: "README.md", exact: true }).click(); + await expect(page.getByRole("toolbar", { name: "Notebook toolbar" })).toHaveCount(0); + if (via === "back") await page.keyboard.press("Meta+["); + else await tabs.getByRole("tab", { name: "scroll-repro.ipynb", exact: true }).click(); + + await expect(cell).toBeInViewport(); + await expect(cell).toHaveClass(/selected/); + await expect.poll(() => viewport(page)).toEqual(before); + } + }); +} + +test("notebook restores the viewport independently of selection and still reveals new locations", async ({ + workbench: page, +}) => { + await openNotebook(page); + const selected = await selectCell36(page); + await notebookScroll(page).evaluate((el) => (el.scrollTop += 1200)); + await expect(selected).not.toBeInViewport(); + const before = await viewport(page); + const tabs = page.getByRole("tablist", { name: "Editor tabs" }); + await tabs.getByRole("tab", { name: "README.md", exact: true }).click(); + await tabs.getByRole("tab", { name: "scroll-repro.ipynb", exact: true }).click(); + await expect.poll(() => viewport(page)).toEqual(before); + await expect(selected).not.toBeInViewport(); + + await page.evaluate(async (path) => { + const module = "/src/features/notebook/session.ts"; + const { existingNotebookSession } = await import(/* @vite-ignore */ module); + existingNotebookSession(path).reveal("cell-70"); + }, NOTEBOOK_PATH); + const revealed = page.getByRole("region", { name: "Cell 70: code", exact: true }); + await expect(revealed).toBeInViewport(); + await expect(revealed).toHaveClass(/selected/); +}); diff --git a/src/features/notebook/NotebookEditor.tsx b/src/features/notebook/NotebookEditor.tsx index 71bd47fa8b..4bcd90e9c9 100644 --- a/src/features/notebook/NotebookEditor.tsx +++ b/src/features/notebook/NotebookEditor.tsx @@ -65,7 +65,14 @@ function NotebookDocument({ session }: { session: NotebookSession }) { estimateSize: () => 220, overscan: 3, initialOffset: session.scrollTop, + initialMeasurementsCache: session.measurements, }); + useLayoutEffect( + () => () => { + session.measurements = virtualizer.takeSnapshot(); + }, + [session, virtualizer], + ); useEffect(() => { invoke(session.load()); }, [session, invoke]); diff --git a/src/features/notebook/session.ts b/src/features/notebook/session.ts index 109bfdd7b9..5b99e04b83 100644 --- a/src/features/notebook/session.ts +++ b/src/features/notebook/session.ts @@ -1,5 +1,6 @@ import { Channel } from "@tauri-apps/api/core"; import type { EditorState } from "@codemirror/state"; +import type { VirtualItem } from "@tanstack/react-virtual"; import type { JupyterServer, NotebookCellDoc, @@ -165,6 +166,8 @@ export class NotebookSession { /** The cell whose editor holds DOM focus, tracked so history can put its caret back. */ focusedCell: string | null = null; scrollTop = 0; + /** Measured cell heights must travel with the scroll offset when a tab remounts. */ + measurements: VirtualItem[] = []; /** The last `reveal` request a cell editor carried out. */ revealed = 0;