Files
DeepTutor/web/lib/reading-display-preferences.ts
LaiYongqiang b29ffffe52 feat(reading): zoom the PDF reader
A PDF page was drawn at `clamp(panel - 48, 240, 1100)` with no zoom control, so
on a 1440px window roughly 735px of page sat inside a ~1165px pane — the page's
own white margins were still on screen, and the type was about a fifth smaller
than the pane could show.

Two modes. **Fit** is the historical behaviour, word for word the old width
formula, so nothing changes for a reader who never touches the bar. **Fixed** is
a percentage of the page's real size (96/72 of its `scale: 1` width — the same
reading Acrobat and Preview show), stepped along a ladder rather than by a fixed
percentage so the displayed number, the checked menu row and the next press all
describe the same size. Gestures: ⌘+/⌘−/⌘0, ctrl+wheel and pinch, each anchored
on the pointer so the page grows from where the reader is looking, in both axes.

The canvas budget is the part that is easy to get wrong. Safari silently drops a
bitmap past roughly 16.7M device pixels and answers with a blank page, so the
shared CSS width is clamped from page one's aspect ratio — and then each page
clamps its own bitmap again, because every page inherits that width while its
height follows its own ratio. The bitmap gives, never the layout, so the column
does not change size from page to page.

**Every step is measured against the width that actually landed.** The budget
clamps the drawn width, so a stored 4x on a tall page lands at 211%: multiplying
the *request* left the first several notches of a zoom-out drawing the same
width, and a zoom-in from there did nothing at all. The button step walks past
the stops the budget has flattened, the wheel gesture starts from what landed,
and the bar disables a control whose press cannot change the width. A zoom that
lands on the committed width drops its scroll anchor instead of leaving one
behind for the next pane resize to consume.

**A reader typing in a field keeps the keys.** The reader binds the same ⌘+ / ⌘−
/ ⌘0 the browser does, and with the pointer over a page those fired while the
reader typed in a note — `preventDefault` swallowed the browser's zoom, so the
field lost the keystroke and the page did not zoom either. The shortcut matcher
takes an `editableTarget` flag now, and `lib/shortcuts.ts` holds the one
predicate both readers use: the text reader binds the same three combinations and
had the same gap.

**The row a reader picks is the size they get.** `MIN_PAGE_WIDTH` (240px) is
above a Letter page's 25% (204px), so picking "25%" drew 29% while the toolbar
said 29% and the row still claimed 25%. Rows print the landed value.

The bar floats over the pane rather than inside the scroll container, so it does
not scroll away; its wrapper is transparent to pointers and only the toolbar
opts back in, or it would swallow wheel, selection and annotation clicks across
the bottom band of every page. It reveals on hover near that band and on focus —
`opacity-0` does not remove an element from the tab order. The menu is a radio
group (`menuitemradio` + `aria-checked`) with arrow keys, Home/End, and Escape
handled in the capture phase so it does not also reach the Guided Learning
handler — `preventDefault()` cannot stop a sibling listener on the same node. The
percentage is announced with `aria-live`, because it is the only feedback a step
produces for a reader who cannot see the page resize.

Safari reports a trackpad pinch as `gesturestart`/`gesturechange` rather than
ctrl+wheel, so those are cancelled on the pane: without it the browser zooms the
whole page. A zoom changed inside the 150ms debounce is flushed on `pagehide` and
on `visibilitychange`, not only on unmount — a closing tab or a backgrounded
phone never unmounts the component.

The bar's locale keys are filed with the reader's own display-control keys rather
than appended at the end of the catalog, which is what keeps this PR and the
lightbox PR from conflicting on the same files.
2026-11-11 18:31:38 +08:00

116 lines
3.2 KiB
TypeScript

import { browserStorage } from "@/shared/storage";
export type ReaderTheme = "auto" | "sepia" | "night";
export type EpubSpreadMode = "none" | "auto";
export interface ReaderDisplayPreferences {
fontSize: number;
lineWidth: number;
serif: boolean;
readerTheme: ReaderTheme;
spreadMode: EpubSpreadMode;
}
export const DEFAULT_FONT_SIZE = 17;
export const MIN_FONT_SIZE = 12;
export const MAX_FONT_SIZE = 28;
export const DEFAULT_LINE_WIDTH = 84;
export const MIN_LINE_WIDTH = 48;
export const MAX_LINE_WIDTH = 104;
export const READER_PREFS_KEY = "dt.reader.textPreferences";
export const DEFAULT_READER_DISPLAY_PREFERENCES: ReaderDisplayPreferences = {
fontSize: DEFAULT_FONT_SIZE,
lineWidth: DEFAULT_LINE_WIDTH,
serif: true,
readerTheme: "auto",
spreadMode: "none",
};
function bounded(
value: unknown,
fallback: number,
minimum: number,
maximum: number,
): number {
return typeof value === "number" && Number.isFinite(value)
? Math.min(maximum, Math.max(minimum, value))
: fallback;
}
export function normaliseReaderDisplayPreferences(
value: unknown,
): ReaderDisplayPreferences {
const row =
value && typeof value === "object"
? (value as Record<string, unknown>)
: {};
const readerTheme = ["auto", "sepia", "night"].includes(
String(row.readerTheme),
)
? (row.readerTheme as ReaderTheme)
: DEFAULT_READER_DISPLAY_PREFERENCES.readerTheme;
const spreadMode = row.spreadMode === "auto" ? "auto" : "none";
return {
fontSize: bounded(
row.fontSize,
DEFAULT_FONT_SIZE,
MIN_FONT_SIZE,
MAX_FONT_SIZE,
),
lineWidth: bounded(
row.lineWidth,
DEFAULT_LINE_WIDTH,
MIN_LINE_WIDTH,
MAX_LINE_WIDTH,
),
serif:
typeof row.serif === "boolean"
? row.serif
: DEFAULT_READER_DISPLAY_PREFERENCES.serif,
readerTheme,
spreadMode,
};
}
export function loadReaderDisplayPreferences(): ReaderDisplayPreferences {
try {
return normaliseReaderDisplayPreferences(
JSON.parse(browserStorage.readRaw("local", READER_PREFS_KEY) || "{}"),
);
} catch {
return DEFAULT_READER_DISPLAY_PREFERENCES;
}
}
export function saveReaderDisplayPreferences(
preferences: ReaderDisplayPreferences,
): void {
browserStorage.writeRaw("local", READER_PREFS_KEY, JSON.stringify(preferences));
}
export type ReaderDisplayShortcut = "increase" | "decrease" | "reset";
export function readerDisplayShortcut(input: {
key: string;
modifier: boolean;
readerHovered: boolean;
readerFocused: boolean;
/**
* The event is going into a field the reader must not take the keys from.
*
* The text reader binds the same ⌘+ / ⌘− / ⌘0 combinations as the PDF reader,
* so it had the same gap: with the pointer over the reader, typing in a note
* lost the keystroke to a font-size change and the browser's own zoom was
* swallowed with it.
*/
editableTarget: boolean;
}): ReaderDisplayShortcut | null {
if (input.editableTarget) return null;
if (!input.modifier || (!input.readerHovered && !input.readerFocused))
return null;
if (input.key === "+" || input.key === "=") return "increase";
if (input.key === "-") return "decrease";
if (input.key === "0") return "reset";
return null;
}