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
@@ -0,0 +1,112 @@
import { ResizeHandle } from '@/ui/components/resize-handle'
import { m } from '@/ui/paraglide/messages.js'
import {
useCallback,
useLayoutEffect,
useRef,
useState,
type ComponentProps,
type Dispatch,
type SetStateAction,
} from 'react'
import { TranscriptPanel } from './transcript-panel'
const minTranscriptWidth = 20
const maxTranscriptWidth = 45
const preferredPlayerWidth = 256
const keyboardSteps = new Map([
['ArrowLeft', -1],
['ArrowRight', 1],
['Home', -maxTranscriptWidth],
['End', maxTranscriptWidth],
])
interface ResizableTranscriptPanelProps extends Omit<
ComponentProps<typeof TranscriptPanel>,
'className'
> {
widthPercent: number
setWidthPercent: Dispatch<SetStateAction<number>>
}
export function ResizableTranscriptPanel({
widthPercent,
setWidthPercent,
...panelProps
}: ResizableTranscriptPanelProps) {
const sheetRef = useRef<HTMLDivElement>(null)
const [maxWidthPercent, setMaxWidthPercent] = useState(maxTranscriptWidth)
const minWidthPercent = Math.min(minTranscriptWidth, maxWidthPercent)
useLayoutEffect(() => {
const viewer = sheetRef.current?.parentElement
if (!viewer) return
const updateLimit = () => {
const viewerWidth = viewer.clientWidth
const playerWidth = Math.min(preferredPlayerWidth, viewerWidth / 2)
const availablePercent = ((viewerWidth - playerWidth) / window.innerWidth) * 100
setMaxWidthPercent(Math.min(maxTranscriptWidth, availablePercent))
}
updateLimit()
const observer = new ResizeObserver(updateLimit)
observer.observe(viewer)
window.addEventListener('resize', updateLimit)
return () => {
observer.disconnect()
window.removeEventListener('resize', updateLimit)
}
}, [])
const adjustWidth = useCallback(
(deltaPercent: number) => {
setWidthPercent((previous) => {
const displayedWidth = Math.min(previous, maxWidthPercent)
const nextWidth = Math.min(
maxWidthPercent,
Math.max(minWidthPercent, displayedWidth + deltaPercent),
)
return nextWidth === displayedWidth ? previous : nextWidth
})
},
[maxWidthPercent, minWidthPercent, setWidthPercent],
)
const onResize = useCallback(
(deltaPixels: number) => adjustWidth((deltaPixels / window.innerWidth) * 100),
[adjustWidth],
)
const displayedWidth = Math.min(widthPercent, maxWidthPercent)
return (
<>
<div ref={sheetRef} className="min-w-0 shrink-0" style={{ width: `${displayedWidth}vw` }}>
<TranscriptPanel {...panelProps} className="h-full" />
</div>
<ResizeHandle
role="separator"
aria-orientation="vertical"
aria-label={m.transcript_resize()}
aria-controls={panelProps.id}
aria-valuemin={Math.round(minWidthPercent)}
aria-valuemax={Math.round(maxWidthPercent)}
aria-valuenow={Math.round(displayedWidth)}
aria-valuetext={`${Math.round(displayedWidth)}%`}
tabIndex={0}
className="shrink-0 focus-visible:outline-2 focus-visible:outline-ring"
hitAreaClassName="left-0 -right-3"
onResize={onResize}
onKeyDown={(event) => {
const step = keyboardSteps.get(event.key)
if (step === undefined) return
event.preventDefault()
event.stopPropagation()
adjustWidth(step)
}}
/>
</>
)
}
@@ -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 })
}
}
+22 -14
View File
@@ -1,14 +1,19 @@
import { cn } from '@/ui/lib/utils'
import { useEffect, useRef, useState } from 'react'
interface ResizeHandleProps {
interface ResizeHandleProps extends Omit<
React.ComponentProps<'div'>,
'children' | 'onMouseDown' | 'onResize'
> {
onResize: (delta: number) => void
className?: string
hitAreaClassName?: string
}
export const ResizeHandle: React.FC<ResizeHandleProps> = ({
onResize,
hitAreaClassName,
className = '',
...props
}) => {
const [isDragging, setIsDragging] = useState(false)
const startXref = useRef(0)
@@ -50,36 +55,39 @@ export const ResizeHandle: React.FC<ResizeHandleProps> = ({
startXref.current = e.clientX
}
// Common hit area that is always larger than the visible element for usability
const HitArea = () => (
<div className="absolute inset-y-0 -left-4 -right-4 z-20 cursor-col-resize touch-none" />
)
return (
<div
className={`group relative z-20 flex h-full w-[2px] cursor-col-resize items-center justify-center bg-border transition-all hover:bg-transparent ${className}`}
{...props}
data-slot="resize-handle"
className={`group/resize relative z-20 flex h-full w-[2px] cursor-col-resize items-center justify-center bg-border transition-all hover:bg-transparent ${className}`}
onMouseDown={handleMouseDown}
>
<HitArea />
<div
aria-hidden="true"
className={cn(
'absolute inset-y-0 -left-4 -right-4 z-20 cursor-col-resize touch-none',
hitAreaClassName,
)}
/>
{/* The glowing vertical line */}
<div
className={`absolute inset-y-0 w-[2px] transition-all duration-300 ease-out
className={`pointer-events-none absolute inset-y-0 w-[2px] transition-all duration-300 ease-out
${
isDragging
? 'bg-blue-600 shadow-[0_0_15px_rgba(37,99,235,0.6)] opacity-100'
: 'bg-blue-400 opacity-0 group-hover:opacity-100 group-hover:shadow-[0_0_8px_rgba(96,165,250,0.5)]'
: 'bg-blue-400 opacity-0 group-hover/resize:opacity-100 group-hover/resize:shadow-[0_0_8px_rgba(96,165,250,0.5)]'
}
`}
/>
{/* The central drag handle (Pill) */}
<div
className={`absolute top-1/2 left-1/2 h-10 w-1.5 -translate-x-1/2 -translate-y-1/2 rounded-full bg-blue-600 transition-all duration-300 ease-out
className={`pointer-events-none absolute top-1/2 left-1/2 h-10 w-1.5 -translate-x-1/2 -translate-y-1/2 rounded-full bg-blue-600 transition-all duration-300 ease-out
${
isDragging
? 'opacity-100 scale-100 shadow-[0_0_10px_rgba(37,99,235,0.5)]'
: 'opacity-0 scale-75 group-hover:opacity-100 group-hover:scale-100'
: 'opacity-0 scale-75 group-hover/resize:opacity-100 group-hover/resize:scale-100'
}`}
/>
</div>