🎥 Make videos programmatically with React
`@remotion/studio`: Fix marquee selection losing items when scrolling
Jonny Burger2ae67dca9e48
diff --git a/packages/studio/src/components/Timeline/TimelineKeyframeDiamond.tsx b/packages/studio/src/components/Timeline/TimelineKeyframeDiamond.tsxindex 9ca3a81121..9e579b95f9 100644--- a/packages/studio/src/components/Timeline/TimelineKeyframeDiamond.tsx+++ b/packages/studio/src/components/Timeline/TimelineKeyframeDiamond.tsx@@ -38,7 +38,7 @@ const TimelineKeyframeDiamondUnmemoized: React.FC<{const ref = useRef<HTMLButtonElement>(null);const {selected, onSelect, selectable, selectionItem} =useTimelineKeyframeSelection(nodePathInfo, frame);- useTimelineMarqueeSelectableItem(selectionItem, ref);+ useTimelineMarqueeSelectableItem(selectionItem, ref, null);const {isKeyframeDragging} = useTimelineKeyframeDragState();const visuallySelected =selected || isKeyframeDragging({nodePathInfo, frame});diff --git a/packages/studio/src/components/Timeline/TimelineKeyframeEasingLine.tsx b/packages/studio/src/components/Timeline/TimelineKeyframeEasingLine.tsxindex 8c87f0e477..44ec2a44f6 100644--- a/packages/studio/src/components/Timeline/TimelineKeyframeEasingLine.tsx+++ b/packages/studio/src/components/Timeline/TimelineKeyframeEasingLine.tsx@@ -89,7 +89,7 @@ const TimelineKeyframeEasingLineInteraction: React.FC<toFrame,segmentIndex,});- useTimelineMarqueeSelectableItem(selectionItem, buttonRef);+ useTimelineMarqueeSelectableItem(selectionItem, buttonRef, null);const interactiveStyle = useMemo(() => ({...style,diff --git a/packages/studio/src/components/Timeline/TimelineSelection.tsx b/packages/studio/src/components/Timeline/TimelineSelection.tsxindex 2dce6cf14e..311555e385 100644--- a/packages/studio/src/components/Timeline/TimelineSelection.tsx+++ b/packages/studio/src/components/Timeline/TimelineSelection.tsx@@ -466,13 +466,7 @@ type TimelineSelectionContextValue = {item: TimelineSelection,getRect: () => DOMRect | null,) => () => void;- readonly getMarqueeSelection: (- marqueeRect: TimelineMarqueeRect,- lockedSelectionKind: TimelineMarqueeSelectionKind | null,- ) => {- readonly lockedSelectionKind: TimelineMarqueeSelectionKind | null;- readonly selectedItems: readonly TimelineSelection[];- };+ readonly getMarqueeSelectionCandidates: () => readonly TimelineMarqueeSelectionCandidate[];readonly containsSelection: (nodePathInfo: SequenceNodePathInfo) => boolean;readonly remapSelectionNodePaths: (mutations: readonly SequenceNodePathMutation[],@@ -490,10 +484,7 @@ const defaultTimelineSelectionContextValue: TimelineSelectionContextValue = {selectItem: () => undefined,selectItems: () => undefined,registerMarqueeSelectableItem: () => () => undefined,- getMarqueeSelection: () => ({- lockedSelectionKind: null,- selectedItems: [],- }),+ getMarqueeSelectionCandidates: () => [],containsSelection: () => false,remapSelectionNodePaths: () => undefined,clearSelection: () => undefined,@@ -1201,44 +1192,22 @@ export const TimelineSelectionProvider: React.FC<{[],);- const getMarqueeSelectionForRect = useCallback(- (- marqueeRect: TimelineMarqueeRect,- lockedSelectionKind: TimelineMarqueeSelectionKind | null,- ) => {- const candidates = [...marqueeSelectableItems.current.values()]- .sort((a, b) => a.order - b.order)- .flatMap((candidate): TimelineMarqueeSelectionCandidate[] => {- if (!canSelectItem(candidate.item)) {- return [];- }-- const rect = candidate.getRect();- if (rect === null) {- return [];- }+ const getMarqueeSelectionCandidates = useCallback(() => {+ return [...marqueeSelectableItems.current.values()]+ .sort((a, b) => a.order - b.order)+ .flatMap((candidate): TimelineMarqueeSelectionCandidate[] => {+ if (!canSelectItem(candidate.item)) {+ return [];+ }- return [- {- item: candidate.item,- rect: {- bottom: rect.bottom,- left: rect.left,- right: rect.right,- top: rect.top,- },- },- ];- });+ const rect = candidate.getRect();+ if (rect === null) {+ return [];+ }- return getTimelineMarqueeSelection({- candidates,- lockedSelectionKind,- marqueeRect,+ return [{item: candidate.item, rect}];});- },- [canSelectItem],- );+ }, [canSelectItem]);const clearSelection = useCallback(() => {selectionScope.current = null;@@ -1342,7 +1311,7 @@ export const TimelineSelectionProvider: React.FC<{selectItem,selectItems,registerMarqueeSelectableItem,- getMarqueeSelection: getMarqueeSelectionForRect,+ getMarqueeSelectionCandidates,containsSelection,remapSelectionNodePaths,clearSelection,@@ -1357,7 +1326,7 @@ export const TimelineSelectionProvider: React.FC<{selectItem,selectItems,registerMarqueeSelectableItem,- getMarqueeSelectionForRect,+ getMarqueeSelectionCandidates,containsSelection,remapSelectionNodePaths,clearSelection,@@ -1421,7 +1390,7 @@ export const useCurrentTimelineSelectionStateAsRef = () => {};export const useTimelineMarqueeSelection = () => {- const {canSelect, getMarqueeSelection, selectedItems, selectItems} =+ const {canSelect, getMarqueeSelectionCandidates, selectedItems, selectItems} =useTimelineSelection();const {isHighestContext} = useZIndex();const [marqueeRect, setMarqueeRect] = useState<TimelineMarqueeRect | null>(@@ -1484,6 +1453,26 @@ export const useTimelineMarqueeSelection = () => {let lastClientY = event.clientY;const extendSelection = event.metaKey || event.ctrlKey;const selectionBeforeMarquee = selectedItems;+ // Keep content-space bounds for this drag even after virtualization+ // unmounts an item. Refresh mounted items as scrolling reveals them.+ const candidates = new Map<string, TimelineMarqueeSelectionCandidate>();+ const refreshCandidates = () => {+ const scrollLeft = scrollable?.scrollLeft ?? 0;+ const scrollTop = verticalScroll?.scrollTop ?? 0;+ for (const candidate of getMarqueeSelectionCandidates()) {+ candidates.set(getTimelineSelectionKey(candidate.item), {+ item: candidate.item,+ rect: {+ left: candidate.rect.left + scrollLeft,+ right: candidate.rect.right + scrollLeft,+ top: candidate.rect.top + scrollTop,+ bottom: candidate.rect.bottom + scrollTop,+ },+ });+ }+ };++ refreshCandidates();const updateSelection = (clientX: number, clientY: number) => {lastClientX = clientX;@@ -1542,7 +1531,19 @@ export const useTimelineMarqueeSelection = () => {startX: anchorX,startY: anchorY,});- const nextSelection = getMarqueeSelection(rect, lockedSelectionKind);+ refreshCandidates();+ const scrollLeft = scrollable?.scrollLeft ?? 0;+ const scrollTop = verticalScroll?.scrollTop ?? 0;+ const nextSelection = getTimelineMarqueeSelection({+ candidates: [...candidates.values()],+ lockedSelectionKind,+ marqueeRect: {+ left: rect.left + scrollLeft,+ right: rect.right + scrollLeft,+ top: rect.top + scrollTop,+ bottom: rect.bottom + scrollTop,+ },+ });lockedSelectionKind = nextSelection.lockedSelectionKind;setMarqueeRect({bottom: Math.min(rect.bottom, bounds.bottom),@@ -1612,7 +1613,7 @@ export const useTimelineMarqueeSelection = () => {},[canSelect,- getMarqueeSelection,+ getMarqueeSelectionCandidates,isHighestContext,selectedItems,selectItems,@@ -1625,6 +1626,7 @@ export const useTimelineMarqueeSelection = () => {export const useTimelineMarqueeSelectableItem = (item: TimelineSelection | null,ref: React.RefObject<Element | null>,+ horizontalBounds: {readonly cropLeft: number; readonly width: number} | null,) => {const selectionContext = useContext(TimelineRowSelectionContext);if (selectionContext === null) {@@ -1640,11 +1642,22 @@ export const useTimelineMarqueeSelectableItem = (return;}- return registerMarqueeSelectableItem(- item,- () => ref.current?.getBoundingClientRect() ?? null,- );- }, [item, ref, registerMarqueeSelectableItem]);+ return registerMarqueeSelectableItem(item, () => {+ const rect = ref.current?.getBoundingClientRect() ?? null;+ if (rect === null || horizontalBounds === null) {+ return rect;+ }++ // Sequence bars are cropped to the horizontal render window. Hit-test+ // the full sequence so scrolling does not change its selectable bounds.+ return new DOMRect(+ rect.left - horizontalBounds.cropLeft,+ rect.top,+ horizontalBounds.width,+ rect.height,+ );+ });+ }, [horizontalBounds, item, ref, registerMarqueeSelectableItem]);};export const useTimelineRowSelection = (diff --git a/packages/studio/src/components/Timeline/TimelineSequence.tsx b/packages/studio/src/components/Timeline/TimelineSequence.tsxindex 3d8620b18c..5124fb5fa1 100644--- a/packages/studio/src/components/Timeline/TimelineSequence.tsx+++ b/packages/studio/src/components/Timeline/TimelineSequence.tsx@@ -305,6 +305,10 @@ const TimelineSequenceCurrentFrame: React.FC<{readonly leftEdgeVisible: boolean;readonly negativeStartClipped: boolean;readonly style: React.CSSProperties;+ readonly marqueeHorizontalBounds: {+ readonly cropLeft: number;+ readonly width: number;+ } | null;readonly children: React.ReactNode;readonly edgeDragHandles: React.ReactNode;readonly nodePathInfo: SequenceNodePathInfo | null;@@ -326,6 +330,7 @@ const TimelineSequenceCurrentFrame: React.FC<{leftEdgeVisible,negativeStartClipped,style,+ marqueeHorizontalBounds,children,edgeDragHandles,nodePathInfo,@@ -342,7 +347,7 @@ const TimelineSequenceCurrentFrame: React.FC<{const {onSelect, selectable, selected, selectionItem} =useTimelineRowSelection(nodePathInfo);const containsSelection = useTimelineRowContainsSelection(nodePathInfo);- useTimelineMarqueeSelectableItem(selectionItem, ref);+ useTimelineMarqueeSelectableItem(selectionItem, ref, marqueeHorizontalBounds);const onPointerDown = useCallback((e: React.PointerEvent<HTMLDivElement>) => {@@ -1280,6 +1285,10 @@ const TimelineSequenceInner: React.FC<{height: '100%',};}, [visibleLayout]);+ const marqueeHorizontalBounds = useMemo(+ () => ({cropLeft: visibleLayout?.cropLeft ?? 0, width}),+ [visibleLayout?.cropLeft, width],+ );const showLeftBorderRadius =visibleLayout?.leftEdgeVisible === true &&localStart >= 0 &&@@ -1452,6 +1461,7 @@ const TimelineSequenceInner: React.FC<{leftEdgeVisible={visibleLayout.leftEdgeVisible}negativeStartClipped={negativeStartClipped}style={style}+ marqueeHorizontalBounds={marqueeHorizontalBounds}nodePathInfo={nodePathInfo}sequenceFrameOffset={sequenceFrameOffset}fromCanUpdate={fromCanUpdate}