🎥 Make videos programmatically with React
@remotion/studio-server: Detect keyframed interpolate props
JonnyBurger1c5ee5d22de2
diff --git a/packages/core/src/use-schema.ts b/packages/core/src/use-schema.tsindex 56c1d0b2fc..d3ecc28f81 100644--- a/packages/core/src/use-schema.ts+++ b/packages/core/src/use-schema.ts@@ -14,9 +14,15 @@ export type CanUpdateSequencePropStatusTrue = {codeValue: unknown;};+export type CanUpdateSequencePropStatusKeyframe = {+ frame: number;+ value: unknown;+};+export type CanUpdateSequencePropStatusFalse = {canUpdate: false;reason: 'computed';+ keyframes?: CanUpdateSequencePropStatusKeyframe[];};export type CanUpdateSequencePropStatus =diff --git a/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts b/packages/studio-server/src/preview-server/routes/can-update-sequence-props.tsindex 7db72cfd94..946816b55a 100644--- a/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts+++ b/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts@@ -1,6 +1,7 @@import {readFileSync} from 'node:fs';import path from 'node:path';import type {+ CallExpression,Expression,File,JSXAttribute,@@ -13,15 +14,20 @@ import type {import {RenderInternals} from '@remotion/renderer';import type {SubscribeToSequencePropsResponse} from '@remotion/studio-shared';import * as recast from 'recast';-import type {CanUpdateSequencePropsResponseTrue, LogLevel} from 'remotion';-import type {SequenceNodePath} from 'remotion';-import type {CanUpdateSequencePropStatus} from 'remotion';+import type {+ CanUpdateSequencePropsResponseTrue,+ CanUpdateSequencePropStatus,+ LogLevel,+ SequenceNodePath,+} from 'remotion';import {parseAst} from '../../codemods/parse-ast';import {getAstNodePath} from '../../helpers/get-ast-node-path';import {JsxElementNotFoundAtLocationError} from '../jsx-element-not-found-at-location-error';import {computeEffectPropStatus} from './can-update-effect-props';type CanUpdatePropStatus = CanUpdateSequencePropStatus;+type ComputedPropStatus = Extract<CanUpdatePropStatus, {canUpdate: false}>;+type PropKeyframes = NonNullable<ComputedPropStatus['keyframes']>;export const isStaticValue = (node: Expression): boolean => {switch (node.type) {@@ -114,6 +120,100 @@ export const extractStaticValue = (node: Expression): unknown => {}};+const getNumericValue = (node: Expression): number | null => {+ if (node.type === 'NumericLiteral') {+ return node.value;+ }++ if (+ node.type === 'UnaryExpression' &&+ (node.operator === '-' || node.operator === '+') &&+ node.argument.type === 'NumericLiteral'+ ) {+ return node.operator === '-' ? -node.argument.value : node.argument.value;+ }++ if (node.type === 'TSAsExpression') {+ return getNumericValue(node.expression as Expression);+ }++ return null;+};++const getInterpolateKeyframes = (+ node: Expression,+): PropKeyframes | undefined => {+ if (node.type === 'TSAsExpression') {+ return getInterpolateKeyframes(node.expression as Expression);+ }++ if (node.type !== 'CallExpression') {+ return undefined;+ }++ const callExpression = node as CallExpression;+ if (+ callExpression.callee.type !== 'Identifier' ||+ callExpression.callee.name !== 'interpolate'+ ) {+ return undefined;+ }++ const inputArg = callExpression.arguments[1];+ const outputArg = callExpression.arguments[2];+ if (+ !inputArg ||+ !outputArg ||+ inputArg.type !== 'ArrayExpression' ||+ outputArg.type !== 'ArrayExpression'+ ) {+ return undefined;+ }++ if (inputArg.elements.length !== outputArg.elements.length) {+ return undefined;+ }++ const keyframes: PropKeyframes = [];+ for (let i = 0; i < inputArg.elements.length; i++) {+ const inputElement = inputArg.elements[i];+ const outputElement = outputArg.elements[i];+ if (+ !inputElement ||+ !outputElement ||+ inputElement.type === 'SpreadElement' ||+ outputElement.type === 'SpreadElement'+ ) {+ return undefined;+ }++ const frame = getNumericValue(inputElement);+ if (frame === null || !isStaticValue(outputElement)) {+ return undefined;+ }++ keyframes.push({+ frame,+ value: extractStaticValue(outputElement),+ });+ }++ return keyframes.length > 0 ? keyframes : undefined;+};++const getComputedStatus = (node: Expression): CanUpdatePropStatus => {+ const keyframes = getInterpolateKeyframes(node);+ if (!keyframes) {+ return {canUpdate: false, reason: 'computed'};+ }++ return {+ canUpdate: false,+ reason: 'computed',+ keyframes,+ };+};+const getPropsStatus = (jsxElement: JSXOpeningElement,): Record<string, CanUpdatePropStatus> => {@@ -150,14 +250,16 @@ const getPropsStatus = (if (value.type === 'JSXExpressionContainer') {const {expression} = value;- if (- expression.type === 'JSXEmptyExpression' ||- !isStaticValue(expression)- ) {+ if (expression.type === 'JSXEmptyExpression') {props[name] = {canUpdate: false, reason: 'computed'};continue;}+ if (!isStaticValue(expression)) {+ props[name] = getComputedStatus(expression);+ continue;+ }+props[name] = {canUpdate: true,codeValue: extractStaticValue(expression),@@ -292,7 +394,7 @@ const getNestedPropStatus = (const propValue = prop.value as Expression;if (!isStaticValue(propValue)) {- return {canUpdate: false, reason: 'computed'};+ return getComputedStatus(propValue);}const codeValue = extractStaticValue(propValue);diff --git a/packages/studio-server/src/test/compute-sequence-props-status.test.ts b/packages/studio-server/src/test/compute-sequence-props-status.test.tsindex a7af997293..4aff60a713 100644--- a/packages/studio-server/src/test/compute-sequence-props-status.test.ts+++ b/packages/studio-server/src/test/compute-sequence-props-status.test.ts@@ -150,3 +150,26 @@ test('computeSequencePropsStatus should report unset when parent attribute missicodeValue: undefined,});});++test('computeSequencePropsStatus should return keyframes for interpolated style props', () => {+ const filePath = path.join(__dirname, 'snapshots', 'keyframed-props.tsx');+ const result = computeSequencePropsStatus({+ fileName: filePath,+ nodePath: getNodePath(filePath, 8),+ keys: ['style.scale'],+ effects: [],+ remotionRoot: '/',+ });++ expect(result.canUpdate).toBe(true);+ if (!result.canUpdate) throw new Error('Expected canUpdate to be true');++ expect(result.props['style.scale']).toEqual({+ canUpdate: false,+ reason: 'computed',+ keyframes: [+ {frame: 0, value: 2},+ {frame: 100, value: 4},+ ],+ });+});diff --git a/packages/studio-server/src/test/snapshots/keyframed-props.tsx b/packages/studio-server/src/test/snapshots/keyframed-props.tsxnew file mode 100644index 0000000000..d0c509c2c4--- /dev/null+++ b/packages/studio-server/src/test/snapshots/keyframed-props.tsx@@ -0,0 +1,14 @@+import React from 'react';+import {interpolate, Sequence, useCurrentFrame} from 'remotion';++export const KeyframedExample: React.FC = () => {+ const frame = useCurrentFrame();++ return (+ <Sequence+ from={0}+ durationInFrames={120}+ style={{scale: interpolate(frame, [0, 100], [2, 4])}}+ />+ );+};