fix(video): animate transcript playback scrolling

This commit is contained in:
yiling
2026-12-03 18:44:17 +08:00
parent ce4ae6e33e
commit cdd45429c4
5 changed files with 183 additions and 36 deletions
@@ -14,6 +14,7 @@ import { useQuery } from '@tanstack/react-query'
import { ChevronDown, ChevronUp, LocateFixed, Search, X } from 'lucide-react'
import { memo, useEffect, useLayoutEffect, useMemo, useRef, useState, type RefObject } from 'react'
import { createTranscriptTimeline } from './transcript-timeline'
import { useTranscriptScroll } from './use-transcript-scroll'
interface TranscriptPanelProps {
id: string
@@ -148,8 +149,8 @@ export const TranscriptPanel = memo(function TranscriptPanel({
const [activeId, setActiveId] = useState<string>()
const toggleSearchRef = useRef<HTMLButtonElement>(null)
const closeRef = useRef<HTMLButtonElement>(null)
const viewportRef = useRef<HTMLDivElement>(null)
const scrollPosition = useRef(0)
const { viewportRef, setViewport, scrollTo, cancelScroll, onScroll, onWheel } =
useTranscriptScroll(() => setFollowing(false))
const transcript = useQuery({
queryKey: [...savedTranscriptLanguagesKey(assetId), language],
@@ -181,31 +182,6 @@ export const TranscriptPanel = memo(function TranscriptPanel({
})
}, [activeAt, mediaControllerRef])
function scrollTo(
target: HTMLElement | null | undefined,
{ bottomInset = 0, followPlayback = false } = {},
) {
const viewport = viewportRef.current
if (!viewport || !target) return
const item = target.getBoundingClientRect()
const { top, bottom } = viewport.getBoundingClientRect()
if (followPlayback) {
const height = bottom - top
const midpoint = item.top + item.height / 2
if (item.top < top || midpoint >= top + height * 0.75) {
viewport.scrollTop += item.top - top - Math.max(0, (height - item.height) / 2)
}
} else {
const visibleBottom = bottom - bottomInset
if (item.top < top || item.height > visibleBottom - top) {
viewport.scrollTop += item.top - top
} else if (item.bottom > visibleBottom) {
viewport.scrollTop += item.bottom - visibleBottom
}
}
scrollPosition.current = viewport.scrollTop
}
useLayoutEffect(() => {
if (following && activeId) {
scrollTo(viewportRef.current?.querySelector(`[data-segment-id="${CSS.escape(activeId)}"]`), {
@@ -259,6 +235,7 @@ export const TranscriptPanel = memo(function TranscriptPanel({
aria-label={m.language()}
value={language}
onChange={(event) => {
cancelScroll()
setPreferredLanguage(event.target.value)
setMatchIndex(0)
setFollowing(true)
@@ -370,15 +347,11 @@ export const TranscriptPanel = memo(function TranscriptPanel({
<ScrollArea
className="h-full [&>div>div]:block!"
viewportProps={{
ref: viewportRef,
ref: setViewport,
tabIndex: 0,
className: '[overflow-anchor:none]',
onScroll: (event) => {
const position = event.currentTarget.scrollTop
if (position === scrollPosition.current) return
scrollPosition.current = position
setFollowing(false)
},
onScroll,
onWheel,
}}
>
<ol className="space-y-0.5 px-2 pt-2 pb-14">