Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Avoid full timeline recalculation during media trims

JonnyBurgerf1e42ba1ddc2
diff --git a/packages/sdk/src/calculate-timeline.ts b/packages/sdk/src/calculate-timeline.ts
index 1da19d3ffb..8c99bafaff 100644
--- a/packages/sdk/src/calculate-timeline.ts
+++ b/packages/sdk/src/calculate-timeline.ts
@@ -1,7 +1,6 @@
import type {_InternalTypes, OverrideIdToNodePaths, TSequence} from 'remotion';
import type {
TimelineTrackData,
- TimelineTrackWithOriginalTimings,
TimelineLoopDisplay,
} from './get-timeline-sequence-sort-key';
import {sortItemsByCommitOrder} from './sort-by-commit-order';
@@ -21,17 +20,31 @@ type SequenceTiming = {
isFrozen: boolean;
};
-export const calculateTimeline = ({
- sequences,
- overrideIdsToNodePaths,
- compositions = [],
- showConnectedCompositionChildren = false,
-}: {
- sequences: TSequence[];
+type TimelineCalculationInput = {
+ sequences: readonly TSequence[];
overrideIdsToNodePaths: OverrideIdToNodePaths;
compositions?: readonly _InternalTypes['AnyComposition'][];
showConnectedCompositionChildren?: boolean;
-}): TimelineTrackData[] => {
+};
+
+type TimelineCalculation = {
+ tracks: TimelineTrackData[];
+ structure: {trackIndexById: ReadonlyMap<string, number>};
+ // A full rebuild invalidates grouping and row membership in the consumer.
+ changedSequenceIds: readonly string[] | null;
+};
+
+const EMPTY_COMPOSITIONS: readonly _InternalTypes['AnyComposition'][] = [];
+
+const calculateTimelineState = ({
+ sequences,
+ overrideIdsToNodePaths,
+ compositions = EMPTY_COMPOSITIONS,
+ showConnectedCompositionChildren = false,
+}: TimelineCalculationInput): {
+ result: TimelineCalculation;
+ update: (input: TimelineCalculationInput) => TimelineCalculation | null;
+} => {
const registeredSequencesById = new Map(
sequences.map((sequence) => [sequence.id, sequence]),
);
@@ -58,12 +71,6 @@ export const calculateTimeline = ({
completeSequences,
(sequence) => sequence.timelineOrder,
);
- const tracks: TimelineTrackWithOriginalTimings[] = [];
-
- if (sortedSequences.length === 0) {
- return [];
- }
-
const connectedCompositionsBySequenceId = new Map<
string,
readonly _InternalTypes['AnyComposition'][]
@@ -210,8 +217,7 @@ export const calculateTimeline = ({
(sequence) => !getTiming(sequence).hasCollapsedConnectedCompositionAncestor,
);
- for (let i = 0; i < timelineSequences.length; i++) {
- const sequence = timelineSequences[i];
+ const getTrack = (sequence: TSequence): TimelineTrackData => {
const timing = getTiming(sequence);
const parentTiming =
sequence.parent === null
@@ -288,7 +294,7 @@ export const calculateTimeline = ({
const connectedCompositions =
connectedCompositionsBySequenceId.get(sequence.id) ?? [];
- tracks.push({
+ return {
...(connectedCompositions.length > 0 ? {connectedCompositions} : {}),
sequence: {
...sequence,
@@ -312,7 +318,6 @@ export const calculateTimeline = ({
parentVisibleEnd: parentTiming
? parentTiming.visibleStart + parentTiming.visibleDuration
: null,
- cascadedDuration: sequence.duration,
keyframeDisplayOffset: hasKeyframeRows
? cascadedStart - sequence.from / parentPlaybackRate
: 0,
@@ -331,8 +336,10 @@ export const calculateTimeline = ({
supportsEffects: sequence.controls?.supportsEffects === true,
}
: null,
- });
- }
+ };
+ };
+
+ const tracks = timelineSequences.map(getTrack);
const sequenceRanks = new Map<string, number>();
for (let i = 0; i < tracks.length; i++) {
@@ -341,7 +348,7 @@ export const calculateTimeline = ({
const tracksById = new Map(tracks.map((track) => [track.sequence.id, track]));
const sortKeysById = new Map<string, string>();
- const getSortKey = (track: TimelineTrackWithOriginalTimings): string => {
+ const getSortKey = (track: TimelineTrackData): string => {
const cached = sortKeysById.get(track.sequence.id);
if (cached !== undefined) {
return cached;
@@ -360,14 +367,11 @@ export const calculateTimeline = ({
const sortedTracks: TimelineTrackData[] = tracks
.map((track) => ({track, sortKey: getSortKey(track)}))
.sort((a, b) => a.sortKey.localeCompare(b.sortKey))
- .map(({track}) => {
- const {cascadedDuration, ...cleanTrack} = track;
- return cleanTrack;
- });
+ .map(({track}) => track);
const nodePathIndexCounters = new Map<string, number>();
- return sortedTracks
+ let currentTracks = sortedTracks
.map((track): TimelineTrackData => {
if (track.nodePathInfo === null) {
return track;
@@ -410,4 +414,157 @@ export const calculateTimeline = ({
},
};
});
+
+ const trackIndexById = new Map(
+ currentTracks.map((track, index) => [track.sequence.id, index]),
+ );
+ const structure = {trackIndexById};
+ let currentSequences = sequences;
+ const update = (
+ input: TimelineCalculationInput,
+ ): TimelineCalculation | null => {
+ if (
+ input.overrideIdsToNodePaths !== overrideIdsToNodePaths ||
+ (input.compositions ?? EMPTY_COMPOSITIONS) !== compositions ||
+ (input.showConnectedCompositionChildren ?? false) !==
+ showConnectedCompositionChildren
+ ) {
+ return null;
+ }
+
+ if (input.sequences === currentSequences) {
+ return {tracks: currentTracks, structure, changedSequenceIds: []};
+ }
+
+ if (
+ input.sequences.length !== currentSequences.length ||
+ sequencesById.size !== currentSequences.length
+ ) {
+ return null;
+ }
+
+ const changed: TSequence[] = [];
+ for (let i = 0; i < input.sequences.length; i++) {
+ const sequence = input.sequences[i];
+ const previous = currentSequences[i];
+ if (sequence === previous) {
+ continue;
+ }
+
+ // Retain ordering and node-path indexes only for ordinary media leaves.
+ // Changes to registrations, controls or hierarchy need a full rebuild.
+ if (
+ sequence.id !== previous.id ||
+ (sequence.type !== 'audio' && sequence.type !== 'video') ||
+ sequence.frozenMediaFrame !== null ||
+ sequence.autoDuration ||
+ sequence.isInsideSeries ||
+ sequence.timelineTrack !== undefined ||
+ childrenByParentId.has(sequence.id) ||
+ !trackIndexById.has(sequence.id)
+ ) {
+ return null;
+ }
+
+ const keys = Object.keys(sequence);
+ if (keys.length !== Object.keys(previous).length) {
+ return null;
+ }
+
+ for (const key of keys) {
+ if (!Object.hasOwn(previous, key)) {
+ return null;
+ }
+
+ if (
+ key === 'from' ||
+ key === 'duration' ||
+ key === 'trimBefore' ||
+ key === 'startMediaFrom' ||
+ key === 'mediaFrameAtSequenceZero'
+ ) {
+ continue;
+ }
+
+ if (
+ sequence[key as keyof TSequence] !== previous[key as keyof TSequence]
+ ) {
+ return null;
+ }
+ }
+
+ const timing = timingsById.get(sequence.id)!;
+ if (timing.loopOwner !== null || timing.isFrozen) {
+ return null;
+ }
+
+ // Only an immediate child's duration contributes to an inferred bar.
+ // A fixed Series.Sequence insulates its Series from a nested media trim.
+ if (
+ sequence.parent !== null &&
+ sequencesById.get(sequence.parent)!.autoDuration
+ ) {
+ return null;
+ }
+
+ changed.push(sequence);
+ }
+
+ if (changed.length > 0) {
+ const nextTracks = currentTracks.slice();
+ for (const sequence of changed) {
+ const index = trackIndexById.get(sequence.id)!;
+ const previousTrack = currentTracks[index];
+ sequencesById.set(sequence.id, sequence);
+ timingsById.delete(sequence.id);
+ const track = getTrack(sequence);
+ if (
+ track.sequence.duration > 0 !==
+ previousTrack.sequence.duration > 0
+ ) {
+ return null;
+ }
+
+ nextTracks[index] = {
+ ...track,
+ nodePathInfo: previousTrack.nodePathInfo,
+ };
+ }
+
+ currentTracks = nextTracks;
+ }
+
+ currentSequences = input.sequences;
+ return {
+ tracks: currentTracks,
+ structure,
+ changedSequenceIds: changed.map((sequence) => sequence.id),
+ };
+ };
+
+ return {
+ result: {tracks: currentTracks, structure, changedSequenceIds: null},
+ update,
+ };
+};
+
+export const calculateTimeline = (
+ input: TimelineCalculationInput,
+): TimelineTrackData[] => {
+ return calculateTimelineState(input).result.tracks;
+};
+
+// One instance belongs to one timeline consumer. Compatible commits reuse the
+// hierarchy, timing cache, sort order and node-path indexes from its last rebuild.
+export const createTimelineCalculator = () => {
+ let state: ReturnType<typeof calculateTimelineState> | null = null;
+ return (input: TimelineCalculationInput): TimelineCalculation => {
+ const updated = state?.update(input) ?? null;
+ if (updated !== null) {
+ return updated;
+ }
+
+ state = calculateTimelineState(input);
+ return state.result;
+ };
};
diff --git a/packages/sdk/src/canvas-internals.ts b/packages/sdk/src/canvas-internals.ts
index 2e7f7fb959..b97421b46a 100644
--- a/packages/sdk/src/canvas-internals.ts
+++ b/packages/sdk/src/canvas-internals.ts
@@ -3,7 +3,10 @@ export {
collectReactCommit,
installReactCommitObserver,
} from './react-commit-adapter';
-export {calculateTimeline} from './calculate-timeline';
+export {
+ calculateTimeline,
+ createTimelineCalculator,
+} from './calculate-timeline';
export {useIsCanvasSequenceHovered} from './hover';
export type {
CanvasOutline,
diff --git a/packages/studio/src/components/InspectorPanel/MultiSequenceInspector.tsx b/packages/studio/src/components/InspectorPanel/MultiSequenceInspector.tsx
index e15433baa1..4a5749e3d0 100644
--- a/packages/studio/src/components/InspectorPanel/MultiSequenceInspector.tsx
+++ b/packages/studio/src/components/InspectorPanel/MultiSequenceInspector.tsx
@@ -6,7 +6,7 @@ import React, {
useSyncExternalStore,
} from 'react';
import {Internals} from 'remotion';
-import {calculateTimeline} from '../../helpers/calculate-timeline';
+import {useTimelineCalculator} from '../../helpers/calculate-timeline';
import {StudioServerConnectionCtx} from '../../helpers/client-id';
import {LIGHT_TEXT} from '../../helpers/colors';
import {isStudioInteractivityEnabled} from '../../helpers/interactivity-enabled';
@@ -62,6 +62,7 @@ export const MultiSequenceInspector: React.FC<{
}> = ({selections, readOnlyStudio}) => {
const {previewServerState} = useContext(StudioServerConnectionCtx);
const sequences = Internals.useSequenceManagerSequences();
+ const calculateTimeline = useTimelineCalculator();
const {overrideIdToNodePathMappings} = useContext(
Internals.OverrideIdsToNodePathsGettersContext,
);
@@ -70,7 +71,7 @@ export const MultiSequenceInspector: React.FC<{
Internals.VisualModeDragOverridesContext,
);
const targets = useMemo(() => {
- const tracks = calculateTimeline({
+ const {tracks} = calculateTimeline({
sequences,
overrideIdsToNodePaths: overrideIdToNodePathMappings,
});
@@ -105,9 +106,9 @@ export const MultiSequenceInspector: React.FC<{
}
return result;
- }, [overrideIdToNodePathMappings, selections, sequences]);
+ }, [calculateTimeline, overrideIdToNodePathMappings, selections, sequences]);
const precomposeTargets = useMemo(() => {
- const tracks = calculateTimeline({
+ const {tracks} = calculateTimeline({
sequences,
overrideIdsToNodePaths: overrideIdToNodePathMappings,
});
@@ -131,7 +132,7 @@ export const MultiSequenceInspector: React.FC<{
isTransition: track?.sequence.timelineTrack?.role === 'transition',
};
});
- }, [overrideIdToNodePathMappings, selections, sequences]);
+ }, [calculateTimeline, overrideIdToNodePathMappings, selections, sequences]);
const store = useMemo(() => {
const stores = (targets ?? []).map(
(target) => target.controls.runtimeValues,
diff --git a/packages/studio/src/components/InspectorPanel/use-track-for-selection.ts b/packages/studio/src/components/InspectorPanel/use-track-for-selection.ts
index 631247e93b..4009271bb0 100644
--- a/packages/studio/src/components/InspectorPanel/use-track-for-selection.ts
+++ b/packages/studio/src/components/InspectorPanel/use-track-for-selection.ts
@@ -1,25 +1,30 @@
import {useContext, useMemo} from 'react';
import {Internals} from 'remotion';
-import {findTrackForNodePathInfo} from '../Timeline/find-track-for-node-path-info';
+import {useTimelineCalculator} from '../../helpers/calculate-timeline';
+import {findTrackForNodePathInfoInTimeline} from '../Timeline/find-track-for-node-path-info';
import type {TimelineSelection} from '../Timeline/TimelineSelection';
-export const useTrackForSelection = (selection: TimelineSelection) => {
+export const useTrackForSelection = (selection: TimelineSelection | null) => {
const sequences = Internals.useSequenceManagerSequences();
+ const calculate = useTimelineCalculator();
const {overrideIdToNodePathMappings} = useContext(
Internals.OverrideIdsToNodePathsGettersContext,
);
return useMemo(() => {
- if (selection.type === 'guide') {
+ if (selection === null || selection.type === 'guide') {
return null;
}
+ const {tracks} = calculate({
+ sequences,
+ overrideIdsToNodePaths: overrideIdToNodePathMappings,
+ });
return (
- findTrackForNodePathInfo({
- sequences,
- overrideIdsToNodePaths: overrideIdToNodePathMappings,
+ findTrackForNodePathInfoInTimeline({
+ tracks,
nodePathInfo: selection.nodePathInfo,
}) ?? null
);
- }, [overrideIdToNodePathMappings, selection, sequences]);
+ }, [calculate, overrideIdToNodePathMappings, selection, sequences]);
};
diff --git a/packages/studio/src/components/SelectedOutlineOverlay.tsx b/packages/studio/src/components/SelectedOutlineOverlay.tsx
index e0ad4be680..838bd2b9a5 100644
--- a/packages/studio/src/components/SelectedOutlineOverlay.tsx
+++ b/packages/studio/src/components/SelectedOutlineOverlay.tsx
@@ -16,6 +16,7 @@ import {
type TSequence,
} from 'remotion';
import {NoReactInternals} from 'remotion/no-react';
+import {useTimelineCalculator} from '../helpers/calculate-timeline';
import {StudioServerConnectionCtx} from '../helpers/client-id';
import {
isStudioInteractivityEnabled,
@@ -66,7 +67,6 @@ import {
import {propStatusHas3DTransformValue} from './Timeline/transform-3d-mode';
const {
- calculateTimeline,
getCanvasSelectableOutlines,
getCanvasVisibleOutlineTargets,
getCanvasActiveOutlineTargets,
@@ -827,6 +827,7 @@ const SelectedOutlineOverlayUnmemoized: React.FC<
const {selectedItems, selectItem} = useTimelineSelection();
const currentSelection = useCurrentTimelineSelectionStateAsRef();
const sequences = Internals.useSequenceManagerSequences();
+ const calculateTimeline = useTimelineCalculator();
const {compositions} = useContext(Internals.CompositionManager);
const {propStatuses} = useContext(Internals.VisualModePropStatusesContext);
const {previewServerState} = useContext(StudioServerConnectionCtx);
@@ -889,10 +890,10 @@ const SelectedOutlineOverlayUnmemoized: React.FC<
selectableOutlines ??= getCanvasSelectableOutlines({
tracks: calculateTimeline({
- sequences: [...sequences],
+ sequences,
overrideIdsToNodePaths: overrideIdToNodePathMappings,
compositions,
- }),
+ }).tracks,
timelinePosition: null,
resolveSequenceNodePathInfo: null,
});
@@ -904,6 +905,7 @@ const SelectedOutlineOverlayUnmemoized: React.FC<
});
};
}, [
+ calculateTimeline,
compositions,
editorShowOutlines,
isFullscreen,
diff --git a/packages/studio/src/components/Timeline/Timeline.tsx b/packages/studio/src/components/Timeline/Timeline.tsx
index 2728f8821d..1b7cbcc01e 100644
--- a/packages/studio/src/components/Timeline/Timeline.tsx
+++ b/packages/studio/src/components/Timeline/Timeline.tsx
@@ -16,7 +16,7 @@ import {
import {FastRefreshContext} from '../../fast-refresh-context';
import {areSequenceNodePathInfosEqual} from '../../helpers/are-sequence-node-path-infos-equal';
import {getBrowserStudioOperations} from '../../helpers/browser-studio-operations';
-import {calculateTimeline} from '../../helpers/calculate-timeline';
+import {useTimelineCalculator} from '../../helpers/calculate-timeline';
import {StudioServerConnectionCtx} from '../../helpers/client-id';
import {BACKGROUND} from '../../helpers/colors';
import type {TimelineTrackData} from '../../helpers/get-timeline-sequence-sort-key';
@@ -54,6 +54,8 @@ import {timelineVerticalScroll} from './timeline-refs';
import {
filterTimelineTrackContents,
getTimelineDisplayRows,
+ projectTimelineDisplayRows,
+ type TimelineDisplayRow,
} from './timeline-track-groups';
import {TimelineDragHandler} from './TimelineDragHandler';
import {TimelineHeightContainer} from './TimelineHeightContainer';
@@ -297,106 +299,138 @@ const TimelineInner: React.FC = () => {
const previewConnected = previewServerState.type === 'connected';
const previewInteractive = previewConnected && isStudioInteractivityEnabled();
- const videoConfigIsNull = videoConfig === null;
- const previousTimelineRef = useRef<{
+ const calculate = useTimelineCalculator();
+ const calculation = useMemo(
+ () =>
+ calculate({
+ sequences: videoConfig === null ? [] : sequences,
+ overrideIdsToNodePaths: overrideIdToNodePathMappings,
+ compositions,
+ showConnectedCompositionChildren: compactSeries,
+ }),
+ [
+ calculate,
+ compactSeries,
+ compositions,
+ overrideIdToNodePathMappings,
+ sequences,
+ videoConfig,
+ ],
+ );
+ const previousDisplayRef = useRef<{
readonly sequences: TSequence[];
readonly overrideIdsToNodePaths: typeof overrideIdToNodePathMappings;
readonly compositions: typeof compositions;
+ readonly structure: typeof calculation.structure;
+ readonly tracks: typeof calculation.tracks;
readonly timeline: TimelineTrackWithDisplayGroup[];
+ readonly structureTimeline: TimelineTrackWithDisplayGroup[];
} | null>(null);
+ const {timeline, structureTimeline} = useMemo(() => {
+ const previous = previousDisplayRef.current;
+ if (previous === null || previous.structure !== calculation.structure) {
+ const next = addTimelineDisplayGroups(calculation.tracks);
+ if (
+ previous === null ||
+ previous.overrideIdsToNodePaths !== overrideIdToNodePathMappings ||
+ previous.compositions !== compositions
+ ) {
+ return {timeline: next, structureTimeline: next};
+ }
- const timeline = useMemo((): TimelineTrackWithDisplayGroup[] => {
- if (videoConfigIsNull) {
- return [];
- }
+ // Unsupported edits still derive the full timeline, but unchanged bars
+ // keep the snapshots their memoized thumbnails and waveforms consume.
+ const previousSequencesById = new Map(
+ previous.sequences.map((sequence) => [sequence.id, sequence]),
+ );
+ const currentSequencesById = new Map(
+ sequences.map((sequence) => [sequence.id, sequence]),
+ );
+ const previousTracksById = new Map(
+ previous.timeline.map((track) => [track.sequence.id, track]),
+ );
+ const tracks = next.map((track) => {
+ const oldTrack = previousTracksById.get(track.sequence.id);
+ const currentSource = currentSequencesById.get(track.sequence.id);
+ const oldNodePath = oldTrack?.nodePathInfo ?? null;
+ const nodePath = track.nodePathInfo;
+ const oldLoop = oldTrack?.sequence.loopDisplay;
+ const loop = track.sequence.loopDisplay;
+ if (
+ oldTrack === undefined ||
+ currentSource !== previousSequencesById.get(track.sequence.id) ||
+ oldTrack.depth !== track.depth ||
+ oldTrack.cascadedStart !== track.cascadedStart ||
+ oldTrack.localStart !== track.localStart ||
+ oldTrack.parentVisibleStart !== track.parentVisibleStart ||
+ oldTrack.parentVisibleEnd !== track.parentVisibleEnd ||
+ oldTrack.keyframeDisplayOffset !== track.keyframeDisplayOffset ||
+ oldTrack.keyframePlaybackRate !== track.keyframePlaybackRate ||
+ oldTrack.sequenceFrameOffset !== track.sequenceFrameOffset ||
+ oldTrack.sequence.from !== track.sequence.from ||
+ oldTrack.sequence.duration !== track.sequence.duration ||
+ oldTrack.sequence.sequencePlaybackRate !==
+ track.sequence.sequencePlaybackRate ||
+ oldTrack.sequence.premountDisplay !==
+ track.sequence.premountDisplay ||
+ oldTrack.sequence.postmountDisplay !==
+ track.sequence.postmountDisplay ||
+ oldTrack.displayGroup?.key !== track.displayGroup?.key ||
+ oldTrack.displayGroup?.numberOfSequences !==
+ track.displayGroup?.numberOfSequences ||
+ (oldNodePath === null) !== (nodePath === null) ||
+ (oldNodePath !== null &&
+ nodePath !== null &&
+ !areSequenceNodePathInfosEqual(oldNodePath, nodePath)) ||
+ (oldLoop === undefined) !== (loop === undefined) ||
+ (oldLoop !== undefined &&
+ loop !== undefined &&
+ (oldLoop.durationInFrames !== loop.durationInFrames ||
+ oldLoop.numberOfTimes !== loop.numberOfTimes ||
+ oldLoop.startOffset !== loop.startOffset ||
+ oldLoop.phaseOffsetInFrames !== loop.phaseOffsetInFrames ||
+ oldLoop.mediaOffsetInFrames !== loop.mediaOffsetInFrames))
+ ) {
+ return track;
+ }
- const next = addTimelineDisplayGroups(
- calculateTimeline({
- sequences,
- overrideIdsToNodePaths: overrideIdToNodePathMappings,
- compositions,
- showConnectedCompositionChildren: compactSeries,
- }),
- );
- const previous = previousTimelineRef.current;
- if (
- previous === null ||
- previous.overrideIdsToNodePaths !== overrideIdToNodePathMappings ||
- previous.compositions !== compositions
- ) {
- return next;
+ return oldTrack;
+ });
+ return {timeline: tracks, structureTimeline: tracks};
}
- const previousSequencesById = new Map(
- previous.sequences.map((sequence) => [sequence.id, sequence]),
- );
- const currentSequencesById = new Map(
- sequences.map((sequence) => [sequence.id, sequence]),
- );
- const previousTracksById = new Map(
- previous.timeline.map((track) => [track.sequence.id, track]),
- );
- return next.map((track) => {
- const oldTrack = previousTracksById.get(track.sequence.id);
- const currentSource = currentSequencesById.get(track.sequence.id);
- const oldNodePath = oldTrack?.nodePathInfo ?? null;
- const nodePath = track.nodePathInfo;
- const oldLoop = oldTrack?.sequence.loopDisplay;
- const loop = track.sequence.loopDisplay;
- if (
- oldTrack === undefined ||
- currentSource !== previousSequencesById.get(track.sequence.id) ||
- oldTrack.depth !== track.depth ||
- oldTrack.cascadedStart !== track.cascadedStart ||
- oldTrack.localStart !== track.localStart ||
- oldTrack.parentVisibleStart !== track.parentVisibleStart ||
- oldTrack.parentVisibleEnd !== track.parentVisibleEnd ||
- oldTrack.keyframeDisplayOffset !== track.keyframeDisplayOffset ||
- oldTrack.keyframePlaybackRate !== track.keyframePlaybackRate ||
- oldTrack.sequenceFrameOffset !== track.sequenceFrameOffset ||
- oldTrack.sequence.from !== track.sequence.from ||
- oldTrack.sequence.duration !== track.sequence.duration ||
- oldTrack.sequence.sequencePlaybackRate !==
- track.sequence.sequencePlaybackRate ||
- oldTrack.sequence.premountDisplay !== track.sequence.premountDisplay ||
- oldTrack.sequence.postmountDisplay !==
- track.sequence.postmountDisplay ||
- oldTrack.displayGroup?.key !== track.displayGroup?.key ||
- oldTrack.displayGroup?.numberOfSequences !==
- track.displayGroup?.numberOfSequences ||
- (oldNodePath === null) !== (nodePath === null) ||
- (oldNodePath !== null &&
- nodePath !== null &&
- !areSequenceNodePathInfosEqual(oldNodePath, nodePath)) ||
- (oldLoop === undefined) !== (loop === undefined) ||
- (oldLoop !== undefined &&
- loop !== undefined &&
- (oldLoop.durationInFrames !== loop.durationInFrames ||
- oldLoop.numberOfTimes !== loop.numberOfTimes ||
- oldLoop.startOffset !== loop.startOffset ||
- oldLoop.phaseOffsetInFrames !== loop.phaseOffsetInFrames ||
- oldLoop.mediaOffsetInFrames !== loop.mediaOffsetInFrames))
- ) {
- return track;
- }
+ if (previous.tracks === calculation.tracks) {
+ return previous;
+ }
- return oldTrack;
- });
- }, [
- sequences,
- videoConfigIsNull,
- overrideIdToNodePathMappings,
- compositions,
- compactSeries,
- ]);
+ // Group identities and counts depend on structure, not media trimming.
+ return {
+ structureTimeline: previous.structureTimeline,
+ timeline: calculation.tracks.map((track, index) =>
+ track === previous.tracks[index]
+ ? previous.timeline[index]
+ : {...track, displayGroup: previous.timeline[index].displayGroup},
+ ),
+ };
+ }, [calculation, compositions, overrideIdToNodePathMappings, sequences]);
useLayoutEffect(() => {
- previousTimelineRef.current = {
+ previousDisplayRef.current = {
sequences,
overrideIdsToNodePaths: overrideIdToNodePathMappings,
compositions,
+ structure: calculation.structure,
+ tracks: calculation.tracks,
timeline,
+ structureTimeline,
};
- }, [compositions, overrideIdToNodePathMappings, sequences, timeline]);
+ }, [
+ calculation,
+ compositions,
+ overrideIdToNodePathMappings,
+ sequences,
+ structureTimeline,
+ timeline,
+ ]);
const durationInFrames = videoConfig?.durationInFrames ?? 0;
const activeFromDragOverrideKeys = Internals.useActiveFromDragOverrideKeys();
@@ -419,13 +453,37 @@ const TimelineInner: React.FC = () => {
return shouldShowTrackInTimeline(t, durationInFrames);
});
}, [activeFromDragOverrideKeys, durationInFrames, timeline]);
- // Keep `filtered` complete so a future toggle can show every programmatic
- // instance without recalculating the timeline or losing its instance index.
- const collapsed = useMemo(() => {
+ const previousLayoutRef = useRef<{
+ readonly structure: typeof calculation.structure;
+ readonly tracks: TimelineTrackWithDisplayGroup[];
+ readonly sequences: TSequence[];
+ } | null>(null);
+ const filteredLayout = useMemo(() => {
+ const previous = previousLayoutRef.current;
+ if (
+ previous !== null &&
+ previous.structure === calculation.structure &&
+ previous.tracks.length === filtered.length &&
+ previous.tracks.every(
+ (track, index) => track.sequence.id === filtered[index].sequence.id,
+ )
+ ) {
+ return previous;
+ }
+
+ // Visibility changes still rebuild membership, including a drag owner
+ // retained while it crosses the edge of the composition.
+ return {structure: calculation.structure, tracks: filtered, sequences};
+ }, [calculation.structure, filtered, sequences]);
+ useLayoutEffect(() => {
+ previousLayoutRef.current = filteredLayout;
+ }, [filteredLayout]);
+ // Keep every instance in `filtered`; collapse only the displayed structure.
+ const collapsedLayout = useMemo(() => {
const seenDisplayGroups = new Set<string>();
- return filterTimelineTrackContents(
- filtered,
- sequences,
+ const tracks = filterTimelineTrackContents(
+ filteredLayout.tracks,
+ filteredLayout.sequences,
compactSeries,
).filter((track) => {
if (track.sequence.timelineTrack || track.displayGroup === null) {
@@ -439,18 +497,57 @@ const TimelineInner: React.FC = () => {
seenDisplayGroups.add(track.displayGroup.key);
return true;
});
- }, [compactSeries, filtered, sequences]);
+ return {tracks, sequences: filteredLayout.sequences};
+ }, [compactSeries, filteredLayout]);
+ const previousCollapsedRef = useRef<{
+ readonly layout: typeof collapsedLayout;
+ readonly timeline: typeof timeline;
+ readonly tracks: TimelineTrackWithDisplayGroup[];
+ } | null>(null);
+ const collapsed = useMemo(() => {
+ const previous = previousCollapsedRef.current;
+ return collapsedLayout.tracks.map((layoutTrack, index) => {
+ const trackIndex = calculation.structure.trackIndexById.get(
+ layoutTrack.sequence.id,
+ )!;
+ const track = timeline[trackIndex];
+ if (
+ previous?.layout === collapsedLayout &&
+ previous.timeline[trackIndex] === track
+ ) {
+ return previous.tracks[index];
+ }
- const {visibleTracks, value: layerChildrenValue} = useTimelineLayerChildren(
+ return {
+ ...track,
+ depth: layoutTrack.depth,
+ displayGroup: layoutTrack.displayGroup,
+ };
+ });
+ }, [calculation.structure, collapsedLayout, timeline]);
+ useLayoutEffect(() => {
+ previousCollapsedRef.current = {
+ layout: collapsedLayout,
+ timeline,
+ tracks: collapsed,
+ };
+ }, [collapsed, collapsedLayout, timeline]);
+ const {
+ visibleTracks,
+ visibleLayoutTracks,
+ value: layerChildrenValue,
+ } = useTimelineLayerChildren(
collapsed,
- sequences,
+ collapsedLayout,
canvasContent?.type === 'composition' ? canvasContent.compositionId : null,
compactSeries,
);
const {fastRefreshes} = useContext(FastRefreshContext);
+ // Eligible media leaves do not change Track clip ranges. A change to any
+ // Track member invalidates the structure and runs overlap validation again.
const trackOverlapError = useMemo(
- () => getTimelineTrackOverlapError(timeline, durationInFrames),
- [timeline, durationInFrames],
+ () => getTimelineTrackOverlapError(structureTimeline, durationInFrames),
+ [structureTimeline, durationInFrames],
);
useEffect(() => {
if (trackOverlapError === null) {
@@ -572,9 +669,39 @@ const TimelineInner: React.FC = () => {
]);
const maxTimelineTracks = getStudioMaxTimelineTracks();
+ const layoutRows = useMemo(
+ () => getTimelineDisplayRows(visibleLayoutTracks),
+ [visibleLayoutTracks],
+ );
+ const previousRowsRef = useRef<{
+ readonly layoutRows: readonly TimelineDisplayRow[];
+ readonly rows: readonly TimelineDisplayRow[];
+ }>({layoutRows, rows: layoutRows});
const displayRows = useMemo(
- () => getTimelineDisplayRows(visibleTracks),
- [visibleTracks],
+ () =>
+ projectTimelineDisplayRows({
+ rows:
+ previousRowsRef.current.layoutRows === layoutRows
+ ? previousRowsRef.current.rows
+ : layoutRows,
+ tracksById: new Map(
+ visibleTracks.map((track) => [track.sequence.id, track]),
+ ),
+ }),
+ [layoutRows, visibleTracks],
+ );
+ useLayoutEffect(() => {
+ previousRowsRef.current = {layoutRows, rows: displayRows};
+ }, [displayRows, layoutRows]);
+ const virtualizationLayout = useMemo(
+ () => ({
+ timeline:
+ maxTimelineTracks !== null && layoutRows.length > maxTimelineTracks
+ ? layoutRows.slice(0, maxTimelineTracks)
+ : layoutRows,
+ sequences: collapsedLayout.sequences,
+ }),
+ [collapsedLayout.sequences, layoutRows, maxTimelineTracks],
);
const shownRows = useMemo(() => {
return maxTimelineTracks !== null && displayRows.length > maxTimelineTracks
@@ -614,6 +741,7 @@ const TimelineInner: React.FC = () => {
hasBeenCut={hasBeenCut}
isStill={isStill}
timeline={shownRows}
+ layoutSnapshot={virtualizationLayout}
>
{isStudioInteractivityEnabled() ? (
<TimelineSelectAllKeybindings timeline={shown} />
diff --git a/packages/studio/src/components/Timeline/TimelineLayerChildren.tsx b/packages/studio/src/components/Timeline/TimelineLayerChildren.tsx
index e268bf25c1..52979234db 100644
--- a/packages/studio/src/components/Timeline/TimelineLayerChildren.tsx
+++ b/packages/studio/src/components/Timeline/TimelineLayerChildren.tsx
@@ -47,8 +47,11 @@ export const TimelineLayerChildrenProvider =
TimelineLayerChildrenContext.Provider;
export const useTimelineLayerChildren = (
- tracks: TimelineTrackWithDisplayGroup[],
- sequences: TSequence[],
+ tracks: readonly TimelineTrackWithDisplayGroup[],
+ layoutSnapshot: {
+ readonly tracks: readonly TimelineTrackWithDisplayGroup[];
+ readonly sequences: readonly TSequence[];
+ },
compositionId: string | null,
compactSeries: boolean,
) => {
@@ -71,6 +74,7 @@ export const useTimelineLayerChildren = (
}
});
const hierarchy = useMemo(() => {
+ const {tracks: layoutTracks, sequences} = layoutSnapshot;
const byId = new Map(sequences.map((sequence) => [sequence.id, sequence]));
const siblingIndices = new Map<string, number>();
const siblingCounts = new Map<string | null, number>();
@@ -83,7 +87,7 @@ export const useTimelineLayerChildren = (
const keys = new Map<string, string>();
const parents = new Set<string>();
const ancestors = new Map<string, string[]>();
- for (const track of tracks) {
+ for (const track of layoutTracks) {
const parentIds: string[] = [];
let {parent} = track.sequence;
while (parent !== null && !parentIds.includes(parent)) {
@@ -120,12 +124,12 @@ export const useTimelineLayerChildren = (
}
return {keys, parents, ancestors, byId};
- }, [compositionId, sequences, tracks]);
+ }, [compositionId, layoutSnapshot]);
const toggle = useCallback(
(key: string) => {
const affectedKeys = new Set([key]);
if (compactSeries) {
- const rows = getTimelineDisplayRows(tracks);
+ const rows = getTimelineDisplayRows(layoutSnapshot.tracks);
const index = rows.findIndex(
(row) => hierarchy.keys.get(row.track.sequence.id) === key,
);
@@ -133,7 +137,7 @@ export const useTimelineLayerChildren = (
const layout = getTimelineSeriesLayout({
rows,
heights: rows.map(() => 1),
- sequences,
+ sequences: layoutSnapshot.sequences,
compactSeries,
paddingStart: 0,
});
@@ -176,12 +180,12 @@ export const useTimelineLayerChildren = (
return next;
});
},
- [compactSeries, hierarchy, sequences, tracks],
+ [compactSeries, hierarchy, layoutSnapshot],
);
- const visibleTracks = useMemo(
+ const visibleTrackIndexes = useMemo(
() =>
- tracks.filter((track) => {
- return !(hierarchy.ancestors.get(track.sequence.id) ?? []).some(
+ layoutSnapshot.tracks.flatMap((track, index) => {
+ const hidden = (hierarchy.ancestors.get(track.sequence.id) ?? []).some(
(id) => {
// Keep packed Series bars when their contents collapse.
// Regular Track clips have their own rows and collapse normally.
@@ -200,8 +204,17 @@ export const useTimelineLayerChildren = (
return key !== undefined && collapsed[key];
},
);
+ return hidden ? [] : [index];
}),
- [hierarchy, collapsed, tracks],
+ [hierarchy, collapsed, layoutSnapshot],
+ );
+ const visibleTracks = useMemo(
+ () => visibleTrackIndexes.map((index) => tracks[index]),
+ [tracks, visibleTrackIndexes],
+ );
+ const visibleLayoutTracks = useMemo(
+ () => visibleTrackIndexes.map((index) => layoutSnapshot.tracks[index]),
+ [layoutSnapshot, visibleTrackIndexes],
);
const value = useMemo(
() => ({
@@ -212,7 +225,7 @@ export const useTimelineLayerChildren = (
}),
[collapsed, hierarchy, toggle],
);
- return {visibleTracks, value};
+ return {visibleTracks, visibleLayoutTracks, value};
};
export const TimelineLayerChildrenToggle: React.FC<{
diff --git a/packages/studio/src/components/Timeline/TimelineVirtualization.tsx b/packages/studio/src/components/Timeline/TimelineVirtualization.tsx
index d045a6176d..8bfae375c4 100644
--- a/packages/studio/src/components/Timeline/TimelineVirtualization.tsx
+++ b/packages/studio/src/components/Timeline/TimelineVirtualization.tsx
@@ -14,7 +14,7 @@ import React, {
useRef,
useState,
} from 'react';
-import {Internals} from 'remotion';
+import type {TSequence} from 'remotion';
import {TIMELINE_ITEM_BORDER_BOTTOM} from '../../helpers/timeline-layout';
import {MAX_TIMELINE_TRACKS_NOTICE_HEIGHT} from './MaxTimelineTracks';
import {timelineVerticalScroll} from './timeline-refs';
@@ -71,8 +71,11 @@ export const TimelineVirtualizationProvider: React.FC<{
readonly hasBeenCut: boolean;
readonly isStill: boolean;
readonly timeline: readonly TimelineDisplayRow[];
-}> = ({children, hasBeenCut, isStill, timeline}) => {
- const sequences = Internals.useSequenceManagerSequences();
+ readonly layoutSnapshot: {
+ readonly timeline: readonly TimelineDisplayRow[];
+ readonly sequences: readonly TSequence[];
+ };
+}> = ({children, hasBeenCut, isStill, timeline, layoutSnapshot}) => {
const compactSeries = useCompactSeries();
const representativeTracks = useMemo(
() => timeline.map((row) => row.track),
@@ -83,14 +86,14 @@ export const TimelineVirtualizationProvider: React.FC<{
});
const trackHeights = useMemo(
() =>
- timeline.map(
+ layoutSnapshot.timeline.map(
(row, index) =>
(row.items === null
? individualHeights[index]
: TIMELINE_PACKED_TRACK_HEIGHT + TIMELINE_ITEM_BORDER_BOTTOM) +
row.auxiliaryRows.length * TIMELINE_PACKED_AUXILIARY_ROW_HEIGHT,
),
- [individualHeights, timeline],
+ [individualHeights, layoutSnapshot],
);
const {consumeRevealRequest, revealRequest, selectedItems} =
useTimelineSelection();
@@ -100,15 +103,35 @@ export const TimelineVirtualizationProvider: React.FC<{
(hasBeenCut ? MAX_TIMELINE_TRACKS_NOTICE_HEIGHT : 0);
const rowsRef = useRef<readonly TimelineVirtualRow[]>([]);
- const layout = useMemo(() => {
- const seriesLayout = getTimelineSeriesLayout({
- rows: timeline,
- heights: trackHeights,
- sequences,
- compactSeries,
- paddingStart,
- });
+ // Ordinary leaf trims update the rendered track while retaining the facts
+ // that determine scene packing, hierarchy, and vertical positions.
+ const seriesLayout = useMemo(
+ () =>
+ getTimelineSeriesLayout({
+ rows: layoutSnapshot.timeline,
+ heights: trackHeights,
+ sequences: layoutSnapshot.sequences,
+ compactSeries,
+ paddingStart,
+ }),
+ [compactSeries, layoutSnapshot, paddingStart, trackHeights],
+ );
+ const rootTrackIndexes = useMemo(() => {
+ const indexes = new Map<string, number>();
+ for (let index = 0; index < layoutSnapshot.timeline.length; index++) {
+ for (const {nodePathInfo} of [
+ layoutSnapshot.timeline[index].track,
+ ...(layoutSnapshot.timeline[index].items ?? []),
+ ]) {
+ if (nodePathInfo !== null) {
+ indexes.set(getTimelineSequenceSelectionKey(nodePathInfo), index);
+ }
+ }
+ }
+ return indexes;
+ }, [layoutSnapshot]);
+ const layout = useMemo(() => {
const rows = timeline.map(
(
{track, items, childScenes, auxiliaryRows},
@@ -163,27 +186,12 @@ export const TimelineVirtualizationProvider: React.FC<{
};
},
);
- const rootTrackIndexes = new Map<string, number>();
- for (let index = 0; index < timeline.length; index++) {
- for (const {nodePathInfo} of [
- timeline[index].track,
- ...(timeline[index].items ?? []),
- ]) {
- if (nodePathInfo !== null) {
- rootTrackIndexes.set(
- getTimelineSequenceSelectionKey(nodePathInfo),
- index,
- );
- }
- }
- }
-
return {
...seriesLayout,
rootTrackIndexes,
rows,
};
- }, [compactSeries, paddingStart, sequences, timeline, trackHeights]);
+ }, [rootTrackIndexes, seriesLayout, timeline]);
useLayoutEffect(() => {
rowsRef.current = layout.rows;
}, [layout.rows]);
@@ -456,7 +464,7 @@ export const TimelineVirtualizationProvider: React.FC<{
.map(
(index): VirtualItem => ({
index,
- key: timeline[index].track.sequence.id,
+ key: layoutSnapshot.timeline[index].track.sequence.id,
start: layout.offsets[index],
end: layout.offsets[index] + layout.rowHeights[index],
size: layout.rowHeights[index],
@@ -469,7 +477,7 @@ export const TimelineVirtualizationProvider: React.FC<{
layout.offsets,
layout.rowHeights,
selectedTrackIndexes,
- timeline,
+ layoutSnapshot,
verticalRenderWindow,
virtualGroups,
],
diff --git a/packages/studio/src/components/Timeline/find-track-for-node-path-info.ts b/packages/studio/src/components/Timeline/find-track-for-node-path-info.ts
index cbc3bcb553..0793aac443 100644
--- a/packages/studio/src/components/Timeline/find-track-for-node-path-info.ts
+++ b/packages/studio/src/components/Timeline/find-track-for-node-path-info.ts
@@ -1,18 +1,18 @@
import {stringifySequenceExpandedRowKey} from '@remotion/studio-shared';
import type {OverrideIdToNodePaths, TSequence} from 'remotion';
import {calculateTimeline} from '../../helpers/calculate-timeline';
-import type {SequenceNodePathInfo} from '../../helpers/get-timeline-sequence-sort-key';
+import type {
+ SequenceNodePathInfo,
+ TimelineTrackData,
+} from '../../helpers/get-timeline-sequence-sort-key';
-export const findTrackForNodePathInfo = ({
- sequences,
- overrideIdsToNodePaths,
+export const findTrackForNodePathInfoInTimeline = ({
+ tracks,
nodePathInfo,
}: {
- sequences: TSequence[];
- overrideIdsToNodePaths: OverrideIdToNodePaths;
+ tracks: readonly TimelineTrackData[];
nodePathInfo: SequenceNodePathInfo;
}) => {
- const tracks = calculateTimeline({sequences, overrideIdsToNodePaths});
return tracks.find(
(candidate) =>
candidate.nodePathInfo !== null &&
@@ -23,3 +23,16 @@ export const findTrackForNodePathInfo = ({
candidate.nodePathInfo.index === nodePathInfo.index,
);
};
+
+export const findTrackForNodePathInfo = ({
+ sequences,
+ overrideIdsToNodePaths,
+ nodePathInfo,
+}: {
+ sequences: TSequence[];
+ overrideIdsToNodePaths: OverrideIdToNodePaths;
+ nodePathInfo: SequenceNodePathInfo;
+}) => {
+ const tracks = calculateTimeline({sequences, overrideIdsToNodePaths});
+ return findTrackForNodePathInfoInTimeline({tracks, nodePathInfo});
+};
diff --git a/packages/studio/src/components/Timeline/timeline-track-groups.ts b/packages/studio/src/components/Timeline/timeline-track-groups.ts
index 52a7457ad7..55840872ba 100644
--- a/packages/studio/src/components/Timeline/timeline-track-groups.ts
+++ b/packages/studio/src/components/Timeline/timeline-track-groups.ts
@@ -264,3 +264,69 @@ export const getTimelineDisplayRows = (
return rows;
};
+
+// Only use this when visible membership and packing are unchanged. Replacing a
+// media leaf's calculated snapshot does not require grouping every track again.
+export const projectTimelineDisplayRows = ({
+ rows,
+ tracksById,
+}: {
+ readonly rows: readonly TimelineDisplayRow[];
+ readonly tracksById: ReadonlyMap<string, TimelineTrackWithDisplayGroup>;
+}): readonly TimelineDisplayRow[] => {
+ const updateTracks = (
+ tracks: readonly TimelineTrackWithDisplayGroup[],
+ ): readonly TimelineTrackWithDisplayGroup[] => {
+ let updatedTracks: TimelineTrackWithDisplayGroup[] | null = null;
+ for (let index = 0; index < tracks.length; index++) {
+ const track = tracksById.get(tracks[index].sequence.id) ?? tracks[index];
+ if (track !== tracks[index]) {
+ updatedTracks ??= [...tracks];
+ updatedTracks[index] = track;
+ }
+ }
+
+ return updatedTracks ?? tracks;
+ };
+
+ let updated: TimelineDisplayRow[] | null = null;
+ for (let index = 0; index < rows.length; index++) {
+ const row = rows[index];
+ const track = tracksById.get(row.track.sequence.id) ?? row.track;
+ const items = row.items === null ? null : updateTracks(row.items);
+ const childScenes =
+ row.childScenes === null ? null : updateTracks(row.childScenes);
+ let auxiliaryRows: (readonly TimelineTrackWithDisplayGroup[])[] | null =
+ null;
+ for (
+ let auxiliaryIndex = 0;
+ auxiliaryIndex < row.auxiliaryRows.length;
+ auxiliaryIndex++
+ ) {
+ const auxiliaryRow = updateTracks(row.auxiliaryRows[auxiliaryIndex]);
+ if (auxiliaryRow !== row.auxiliaryRows[auxiliaryIndex]) {
+ auxiliaryRows ??= [...row.auxiliaryRows];
+ auxiliaryRows[auxiliaryIndex] = auxiliaryRow;
+ }
+ }
+
+ if (
+ track === row.track &&
+ items === row.items &&
+ childScenes === row.childScenes &&
+ auxiliaryRows === null
+ ) {
+ continue;
+ }
+
+ updated ??= [...rows];
+ updated[index] = {
+ track,
+ items,
+ childScenes,
+ auxiliaryRows: auxiliaryRows ?? row.auxiliaryRows,
+ };
+ }
+
+ return updated ?? rows;
+};
diff --git a/packages/studio/src/components/Timeline/use-timeline-sequence-registry.ts b/packages/studio/src/components/Timeline/use-timeline-sequence-registry.ts
index d119b16d9f..6e450780fc 100644
--- a/packages/studio/src/components/Timeline/use-timeline-sequence-registry.ts
+++ b/packages/studio/src/components/Timeline/use-timeline-sequence-registry.ts
@@ -19,15 +19,96 @@ type TimelineSequenceRegistryEntry = {
readonly nextTouchingSequence: TSequence | null;
};
+type SequenceAdjacency = {
+ readonly previousId: string | null;
+ readonly nextId: string | null;
+};
+
+type IndexedSequenceRegistry = {
+ readonly sequences: TSequence[];
+ readonly entries: Map<string, TimelineSequenceRegistryEntry>;
+ readonly indexById: ReadonlyMap<string, number>;
+ readonly adjacentById: ReadonlyMap<string, SequenceAdjacency>;
+ readonly touchingAdjacentById: ReadonlyMap<string, SequenceAdjacency>;
+ readonly normalTrackClipIds: ReadonlySet<string>;
+};
+
const indexedRegistries = new WeakMap<
SequenceRegistry,
- {
- readonly sequences: TSequence[];
- readonly entries: ReadonlyMap<string, TimelineSequenceRegistryEntry>;
- }
+ IndexedSequenceRegistry
>();
const subscribeToNoRegistry = () => () => undefined;
+const updateRegistryEntries = ({
+ index,
+ previousEntries,
+ sequenceIds,
+}: {
+ index: IndexedSequenceRegistry;
+ previousEntries: ReadonlyMap<string, TimelineSequenceRegistryEntry> | null;
+ sequenceIds: Iterable<string>;
+}) => {
+ for (const sequenceId of sequenceIds) {
+ const sequence = index.sequences[index.indexById.get(sequenceId)!];
+ const adjacent = index.adjacentById.get(sequenceId);
+ const previousSequenceId = adjacent?.previousId ?? null;
+ const previousOverrideId =
+ previousSequenceId === null
+ ? null
+ : (index.sequences[index.indexById.get(previousSequenceId)!].controls
+ ?.overrideId ?? null);
+ const nextSequenceId = adjacent?.nextId ?? null;
+ const nextOverrideId =
+ nextSequenceId === null
+ ? null
+ : (index.sequences[index.indexById.get(nextSequenceId)!].controls
+ ?.overrideId ?? null);
+ const touchingAdjacent = index.touchingAdjacentById.get(sequenceId);
+ const previousCandidateId = touchingAdjacent?.previousId ?? null;
+ const nextCandidateId = touchingAdjacent?.nextId ?? null;
+ const previousCandidate =
+ previousCandidateId === null
+ ? null
+ : index.sequences[index.indexById.get(previousCandidateId)!];
+ const nextCandidate =
+ nextCandidateId === null
+ ? null
+ : index.sequences[index.indexById.get(nextCandidateId)!];
+ const previousTouchingSequence =
+ previousCandidate?.parent === sequence.parent &&
+ Math.abs(
+ previousCandidate.from + previousCandidate.duration - sequence.from,
+ ) < 0.000001
+ ? previousCandidate
+ : null;
+ const nextTouchingSequence =
+ nextCandidate?.parent === sequence.parent &&
+ Math.abs(sequence.from + sequence.duration - nextCandidate.from) <
+ 0.000001
+ ? nextCandidate
+ : null;
+ const previousEntry = previousEntries?.get(sequenceId);
+ index.entries.set(
+ sequenceId,
+ previousEntry?.sequence === sequence &&
+ previousEntry.previousSequenceId === previousSequenceId &&
+ previousEntry.previousOverrideId === previousOverrideId &&
+ previousEntry.nextOverrideId === nextOverrideId &&
+ previousEntry.previousTouchingSequence === previousTouchingSequence &&
+ previousEntry.nextTouchingSequence === nextTouchingSequence
+ ? previousEntry
+ : {
+ sequence,
+ previousSequenceId,
+ previousOverrideId,
+ nextOverrideId,
+ previousTouchingSequence,
+ nextTouchingSequence,
+ },
+ );
+ }
+};
+
export const useTimelineSequenceRegistry = (sequenceId: string) => {
const registry = useContext(Internals.SequenceRegistryContext);
const getSnapshot = useCallback(() => {
@@ -41,10 +122,111 @@ export const useTimelineSequenceRegistry = (sequenceId: string) => {
return previous.entries.get(sequenceId) ?? null;
}
+ // A trim changes registration objects but normally leaves sibling order and
+ // track membership intact. Keep the structural index and update only entries
+ // that can observe a changed sequence, including their touching neighbors.
+ if (previous && previous.sequences.length === sequences.length) {
+ let canReuseIndex = true;
+ const changed = new Set<string>();
+ for (let i = 0; i < sequences.length; i++) {
+ const sequence = sequences[i];
+ const old = previous.sequences[i];
+ if (old === sequence) {
+ continue;
+ }
+
+ if (
+ sequence.id !== old.id ||
+ sequence.parent !== old.parent ||
+ sequence.timelineOrder !== old.timelineOrder ||
+ sequence.controls?.componentIdentity !==
+ old.controls?.componentIdentity ||
+ sequence.timelineTrack?.id !== old.timelineTrack?.id ||
+ sequence.timelineTrack?.role !== old.timelineTrack?.role ||
+ sequence.type !== old.type ||
+ sequence.showInTimeline !== old.showInTimeline ||
+ sequence.duration <= 0 !== old.duration <= 0
+ ) {
+ canReuseIndex = false;
+ break;
+ }
+
+ changed.add(sequence.id);
+ }
+
+ if (canReuseIndex) {
+ for (const id of changed) {
+ if (!previous.normalTrackClipIds.has(id)) {
+ continue;
+ }
+
+ const position = previous.indexById.get(id)!;
+ const sequence = sequences[position];
+ const adjacent = previous.touchingAdjacentById.get(id)!;
+ // Track clips are sorted by from, with registry order breaking ties.
+ // Check the new values on both sides, even when both clips changed.
+ for (const [neighborId, isPrevious] of [
+ [adjacent.previousId, true],
+ [adjacent.nextId, false],
+ ] as const) {
+ if (neighborId === null) {
+ continue;
+ }
+
+ const neighborPosition = previous.indexById.get(neighborId)!;
+ const neighbor = sequences[neighborPosition];
+ const comparison =
+ sequence.from - neighbor.from || position - neighborPosition;
+ if (
+ (isPrevious && comparison < 0) ||
+ (!isPrevious && comparison > 0)
+ ) {
+ canReuseIndex = false;
+ break;
+ }
+ }
+
+ if (!canReuseIndex) {
+ break;
+ }
+ }
+ }
+
+ if (canReuseIndex) {
+ const affected = new Set(changed);
+ for (const id of changed) {
+ for (const adjacency of [
+ previous.adjacentById.get(id),
+ previous.touchingAdjacentById.get(id),
+ ]) {
+ if (adjacency && adjacency.previousId !== null) {
+ affected.add(adjacency.previousId);
+ }
+
+ if (adjacency && adjacency.nextId !== null) {
+ affected.add(adjacency.nextId);
+ }
+ }
+ }
+
+ const updatedIndex = {...previous, sequences};
+ updateRegistryEntries({
+ index: updatedIndex,
+ previousEntries: previous.entries,
+ sequenceIds: affected,
+ });
+ indexedRegistries.set(registry, updatedIndex);
+ return updatedIndex.entries.get(sequenceId) ?? null;
+ }
+ }
+
// Share the index across all clips, but keep registrations from different
// preview roots isolated. Stable entries keep unrelated clips from rendering.
const siblingsByParent = new Map<string | null, Map<string, TSequence[]>>();
- for (const sequence of sequences) {
+ const indexById = new Map<string, number>();
+ for (let i = 0; i < sequences.length; i++) {
+ const sequence = sequences[i];
+ indexById.set(sequence.id, i);
const identity = sequence.controls?.componentIdentity;
if (!identity || !isCascadingSequence(sequence)) {
continue;
@@ -57,10 +239,7 @@ export const useTimelineSequenceRegistry = (sequenceId: string) => {
siblingsByParent.set(sequence.parent, byIdentity);
}
- const adjacentById = new Map<
- string,
- {readonly previous: TSequence | null; readonly next: TSequence | null}
- >();
+ const adjacentById = new Map<string, SequenceAdjacency>();
for (const byIdentity of siblingsByParent.values()) {
for (const siblings of byIdentity.values()) {
const sorted = CanvasInternals.sortItemsByCommitOrder(
@@ -69,36 +248,14 @@ export const useTimelineSequenceRegistry = (sequenceId: string) => {
);
for (let i = 0; i < sorted.length; i++) {
adjacentById.set(sorted[i].id, {
- previous: sorted[i - 1] ?? null,
- next: sorted[i + 1] ?? null,
+ previousId: sorted[i - 1]?.id ?? null,
+ nextId: sorted[i + 1]?.id ?? null,
});
}
}
}
- const touchingById = new Map<
- string,
- {previous: TSequence | null; next: TSequence | null}
- >();
- for (const clip of sequences) {
- const adjacent = adjacentById.get(clip.id);
- if (!adjacent) continue;
- touchingById.set(clip.id, {
- previous:
- adjacent.previous &&
- Math.abs(
- adjacent.previous.from + adjacent.previous.duration - clip.from,
- ) < 0.000001
- ? adjacent.previous
- : null,
- next:
- adjacent.next &&
- Math.abs(clip.from + clip.duration - adjacent.next.from) < 0.000001
- ? adjacent.next
- : null,
- });
- }
-
+ const touchingAdjacentById = new Map(adjacentById);
const normalTrackIds = new Set(
sequences
.filter(
@@ -125,61 +282,34 @@ export const useTimelineSequenceRegistry = (sequenceId: string) => {
clipsByTrack.set(sequence.timelineTrack.id, clips);
}
+ const normalTrackClipIds = new Set<string>();
for (const clips of clipsByTrack.values()) {
clips.sort((a, b) => a.from - b.from);
for (let i = 0; i < clips.length; i++) {
const clip = clips[i];
- const previousClip = clips[i - 1] ?? null;
- const nextClip = clips[i + 1] ?? null;
- touchingById.set(clip.id, {
- previous:
- previousClip?.parent === clip.parent &&
- Math.abs(previousClip.from + previousClip.duration - clip.from) <
- 0.000001
- ? previousClip
- : null,
- next:
- nextClip?.parent === clip.parent &&
- Math.abs(clip.from + clip.duration - nextClip.from) < 0.000001
- ? nextClip
- : null,
+ normalTrackClipIds.add(clip.id);
+ touchingAdjacentById.set(clip.id, {
+ previousId: clips[i - 1]?.id ?? null,
+ nextId: clips[i + 1]?.id ?? null,
});
}
}
- const entries = new Map<string, TimelineSequenceRegistryEntry>();
- for (const sequence of sequences) {
- const adjacent = adjacentById.get(sequence.id);
- const previousSequenceId = adjacent?.previous?.id ?? null;
- const previousOverrideId =
- adjacent?.previous?.controls?.overrideId ?? null;
- const nextOverrideId = adjacent?.next?.controls?.overrideId ?? null;
- const previousTouchingSequence =
- touchingById.get(sequence.id)?.previous ?? null;
- const nextTouchingSequence = touchingById.get(sequence.id)?.next ?? null;
- const previousEntry = previous?.entries.get(sequence.id);
- entries.set(
- sequence.id,
- previousEntry?.sequence === sequence &&
- previousEntry.previousSequenceId === previousSequenceId &&
- previousEntry.previousOverrideId === previousOverrideId &&
- previousEntry.nextOverrideId === nextOverrideId &&
- previousEntry.previousTouchingSequence === previousTouchingSequence &&
- previousEntry.nextTouchingSequence === nextTouchingSequence
- ? previousEntry
- : {
- sequence,
- previousSequenceId,
- previousOverrideId,
- nextOverrideId,
- previousTouchingSequence,
- nextTouchingSequence,
- },
- );
- }
-
- indexedRegistries.set(registry, {sequences, entries});
- return entries.get(sequenceId) ?? null;
+ const index: IndexedSequenceRegistry = {
+ sequences,
+ entries: new Map(),
+ indexById,
+ adjacentById,
+ touchingAdjacentById,
+ normalTrackClipIds,
+ };
+ updateRegistryEntries({
+ index,
+ previousEntries: previous?.entries ?? null,
+ sequenceIds: indexById.keys(),
+ });
+ indexedRegistries.set(registry, index);
+ return index.entries.get(sequenceId) ?? null;
}, [registry, sequenceId]);
return useSyncExternalStore(
diff --git a/packages/studio/src/components/WebMcp.tsx b/packages/studio/src/components/WebMcp.tsx
index fda8c00e05..4cba7a2cb5 100644
--- a/packages/studio/src/components/WebMcp.tsx
+++ b/packages/studio/src/components/WebMcp.tsx
@@ -40,6 +40,7 @@ import {persistMuteOption} from '../state/mute';
import {commonPlaybackRates, persistPlaybackRate} from '../state/playbackrate';
import {TimelineZoomCtx} from '../state/timeline-zoom';
import {useSelectComposition} from './InitialCompositionLoader';
+import {useTrackForSelection} from './InspectorPanel/use-track-for-selection';
import {
isOptionalPackageInstalled,
markOptionalPackageInstalled,
@@ -53,7 +54,6 @@ import {
getSequencesWithSelectableOutlines,
measureOutlineTargets,
} from './selected-outline-measurement';
-import {findTrackForNodePathInfo} from './Timeline/find-track-for-node-path-info';
import {getCurrentDuration, getCurrentFrame} from './Timeline/imperative-state';
import {parseKeyframeFieldFromNodePath} from './Timeline/parse-keyframe-field-from-node-path';
import {shouldShowTrackInTimeline} from './Timeline/should-show-track-in-timeline';
@@ -199,24 +199,8 @@ const WebMcpSelectionSync: FC<{
readonly selectedSequenceRef: MutableRefObject<WebMcpSequence | null>;
}> = ({currentSelectionRef, selectedSequenceRef}) => {
const {selectedItems} = useTimelineSelection();
- const sequences = Internals.useSequenceManagerSequences();
- const {overrideIdToNodePathMappings} = useContext(
- Internals.OverrideIdsToNodePathsGettersContext,
- );
const selectedItem = selectedItems.length === 1 ? selectedItems[0] : null;
- const track = useMemo(() => {
- if (selectedItem === null || selectedItem.type === 'guide') {
- return null;
- }
-
- return (
- findTrackForNodePathInfo({
- sequences,
- overrideIdsToNodePaths: overrideIdToNodePathMappings,
- nodePathInfo: selectedItem.nodePathInfo,
- }) ?? null
- );
- }, [overrideIdToNodePathMappings, selectedItem, sequences]);
+ const track = useTrackForSelection(selectedItem);
const {resolvedLocation, stack} = useResolveStackAndReactToChange(
track?.sequence.getStack ?? getNoStack,
track?.sequence.controls?.overrideId ??
diff --git a/packages/studio/src/components/selected-outline-measurement.ts b/packages/studio/src/components/selected-outline-measurement.ts
index 9c7153185c..de3e3348f6 100644
--- a/packages/studio/src/components/selected-outline-measurement.ts
+++ b/packages/studio/src/components/selected-outline-measurement.ts
@@ -405,7 +405,7 @@ export const getSequencesWithSelectableOutlines = ({
}): SequenceWithSelectedOutline[] => {
return getCanvasSelectableOutlines({
tracks: calculateTimeline({
- sequences: [...sequences],
+ sequences,
overrideIdsToNodePaths,
compositions,
}),
diff --git a/packages/studio/src/helpers/calculate-timeline.ts b/packages/studio/src/helpers/calculate-timeline.ts
index d1b17fc236..34c52bd6b9 100644
--- a/packages/studio/src/helpers/calculate-timeline.ts
+++ b/packages/studio/src/helpers/calculate-timeline.ts
@@ -1,2 +1,81 @@
import {CanvasInternals} from '@remotion/sdk';
-export const {calculateTimeline} = CanvasInternals;
+import {useContext, useRef} from 'react';
+import {Internals} from 'remotion';
+
+type TimelineInput = Parameters<typeof CanvasInternals.calculateTimeline>[0];
+type TimelineTracks = ReturnType<typeof CanvasInternals.calculateTimeline>;
+type Compositions = NonNullable<TimelineInput['compositions']>;
+const emptyCompositions: Compositions = [];
+
+// Registry snapshots are immutable and belong to one preview root. Weak keys
+// let interactions share its calculated tracks without retaining old snapshots.
+const timelinesBySnapshot = new WeakMap<
+ TimelineInput['sequences'],
+ WeakMap<
+ TimelineInput['overrideIdsToNodePaths'],
+ WeakMap<Compositions, Map<boolean, TimelineTracks>>
+ >
+>();
+
+const findCachedTimeline = (input: TimelineInput) => {
+ return timelinesBySnapshot
+ .get(input.sequences)
+ ?.get(input.overrideIdsToNodePaths)
+ ?.get(input.compositions?.length ? input.compositions : emptyCompositions)
+ ?.get(input.showConnectedCompositionChildren ?? false);
+};
+
+const cacheTimelineCalculation = (
+ input: TimelineInput,
+ tracks: TimelineTracks,
+) => {
+ const byMapping = timelinesBySnapshot.get(input.sequences) ?? new WeakMap();
+ const byCompositions =
+ byMapping.get(input.overrideIdsToNodePaths) ?? new WeakMap();
+ const compositions = input.compositions?.length
+ ? input.compositions
+ : emptyCompositions;
+ const byVisibility = byCompositions.get(compositions) ?? new Map();
+ byVisibility.set(input.showConnectedCompositionChildren ?? false, tracks);
+ byCompositions.set(compositions, byVisibility);
+ byMapping.set(input.overrideIdsToNodePaths, byCompositions);
+ timelinesBySnapshot.set(input.sequences, byMapping);
+};
+
+export const calculateTimeline = (input: TimelineInput): TimelineTracks => {
+ const cached = findCachedTimeline(input);
+ if (cached) {
+ return cached;
+ }
+
+ const tracks = CanvasInternals.calculateTimeline(input);
+ // Ad hoc callers may mutate their input between calls. Only retain results
+ // for immutable registry snapshots already published by a retained calculator.
+ if (timelinesBySnapshot.has(input.sequences)) {
+ cacheTimelineCalculation(input, tracks);
+ }
+
+ return tracks;
+};
+
+export const createTimelineCalculator = () => {
+ const calculate = CanvasInternals.createTimelineCalculator();
+ return (input: TimelineInput) => {
+ const result = calculate(input);
+ cacheTimelineCalculation(input, result.tracks);
+ return result;
+ };
+};
+
+export const useTimelineCalculator = () => {
+ const registry = useContext(Internals.SequenceRegistryContext);
+ const calculator = useRef<{
+ readonly registry: typeof registry;
+ readonly calculate: ReturnType<typeof createTimelineCalculator>;
+ } | null>(null);
+ if (calculator.current === null || calculator.current.registry !== registry) {
+ calculator.current = {registry, calculate: createTimelineCalculator()};
+ }
+
+ return calculator.current.calculate;
+};