🎥 Make videos programmatically with React
`@remotion/studio`: Make pasting keyframes easier (#9430)
Jonny Burger3b0c5c45d94d
diff --git a/packages/studio-shared/src/keyframe-clipboard-data.ts b/packages/studio-shared/src/keyframe-clipboard-data.tsindex 427c356d6a..e3b7d0ed75 100644--- a/packages/studio-shared/src/keyframe-clipboard-data.ts+++ b/packages/studio-shared/src/keyframe-clipboard-data.ts@@ -34,11 +34,22 @@ export type KeyframeClipboardFieldType = {: never;}[keyof KeyframeClipboardFieldTypeSupport];+export type KeyframeClipboardField =+ | {+ readonly type: 'sequence';+ readonly fieldKey: string;+ }+ | {+ readonly type: 'effect';+ readonly fieldKey: string;+ };+export type KeyframeClipboardData = {readonly type: 'keyframe';readonly version: 1;readonly remotionClipboard: 'keyframe';readonly fieldType: KeyframeClipboardFieldType | null;+ readonly field: KeyframeClipboardField | null;readonly keyframes: readonly {readonly frameOffset: number;readonly value: unknown;@@ -107,6 +118,16 @@ const areValidKeyframes = (return value[0]?.frameOffset === 0;};+const isKeyframeClipboardField = (+ value: unknown,+): value is KeyframeClipboardField => {+ return (+ isRecord(value) &&+ (value.type === 'sequence' || value.type === 'effect') &&+ typeof value.fieldKey === 'string'+ );+};+const parseEasings = ({value,keyframeCount,@@ -148,6 +169,7 @@ export const parseKeyframeClipboardDataResult = (parsed.type !== 'keyframe' ||!areValidKeyframes(parsed.keyframes) ||easing === null ||+ (parsed.field !== null && !isKeyframeClipboardField(parsed.field)) ||(parsed.fieldType !== null &&!isKeyframeClipboardFieldType(parsed.fieldType))) {@@ -161,6 +183,7 @@ export const parseKeyframeClipboardDataResult = (version: 1,remotionClipboard: 'keyframe',fieldType: parsed.fieldType,+ field: parsed.field,keyframes: parsed.keyframes,easing,},diff --git a/packages/studio-shared/src/test/keyframe-clipboard-data.test.ts b/packages/studio-shared/src/test/keyframe-clipboard-data.test.tsindex 09eedf0e4f..fb4e7d46df 100644--- a/packages/studio-shared/src/test/keyframe-clipboard-data.test.ts+++ b/packages/studio-shared/src/test/keyframe-clipboard-data.test.ts@@ -12,6 +12,7 @@ test('parses keyframe clipboard data', () => {version: 1,remotionClipboard: 'keyframe',fieldType: 'number',+ field: {type: 'sequence', fieldKey: 'style.opacity'},keyframes: [{frameOffset: 0, value: 0.5},{frameOffset: 20, value: 1},@@ -24,6 +25,7 @@ test('parses keyframe clipboard data', () => {version: 1,remotionClipboard: 'keyframe',fieldType: 'number',+ field: {type: 'sequence', fieldKey: 'style.opacity'},keyframes: [{frameOffset: 0, value: 0.5},{frameOffset: 20, value: 1},@@ -32,6 +34,34 @@ test('parses keyframe clipboard data', () => {});});+test('rejects invalid keyframe clipboard field identities', () => {+ expect(+ parseKeyframeClipboardData(+ JSON.stringify({+ type: 'keyframe',+ version: 1,+ remotionClipboard: 'keyframe',+ fieldType: 'number',+ field: {type: 'sequence'},+ keyframes: [{frameOffset: 0, value: 1}],+ easing: [],+ }),+ ),+ ).toBe(null);+ expect(+ parseKeyframeClipboardData(+ JSON.stringify({+ type: 'keyframe',+ version: 1,+ remotionClipboard: 'keyframe',+ fieldType: 'number',+ keyframes: [{frameOffset: 0, value: 1}],+ easing: [],+ }),+ ),+ ).toBe(null);+});+test('allows keyframes without a schema field type', () => {expect(parseKeyframeClipboardData(@@ -40,6 +70,7 @@ test('allows keyframes without a schema field type', () => {version: 1,remotionClipboard: 'keyframe',fieldType: null,+ field: null,keyframes: [{frameOffset: 0, value: '#ff0000'}],easing: [],}),@@ -67,6 +98,7 @@ test('rejects invalid and unsupported keyframe clipboard data', () => {version: 1,remotionClipboard: 'keyframe',fieldType: 'array',+ field: null,keyframes: [{frameOffset: 0, value: []}],easing: [],}),@@ -79,6 +111,7 @@ test('rejects invalid and unsupported keyframe clipboard data', () => {version: 1,remotionClipboard: 'keyframe',fieldType: 'number',+ field: null,keyframes: [{frameOffset: 10, value: 1},{frameOffset: 5, value: 2},@@ -94,6 +127,7 @@ test('rejects invalid and unsupported keyframe clipboard data', () => {version: 1,remotionClipboard: 'keyframe',fieldType: 'number',+ field: null,keyframes: [{frameOffset: 0, value: 1},{frameOffset: 10, value: 2},diff --git a/packages/studio/src/components/Timeline/keyframe-clipboard.ts b/packages/studio/src/components/Timeline/keyframe-clipboard.tsindex 2f350ef1e0..7c241b6ee4 100644--- a/packages/studio/src/components/Timeline/keyframe-clipboard.ts+++ b/packages/studio/src/components/Timeline/keyframe-clipboard.ts@@ -308,6 +308,10 @@ export const getKeyframeClipboardDataFromSelections = ({version: 1,remotionClipboard: 'keyframe',fieldType: firstResolved.fieldType,+ field: {+ type: firstResolved.identity.type,+ fieldKey: firstResolved.identity.fieldKey,+ },keyframes: keyframes.map((keyframe) => {return {frameOffset: keyframe.frame - firstFrame,@@ -392,12 +396,26 @@ export const getPasteKeyframeTarget = ({return {type: 'none'};}- const selection = selectedItems[0];+ const resolvedSelectedItems: readonly TimelineSelection[] =+ selectedItems.length === 1 &&+ selectedItems[0]?.type === 'sequence' &&+ payload.field !== null &&+ payload.field.type === 'sequence'+ ? [+ {+ type: 'sequence-prop',+ nodePathInfo: selectedItems[0].nodePathInfo,+ key: payload.field.fieldKey,+ },+ ]+ : selectedItems;++ const selection = resolvedSelectedItems[0];if (!selection || getKeyframeFieldIdentity(selection) === null) {return {type: 'none'};}- const resolvedFields = selectedItems.map((item) =>+ const resolvedFields = resolvedSelectedItems.map((item) =>resolveKeyframeField({selection: item,sequences,diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.tsindex 5aba17616a..ab92051481 100644--- a/packages/studio/src/test/timeline-selection.test.ts+++ b/packages/studio/src/test/timeline-selection.test.ts@@ -851,6 +851,7 @@ test('copying selected keyframes preserves their frame deltas', () => {version: 1,remotionClipboard: 'keyframe',fieldType: 'number',+ field: {type: 'sequence', fieldKey: 'opacity'},keyframes: [{frameOffset: 0, value: 0.4},{frameOffset: 20, value: 0.8},@@ -859,6 +860,57 @@ test('copying selected keyframes preserves their frame deltas', () => {});});+test('pasting keyframes onto a sequence targets the copied property', () => {+ const nodePathInfo = makeNodePathInfo(['body', 0], []);+ const nodePath = nodePathInfo.sequenceSubscriptionKey;+ const schema = {+ 'style.translate': {+ type: 'translate',+ default: 'none',+ },+ } satisfies InteractivitySchema;+ const propStatuses = {+ [Internals.makeSequencePropsSubscriptionKey(nodePath)]: {+ canUpdate: true,+ props: {+ 'style.translate': {status: 'static', codeValue: 'none'},+ },+ effects: [],+ },+ } satisfies PropStatuses;++ expect(+ getPasteKeyframeTarget({+ selectedItems: [{type: 'sequence', nodePathInfo}],+ payload: {+ type: 'keyframe',+ version: 1,+ remotionClipboard: 'keyframe',+ fieldType: 'translate',+ field: {type: 'sequence', fieldKey: 'style.translate'},+ keyframes: [+ {frameOffset: 0, value: '0px 0px'},+ {frameOffset: 20, value: '100px 0px'},+ ],+ easing: [{type: 'linear'}],+ },+ timelinePosition: 50,+ sequences: [makeTimelineSequence({schema})],+ overrideIdsToNodePaths: {override: nodePath},+ propStatuses,+ }),+ ).toMatchObject({+ type: 'valid',+ nodePath,+ fieldKey: 'style.translate',+ effectIndex: null,+ keyframes: [+ {sourceFrame: 50, value: '0px 0px'},+ {sourceFrame: 70, value: '100px 0px'},+ ],+ });+});+test('copying selected keyframes requires one property', () => {const opacityNodePathInfo = makeNodePathInfo(['body', 0],@@ -940,6 +992,7 @@ test('pasting keyframes targets one selected property at the playhead', () => {version: 1,remotionClipboard: 'keyframe',fieldType: 'number',+ field: null,keyframes: [{frameOffset: 0, value: 0.4},{frameOffset: 20, value: 0.8},@@ -1011,6 +1064,7 @@ test('pasting keyframes replaces the destination range and preserves easing', ()version: 1,remotionClipboard: 'keyframe',fieldType: 'number',+ field: null,keyframes: [{frameOffset: 0, value: 0.25},{frameOffset: 20, value: 0.75},@@ -1072,6 +1126,7 @@ test('pasting a keyframe rejects an incompatible property', () => {version: 1,remotionClipboard: 'keyframe',fieldType: 'number',+ field: null,keyframes: [{frameOffset: 0, value: 0.4}],easing: [],},