Files
DeepTutor/web/lib/transcript-follow.ts
Bingxi Zhao (Frank) 2e7cd82409 follow-up to #1351: clamp toward the band, do not subtract it
The clamp read `min(clampedTop, rowCenter - lowerMargin)`, and
`rowCenter - 0.75h` is always the smaller of the two, so every cue
subtracted three quarters of a viewport from the centring offset — putting
the active row below the bottom edge, or pinned at the top for early cues.
The intent was to move only when the row leaves the middle 50%.

The arithmetic *is* the behaviour, so it moves to a pure
`transcriptFollowScrollTop` with the six cases pinned: inside the band
(no scroll at all, so the list stops creeping under the reader), above it,
below it, first cue, last cue, and a list too short to scroll.

(cherry picked from commit 0e51f652024d755460ea122447548d40e9285c16)
2026-09-10 23:55:18 +08:00

35 lines
1.7 KiB
TypeScript

/**
* Where to scroll a transcript list so the active caption row stays readable.
*
* Following captions is not the same as centring them. A row already
* comfortably in view must not be nudged on every cue, or the list creeps
* continuously while the reader is trying to read it. So the row is given a
* band — the middle half of the viewport — and the list moves only when the
* row would leave that band, and then only as far as the nearest edge of it.
*
* Kept as a pure function because the arithmetic is the whole behaviour: the
* first attempt at this subtracted the band from the centring offset
* unconditionally, which pushed the active row three quarters of a viewport
* below where it belonged (off screen, or pinned to the top for early cues).
*/
export function transcriptFollowScrollTop(metrics: {
/** The row's offset from the top of the list's scrollable content. */
rowOffset: number;
rowHeight: number;
/** The list's visible height (``clientHeight``). */
viewportHeight: number;
/** The full scrollable height (``scrollHeight``). */
contentHeight: number;
currentScrollTop: number;
}): number {
const { rowOffset, rowHeight, viewportHeight, contentHeight, currentScrollTop } = metrics;
// The scrollTop that puts the row's centre at the viewport's centre.
const centred = rowOffset - viewportHeight / 2 + rowHeight / 2;
// A larger scrollTop moves the row *up*, so these two positions are where
// the row sits at 75% and 25% of the viewport respectively.
const band = viewportHeight * 0.25;
const nearest = Math.min(Math.max(currentScrollTop, centred - band), centred + band);
const maxScrollTop = Math.max(0, contentHeight - viewportHeight);
return Math.min(Math.max(0, nearest), maxScrollTop);
}