Skip to content

🎥 Make videos programmatically with React

`@remotion/studio-server`: Remove default value after keyframe deletion (#9318)

Jonny Burgerca4c63cb2a97
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 4242235d32..e352d06f66 100644
--- a/packages/studio-server/src/codemods/update-keyframes/update-keyframes.ts
+++ b/packages/studio-server/src/codemods/update-keyframes/update-keyframes.ts
@@ -197,6 +197,7 @@ export type EffectKeyframeUpdate = {
type WritableProp = {
expression: Expression;
setExpression: (expression: ExpressionKind) => void;
+ remove: () => void;
};
type MissingPropInitialValue = {
@@ -1505,6 +1506,41 @@ const getInitialValueForMissingProp = ({
return newValue;
};
+const shouldRemovePropAfterKeyframeOperation = ({
+ expression,
+ key,
+ operation,
+ schema,
+ videoConfigValues,
+}: {
+ expression: Expression;
+ key: string;
+ operation: KeyframeOperation;
+ schema: InteractivitySchema | null;
+ videoConfigValues: VideoConfigIdentifierValues;
+}) => {
+ if (operation.type !== 'remove' || !schema) {
+ return false;
+ }
+
+ const existing = getInterpolationExpression(expression, videoConfigValues);
+ if (!existing || existing.keyframes.length !== 1) {
+ return false;
+ }
+
+ const field = findFieldInSchema(schema, key);
+ if (!field || field.type === 'hidden' || field.default === undefined) {
+ return false;
+ }
+
+ const valueAfterRemoval =
+ operation.valueWhenLastKeyframeDeleted === null
+ ? existing.keyframes[0].value
+ : operation.valueWhenLastKeyframeDeleted;
+
+ return JSON.stringify(valueAfterRemoval) === JSON.stringify(field.default);
+};
+
const getObjectExpression = (attr: JSXAttribute): ObjectExpression | null => {
if (!attr.value || attr.value.type !== 'JSXExpressionContainer') {
return null;
@@ -1564,7 +1600,7 @@ const getSequenceWritableProp = ({
}): WritableProp => {
const dotIndex = key.indexOf('.');
if (dotIndex === -1) {
- const {attr: topLevelAttr} = findJsxAttribute(attributes, key);
+ const {attrIndex, attr: topLevelAttr} = findJsxAttribute(attributes, key);
if (!topLevelAttr) {
if (missingPropInitialValue) {
return {
@@ -1572,6 +1608,7 @@ const getSequenceWritableProp = ({
setExpression: (nextExpression) => {
attributes.push(createJsxExpressionAttribute(key, nextExpression));
},
+ remove: () => undefined,
};
}
@@ -1594,12 +1631,18 @@ const getSequenceWritableProp = ({
| null
| undefined;
},
+ remove: () => {
+ attributes.splice(attrIndex, 1);
+ },
};
}
const parentKey = key.slice(0, dotIndex);
const childKey = key.slice(dotIndex + 1);
- const {attr: parentAttr} = findJsxAttribute(attributes, parentKey);
+ const {attrIndex: parentAttrIndex, attr: parentAttr} = findJsxAttribute(
+ attributes,
+ parentKey,
+ );
if (!parentAttr) {
if (missingPropInitialValue) {
return {
@@ -1613,6 +1656,7 @@ const getSequenceWritableProp = ({
}),
);
},
+ remove: () => undefined,
};
}
@@ -1624,7 +1668,7 @@ const getSequenceWritableProp = ({
throw new Error(`Cannot update keyframes: "${parentKey}" is computed`);
}
- const {prop} = findObjectProperty(objExpr, childKey);
+ const {propIndex, prop} = findObjectProperty(objExpr, childKey);
if (!prop) {
if (missingPropInitialValue) {
return {
@@ -1634,6 +1678,7 @@ const getSequenceWritableProp = ({
createObjectProperty(childKey, nextExpression) as ObjectProperty,
);
},
+ remove: () => undefined,
};
}
@@ -1645,6 +1690,12 @@ const getSequenceWritableProp = ({
setExpression: (nextExpression) => {
prop.value = nextExpression as ObjectProperty['value'];
},
+ remove: () => {
+ objExpr.properties.splice(propIndex, 1);
+ if (objExpr.properties.length === 0) {
+ attributes.splice(parentAttrIndex, 1);
+ }
+ },
};
};
@@ -1657,7 +1708,7 @@ const getEffectWritableProp = ({
key: string;
missingPropInitialValue: MissingPropInitialValue | null;
}): WritableProp => {
- const {prop} = findObjectProperty(objExpr, key);
+ const {propIndex, prop} = findObjectProperty(objExpr, key);
if (!prop) {
if (missingPropInitialValue) {
return {
@@ -1667,6 +1718,7 @@ const getEffectWritableProp = ({
createObjectProperty(key, nextExpression) as ObjectProperty,
);
},
+ remove: () => undefined,
};
}
@@ -1678,6 +1730,9 @@ const getEffectWritableProp = ({
setExpression: (nextExpression) => {
prop.value = nextExpression as ObjectProperty['value'];
},
+ remove: () => {
+ objExpr.properties.splice(propIndex, 1);
+ },
};
};
@@ -1762,7 +1817,19 @@ export const updateSequenceKeyframesAst = ({
videoConfigValues: videoConfigIdentifierValues,
});
newValueStrings.push(recast.print(nextExpression).code);
- prop.setExpression(nextExpression);
+ if (
+ shouldRemovePropAfterKeyframeOperation({
+ expression: prop.expression,
+ key: update.key,
+ operation: update.operation,
+ schema: schema ?? null,
+ videoConfigValues: videoConfigIdentifierValues,
+ })
+ ) {
+ prop.remove();
+ } else {
+ prop.setExpression(nextExpression);
+ }
if (introduced.calleeName) {
requiredImports.add(introduced.calleeName);
@@ -1935,7 +2002,19 @@ export const updateEffectKeyframesAst = ({
videoConfigValues: videoConfigIdentifierValues,
});
newValueStrings.push(recast.print(nextExpression).code);
- prop.setExpression(nextExpression);
+ if (
+ shouldRemovePropAfterKeyframeOperation({
+ expression: prop.expression,
+ key: update.key,
+ operation: update.operation,
+ schema: schema ?? null,
+ videoConfigValues: videoConfigIdentifierValues,
+ })
+ ) {
+ prop.remove();
+ } else {
+ prop.setExpression(nextExpression);
+ }
if (introduced.calleeName) {
requiredImports.add(introduced.calleeName);
diff --git a/packages/studio-server/src/preview-server/routes/delete-keyframes.ts b/packages/studio-server/src/preview-server/routes/delete-keyframes.ts
index 9714bb890c..851353d3cd 100644
--- a/packages/studio-server/src/preview-server/routes/delete-keyframes.ts
+++ b/packages/studio-server/src/preview-server/routes/delete-keyframes.ts
@@ -190,6 +190,7 @@ export const deleteKeyframes = async ({
const result = await updateSequenceKeyframes({
input: output,
nodePath: firstSequenceKeyframe.nodePath.nodePath,
+ schema: firstSequenceKeyframe.schema,
videoConfigValues: firstSequenceKeyframe.nodePath.videoConfigValues,
updates: keyframeGroup.map((keyframe) => ({
key: keyframe.key,
@@ -230,6 +231,7 @@ export const deleteKeyframes = async ({
input: output,
sequenceNodePath: firstEffectKeyframe.sequenceNodePath.nodePath,
effectIndex: firstEffectKeyframe.effectIndex,
+ schema: firstEffectKeyframe.schema,
videoConfigValues:
firstEffectKeyframe.sequenceNodePath.videoConfigValues,
updates: keyframeGroup.map((keyframe) => ({
diff --git a/packages/studio-server/src/test/delete-keyframes.test.ts b/packages/studio-server/src/test/delete-keyframes.test.ts
index ee48de571f..c14543a978 100644
--- a/packages/studio-server/src/test/delete-keyframes.test.ts
+++ b/packages/studio-server/src/test/delete-keyframes.test.ts
@@ -24,7 +24,10 @@ export const Comp = () => {
\tconst frame = useCurrentFrame();
\treturn (
\t\t<AbsoluteFill
-\t\t\tstyle={{opacity: interpolate(frame, [0, 10], [0, 1])}}
+\t\t\tstyle={{
+\t\t\t\topacity: interpolate(frame, [0, 10], [0, 1]),
+\t\t\t\tscale: interpolate(frame, [30], [1]),
+\t\t\t}}
\t\t\teffects={[tint({amount: interpolate(frame, [0, 20], [0.2, 0.8])})]}
\t\t/>
\t);
@@ -48,7 +51,7 @@ test('deleteKeyframes batches sequence and effect deletes into one undo entry',
const nodePath = {
absolutePath: fileName,
nodePath: lineColumnToNodePath(input, 7),
- sequenceKeys: ['style.opacity'],
+ sequenceKeys: ['style.opacity', 'style.scale'],
effectKeys: [['amount']],
videoConfigValues: null,
};
@@ -74,6 +77,14 @@ test('deleteKeyframes batches sequence and effect deletes into one undo entry',
schema: NoReactInternals.sequenceSchema,
valueWhenLastKeyframeDeleted: 0.4,
},
+ {
+ fileName,
+ nodePath,
+ key: 'style.scale',
+ frame: 30,
+ schema: NoReactInternals.sequenceSchema,
+ valueWhenLastKeyframeDeleted: 1,
+ },
],
effectKeyframes: [
{
@@ -101,7 +112,8 @@ test('deleteKeyframes batches sequence and effect deletes into one undo entry',
});
const output = readFileSync(filePath, 'utf-8');
- expect(output).toContain('style={{opacity: 0.4}}');
+ expect(output).toContain('opacity: 0.4');
+ expect(output).not.toContain('scale: 1');
expect(output).toContain('amount: 0.6');
expect(getUndoStack()).toHaveLength(1);
diff --git a/packages/studio-server/src/test/update-keyframes.test.ts b/packages/studio-server/src/test/update-keyframes.test.ts
index 70160d5694..ccb4f710f6 100644
--- a/packages/studio-server/src/test/update-keyframes.test.ts
+++ b/packages/studio-server/src/test/update-keyframes.test.ts
@@ -1540,6 +1540,35 @@ test('updateSequenceKeyframes converts the last keyframe to a static value', asy
});
});
+test('updateSequenceKeyframes removes a default-valued prop with its last keyframe', async () => {
+ const input = sequenceInput.replace(
+ 'interpolate(frame, [0, 100], [2, 4])',
+ 'interpolate(frame, [12], [1])',
+ );
+ const {output, oldValueStrings, newValueStrings} =
+ await updateSequenceKeyframes({
+ videoConfigValues: null,
+ input,
+ nodePath: lineColumnToNodePath(input, getLine(input, 'scale')),
+ schema: NoReactInternals.sequenceSchema,
+ updates: [
+ {
+ key: 'style.scale',
+ operation: {
+ type: 'remove',
+ frame: 12,
+ valueWhenLastKeyframeDeleted: 1,
+ },
+ },
+ ],
+ });
+
+ expect(oldValueStrings).toEqual(['interpolate(frame, [12], [1])']);
+ expect(newValueStrings).toEqual(['1']);
+ expect(output).not.toContain('style={{scale: 1}}');
+ expect(output).toContain('<div />');
+});
+
test('updateSequenceKeyframes preserves the playhead value when all keyframes are removed', async () => {
for (const frames of [
[0, 100],