Skip to content

🎥 Make videos programmatically with React

fix(studio): scroll selected captions into the inspector

snowyukitty5382152a5d8f
diff --git a/packages/core/src/CompositionManager.tsx b/packages/core/src/CompositionManager.tsx
index df02f3689a..e3d306937e 100644
--- a/packages/core/src/CompositionManager.tsx
+++ b/packages/core/src/CompositionManager.tsx
@@ -142,6 +142,8 @@ export type TSequence = {
timelineOrder: number | null;
loopDisplay: LoopDisplay | undefined;
getStack: () => string | null;
+ /** Read the committed child clock without updating timeline metadata each frame. */
+ getCurrentFrame?: (() => number) | null;
premountDisplay: number | null;
postmountDisplay: number | null;
controls: SequenceRegistrationControls | null;
diff --git a/packages/core/src/Sequence.tsx b/packages/core/src/Sequence.tsx
index 62d71d889d..143aa9064a 100644
--- a/packages/core/src/Sequence.tsx
+++ b/packages/core/src/Sequence.tsx
@@ -4,6 +4,7 @@ import React, {
forwardRef,
useCallback,
useContext,
+ useLayoutEffect,
useMemo,
useRef,
useState,
@@ -526,6 +527,14 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
? (getStackForControls(controls) ?? stack ?? null)
: (stack ?? null);
const registeredFrozenFrame = typeof freeze === 'number' ? freeze : null;
+ const currentFrame =
+ registeredFrozenFrame ??
+ (absoluteFrame - currentSequenceStart) * cumulativePlaybackRate;
+ const currentFrameRef = useRef(currentFrame);
+ useLayoutEffect(() => {
+ currentFrameRef.current = currentFrame;
+ }, [currentFrame]);
+ const getCurrentFrame = useCallback(() => currentFrameRef.current, []);
const registeredTrimBefore = trimBefore === 0 ? null : trimBefore;
const parentCumulatedNegativeFrom =
parentSequence?.cumulatedNegativeFrom ?? 0;
@@ -640,6 +649,7 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
timelineOrder: null,
src: isMedia.src,
getStack: () => stackRef.current,
+ getCurrentFrame,
refForOutline: refForOutline ?? null,
isInsideSeries,
frozenFrame: registeredFrozenFrame,
@@ -669,6 +679,7 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
timelineOrder: null,
src: isMedia.data.src,
getStack: () => stackRef.current,
+ getCurrentFrame,
startMediaFrom: startMediaFrom ?? isMedia.data.startMediaFrom,
mediaFrameAtSequenceZero,
volume: isMedia.data.volumes,
@@ -698,6 +709,7 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
timelineOrder: null,
loopDisplay: resolvedLoopDisplay,
getStack: () => stackRef.current,
+ getCurrentFrame,
premountDisplay: premountDisplay ?? null,
postmountDisplay: postmountDisplay ?? null,
controls: registrationControls,
@@ -710,6 +722,7 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
};
}, [
id,
+ getCurrentFrame,
timelineClipName,
playbackRate,
parentSequence?.id,
diff --git a/packages/studio/src/components/InlineCaptionInspector.tsx b/packages/studio/src/components/InlineCaptionInspector.tsx
index b3283843e4..5aee156b06 100644
--- a/packages/studio/src/components/InlineCaptionInspector.tsx
+++ b/packages/studio/src/components/InlineCaptionInspector.tsx
@@ -133,6 +133,7 @@ export const InlineCaptionInspector: React.FC<{
readonly controls: SequenceRegistrationControls;
readonly expanded: boolean;
readonly nodePath: SequencePropsSubscriptionKey;
+ readonly onCaptionsRendered: () => void;
readonly onToggle: () => void;
readonly readOnlyStudio: boolean;
readonly validatedLocation: CodePosition;
@@ -141,6 +142,7 @@ export const InlineCaptionInspector: React.FC<{
controls,
expanded,
nodePath,
+ onCaptionsRendered,
onToggle,
readOnlyStudio,
validatedLocation,
@@ -176,6 +178,12 @@ export const InlineCaptionInspector: React.FC<{
clearDragOverrides(nodePath);
}, [captions, clearDragOverrides, nodePath, runtimeSignature]);
+ useEffect(() => {
+ if (expanded) {
+ onCaptionsRendered();
+ }
+ }, [draftCaptions, expanded, onCaptionsRendered]);
+
const updateCaptions = useCallback(
(nextCaptions: Caption[]) => {
setDraftCaptions(nextCaptions);
diff --git a/packages/studio/src/components/InspectorPanel/SequenceSelectionInspector.tsx b/packages/studio/src/components/InspectorPanel/SequenceSelectionInspector.tsx
index 0834699b70..64e5001a98 100644
--- a/packages/studio/src/components/InspectorPanel/SequenceSelectionInspector.tsx
+++ b/packages/studio/src/components/InspectorPanel/SequenceSelectionInspector.tsx
@@ -489,6 +489,7 @@ const SequenceExpandedInspector: React.FC<{
nodePathInfo={track.nodePathInfo}
keyframeDisplayOffset={track.keyframeDisplayOffset}
keyframePlaybackRate={track.keyframePlaybackRate}
+ sequenceFrameOffset={track.sequenceFrameOffset}
renderTransformControls={() => <AlignmentControls track={track} />}
/>
<CollapsibleInspectorSection
diff --git a/packages/studio/src/components/InspectorSequenceSection.tsx b/packages/studio/src/components/InspectorSequenceSection.tsx
index a19c1f34ee..8351c5613b 100644
--- a/packages/studio/src/components/InspectorSequenceSection.tsx
+++ b/packages/studio/src/components/InspectorSequenceSection.tsx
@@ -42,6 +42,7 @@ import {
getBorderRadiusConversion,
getBorderRadiusConversionChanges,
} from './Timeline/border-radius-representation';
+import {getCurrentFps, getCurrentFrame} from './Timeline/imperative-state';
import {saveSequenceProps} from './Timeline/save-sequence-prop';
import {
getTimelineAssetLinkInfo,
@@ -312,6 +313,7 @@ export const InspectorSequenceSection: React.FC<{
readonly nodePathInfo: SequenceNodePathInfo;
readonly keyframeDisplayOffset: number;
readonly keyframePlaybackRate: number;
+ readonly sequenceFrameOffset: number;
readonly renderTransformControls: () => React.ReactNode;
}> = ({
sequence,
@@ -320,6 +322,7 @@ export const InspectorSequenceSection: React.FC<{
nodePathInfo,
keyframeDisplayOffset,
keyframePlaybackRate,
+ sequenceFrameOffset,
renderTransformControls,
}) => {
const {tree, propStatuses, runtimeValues} = useTimelineExpandedTree({
@@ -348,6 +351,14 @@ export const InspectorSequenceSection: React.FC<{
inspectorRevealItemKey === selectedEffectKey
? (inspectorRevealRequest?.token ?? null)
: null;
+ const captionsInspectorRef = useRef<HTMLDivElement>(null);
+ const handledCaptionSelection = useRef<readonly TimelineSelection[] | null>(
+ null,
+ );
+ const pendingCaptionScroll = useRef<{
+ readonly selection: readonly TimelineSelection[];
+ readonly index: number;
+ } | null>(null);
const selectedEffectRowRef = useRef<HTMLDivElement>(null);
const scrolledInspectorRevealToken = useRef<number | null>(null);
const [collapsedKeys, setCollapsedKeys] = useState<ReadonlySet<string>>(
@@ -736,11 +747,86 @@ export const InspectorSequenceSection: React.FC<{
getDragOverrides(nodePathInfo.sequenceSubscriptionKey),
);
const hasCaptionsSchema = schema.captions?.type === 'remotion-captions';
- const inlineCaptions = hasCaptionsSchema
- ? Array.isArray(runtimeValues.captions)
- ? (runtimeValues.captions as Caption[])
- : []
- : null;
+ const inlineCaptions = useMemo(
+ () =>
+ hasCaptionsSchema
+ ? Array.isArray(runtimeValues.captions)
+ ? (runtimeValues.captions as Caption[])
+ : []
+ : null,
+ [hasCaptionsSchema, runtimeValues.captions],
+ );
+ useEffect(() => {
+ if (handledCaptionSelection.current === selectedItems) {
+ return;
+ }
+
+ pendingCaptionScroll.current = null;
+ if (
+ selectedItems.length !== 1 ||
+ selectedItems[0].type !== 'sequence' ||
+ getTimelineSequenceSelectionKey(selectedItems[0].nodePathInfo) !==
+ sequenceKey ||
+ inlineCaptions === null
+ ) {
+ handledCaptionSelection.current = selectedItems;
+ return;
+ }
+
+ if (inlineCaptions.length === 0) {
+ return;
+ }
+
+ handledCaptionSelection.current = selectedItems;
+ const sequenceFrame =
+ sequence.getCurrentFrame?.() ??
+ sequence.frozenFrame ??
+ (getCurrentFrame() - sequence.from) * sequence.sequencePlaybackRate +
+ sequenceFrameOffset;
+ const timeMs = (sequenceFrame / getCurrentFps()) * 1000;
+ const index = inlineCaptions.findIndex(
+ (caption) => caption.startMs <= timeMs && caption.endMs > timeMs,
+ );
+ if (index === -1) {
+ return;
+ }
+
+ pendingCaptionScroll.current = {selection: selectedItems, index};
+ setAdditionalSectionExpanded('captions', true);
+ }, [
+ inlineCaptions,
+ selectedItems,
+ sequence,
+ sequenceFrameOffset,
+ sequenceKey,
+ setAdditionalSectionExpanded,
+ ]);
+
+ const revealPendingCaption = useCallback(() => {
+ const pending = pendingCaptionScroll.current;
+ if (
+ !captionsExpanded ||
+ pending === null ||
+ pending.selection !== selectedItems
+ ) {
+ return;
+ }
+
+ const input = captionsInspectorRef.current?.querySelector<HTMLInputElement>(
+ `[data-caption-index="${pending.index}"]`,
+ );
+ if (!input) {
+ return;
+ }
+
+ input.scrollIntoView({block: 'center'});
+ pendingCaptionScroll.current = null;
+ }, [captionsExpanded, selectedItems]);
+
+ useEffect(() => {
+ revealPendingCaption();
+ }, [inlineCaptions, revealPendingCaption]);
+
const showEffectsSection = nodePathInfo.supportsEffects || hasEffects;
const canAddEffect =
nodePathInfo.supportsEffects &&
@@ -937,15 +1023,18 @@ export const InspectorSequenceSection: React.FC<{
};
const captionsInspector = inlineCaptions ? (
- <InlineCaptionInspector
- captions={inlineCaptions}
- controls={sequence.controls}
- expanded={captionsExpanded}
- nodePath={nodePathInfo.sequenceSubscriptionKey}
- onToggle={() => toggleAdditionalSection('captions')}
- readOnlyStudio={readOnlyStudio}
- validatedLocation={validatedLocation}
- />
+ <div key={sequenceKey} ref={captionsInspectorRef}>
+ <InlineCaptionInspector
+ captions={inlineCaptions}
+ controls={sequence.controls}
+ expanded={captionsExpanded}
+ nodePath={nodePathInfo.sequenceSubscriptionKey}
+ onCaptionsRendered={revealPendingCaption}
+ onToggle={() => toggleAdditionalSection('captions')}
+ readOnlyStudio={readOnlyStudio}
+ validatedLocation={validatedLocation}
+ />
+ </div>
) : null;
if (