Skip to content

🎥 Make videos programmatically with React

Speed up track lookup for grouped timeline drags

Jonny Burger91756cac5c68
diff --git a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
index 85af9f65e0..cf798f0abc 100644
--- a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
@@ -43,7 +43,10 @@ import type {
MoveEffectKeyframeChange,
MoveSequenceKeyframeChange,
} from './call-move-keyframe';
-import {findTrackForNodePathInfo} from './find-track-for-node-path-info';
+import {
+ createTrackLookupForNodePathInfo,
+ findTrackForNodePathInfo,
+} from './find-track-for-node-path-info';
import {getKeyframeTrimConstraints} from './get-keyframe-trim-constraints';
import {
getKeyframedSequenceDragTargets,
@@ -1043,9 +1046,14 @@ export const getTimelineSequenceDurationDragTargets = ({
? selectedSequenceItems.map((item) => item.nodePathInfo)
: [draggedNodePathInfo];
const targets = new Map<string, TimelineSequenceDurationDragTarget>();
+ const findTrack =
+ targetNodePathInfos.length > 1
+ ? createTrackLookupForNodePathInfo(tracks)
+ : (nodePathInfo: SequenceNodePathInfo) =>
+ findTrackForNodePathInfo({tracks, nodePathInfo});
for (const nodePathInfo of targetNodePathInfos) {
- const track = findTrackForNodePathInfo({tracks, nodePathInfo});
+ const track = findTrack(nodePathInfo);
const originalSequence = track
? sequences.find((sequence) => sequence.id === track.sequence.id)
: null;
@@ -1260,9 +1268,14 @@ export const getTimelineSequenceLeftEdgeDragTargets = ({
? selectedSequenceItems.map((item) => item.nodePathInfo)
: [draggedNodePathInfo];
const targets = new Map<string, TimelineSequenceLeftEdgeDragTarget>();
+ const findTrack =
+ targetNodePathInfos.length > 1
+ ? createTrackLookupForNodePathInfo(tracks)
+ : (nodePathInfo: SequenceNodePathInfo) =>
+ findTrackForNodePathInfo({tracks, nodePathInfo});
for (const nodePathInfo of targetNodePathInfos) {
- const track = findTrackForNodePathInfo({tracks, nodePathInfo});
+ const track = findTrack(nodePathInfo);
const originalSequence = track
? sequences.find((sequence) => sequence.id === track.sequence.id)
: null;
@@ -1862,8 +1875,13 @@ export const getTimelineSequenceFromDragTargets = ({
const sequencesById = new Map(
sequences.map((sequence) => [sequence.id, sequence]),
);
+ const findTrack =
+ targetNodePathInfos.length > 1
+ ? createTrackLookupForNodePathInfo(tracks)
+ : (nodePathInfo: SequenceNodePathInfo) =>
+ findTrackForNodePathInfo({tracks, nodePathInfo});
const selectedSequences = targetNodePathInfos.flatMap((nodePathInfo) => {
- const track = findTrackForNodePathInfo({tracks, nodePathInfo});
+ const track = findTrack(nodePathInfo);
const originalSequence = track
? sequencesById.get(track.sequence.id)
: undefined;
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 4c23ba0ee5..1a917f021a 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
@@ -23,3 +23,52 @@ export const findTrackForNodePathInfo = ({
candidate.nodePathInfo.index === nodePathInfo.index,
);
};
+
+export const createTrackLookupForNodePathInfo = (
+ tracks: readonly TimelineTrackData[],
+) => {
+ const tracksBySource = new Map<string, Map<number, TimelineTrackData>>();
+ let nextTrackIndex = 0;
+ return (
+ nodePathInfo: SequenceNodePathInfo,
+ ): TimelineTrackData | undefined => {
+ // The original strict instance-index comparison never matches NaN.
+ if (Number.isNaN(nodePathInfo.index)) {
+ return undefined;
+ }
+
+ const key = stringifySequenceExpandedRowKey(
+ nodePathInfo.sequenceSubscriptionKey,
+ );
+ const cached = tracksBySource.get(key)?.get(nodePathInfo.index);
+ if (cached !== undefined) {
+ return cached;
+ }
+
+ // Stop at the requested track so small selections only index their prefix.
+ while (nextTrackIndex < tracks.length) {
+ const track = tracks[nextTrackIndex++];
+ const info = track.nodePathInfo;
+ if (info === null || Number.isNaN(info.index)) {
+ continue;
+ }
+
+ const sourceKey = stringifySequenceExpandedRowKey(
+ info.sequenceSubscriptionKey,
+ );
+ const instances =
+ tracksBySource.get(sourceKey) ?? new Map<number, TimelineTrackData>();
+ // Preserve the first match for each source and rendered instance.
+ if (!instances.has(info.index)) {
+ instances.set(info.index, track);
+ }
+
+ tracksBySource.set(sourceKey, instances);
+ if (sourceKey === key && info.index === nodePathInfo.index) {
+ return track;
+ }
+ }
+
+ return undefined;
+ };
+};