Skip to content

feat: reopen every document where the reader left it - #48

Merged
bencode merged 1 commit into
mainfrom
feat/reading-position
Oct 1, 2026
Merged

bencode merged 1 commit into
mainfrom
feat/reading-position

Conversation

@bencode

@bencode bencode commented Oct 1, 2026

Copy link
Copy Markdown
Owner

Closes #40

What

Format Position kept
PDF page
Markdown, Lab, Word, plain text, code top-level block + share of it scrolled past
Excel sheet + scroll offset (rows have a fixed height)

HTML, p5 sketches and images are unchanged.

Text that reflows is anchored to a block, not to pixels: after the width or text size changes, the same paragraph is on top. Images, formulas and diagrams render after the text, so the anchor is applied again on each content resize until the reader's first wheel, touch, key or pointer input.

Shape

  • Readers are pure. They take defaultPosition once on mount and report through onPositionChange, the same contract as an uncontrolled input's defaultValue and onChange. The shared mutable scrollPositions map that readers wrote into is removed.
  • State. Positions sit in the workspace store next to tabs. setPosition and closeDocuments are pure reducers.
  • Single I/O point. use-workspace already saves the tabs. It now also writes positions, under the same gamma-reader.workspace key, with a 1 s debounce and an immediate write on pagehide and unmount. No new storage key, so project deletion cleanup already covers it.
  • Pruning. Only positions for open tabs are written. Stored entries are validated one by one, and an unreadable entry costs only itself.
  • Two browser tabs on one project. The last write wins, exactly as for the tab list today.
  • Anchor math. It lives in core/reading-position.ts as pure functions with unit tests. All DOM timing is contained in markdown-reader/use-flow-position.ts.

Checks

  • pnpm check passes: 246 unit and 50 integration tests.
    • New tests: the anchor round-trip under reflow, and the integration case "reopens a document where the reader left it after a reload".
    • The existing in-session tab-switch scroll tests pass unchanged.
  • In a browser against the dev server:
    • A PDF on page 3 reopens on page 3 after a reload.
    • Start here.md scrolled to block 10 at a 678px width reopens on block 10 after narrowing to 495px. The pixel offset moved from 665 to 804.
    • The spreadsheet reopens on its second sheet.
    • A closed and reopened tab starts at the top.
  • Not exercised: a slow-loading image above the saved position. The resize re-apply covers it, but the headless check had no such document.

A PDF returns to its page, a sheet to its tab and row, and text that
reflows — Markdown, Labs, Word, plain text and code — to the block and the
share of it the reader had scrolled past, which survives a change of width
or text size where a pixel offset would not. Images and diagrams that
finish after the text push it down, so the place is applied again as the
content settles, until the reader does anything.

Readers stay pure: each takes a defaultPosition when it mounts and reports
onPositionChange, like an uncontrolled input. The shared mutable scroll map
they used to write is gone. Positions live in the workspace store beside
the tabs and are written by the one place that already saves the tabs,
under the same key, a second after the reader pauses and before the page
goes away. A position lasts as long as its tab.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remember where the reader stopped — reopen documents at their page or scroll position

1 participant