Files
DeepTutor/web/lib/shortcuts.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

70 lines
2.9 KiB
TypeScript

/**
* Keyboard shortcuts: one description used both to match a key event and to
* print the keys in a tooltip, so the two can never disagree.
*/
export interface Shortcut {
/** A letter or digit, e.g. "K" or "1". */
key: string
/** ⌘ on macOS, Ctrl elsewhere. */
mod?: boolean
alt?: boolean
shift?: boolean
}
export function isMacPlatform(): boolean {
if (typeof navigator === 'undefined') return false
return /Mac|iPhone|iPad|iPod/.test(navigator.platform || navigator.userAgent)
}
/** The keys as printed: ⌘ ⌥ ⇧ K on macOS, Ctrl Alt Shift K elsewhere. */
export function shortcutKeys(shortcut: Shortcut, mac = isMacPlatform()): string[] {
const keys: string[] = []
if (shortcut.mod) keys.push(mac ? '⌘' : 'Ctrl')
if (shortcut.alt) keys.push(mac ? '⌥' : 'Alt')
if (shortcut.shift) keys.push(mac ? '⇧' : 'Shift')
keys.push(shortcut.key.toUpperCase())
return keys
}
function physicalCode(key: string): string {
return /^\d$/.test(key) ? `Digit${key}` : `Key${key.toUpperCase()}`
}
export function matchesShortcut(event: KeyboardEvent, shortcut: Shortcut, mac = isMacPlatform()): boolean {
const mod = mac ? event.metaKey : event.ctrlKey
if (Boolean(shortcut.mod) !== mod) return false
if (Boolean(shortcut.alt) !== event.altKey || Boolean(shortcut.shift) !== event.shiftKey) return false
// ⌥ rewrites event.key on macOS (⌥1 is "¡"), so compare the physical key.
return event.code === physicalCode(shortcut.key)
}
/** The modern shell's shortcuts, in one place. */
export const SHORTCUTS = {
search: { key: 'K', mod: true },
newChat: { key: 'O', mod: true, shift: true },
/** A side chat with Dito about the open conversation, in the viewer. */
sideChat: { key: 'S', mod: true, alt: true },
category: (index: number): Shortcut => ({ key: String(index + 1), mod: true, alt: true }),
} as const
/**
* Whether a key event is going into something the reader must not steal it from.
*
* A reader shortcut is bound to a modifier combination that the browser and the
* page's own inputs also use: ⌘+ is both "zoom the page" and "grow the type",
* and ⌘− is both "zoom out" and "shrink it". With the pointer parked over the
* reader those bindings fire while the reader types in a note, a search box or
* any other field, and `preventDefault` swallows the browser's own zoom — so the
* field loses the keystroke and the page does not zoom either.
*
* Kept here rather than in each reader so the two cannot drift: the PDF reader
* and the text reader bind the same three combinations and had the same gap.
*/
export function isEditableTarget(target: EventTarget | null): boolean {
const element = target as HTMLElement | null
if (!element || typeof element.closest !== 'function') return false
if (element.isContentEditable) return true
return Boolean(element.closest('input, textarea, select, [contenteditable="true"]'))
}