Skip to content

🎥 Make videos programmatically with React

`@remotion/studio-server`: Allow keyframes on missing sequence props (#8059)

Jonny Burger8a61c23fd30e
diff --git a/packages/studio-server/src/codemods/update-keyframes/update-keyframes.ts b/packages/studio-server/src/codemods/update-keyframes/update-keyframes.ts
index 5cb483d79f..4e20dc6567 100644
--- a/packages/studio-server/src/codemods/update-keyframes/update-keyframes.ts
+++ b/packages/studio-server/src/codemods/update-keyframes/update-keyframes.ts
@@ -19,7 +19,11 @@ import {
} from '@remotion/studio-shared';
import type {ExpressionKind, SpreadElementKind} from 'ast-types/lib/gen/kinds';
import * as recast from 'recast';
-import type {SequenceNodePath, SequenceSchema} from 'remotion';
+import type {
+ SequenceFieldSchema,
+ SequenceNodePath,
+ SequenceSchema,
+} from 'remotion';
import {getAstNodePath} from '../../helpers/get-ast-node-path';
import {
extractStaticValue,
@@ -68,6 +72,10 @@ type WritableProp = {
setExpression: (expression: ExpressionKind) => void;
};
+type MissingPropInitialValue = {
+ value: unknown;
+};
+
type InterpolateKeyframe = {
frame: number;
output: ExpressionKind;
@@ -485,6 +493,47 @@ const findObjectProperty = (
};
};
+const findFieldInSchema = (
+ schema: SequenceSchema,
+ key: string,
+): SequenceFieldSchema | undefined => {
+ if (key in schema) {
+ return schema[key];
+ }
+
+ for (const field of Object.values(schema)) {
+ if (field.type !== 'enum') {
+ continue;
+ }
+
+ for (const variant of Object.values(field.variants)) {
+ const found = findFieldInSchema(variant, key);
+ if (found) {
+ return found;
+ }
+ }
+ }
+
+ return undefined;
+};
+
+const getInitialValueForMissingProp = ({
+ schema,
+ key,
+ newValue,
+}: {
+ schema: SequenceSchema | null;
+ key: string;
+ newValue: unknown;
+}): unknown => {
+ const field = schema ? findFieldInSchema(schema, key) : undefined;
+ if (field && field.type !== 'hidden' && field.default !== undefined) {
+ return field.default;
+ }
+
+ return newValue;
+};
+
const getObjectExpression = (attr: JSXAttribute): ObjectExpression | null => {
if (!attr.value || attr.value.type !== 'JSXExpressionContainer') {
return null;
@@ -497,17 +546,64 @@ const getObjectExpression = (attr: JSXAttribute): ObjectExpression | null => {
return attr.value.expression as ObjectExpression;
};
+const createJsxExpressionAttribute = (
+ key: string,
+ expression: ExpressionKind,
+): JSXAttribute => {
+ return b.jsxAttribute(
+ b.jsxIdentifier(key),
+ b.jsxExpressionContainer(expression),
+ ) as JSXAttribute;
+};
+
+const createObjectProperty = (key: string, value: ExpressionKind) =>
+ b.objectProperty(b.identifier(key), value);
+
+const createObjectExpressionAttribute = ({
+ parentKey,
+ childKey,
+ expression,
+}: {
+ parentKey: string;
+ childKey: string;
+ expression: ExpressionKind;
+}): JSXAttribute => {
+ return createJsxExpressionAttribute(
+ parentKey,
+ b.objectExpression([
+ createObjectProperty(childKey, expression),
+ ]) as ExpressionKind,
+ );
+};
+
+const createMissingPropExpression = (
+ missingPropInitialValue: MissingPropInitialValue,
+): Expression => {
+ return parseValueExpression(missingPropInitialValue.value) as Expression;
+};
+
const getSequenceWritableProp = ({
attributes,
key,
+ missingPropInitialValue,
}: {
attributes: (JSXAttribute | JSXSpreadAttribute)[];
key: string;
+ missingPropInitialValue: MissingPropInitialValue | null;
}): WritableProp => {
const dotIndex = key.indexOf('.');
if (dotIndex === -1) {
const {attr: topLevelAttr} = findJsxAttribute(attributes, key);
if (!topLevelAttr) {
+ if (missingPropInitialValue) {
+ return {
+ expression: createMissingPropExpression(missingPropInitialValue),
+ setExpression: (nextExpression) => {
+ attributes.push(createJsxExpressionAttribute(key, nextExpression));
+ },
+ };
+ }
+
throw new Error(`Cannot update keyframes: "${key}" is not set`);
}
@@ -534,6 +630,21 @@ const getSequenceWritableProp = ({
const childKey = key.slice(dotIndex + 1);
const {attr: parentAttr} = findJsxAttribute(attributes, parentKey);
if (!parentAttr) {
+ if (missingPropInitialValue) {
+ return {
+ expression: createMissingPropExpression(missingPropInitialValue),
+ setExpression: (nextExpression) => {
+ attributes.push(
+ createObjectExpressionAttribute({
+ parentKey,
+ childKey,
+ expression: nextExpression,
+ }),
+ );
+ },
+ };
+ }
+
throw new Error(`Cannot update keyframes: "${parentKey}" is not set`);
}
@@ -544,6 +655,17 @@ const getSequenceWritableProp = ({
const {prop} = findObjectProperty(objExpr, childKey);
if (!prop) {
+ if (missingPropInitialValue) {
+ return {
+ expression: createMissingPropExpression(missingPropInitialValue),
+ setExpression: (nextExpression) => {
+ objExpr.properties.push(
+ createObjectProperty(childKey, nextExpression) as ObjectProperty,
+ );
+ },
+ };
+ }
+
throw new Error(`Cannot update keyframes: "${key}" is not set`);
}
@@ -594,6 +716,16 @@ export const updateSequenceKeyframesAst = ({
const prop = getSequenceWritableProp({
attributes: node.attributes,
key: update.key,
+ missingPropInitialValue:
+ update.operation.type === 'add'
+ ? {
+ value: getInitialValueForMissingProp({
+ schema: schema ?? null,
+ key: update.key,
+ newValue: update.operation.value,
+ }),
+ }
+ : null,
});
oldValueStrings.push(recast.print(prop.expression).code);
const {expression: nextExpression, introduced} = applyKeyframeOperation({
diff --git a/packages/studio-server/src/test/update-keyframes.test.ts b/packages/studio-server/src/test/update-keyframes.test.ts
index fdae35c806..e6d32266b8 100644
--- a/packages/studio-server/src/test/update-keyframes.test.ts
+++ b/packages/studio-server/src/test/update-keyframes.test.ts
@@ -253,6 +253,54 @@ test('updateSequenceKeyframes converts static translate to interpolateTranslate'
expect(output).toContain('interpolateTranslate');
});
+test('updateSequenceKeyframes adds a missing nested prop before keyframing it', async () => {
+ const input = `import React from 'react';
+import {AbsoluteFill} from 'remotion';
+
+export const Example: React.FC = () => {
+\treturn (
+\t\t<AbsoluteFill>
+\t\t\t<div style={{opacity: 1}} />
+\t\t</AbsoluteFill>
+\t);
+};
+`;
+ const {output, oldValueStrings, updatedNodePath} =
+ await updateSequenceKeyframes({
+ input,
+ nodePath: lineColumnToNodePath(input, getLine(input, '<div')),
+ schema: translateSchema,
+ updates: [
+ {
+ key: 'style.translate',
+ operation: {type: 'add', frame: 44, value: '100px 20px'},
+ },
+ ],
+ });
+
+ expect(oldValueStrings).toEqual(['"0px 0px"']);
+ expect(output).toContain(
+ "translate: interpolateTranslate(frame, [44], ['100px 20px'])",
+ );
+ expect(output).toContain('useCurrentFrame');
+ expect(output).toContain('interpolateTranslate');
+ const status = computeSequencePropsStatusFromContent({
+ fileContents: output,
+ nodePath: updatedNodePath,
+ keys: ['style.translate'],
+ effects: [],
+ });
+ expect(status.props['style.translate']).toEqual({
+ status: 'keyframed',
+ codeValue: undefined,
+ interpolationFunction: 'interpolateTranslate',
+ keyframes: [{frame: 44, value: '100px 20px'}],
+ easing: [],
+ clamping: {left: 'extend', right: 'extend'},
+ posterize: undefined,
+ });
+});
+
test('updateSequenceKeyframes migrates translate away from interpolateColors', async () => {
const input = translateInput
.replace(