2026-09-23·作者 Sijie Wang#lucerna#software#architecture

raw-display-cursor

The dual-coordinate backbone (raw ↔ display)

Parent: key-designs

The single most load-bearing idea in the reader. Every persisted CachedSentence (core/src/entities/CachedSentence.ts) carries two coordinate systems:

  • rawStart / rawText — offsets into the source text (where tokenization, lemmatization, and stable identity live);
  • displayStart / displayText — offsets into the sanitized text the user actually reads (whitespace collapsed, soft-hyphens/markup removed);
  • displayToRaw[] — a per-sentence map converting a display offset back to a raw offset (sentenceDisplayToBookRaw / sentenceBookRawToDisplay with binary search).

The rule: every durable position — a bookmark, a highlight's startOffset/endOffset, a saved word's rawStart, the reading Anchor — is stored as a book-absolute raw offset; the display layer is reconstructed on render (Page.ts composes page-local ↔ book-raw via pageDisplayToRaw/pageRawToDisplay).

Why this is the backbone: a reader re-paginates constantly (font size, viewport, window resize), and the sanitizer changes the displayed characters. If positions were stored as display offsets or page numbers, every note/highlight/vocab anchor would drift the moment layout changed. Anchoring to raw source offsets makes them layout-independent — the whole point of a reading app where users mark things and come back. Anchor even stores a ≤200-char display prefix and re-resolves it to a sentenceIdx by prefix-match, so position survives even if offsets shift.

The (raw, display, displayToRaw) triple is built per sentence by services/sentenceSanitize.ts and persisted through the sentence cache (an in-memory building map committed atomically to sentences_real, version-gated by SANITIZER_VERSION + PAGINATION_VERSION so a logic bump invalidates stale rows). This is why it is a typed value-object boundary, not scattered offset arithmetic: the conversion is non-trivial and one-directionally lossy.

關於這一條

sijie 的一條 wiki 條目。本站的 AI 以同一套語料庫為依據、以 sijie 的口吻作答,並附有指向此類條目的引用——回答會花 sijie 的錢,所以它在存取碼後等待: 輸入存取碼 →