Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Preserve selection when dragging timeline items

JonnyBurger1b729a9eeeda
diff --git a/packages/studio/src/components/Timeline/TimelineRowChrome.tsx b/packages/studio/src/components/Timeline/TimelineRowChrome.tsx
index 36de2e73a8..58d6ccc86b 100644
--- a/packages/studio/src/components/Timeline/TimelineRowChrome.tsx
+++ b/packages/studio/src/components/Timeline/TimelineRowChrome.tsx
@@ -10,7 +10,7 @@ import {TimelineRowLayoutContext} from './TimelineRowLayoutContext';
import type {TimelineSelectionInteraction} from './TimelineSelection';
import {
getTimelineRowHighlightBackground,
- shouldSelectTimelineRowOnPointerDown,
+ isTimelineSelectionModifierEvent,
TIMELINE_SELECTED_BACKGROUND,
} from './TimelineSelection';
@@ -132,14 +132,7 @@ export const TimelineRowChrome: React.FC<{
(e: React.PointerEvent<HTMLDivElement>) => {
if (e.button === 0) {
e.stopPropagation();
- if (
- shouldSelectTimelineRowOnPointerDown({
- selected,
- shiftKey: e.shiftKey,
- metaKey: e.metaKey,
- ctrlKey: e.ctrlKey,
- })
- ) {
+ if (isTimelineSelectionModifierEvent(e)) {
onSelect({
shiftKey: e.shiftKey,
toggleKey: e.metaKey || e.ctrlKey,
@@ -147,7 +140,7 @@ export const TimelineRowChrome: React.FC<{
}
}
},
- [onSelect, selected],
+ [onSelect],
);
const onContextMenu = useCallback(
@@ -160,7 +153,7 @@ export const TimelineRowChrome: React.FC<{
[onSelect, selected],
);
- const performDoubleClickIfNotInteractive = useCallback(
+ const onClick = useCallback(
(e: React.MouseEvent<HTMLDivElement>) => {
const {target} = e;
if (
@@ -175,19 +168,21 @@ export const TimelineRowChrome: React.FC<{
return;
}
- onDoubleClick?.(e);
- },
- [onDoubleClick],
- );
- const onClick = useCallback(
- (e: React.MouseEvent<HTMLDivElement>) => {
+ if (
+ selectable &&
+ e.button === 0 &&
+ !isTimelineSelectionModifierEvent(e)
+ ) {
+ onSelect();
+ }
+
if (!doubleClickTracker.acceptClickAsDoubleClick(e)) {
return;
}
- performDoubleClickIfNotInteractive(e);
+ onDoubleClick?.(e);
},
- [doubleClickTracker, performDoubleClickIfNotInteractive],
+ [doubleClickTracker, onDoubleClick, onSelect, selectable],
);
const highlightBackground = getTimelineRowHighlightBackground({
diff --git a/packages/studio/src/components/Timeline/TimelineSequence.tsx b/packages/studio/src/components/Timeline/TimelineSequence.tsx
index 5308d411d5..76ee2d66b5 100644
--- a/packages/studio/src/components/Timeline/TimelineSequence.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequence.tsx
@@ -93,7 +93,6 @@ import {
getTimelineColor,
getTimelineSequenceSelectionKey,
isTimelineSelectionModifierEvent,
- shouldSelectTimelineRowOnPointerDown,
TIMELINE_MARQUEE_ITEM_ATTR,
useTimelineMarqueeSelectableItem,
useTimelineRowContainsSelection,
@@ -403,18 +402,12 @@ const TimelineSequenceBar: React.FC<{
(e: React.PointerEvent<HTMLDivElement>) => {
if (e.button === 0) {
e.stopPropagation();
- if (
- shouldSelectTimelineRowOnPointerDown({
- selected,
- shiftKey: e.shiftKey,
- metaKey: e.metaKey,
- ctrlKey: e.ctrlKey,
- })
- ) {
+ if (isTimelineSelectionModifierEvent(e)) {
onSelect({
shiftKey: e.shiftKey,
toggleKey: e.metaKey || e.ctrlKey,
});
+ return;
}
if (fromCanUpdate) {
@@ -422,7 +415,7 @@ const TimelineSequenceBar: React.FC<{
}
}
},
- [fromCanUpdate, onMoveDragPointerDown, onSelect, selected],
+ [fromCanUpdate, onMoveDragPointerDown, onSelect],
);
const negativeStartEnd = negativeStart
? negativeStart.left + negativeStart.width
@@ -1061,16 +1054,26 @@ const useTimelineSequenceState = (props: TimelineSequenceProps) => {
);
const onSequenceClick = useCallback(
(e: React.MouseEvent<HTMLDivElement>) => {
+ if (dragAwareDoubleClick.consumePointerGestureWasDragged()) {
+ return;
+ }
+
+ if (
+ selectable &&
+ e.button === 0 &&
+ !isTimelineSelectionModifierEvent(e)
+ ) {
+ onSelect();
+ }
+
if (!dragAwareDoubleClick.acceptClickAsDoubleClick(e)) {
return;
}
performSequenceDoubleClick(e);
},
- [dragAwareDoubleClick, performSequenceDoubleClick],
+ [dragAwareDoubleClick, onSelect, performSequenceDoubleClick, selectable],
);
- const canHandleSequenceDoubleClick =
- connectedCompositions.length === 1 || canOpenSource;
const canDeleteFromSource = Boolean(nodePath && validatedLocation?.source);
const deleteDisabled =
!previewInteractive || !s.controls || !canDeleteFromSource;
@@ -1479,7 +1482,6 @@ const useTimelineSequenceState = (props: TimelineSequenceProps) => {
selected,
getContextMenuItems,
frozenFrame,
- canHandleSequenceDoubleClick,
onSequenceClick,
timelineVolume,
displayDurationInFrames,
@@ -1555,7 +1557,6 @@ const TimelineSequenceLayoutInner: React.FC<{
selected,
getContextMenuItems,
frozenFrame,
- canHandleSequenceDoubleClick,
onSequenceClick,
timelineVolume,
displayDurationInFrames,
@@ -1891,7 +1892,7 @@ const TimelineSequenceLayoutInner: React.FC<{
frozenFrame={frozenFrame}
onMoveDragPointerDown={onMoveDragPointerDown}
onPointerDownCapture={dragAwareDoubleClick.beginPointerGesture}
- onClick={canHandleSequenceDoubleClick ? onSequenceClick : null}
+ onClick={onSequenceClick}
edgeDragHandles={
<>
{(showLeftEdgeDragHandle || secondaryLeftEdgeAction) &&
diff --git a/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx b/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
index 856441b7ce..a81e407f56 100644
--- a/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
@@ -884,6 +884,15 @@ const TimelineSequenceItemInner: React.FC<{
if (!didDrag) {
didDrag = true;
capturePointer();
+ const draggedItem = {type: 'sequence', nodePathInfo} as const;
+
+ if (
+ !currentSelection.current.isSelected(draggedItem) &&
+ !currentSelection.current.containsSelection(nodePathInfo)
+ ) {
+ currentSelection.current.selectItem(draggedItem);
+ }
+
previousUserSelect = document.body.style.userSelect;
previousWebkitUserSelect = document.body.style.webkitUserSelect;
document.body.style.userSelect = 'none';
@@ -995,6 +1004,7 @@ const TimelineSequenceItemInner: React.FC<{
},
[
canReorderSequence,
+ currentSelection,
isStill,
nodePath,
nodePathKey,
diff --git a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
index d64e717966..c546ad5c1d 100644
--- a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
@@ -58,6 +58,7 @@ import {
saveSequenceProps,
type SaveSequencePropChange,
} from './save-sequence-prop';
+import {useTimelineKeyframeDragState} from './TimelineKeyframeDragState';
import {
getTimelineSequenceSelectionKey,
shouldSelectTimelineRowOnPointerDown,
@@ -2150,6 +2151,8 @@ export const useTimelineSequenceFromDrag = ({
);
const {previewServerState} = useContext(StudioServerConnectionCtx);
const currentSelection = useCurrentTimelineSelectionStateAsRef();
+ const {setDraggedKeyframes, clearDraggedKeyframes} =
+ useTimelineKeyframeDragState();
const {editorSnapping} = useContext(EditorSnappingContext);
const updateSnapFrameRef = useContext(TimelineSnapIndicatorContext);
@@ -2161,6 +2164,11 @@ export const useTimelineSequenceFromDrag = ({
didMove: boolean;
pxPerFrame: number;
targets: readonly TimelineSequenceFromDragTarget[];
+ selectionBeforeDrag: readonly TimelineSelection[];
+ animationSelections: readonly Extract<
+ TimelineSelection,
+ {type: 'keyframe' | 'easing'}
+ >[];
} | null>(null);
const latestRef = useRef({
@@ -2198,6 +2206,7 @@ export const useTimelineSequenceFromDrag = ({
updateSnapFrameRef?.current(null);
dragStateRef.current = null;
latestRef.current.onDragEnd(dragState.didMove);
+ clearDraggedKeyframes();
document.body.style.userSelect = '';
document.body.style.webkitUserSelect = '';
const {
@@ -2231,6 +2240,33 @@ export const useTimelineSequenceFromDrag = ({
return;
}
+ if (dragState.animationSelections.length > 0) {
+ currentSelection.current.selectItems(
+ dragState.selectionBeforeDrag.map((selection) => {
+ if (
+ !dragState.animationSelections.some((item) => item === selection)
+ ) {
+ return selection;
+ }
+
+ if (selection.type === 'keyframe') {
+ return {
+ ...selection,
+ frame: selection.frame + dragState.latestDeltaFrames,
+ };
+ }
+
+ return selection.type === 'easing'
+ ? {
+ ...selection,
+ fromFrame: selection.fromFrame + dragState.latestDeltaFrames,
+ toFrame: selection.toFrame + dragState.latestDeltaFrames,
+ }
+ : selection;
+ }),
+ );
+ }
+
const savePromise = saveSequenceProps({
addedKeyframes: null,
changes,
@@ -2266,7 +2302,7 @@ export const useTimelineSequenceFromDrag = ({
});
});
},
- [updateSnapFrameRef],
+ [clearDraggedKeyframes, currentSelection, updateSnapFrameRef],
);
const onPointerDown = useCallback(
@@ -2302,6 +2338,8 @@ export const useTimelineSequenceFromDrag = ({
stopPointerSessionRef.current?.();
e.preventDefault();
+ const draggedSelectionKey =
+ getTimelineSequenceSelectionKey(latestNodePathInfo);
dragStateRef.current = {
initialClientX: e.clientX,
latestDeltaFrames: 0,
@@ -2309,6 +2347,18 @@ export const useTimelineSequenceFromDrag = ({
didMove: false,
pxPerFrame,
targets,
+ selectionBeforeDrag: latestSelectedItems,
+ animationSelections: latestSelectedItems.filter(
+ (
+ selection,
+ ): selection is Extract<
+ TimelineSelection,
+ {type: 'keyframe' | 'easing'}
+ > =>
+ (selection.type === 'keyframe' || selection.type === 'easing') &&
+ getTimelineSequenceSelectionKey(selection.nodePathInfo) ===
+ draggedSelectionKey,
+ ),
};
document.body.style.userSelect = 'none';
document.body.style.webkitUserSelect = 'none';
@@ -2343,6 +2393,16 @@ export const useTimelineSequenceFromDrag = ({
// remain targeted at the bar.
capturePointer();
dragState.didMove = true;
+ const draggedItem: TimelineSelection = {
+ type: 'sequence',
+ nodePathInfo: latestNodePathInfo,
+ };
+ if (
+ !currentSelection.current.isSelected(draggedItem) &&
+ !currentSelection.current.containsSelection(latestNodePathInfo)
+ ) {
+ currentSelection.current.selectItem(draggedItem);
+ }
}
updateSnapFrameRef?.current(dragState.didMove ? snapFrame : null);
@@ -2352,6 +2412,21 @@ export const useTimelineSequenceFromDrag = ({
}
dragState.lastPreviewDeltaFrames = deltaFrames;
+ if (dragState.animationSelections.length > 0) {
+ setDraggedKeyframes(
+ dragState.animationSelections.flatMap((selection) =>
+ selection.type === 'keyframe'
+ ? [
+ {
+ nodePathInfo: selection.nodePathInfo,
+ frame: selection.frame + deltaFrames,
+ },
+ ]
+ : [],
+ ),
+ );
+ }
+
const updates: DragOverrideUpdate[] = [];
const effectUpdates: EffectDragOverrideUpdate[] = [];
@@ -2440,6 +2515,7 @@ export const useTimelineSequenceFromDrag = ({
finishDrag,
propStatusesRef,
sequencesRef,
+ setDraggedKeyframes,
timelineDurationInFrames,
updateSnapFrameRef,
windowWidth,