Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Transcript scroll: shared content-visibility-aware converge-on-measured-position helper for main panel + speaker modal (fix overshoot)
Murtaza Nasir6f45d449375e
diff --git a/static/js/app.modular.js b/static/js/app.modular.jsindex ce90cb74..ee1fe4cb 100644--- a/static/js/app.modular.js+++ b/static/js/app.modular.js@@ -2366,17 +2366,51 @@ document.addEventListener('DOMContentLoaded', async () => {// The main transcript panel STILL uses virtual scrolling// (smaller panel, different perf characteristics, no Prev/Next// nav buttons to expose the variable-height inaccuracy).+ // Robust scroll-to-element for the transcript. Both the main panel+ // and the speaker modal use content-visibility:auto with an+ // ESTIMATED 60px intrinsic size for off-screen segments (paint+ // perf). A single scrollIntoView is inaccurate for a far jump: it+ // targets the estimate, and the act of scrolling then renders the+ // real (taller) heights, which shifts the target out from under the+ // animation — the "moving target" — so it overshoots and only+ // settles on a later change. This converges on the REAL measured+ // position by re-centering INSTANTLY each frame until the target+ // stops moving. A near target (normal playback advancing one+ // segment) is already rendered, so a single smooth scroll is+ // accurate and keeps the follow animation smooth.+ const scrollSegmentIntoView = (el) => {+ if (!el) return;+ const r0 = el.getBoundingClientRect();+ const near = r0.bottom > -50 && r0.top < window.innerHeight + 50;+ if (near) {+ el.scrollIntoView({ block: 'center', behavior: 'smooth' });+ return;+ }+ let attempts = 0;+ let lastTop = null;+ const step = () => {+ if (!el.isConnected) return;+ el.scrollIntoView({ block: 'center', behavior: 'auto' });+ const top = el.getBoundingClientRect().top;+ attempts += 1;+ if ((lastTop !== null && Math.abs(top - lastTop) < 1) || attempts >= 8) return;+ lastTop = top;+ requestAnimationFrame(step);+ };+ requestAnimationFrame(step);+ };+ utils.scrollSegmentIntoView = scrollSegmentIntoView;+const scrollToSegmentIndex = (index) => {+ let el = null;if (showSpeakerModal.value) {const container = speakerModalTranscriptRef.value;if (!container) return;- const target = container.querySelector(`[data-segment-index="${index}"]`);- if (target) {- target.scrollIntoView({ block: 'center', behavior: 'smooth' });- }+ el = container.querySelector(`[data-segment-index="${index}"]`);} else {- mainTranscriptVirtualScroll.scrollToIndex(index, 'smooth');+ el = document.querySelector(`.transcript-segment[data-segment-index="${index}"], .speaker-segment[data-segment-index="${index}"], .speaker-bubble[data-segment-index="${index}"]`);}+ if (el) scrollSegmentIntoView(el);};// Add scrollToSegmentIndex to utils for composables that need itdiff --git a/static/js/modules/composables/ui.js b/static/js/modules/composables/ui.jsindex 10f85310..f118546a 100644--- a/static/js/modules/composables/ui.js+++ b/static/js/modules/composables/ui.js@@ -1420,27 +1420,14 @@ export function useUI(state, utils, processedTranscription) {const el = document.querySelector(sel);if (!el) return;- // The transcript renders ALL segments but uses content-visibility:auto- // with an estimated 60px intrinsic size for off-screen ones (for paint- // perf). A single long-distance scrollIntoView therefore targets that- // ESTIMATE and lands minutes off (real paragraphs are taller than- // 60px), only self-correcting on the next segment change. For a far- // jump (a seek), scroll INSTANTLY first — that forces content-visibility- // to render the real heights around the target — then re-center with a- // smooth scroll on the next frame using the now-measured layout. For a- // near target (normal playback advancing one segment) a single smooth- // scroll is already accurate, so skip the two-phase to avoid a jolt.- const rect = el.getBoundingClientRect();- const far = rect.bottom < -200 || rect.top > window.innerHeight + 200;- if (!far) {+ // Use the shared content-visibility-aware scroll: it converges on the+ // real measured position for far jumps (seeks) so it doesn't overshoot,+ // and uses a single smooth scroll for near targets (normal playback).+ if (utils.scrollSegmentIntoView) {+ utils.scrollSegmentIntoView(el);+ } else {el.scrollIntoView({ behavior: 'smooth', block: 'center' });- return;}- el.scrollIntoView({ behavior: 'auto', block: 'center' });- requestAnimationFrame(() => requestAnimationFrame(() => {- const real = document.querySelector(sel);- if (real) real.scrollIntoView({ behavior: 'smooth', block: 'center' });- }));};const toggleFollowPlayerMode = () => {