Skip to content

🎥 Make videos programmatically with React

Index timeline selection containment checks

Jonny Burgera75d53d8a341
diff --git a/packages/studio/src/components/Timeline/TimelineSelection.tsx b/packages/studio/src/components/Timeline/TimelineSelection.tsx
index 531d7cd989..e78d6ff7c9 100644
--- a/packages/studio/src/components/Timeline/TimelineSelection.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSelection.tsx
@@ -633,41 +633,91 @@ const getTimelineSelectedKeys = (
return keys;
};
-const nodePathDescendsFrom = (
- descendant: SequenceNodePathInfo,
- ancestor: SequenceNodePathInfo,
+const containedRowsByItems = new WeakMap<
+ readonly TimelineSelection[],
+ {
+ readonly ancestorRows: ReadonlyMap<
+ string,
+ ReadonlyMap<number, ReadonlySet<string>>
+ >;
+ readonly animationRows: ReadonlySet<string>;
+ }
+>();
+
+const timelineRowContainsSelection = (
+ nodePathInfo: SequenceNodePathInfo,
+ selectedItems: readonly TimelineSelection[],
): boolean => {
- if (
- stringifySequenceExpandedRowKey(descendant.sequenceSubscriptionKey) !==
- stringifySequenceExpandedRowKey(ancestor.sequenceSubscriptionKey)
- ) {
- return false;
+ let index = containedRowsByItems.get(selectedItems);
+ if (index === undefined) {
+ const ancestorRows = new Map<string, Map<number, Set<string>>>();
+ const animationRows = new Set<string>();
+ for (const item of selectedItems) {
+ if (item.type === 'guide') {
+ continue;
+ }
+
+ if (item.type === 'keyframe' || item.type === 'easing') {
+ animationRows.add(timelineNodePathInfoToKey(item.nodePathInfo));
+ }
+
+ const {
+ auxiliaryKeys,
+ sequenceSubscriptionKey,
+ index: instanceIndex,
+ } = item.nodePathInfo;
+ // NaN never matched the strict instance-index comparison.
+ if (auxiliaryKeys.length === 0 || Number.isNaN(instanceIndex)) {
+ continue;
+ }
+
+ const sequenceKey = stringifySequenceExpandedRowKey(
+ sequenceSubscriptionKey,
+ );
+ let instances = ancestorRows.get(sequenceKey);
+ if (instances === undefined) {
+ instances = new Map();
+ ancestorRows.set(sequenceKey, instances);
+ }
+
+ let prefixes = instances.get(instanceIndex);
+ if (prefixes === undefined) {
+ prefixes = new Set();
+ instances.set(instanceIndex, prefixes);
+ }
+
+ // Only strict ancestors contain a selected row. Preserve auxiliary
+ // segments separately: a property name can itself contain a dot.
+ for (let length = 0; length < auxiliaryKeys.length; length++) {
+ prefixes.add(JSON.stringify(auxiliaryKeys.slice(0, length)));
+ }
+ }
+
+ index = {ancestorRows, animationRows};
+ containedRowsByItems.set(selectedItems, index);
}
- if (descendant.index !== ancestor.index) {
- return false;
+ if (
+ index.animationRows.size > 0 &&
+ index.animationRows.has(timelineNodePathInfoToKey(nodePathInfo))
+ ) {
+ return true;
}
- // Must be strictly deeper than `ancestor` (i.e. a descendant), not the same row.
- if (descendant.auxiliaryKeys.length <= ancestor.auxiliaryKeys.length) {
+ if (index.ancestorRows.size === 0) {
return false;
}
- return ancestor.auxiliaryKeys.every(
- (key, i) => descendant.auxiliaryKeys[i] === key,
+ return (
+ index.ancestorRows
+ .get(
+ stringifySequenceExpandedRowKey(nodePathInfo.sequenceSubscriptionKey),
+ )
+ ?.get(nodePathInfo.index)
+ ?.has(JSON.stringify(nodePathInfo.auxiliaryKeys)) ?? false
);
};
-const timelineRowContainsSelection = (
- nodePathInfo: SequenceNodePathInfo,
- selection: TimelineSelection,
-): boolean =>
- selection.type !== 'guide' &&
- (nodePathDescendsFrom(selection.nodePathInfo, nodePathInfo) ||
- ((selection.type === 'keyframe' || selection.type === 'easing') &&
- timelineNodePathInfoToKey(selection.nodePathInfo) ===
- timelineNodePathInfoToKey(nodePathInfo)));
-
export const getSelectableTimelineSequenceSelections = (
tracks: readonly Pick<TimelineTrackData, 'nodePathInfo'>[],
): TimelineSelection[] => {
@@ -1399,9 +1449,7 @@ export const TimelineSelectionProvider: React.FC<{
const containsSelection = useCallback(
(nodePathInfo: SequenceNodePathInfo) => {
- return availableSelectedItems.some((selected) =>
- timelineRowContainsSelection(nodePathInfo, selected),
- );
+ return timelineRowContainsSelection(nodePathInfo, availableSelectedItems);
},
[availableSelectedItems],
);
@@ -2092,11 +2140,10 @@ export const useTimelineRowContainsSelection = (
return false;
}
- return selectionContext
- .getSnapshot()
- .selectedItems.some((selected) =>
- timelineRowContainsSelection(nodePathInfo, selected),
- );
+ return timelineRowContainsSelection(
+ nodePathInfo,
+ selectionContext.getSnapshot().selectedItems,
+ );
}, [nodePathInfo, selectionContext]);
return useSyncExternalStore(