import { useEffect, useRef, useState } from "react"; import { continueRender, delayRender, useCurrentFrame, useVideoConfig, } from "remotion"; import { StyleAndClass } from "@asius/base"; import { TranscriptionProps } from "@asius/base"; import { useTextStyles } from "../../useTextStyles"; export * from "./default"; export const Transcription = ({ textStyle, src: words, style, className, animationStyle, animationType, scrollByPage, startFrom, height = 0, }: TranscriptionProps & StyleAndClass) => { const { fps } = useVideoConfig(); const currentFrame = useCurrentFrame(); let frame = startFrom ? currentFrame + startFrom * fps : currentFrame; if (frame < 0) frame = 0; const windowRef = useRef(null); const zoomRef = useRef(null); const [handle] = useState(() => delayRender()); const [linesRendered, setLinesRendered] = useState(0); const lineHeight = (textStyle.lineHeight || 1) * (textStyle.fontSize || 1); const linesPerPage = Math.floor(height / lineHeight) || 1; const getStyle = useTextStyles(); useEffect(() => { if (words) { const linesRendered = Math.round( (windowRef.current?.getBoundingClientRect().height as number) / (zoomRef.current?.getBoundingClientRect().height as number) ); setLinesRendered(linesRendered); continueRender(handle); } }, [handle, words, frame, linesPerPage, lineHeight]); const linesOffset = Math.max( 0, !scrollByPage ? linesRendered - linesPerPage : Math.floor((linesRendered - 1) / linesPerPage) * linesPerPage ); const playedSubs = words.filter((s) => s.start * fps <= frame); const unPlayedSubs = words.filter((s) => s.start * fps > frame); return (

{playedSubs.map((item, i) => { let isHighlighted = false; if (animationType === "previous-text") isHighlighted = true; else if ( animationType === "current-word" && i === playedSubs.length - 1 ) isHighlighted = true; return ( {item.text}{" "} ); })} {unPlayedSubs.map((item, i) => ( {item.text} ))}

); };