feat(video): resize transcripts and follow playback earlier

This commit is contained in:
yiling
2026-12-03 16:30:53 +08:00
parent 543862ff9b
commit ce4ae6e33e
10 changed files with 236 additions and 30 deletions
@@ -181,30 +181,43 @@ export const TranscriptPanel = memo(function TranscriptPanel({
})
}, [activeAt, mediaControllerRef])
function scrollTo(target: HTMLElement | null | undefined, bottomInset = 0) {
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()
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
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)}"]`))
scrollTo(viewportRef.current?.querySelector(`[data-segment-id="${CSS.escape(activeId)}"]`), {
followPlayback: true,
})
}
}, [activeId, following])
useLayoutEffect(() => {
if (selectedMatch === undefined) return
setFollowing(false)
scrollTo(viewportRef.current?.querySelector('[data-current-match]'), 48)
scrollTo(viewportRef.current?.querySelector('[data-current-match]'), { bottomInset: 48 })
}, [matches, selectedMatch])
function toggleSearch() {
@@ -221,7 +234,7 @@ export const TranscriptPanel = memo(function TranscriptPanel({
setFollowing(false)
setMatchIndex((selectedMatch + step + matches.total) % matches.total)
if (matches.total === 1) {
scrollTo(viewportRef.current?.querySelector('[data-current-match]'), 48)
scrollTo(viewportRef.current?.querySelector('[data-current-match]'), { bottomInset: 48 })
}
}