Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Drag easing segments with keyframes

Jonny Burger418a2ccdc2eb
diff --git a/packages/studio/src/components/Timeline/TimelineKeyframeEasingLine.tsx b/packages/studio/src/components/Timeline/TimelineKeyframeEasingLine.tsx
index 3390fd1224..02ee4e5cde 100644
--- a/packages/studio/src/components/Timeline/TimelineKeyframeEasingLine.tsx
+++ b/packages/studio/src/components/Timeline/TimelineKeyframeEasingLine.tsx
@@ -22,6 +22,7 @@ import {
type TimelineEasingValue,
updateSelectedTimelineEasings,
} from './update-selected-easing';
+import {useTimelineEasingKeyframeDrag} from './use-timeline-keyframe-drag';
const hitTargetHeight = 12;
const lineHeight = 2;
@@ -251,21 +252,12 @@ const TimelineKeyframeEasingLineUnmemoized: React.FC<{
[selected],
);
- const onPointerDown = useCallback(
- (event: React.PointerEvent<HTMLButtonElement>) => {
- if (!selectable || event.button !== 0) {
- return;
- }
-
- event.preventDefault();
- event.stopPropagation();
- onSelect({
- shiftKey: event.shiftKey,
- toggleKey: event.metaKey || event.ctrlKey,
- });
- },
- [onSelect, selectable],
- );
+ const onPointerDown = useTimelineEasingKeyframeDrag({
+ onSelect,
+ selectable,
+ selected,
+ selectionItem,
+ });
if (style === null) {
return null;
diff --git a/packages/studio/src/components/Timeline/use-timeline-keyframe-drag.ts b/packages/studio/src/components/Timeline/use-timeline-keyframe-drag.ts
index e581854839..b4352ecc41 100644
--- a/packages/studio/src/components/Timeline/use-timeline-keyframe-drag.ts
+++ b/packages/studio/src/components/Timeline/use-timeline-keyframe-drag.ts
@@ -21,6 +21,7 @@ import {parseKeyframeFieldFromNodePath} from './parse-keyframe-field-from-node-p
import {useTimelineKeyframeDragState} from './TimelineKeyframeDragState';
import {
useCurrentTimelineSelectionStateAsRef,
+ type TimelineEasingSelection,
type TimelineSelection,
type TimelineSelectionInteraction,
} from './TimelineSelection';
@@ -54,6 +55,75 @@ const isKeyframeSelection = (
selection: TimelineSelection,
): selection is TimelineKeyframeSelection => selection.type === 'keyframe';
+const isEasingSelection = (
+ selection: TimelineSelection,
+): selection is TimelineEasingSelection => selection.type === 'easing';
+
+const keyframesForEasing = (
+ easing: TimelineEasingSelection,
+): TimelineKeyframeSelection[] => [
+ {
+ type: 'keyframe',
+ nodePathInfo: easing.nodePathInfo,
+ frame: easing.fromFrame,
+ },
+ {
+ type: 'keyframe',
+ nodePathInfo: easing.nodePathInfo,
+ frame: easing.toFrame,
+ },
+];
+
+const deduplicateKeyframeSelections = (
+ keyframes: readonly TimelineKeyframeSelection[],
+) => {
+ const seen = new Set<string>();
+ return keyframes.filter((keyframe) => {
+ const key = JSON.stringify({
+ nodePathInfo: keyframe.nodePathInfo,
+ frame: keyframe.frame,
+ });
+ if (seen.has(key)) {
+ return false;
+ }
+
+ seen.add(key);
+ return true;
+ });
+};
+
+export const getKeyframesForTimelineEasingDrag = ({
+ currentSelections,
+ interaction,
+ selectionItem,
+ selected,
+}: {
+ readonly currentSelections: readonly TimelineSelection[];
+ readonly interaction: TimelineSelectionInteraction;
+ readonly selectionItem: TimelineEasingSelection;
+ readonly selected: boolean;
+}): TimelineKeyframeSelection[] => {
+ if (interaction.shiftKey || interaction.toggleKey) {
+ return keyframesForEasing(selectionItem);
+ }
+
+ const selectedKeyframes = currentSelections.filter(isKeyframeSelection);
+ if (selectedKeyframes.length > 0) {
+ return selectedKeyframes;
+ }
+
+ if (!selected) {
+ return keyframesForEasing(selectionItem);
+ }
+
+ const selectedEasings = currentSelections.filter(isEasingSelection);
+ return deduplicateKeyframeSelections(
+ (selectedEasings.length > 0 ? selectedEasings : [selectionItem]).flatMap(
+ keyframesForEasing,
+ ),
+ );
+};
+
const getCodeValueForTarget = ({
propStatuses,
nodePath,
@@ -610,3 +680,269 @@ export const useTimelineKeyframeDrag = ({
],
);
};
+
+export const useTimelineEasingKeyframeDrag = ({
+ onSelect,
+ selectable,
+ selected,
+ selectionItem,
+}: {
+ readonly onSelect: (interaction?: TimelineSelectionInteraction) => void;
+ readonly selectable: boolean;
+ readonly selected: boolean;
+ readonly selectionItem: TimelineEasingSelection;
+}) => {
+ const videoConfig = useVideoConfig();
+ const timelineWidth = useContext(TimelineWidthContext);
+ const {previewServerState} = useContext(StudioServerConnectionCtx);
+ const sequencesRef = useContext(Internals.SequenceManagerRefContext);
+ const {overrideIdToNodePathMappings} = useContext(
+ Internals.OverrideIdsToNodePathsGettersContext,
+ );
+ const propStatusesRef = useContext(
+ Internals.VisualModePropStatusesRefContext,
+ );
+ const {
+ clearDragOverrides,
+ clearEffectDragOverrides,
+ setPropStatuses,
+ setDragOverrides,
+ setEffectDragOverrides,
+ } = useContext(Internals.VisualModeSettersContext);
+ const currentSelection = useCurrentTimelineSelectionStateAsRef();
+ const {clearDraggedKeyframes, setDraggedKeyframes} =
+ useTimelineKeyframeDragState();
+
+ return useCallback(
+ (e: React.PointerEvent<HTMLButtonElement>) => {
+ if (e.button !== 0 || !selectable) {
+ return;
+ }
+
+ e.preventDefault();
+ e.stopPropagation();
+
+ const interaction = {
+ shiftKey: e.shiftKey,
+ toggleKey: e.metaKey || e.ctrlKey,
+ };
+
+ if (timelineWidth === null || previewServerState.type !== 'connected') {
+ onSelect(interaction);
+ return;
+ }
+
+ const keyframesToDrag = getKeyframesForTimelineEasingDrag({
+ currentSelections: currentSelection.current.selectedItems,
+ interaction,
+ selectionItem,
+ selected,
+ });
+
+ const startClientX = e.clientX;
+ let dragTargets: TimelineKeyframeDragTarget[] | null = null;
+ let hasDragged = false;
+ let lastDelta = 0;
+ const propStatuses = propStatusesRef.current;
+ const sequences = sequencesRef.current;
+
+ const resolveDragTargets = () => {
+ if (dragTargets !== null) {
+ return dragTargets;
+ }
+
+ dragTargets = keyframesToDrag
+ .map((keyframe) =>
+ getTimelineKeyframeDragTarget({
+ propStatuses,
+ displayFrame: keyframe.frame,
+ nodePathInfo: keyframe.nodePathInfo,
+ overrideIdsToNodePaths: overrideIdToNodePathMappings,
+ sequences,
+ }),
+ )
+ .filter(
+ (target): target is TimelineKeyframeDragTarget => target !== null,
+ );
+ return dragTargets;
+ };
+
+ const cleanup = () => {
+ window.removeEventListener('pointermove', onPointerMove);
+ window.removeEventListener('pointerup', onPointerUp);
+ window.removeEventListener('pointercancel', onPointerCancel);
+ };
+
+ const clearActiveOverrides = () => {
+ const targets = dragTargets;
+ if (targets === null) {
+ return;
+ }
+
+ clearDragOverridesForTargets({
+ clearDragOverrides,
+ clearEffectDragOverrides,
+ targets,
+ });
+ };
+
+ const onPointerMove = (event: PointerEvent) => {
+ const clientXDelta = event.clientX - startClientX;
+ if (!hasDragged && Math.abs(clientXDelta) < pointerDragThreshold) {
+ return;
+ }
+
+ const targets = resolveDragTargets();
+ if (targets.length === 0) {
+ cleanup();
+ clearDraggedKeyframes();
+ return;
+ }
+
+ const rawDelta = getFrameDelta({
+ clientXDelta,
+ durationInFrames: videoConfig.durationInFrames,
+ timelineWidth,
+ });
+ const delta = getBoundedKeyframeDragDelta({
+ delta: rawDelta,
+ durationInFrames: videoConfig.durationInFrames,
+ targets,
+ });
+
+ if (hasDragged && delta === lastDelta) {
+ return;
+ }
+
+ hasDragged = true;
+ lastDelta = delta;
+ if (!canMoveTimelineKeyframeDragTargets({targets, delta})) {
+ clearActiveOverrides();
+ clearDraggedKeyframes();
+ return;
+ }
+
+ setDraggedKeyframes(
+ targets.map((target) => ({
+ nodePathInfo: target.nodePathInfo,
+ frame: target.displayFrame + delta,
+ })),
+ );
+ applyDragOverrides({
+ delta,
+ setDragOverrides,
+ setEffectDragOverrides,
+ targets,
+ });
+ };
+
+ const onPointerUp = () => {
+ cleanup();
+
+ const targets = dragTargets;
+ if (!hasDragged) {
+ onSelect(interaction);
+ clearActiveOverrides();
+ clearDraggedKeyframes();
+ return;
+ }
+
+ if (lastDelta === 0 || targets === null) {
+ clearActiveOverrides();
+ clearDraggedKeyframes();
+ return;
+ }
+
+ if (
+ !canMoveTimelineKeyframeDragTargets({
+ targets,
+ delta: lastDelta,
+ })
+ ) {
+ clearActiveOverrides();
+ clearDraggedKeyframes();
+ return;
+ }
+
+ currentSelection.current.selectItems(
+ targets.map((target) => ({
+ type: 'keyframe',
+ nodePathInfo: target.nodePathInfo,
+ frame: target.displayFrame + lastDelta,
+ })),
+ );
+
+ clearActiveOverrides();
+ clearDraggedKeyframes();
+
+ callMoveKeyframes({
+ sequenceKeyframes: targets
+ .filter(
+ (
+ target,
+ ): target is TimelineKeyframeDragTarget & {
+ type: 'sequence';
+ } => target.type === 'sequence',
+ )
+ .map((target) => ({
+ fileName: target.fileName,
+ nodePath: target.nodePath,
+ fieldKey: target.fieldKey,
+ fromFrame: target.sourceFrame,
+ toFrame: target.sourceFrame + lastDelta,
+ schema: target.schema,
+ })),
+ effectKeyframes: targets
+ .filter(
+ (
+ target,
+ ): target is TimelineKeyframeDragTarget & {
+ type: 'effect';
+ } => target.type === 'effect',
+ )
+ .map((target) => ({
+ fileName: target.fileName,
+ nodePath: target.nodePath,
+ effectIndex: target.effectIndex,
+ fieldKey: target.fieldKey,
+ fromFrame: target.sourceFrame,
+ toFrame: target.sourceFrame + lastDelta,
+ schema: target.schema,
+ })),
+ setPropStatuses,
+ clientId: previewServerState.clientId,
+ }).catch(() => undefined);
+ };
+
+ const onPointerCancel = () => {
+ cleanup();
+ clearActiveOverrides();
+ clearDraggedKeyframes();
+ };
+
+ window.addEventListener('pointermove', onPointerMove);
+ window.addEventListener('pointerup', onPointerUp);
+ window.addEventListener('pointercancel', onPointerCancel);
+ },
+ [
+ clearDragOverrides,
+ clearEffectDragOverrides,
+ clearDraggedKeyframes,
+ currentSelection,
+ onSelect,
+ overrideIdToNodePathMappings,
+ propStatusesRef,
+ previewServerState,
+ selectable,
+ selected,
+ selectionItem,
+ sequencesRef,
+ setPropStatuses,
+ setDragOverrides,
+ setDraggedKeyframes,
+ setEffectDragOverrides,
+ timelineWidth,
+ videoConfig.durationInFrames,
+ ],
+ );
+};
diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.ts
index 46bedeb9ec..0564dabf0d 100644
--- a/packages/studio/src/test/timeline-selection.test.ts
+++ b/packages/studio/src/test/timeline-selection.test.ts
@@ -78,6 +78,7 @@ import {
parseTransformOrigin,
serializeTransformOrigin,
} from '../components/Timeline/transform-origin-utils';
+import {getKeyframesForTimelineEasingDrag} from '../components/Timeline/use-timeline-keyframe-drag';
import type {SequenceNodePathInfo} from '../helpers/get-timeline-sequence-sort-key';
import {
loadEditorShowOutlinesOption,
@@ -3187,6 +3188,77 @@ test('Selecting an easing segment replaces keyframe selection with the new type'
});
});
+test('Dragging an easing segment drags selected keyframes', () => {
+ const selectedKeyframeA = {
+ type: 'keyframe' as const,
+ nodePathInfo: makeNodePathInfo(['body', 0], ['controls', 'opacity']),
+ frame: 10,
+ };
+ const selectedKeyframeB = {
+ type: 'keyframe' as const,
+ nodePathInfo: makeNodePathInfo(['body', 0], ['controls', 'opacity']),
+ frame: 30,
+ };
+ const easing = {
+ type: 'easing' as const,
+ nodePathInfo: makeNodePathInfo(['body', 0], ['controls', 'opacity']),
+ fromFrame: 10,
+ toFrame: 20,
+ segmentIndex: 0,
+ };
+
+ expect(
+ getKeyframesForTimelineEasingDrag({
+ currentSelections: [selectedKeyframeA, selectedKeyframeB],
+ interaction: {shiftKey: false, toggleKey: false},
+ selectionItem: easing,
+ selected: false,
+ }),
+ ).toEqual([selectedKeyframeA, selectedKeyframeB]);
+});
+
+test('Dragging only selected easing segments drags connected keyframes', () => {
+ const easingA = {
+ type: 'easing' as const,
+ nodePathInfo: makeNodePathInfo(['body', 0], ['controls', 'opacity']),
+ fromFrame: 10,
+ toFrame: 20,
+ segmentIndex: 0,
+ };
+ const easingB = {
+ type: 'easing' as const,
+ nodePathInfo: makeNodePathInfo(['body', 0], ['controls', 'opacity']),
+ fromFrame: 20,
+ toFrame: 30,
+ segmentIndex: 1,
+ };
+
+ expect(
+ getKeyframesForTimelineEasingDrag({
+ currentSelections: [easingA, easingB],
+ interaction: {shiftKey: false, toggleKey: false},
+ selectionItem: easingA,
+ selected: true,
+ }),
+ ).toEqual([
+ {
+ type: 'keyframe',
+ nodePathInfo: easingA.nodePathInfo,
+ frame: 10,
+ },
+ {
+ type: 'keyframe',
+ nodePathInfo: easingA.nodePathInfo,
+ frame: 20,
+ },
+ {
+ type: 'keyframe',
+ nodePathInfo: easingB.nodePathInfo,
+ frame: 30,
+ },
+ ]);
+});
+
test('Timeline double-click actions ignore selection modifier clicks', () => {
expect(
isTimelineSelectionModifierEvent({