DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
follow-up to #1351: clamp toward the band, do not subtract it
Bingxi Zhao (Frank)2e7cd8240977
diff --git a/web/components/watching/WatchingPane.tsx b/web/components/watching/WatchingPane.tsxindex 62217fa57..336030960 100644--- a/web/components/watching/WatchingPane.tsx+++ b/web/components/watching/WatchingPane.tsx@@ -40,6 +40,7 @@ import {import { stepTranscriptMatch, transcriptMatchIndexes } from '@/lib/transcript-search'import { videoTimeFromHref } from '@/lib/watching-citations'import { WatchingPlayer } from './WatchingPlayer'+import { transcriptFollowScrollTop } from '@/lib/transcript-follow'export const WATCHING_ASK_EVENT = 'dt:watching-ask'@@ -180,18 +181,18 @@ export function WatchingPane({ onClose }: { onClose(): void }) {const list = transcriptListRef.currentconst activeRow = list?.querySelector<HTMLButtonElement>('[data-active-cue="true"]')if (!list || !activeRow) return- const rowCenter =- activeRow.getBoundingClientRect().top -- list.getBoundingClientRect().top +- list.scrollTop -- list.clientHeight / 2 +- activeRow.clientHeight / 2- // Clamp the active row's centre to the middle 50% of the viewport so it- // sits near the caption overlay without jumping to the exact midpoint.- const upperMargin = list.clientHeight * 0.25- const lowerMargin = list.clientHeight * 0.75- const clampedTop = Math.min(Math.max(0, rowCenter - upperMargin), list.scrollHeight - list.clientHeight)- const targetTop = Math.max(0, Math.min(clampedTop, rowCenter - lowerMargin))+ const targetTop = transcriptFollowScrollTop({+ rowOffset:+ activeRow.getBoundingClientRect().top -+ list.getBoundingClientRect().top ++ list.scrollTop,+ rowHeight: activeRow.clientHeight,+ viewportHeight: list.clientHeight,+ contentHeight: list.scrollHeight,+ currentScrollTop: list.scrollTop,+ })+ // Already inside the band: leave the list where the reader put it.+ if (Math.abs(targetTop - list.scrollTop) < 1) returnconst prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matcheslist.scrollTo({top: targetTop,diff --git a/web/lib/transcript-follow.ts b/web/lib/transcript-follow.tsnew file mode 100644index 000000000..7f2b7156d--- /dev/null+++ b/web/lib/transcript-follow.ts@@ -0,0 +1,34 @@+/**+ * 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);+}diff --git a/web/tests/transcript-follow.test.ts b/web/tests/transcript-follow.test.tsnew file mode 100644index 000000000..a8af6da6e--- /dev/null+++ b/web/tests/transcript-follow.test.ts@@ -0,0 +1,81 @@+import assert from "node:assert/strict";+import test from "node:test";++import { transcriptFollowScrollTop } from "../lib/transcript-follow";++// A 400px-tall list over 4000px of cues; each row 40px. The band the active+// row may sit in without the list moving is 100px..300px of the viewport.+const list = { rowHeight: 40, viewportHeight: 400, contentHeight: 4000 };++/**+ * Where the row's centre lands on screen, given the scrollTop we chose. The+ * band is about the centre, which is why the assertions read it and not the+ * row's top edge.+ */+function viewportCentre(rowOffset: number, scrollTop: number): number {+ return rowOffset - scrollTop + list.rowHeight / 2;+}++test("a row already inside the middle band does not move the list", () => {+ // scrollTop 800 puts the row at 200px — dead centre of the band.+ const target = transcriptFollowScrollTop({+ ...list,+ rowOffset: 1000,+ currentScrollTop: 800,+ });+ assert.equal(target, 800);+});++test("a row below the band is brought to the band's lower edge, not off screen", () => {+ // scrollTop 640 puts the row at 360px, past the 300px edge.+ const target = transcriptFollowScrollTop({+ ...list,+ rowOffset: 1000,+ currentScrollTop: 640,+ });+ const landed = viewportCentre(1000, target);+ assert.ok(+ landed <= 300 && landed >= 100,+ `expected the row inside 100..300, got ${landed}`,+ );+ assert.equal(landed, 300);+});++test("a row above the band is brought to the band's upper edge", () => {+ // scrollTop 950 puts the row at 50px, above the 100px edge.+ const target = transcriptFollowScrollTop({+ ...list,+ rowOffset: 1000,+ currentScrollTop: 950,+ });+ assert.equal(viewportCentre(1000, target), 100);+});++test("early cues clamp at the top instead of scrolling past it", () => {+ const target = transcriptFollowScrollTop({+ ...list,+ rowOffset: 40,+ currentScrollTop: 0,+ });+ assert.equal(target, 0);+});++test("the last cue never scrolls past the end of the content", () => {+ const target = transcriptFollowScrollTop({+ ...list,+ rowOffset: 3960,+ currentScrollTop: 3600,+ });+ assert.equal(target, list.contentHeight - list.viewportHeight);+});++test("a short list that does not scroll stays at zero", () => {+ const target = transcriptFollowScrollTop({+ rowHeight: 40,+ viewportHeight: 400,+ contentHeight: 200,+ rowOffset: 160,+ currentScrollTop: 0,+ });+ assert.equal(target, 0);+});