Skip to content

πŸŽ₯ Make videos programmatically with React

`@remotion/studio`: Allow copy-pasting easings (#8496)

Jonny Burger46086821216b
diff --git a/packages/docs/docs/studio/interactivity.mdx b/packages/docs/docs/studio/interactivity.mdx
index ddb6d0e615..d3c9bc2193 100644
--- a/packages/docs/docs/studio/interactivity.mdx
+++ b/packages/docs/docs/studio/interactivity.mdx
@@ -51,6 +51,7 @@ Edits can be undone with <kbd>⌘/Ctrl</kbd> + <kbd>Z</kbd> and redone with <kbd
- Select easing segments between keyframes.
- Open the easing context menu to set `Linear`, choose a preset or open the custom easing editor.
- Apply easing edits to all selected easing segments.
+- Copy one selected easing segment with <kbd>⌘/Ctrl</kbd> + <kbd>C</kbd> and paste it onto selected easing segments with <kbd>⌘/Ctrl</kbd> + <kbd>V</kbd>.
- Press <kbd>Delete</kbd> or <kbd>Backspace</kbd> to reset selected easing segments to linear when only easing segments are selected.
## Deleting, resetting and duplicating
diff --git a/packages/studio-shared/src/easing-clipboard-data.ts b/packages/studio-shared/src/easing-clipboard-data.ts
new file mode 100644
index 0000000000..58cf49a56d
--- /dev/null
+++ b/packages/studio-shared/src/easing-clipboard-data.ts
@@ -0,0 +1,95 @@
+import type {KeyframeEasing} from './keyframe-easing-presets';
+
+export type EasingClipboardData = {
+ readonly type: 'easing';
+ readonly version: 1;
+ readonly remotionClipboard: 'easing';
+ readonly easing: KeyframeEasing;
+};
+
+export type EasingClipboardDataParseResult =
+ | {
+ readonly status: 'valid';
+ readonly data: EasingClipboardData;
+ }
+ | {
+ readonly status: 'unsupported-version';
+ readonly version: unknown;
+ }
+ | {
+ readonly status: 'invalid';
+ };
+
+const isRecord = (value: unknown): value is Record<string, unknown> => {
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
+};
+
+const isFiniteNumber = (value: unknown): value is number => {
+ return typeof value === 'number' && Number.isFinite(value);
+};
+
+const isEasing = (value: unknown): value is KeyframeEasing => {
+ return (
+ isRecord(value) &&
+ (value.type === 'linear' ||
+ (value.type === 'bezier' &&
+ isFiniteNumber(value.x1) &&
+ isFiniteNumber(value.y1) &&
+ isFiniteNumber(value.x2) &&
+ isFiniteNumber(value.y2)) ||
+ (value.type === 'spring' &&
+ isFiniteNumber(value.damping) &&
+ isFiniteNumber(value.mass) &&
+ isFiniteNumber(value.stiffness) &&
+ typeof value.overshootClamping === 'boolean'))
+ );
+};
+
+export const parseEasingClipboardDataResult = (
+ value: string,
+): EasingClipboardDataParseResult => {
+ try {
+ const parsed: unknown = JSON.parse(value);
+ if (!isRecord(parsed)) {
+ return {status: 'invalid'};
+ }
+
+ if (parsed.remotionClipboard !== 'easing') {
+ return {status: 'invalid'};
+ }
+
+ if (parsed.version !== 1) {
+ return {
+ status: 'unsupported-version',
+ version: parsed.version,
+ };
+ }
+
+ if (parsed.type !== 'easing' || !isEasing(parsed.easing)) {
+ return {status: 'invalid'};
+ }
+
+ return {
+ status: 'valid',
+ data: {
+ type: 'easing',
+ version: 1,
+ remotionClipboard: 'easing',
+ easing: parsed.easing,
+ },
+ };
+ } catch {
+ return {status: 'invalid'};
+ }
+};
+
+export const parseEasingClipboardData = (
+ value: string,
+): EasingClipboardData | null => {
+ const result = parseEasingClipboardDataResult(value);
+ if (result.status !== 'valid') {
+ return null;
+ }
+
+ return result.data;
+};
diff --git a/packages/studio-shared/src/index.ts b/packages/studio-shared/src/index.ts
index d5550fcd49..ee80a5ba6d 100644
--- a/packages/studio-shared/src/index.ts
+++ b/packages/studio-shared/src/index.ts
@@ -123,6 +123,7 @@ export {DEFAULT_BUFFER_STATE_DELAY_IN_MILLISECONDS} from './default-buffer-state
export {
KEYFRAME_EASING_PRESETS,
LINEAR_KEYFRAME_EASING,
+ type KeyframeEasing,
type KeyframeEasingPreset,
} from './keyframe-easing-presets';
export {
@@ -132,6 +133,12 @@ export {
type FileType,
type ImageFileType,
} from './detect-file-type';
+export {
+ parseEasingClipboardData,
+ parseEasingClipboardDataResult,
+ type EasingClipboardData,
+ type EasingClipboardDataParseResult,
+} from './easing-clipboard-data';
export {
parseEffectClipboardData,
parseEffectClipboardDataResult,
diff --git a/packages/studio-shared/src/test/effect-clipboard-data.test.ts b/packages/studio-shared/src/test/effect-clipboard-data.test.ts
index bb93442f88..e833986e6e 100644
--- a/packages/studio-shared/src/test/effect-clipboard-data.test.ts
+++ b/packages/studio-shared/src/test/effect-clipboard-data.test.ts
@@ -1,4 +1,8 @@
import {expect, test} from 'bun:test';
+import {
+ parseEasingClipboardData,
+ parseEasingClipboardDataResult,
+} from '../easing-clipboard-data';
import {
parseEffectClipboardData,
parseEffectClipboardDataResult,
@@ -6,6 +10,84 @@ import {
parseEffectPropClipboardDataResult,
} from '../effect-clipboard-data';
+test('parseEasingClipboardData accepts easing payloads', () => {
+ expect(
+ parseEasingClipboardData(
+ JSON.stringify({
+ type: 'easing',
+ version: 1,
+ remotionClipboard: 'easing',
+ easing: {type: 'bezier', x1: 0.42, y1: 0, x2: 0.58, y2: 1},
+ }),
+ ),
+ ).toEqual({
+ type: 'easing',
+ version: 1,
+ remotionClipboard: 'easing',
+ easing: {type: 'bezier', x1: 0.42, y1: 0, x2: 0.58, y2: 1},
+ });
+ expect(
+ parseEasingClipboardData(
+ JSON.stringify({
+ type: 'easing',
+ version: 1,
+ remotionClipboard: 'easing',
+ easing: {type: 'linear'},
+ }),
+ )?.easing,
+ ).toEqual({type: 'linear'});
+ expect(
+ parseEasingClipboardData(
+ JSON.stringify({
+ type: 'easing',
+ version: 1,
+ remotionClipboard: 'easing',
+ easing: {
+ type: 'spring',
+ damping: 12,
+ mass: 1.5,
+ stiffness: 180,
+ overshootClamping: true,
+ },
+ }),
+ )?.easing,
+ ).toEqual({
+ type: 'spring',
+ damping: 12,
+ mass: 1.5,
+ stiffness: 180,
+ overshootClamping: true,
+ });
+});
+
+test('parseEasingClipboardData reports old easing payloads as unsupported', () => {
+ const payload = JSON.stringify({
+ type: 'easing',
+ version: 0,
+ remotionClipboard: 'easing',
+ easing: 'linear',
+ });
+
+ expect(parseEasingClipboardData(payload)).toBe(null);
+ expect(parseEasingClipboardDataResult(payload)).toEqual({
+ status: 'unsupported-version',
+ version: 0,
+ });
+});
+
+test('parseEasingClipboardData rejects malformed easing payloads', () => {
+ expect(
+ parseEasingClipboardData(
+ JSON.stringify({
+ type: 'easing',
+ version: 1,
+ remotionClipboard: 'easing',
+ easing: [0.42, 0, 0.58, 1],
+ }),
+ ),
+ ).toBe(null);
+});
+
test('parseEffectClipboardData accepts keyframed v3 effect payloads', () => {
const parsed = parseEffectClipboardData(
JSON.stringify({
diff --git a/packages/studio/src/components/Timeline/TimelineClipboardKeybindings.tsx b/packages/studio/src/components/Timeline/TimelineClipboardKeybindings.tsx
index b78f9251e5..b67089a3fd 100644
--- a/packages/studio/src/components/Timeline/TimelineClipboardKeybindings.tsx
+++ b/packages/studio/src/components/Timeline/TimelineClipboardKeybindings.tsx
@@ -1,7 +1,9 @@
import {
isKeyframeInterpolationFunction,
+ parseEasingClipboardDataResult,
parseEffectClipboardDataResult,
parseEffectPropClipboardDataResult,
+ type EasingClipboardData,
type EffectClipboardData,
type EffectClipboardInterpolationFunction,
type EffectClipboardParam,
@@ -32,9 +34,15 @@ import {
useTimelineSelection,
type TimelineSelection,
} from './TimelineSelection';
+import {
+ getEasingSelections,
+ getTimelineEasingValueForSelection,
+ updateSelectedTimelineEasings,
+ type EasingSelection,
+} from './update-selected-easing';
const makeClipboardText = (
- payload: EffectClipboardData | EffectPropClipboardData,
+ payload: EffectClipboardData | EffectPropClipboardData | EasingClipboardData,
) => JSON.stringify(payload);
const makeTargetKey = (nodePath: SequencePropsSubscriptionKey): string => {
@@ -336,6 +344,35 @@ export const getEffectPropClipboardDataFromSelection = ({
};
};
+export const getEasingClipboardDataFromSelection = ({
+ selection,
+ sequences,
+ overrideIdsToNodePaths,
+ propStatuses,
+}: {
+ selection: EasingSelection;
+ sequences: TSequence[];
+ overrideIdsToNodePaths: OverrideIdToNodePaths;
+ propStatuses: PropStatuses;
+}): EasingClipboardData | null => {
+ const easing = getTimelineEasingValueForSelection({
+ selection,
+ sequences,
+ overrideIdsToNodePaths,
+ propStatuses,
+ });
+ if (easing === null) {
+ return null;
+ }
+
+ return {
+ type: 'easing',
+ version: 1,
+ remotionClipboard: 'easing',
+ easing,
+ };
+};
+
export const getPasteEffectPropTarget = ({
selectedItems,
payload,
@@ -471,10 +508,50 @@ export const TimelineClipboardKeybindings: React.FC = () => {
callback: (e) => {
const {selectedItems} = currentSelection.current;
const propStatuses = propStatusesRef.current;
+ const sequences = sequencesRef.current;
if (selectedItems.length === 0) {
return;
}
+ const easingSelections = getEasingSelections(selectedItems);
+ if (easingSelections.length > 0) {
+ e.preventDefault();
+ if (
+ easingSelections.length !== 1 ||
+ easingSelections.length !== selectedItems.length
+ ) {
+ showNotification('Select one easing to copy', 3000);
+ return;
+ }
+
+ const payload = getEasingClipboardDataFromSelection({
+ selection: easingSelections[0],
+ sequences,
+ overrideIdsToNodePaths: overrideIdToNodePathMappings,
+ propStatuses,
+ });
+ if (payload === null) {
+ showNotification(
+ 'Cannot copy easing because it cannot be read',
+ 3000,
+ );
+ return;
+ }
+
+ navigator.clipboard
+ .writeText(makeClipboardText(payload))
+ .then(() => {
+ showNotification('Copied easing to clipboard', 1000);
+ })
+ .catch((err) => {
+ showNotification(
+ `Could not copy easing: ${(err as Error).message}`,
+ 2000,
+ );
+ });
+ return;
+ }
+
if (
selectedItems.some(
(selection) => selection.type === 'sequence-effect-prop',
@@ -594,6 +671,61 @@ export const TimelineClipboardKeybindings: React.FC = () => {
.then((text) => {
const propStatuses = propStatusesRef.current;
const sequences = sequencesRef.current;
+ const easingResult = parseEasingClipboardDataResult(text);
+ if (easingResult.status !== 'invalid') {
+ e.preventDefault();
+ if (easingResult.status === 'unsupported-version') {
+ showNotification(
+ 'Cannot paste easing copied from a different Remotion Studio version',
+ 4000,
+ );
+ return;
+ }
+
+ const easingSelections = getEasingSelections(selectedItems);
+ if (
+ easingSelections.length === 0 ||
+ easingSelections.length !== selectedItems.length
+ ) {
+ showNotification('Select an easing to paste onto', 3000);
+ return;
+ }
+
+ const updatePromise = updateSelectedTimelineEasings({
+ selections: easingSelections,
+ sequences,
+ overrideIdsToNodePaths: overrideIdToNodePathMappings,
+ propStatuses,
+ setPropStatuses,
+ clientId,
+ easing: easingResult.data.easing,
+ });
+
+ if (updatePromise === null) {
+ showNotification(
+ 'Cannot paste onto an easing that cannot be updated',
+ 3000,
+ );
+ return;
+ }
+
+ return updatePromise
+ .then(() => {
+ showNotification(
+ easingSelections.length === 1
+ ? 'Pasted easing'
+ : 'Pasted easing to selected segments',
+ 2000,
+ );
+ })
+ .catch((err) => {
+ showNotification(
+ `Could not paste easing: ${(err as Error).message}`,
+ 3000,
+ );
+ });
+ }
+
const effectPropResult = parseEffectPropClipboardDataResult(text);
if (effectPropResult.status !== 'invalid') {
e.preventDefault();
diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.ts
index 58340ca6e0..aed84b1a00 100644
--- a/packages/studio/src/test/timeline-selection.test.ts
+++ b/packages/studio/src/test/timeline-selection.test.ts
@@ -58,6 +58,7 @@ import {
} from '../components/Timeline/duplicate-selected-timeline-item';
import {getTimelinePropResetTargets} from '../components/Timeline/reset-selected-timeline-props';
import {
+ getEasingClipboardDataFromSelection,
getEffectPropClipboardDataFromSelection,
getPasteEffectPropTarget,
getPasteEffectsTarget,
@@ -631,6 +632,129 @@ test('copying a selected effect prop creates an effect prop payload', () => {
});
});
+test('copying a selected sequence easing creates an easing payload', () => {
+ const opacityNodePathInfo = makeNodePathInfo(
+ ['body', 0],
+ ['controls', 'opacity'],
+ );
+ const nodePath = opacityNodePathInfo.sequenceSubscriptionKey;
+ const easing = {
+ type: 'bezier',
+ x1: 0.42,
+ y1: 0,
+ x2: 0.58,
+ y2: 1,
+ } as const;
+ const schema = {
+ opacity: {type: 'number', default: 1, hiddenFromList: false},
+ } satisfies SequenceSchema;
+ const propStatuses = {
+ [Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
+ canUpdate: true,
+ props: {
+ opacity: {
+ status: 'keyframed',
+ interpolationFunction: 'interpolate',
+ keyframes: [
+ {frame: 0, value: 0},
+ {frame: 100, value: 1},
+ ],
+ easing: [easing],
+ clamping: {left: 'clamp', right: 'clamp'},
+ posterize: undefined,
+ },
+ },
+ effects: [],
+ },
+ } satisfies PropStatuses;
+
+ expect(
+ getEasingClipboardDataFromSelection({
+ selection: {
+ type: 'easing',
+ nodePathInfo: opacityNodePathInfo,
+ fromFrame: 0,
+ toFrame: 100,
+ segmentIndex: 0,
+ },
+ sequences: [makeTimelineSequence({schema})],
+ overrideIdsToNodePaths: {override: nodePath},
+ propStatuses,
+ }),
+ ).toEqual({
+ type: 'easing',
+ version: 1,
+ remotionClipboard: 'easing',
+ easing,
+ });
+});
+
+test('copying a selected effect easing creates an easing payload', () => {
+ const effectPropNodePathInfo = makeNodePathInfo(
+ ['body', 0],
+ ['effects', '0', 'intensity'],
+ true,
+ [['0', 'intensity']],
+ );
+ const nodePath = effectPropNodePathInfo.sequenceSubscriptionKey;
+ const schema = {} satisfies SequenceSchema;
+ const effectSchema = {
+ intensity: {type: 'number', default: 0, hiddenFromList: false},
+ } satisfies SequenceSchema;
+ const propStatuses = {
+ [Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
+ canUpdate: true,
+ props: {},
+ effects: [
+ {
+ canUpdate: true,
+ callee: 'halftone',
+ importPath: '@remotion/effects/halftone',
+ effectIndex: 0,
+ props: {
+ intensity: {
+ status: 'keyframed',
+ interpolationFunction: 'interpolateColors',
+ keyframes: [
+ {frame: 0, value: 10},
+ {frame: 100, value: 20},
+ ],
+ easing: [{type: 'linear'}],
+ clamping: {left: 'clamp', right: 'clamp'},
+ posterize: undefined,
+ },
+ },
+ },
+ ],
+ },
+ } satisfies PropStatuses;
+
+ expect(
+ getEasingClipboardDataFromSelection({
+ selection: {
+ type: 'easing',
+ nodePathInfo: effectPropNodePathInfo,
+ fromFrame: 0,
+ toFrame: 100,
+ segmentIndex: 0,
+ },
+ sequences: [
+ makeTimelineSequence({
+ schema,
+ effects: [{schema: effectSchema}],
+ }),
+ ],
+ overrideIdsToNodePaths: {override: nodePath},
+ propStatuses,
+ }),
+ ).toEqual({
+ type: 'easing',
+ version: 1,
+ remotionClipboard: 'easing',
+ easing: {type: 'linear'},
+ });
+});
+
test('pasting an effect prop targets a matching selected effect', () => {
const effectNodePathInfo = makeNodePathInfo(
['body', 0],