mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 14:34:40 +00:00
113 lines
3.3 KiB
TypeScript
113 lines
3.3 KiB
TypeScript
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)
|
|
}}
|
|
/>
|
|
</>
|
|
)
|
|
}
|