Skip to content

🎥 Make videos programmatically with React

coool

Jonny Burger78a3448f1ed3
diff --git a/packages/studio-server/.prettierignore b/packages/studio-server/.prettierignore
new file mode 100644
index 0000000000..88323843c1
--- /dev/null
+++ b/packages/studio-server/.prettierignore
@@ -0,0 +1 @@
+src/test/snapshots
diff --git a/packages/studio-server/src/codemods/update-sequence-props.ts b/packages/studio-server/src/codemods/update-sequence-props.ts
index e49bde6889..428d29c380 100644
--- a/packages/studio-server/src/codemods/update-sequence-props.ts
+++ b/packages/studio-server/src/codemods/update-sequence-props.ts
@@ -8,6 +8,7 @@ import type {
} from '@babel/types';
import type {SequenceNodePath} from '@remotion/studio-shared';
import * as recast from 'recast';
+import type {SequenceSchema} from 'remotion';
import {findJsxElementAtNodePath} from '../preview-server/routes/can-update-sequence-props';
import {formatFileContent} from './format-file-content';
import {parseAst, serializeAst} from './parse-ast';
@@ -21,14 +22,52 @@ export type SequencePropUpdate = {
defaultValue: unknown | null;
};
+const removeVariantKey = ({
+ node,
+ variantKey,
+}: {
+ node: JSXOpeningElementLike;
+ variantKey: string;
+}) => {
+ const dotIndex = variantKey.indexOf('.');
+ if (dotIndex === -1) {
+ const idx = node.attributes?.findIndex(
+ (a) =>
+ a.type === 'JSXAttribute' &&
+ a.name.type === 'JSXIdentifier' &&
+ a.name.name === variantKey,
+ );
+ if (idx !== undefined && idx !== -1 && node.attributes) {
+ node.attributes.splice(idx, 1);
+ }
+
+ return;
+ }
+
+ updateNestedProp({
+ node,
+ parentKey: variantKey.slice(0, dotIndex),
+ childKey: variantKey.slice(dotIndex + 1),
+ value: undefined,
+ defaultValue: null,
+ isDefault: true,
+ });
+};
+
+type JSXOpeningElementLike = NonNullable<
+ ReturnType<typeof findJsxElementAtNodePath>
+>;
+
export const updateSequencePropsAst = ({
input,
nodePath,
updates,
+ schema,
}: {
input: string;
nodePath: SequenceNodePath;
updates: SequencePropUpdate[];
+ schema?: SequenceSchema;
}): {
serialized: string;
oldValueStrings: string[];
@@ -131,6 +170,38 @@ export const updateSequencePropsAst = ({
}
oldValueStrings.push(oldValueString);
+
+ if (schema && !isNested) {
+ const fieldSchema = schema[key];
+ if (fieldSchema && fieldSchema.type === 'enum') {
+ let oldRawValue: unknown;
+ try {
+ oldRawValue = JSON.parse(oldValueString);
+ } catch {
+ oldRawValue = oldValueString;
+ }
+
+ if (oldRawValue !== value) {
+ const oldVariant =
+ typeof oldRawValue === 'string'
+ ? fieldSchema.variants[oldRawValue]
+ : undefined;
+ const newVariant =
+ typeof value === 'string' ? fieldSchema.variants[value] : undefined;
+
+ if (oldVariant) {
+ const newKeys = new Set(newVariant ? Object.keys(newVariant) : []);
+ for (const variantKey of Object.keys(oldVariant)) {
+ if (newKeys.has(variantKey)) {
+ continue;
+ }
+
+ removeVariantKey({node, variantKey});
+ }
+ }
+ }
+ }
+ }
}
return {
@@ -144,11 +215,13 @@ export const updateSequenceProps = async ({
input,
nodePath,
updates,
+ schema,
prettierConfigOverride,
}: {
input: string;
nodePath: SequenceNodePath;
updates: SequencePropUpdate[];
+ schema?: SequenceSchema;
prettierConfigOverride?: Record<string, unknown> | null;
}): Promise<{
output: string;
@@ -160,6 +233,7 @@ export const updateSequenceProps = async ({
input,
nodePath,
updates,
+ schema,
});
const {output, formatted} = await formatFileContent({
diff --git a/packages/studio-server/src/test/discriminated-union-update.test.ts b/packages/studio-server/src/test/discriminated-union-update.test.ts
index 80e5e33594..7b68546d7f 100644
--- a/packages/studio-server/src/test/discriminated-union-update.test.ts
+++ b/packages/studio-server/src/test/discriminated-union-update.test.ts
@@ -2,7 +2,6 @@ import {expect, test} from 'bun:test';
import assert from 'node:assert';
import {readFileSync} from 'node:fs';
import path from 'node:path';
-import type {SchemaFieldInfo} from '@remotion/studio-shared';
import {getSchemaFields} from '@remotion/studio-shared';
import {Internals} from 'remotion';
import {parseAst} from '../codemods/parse-ast';
@@ -20,7 +19,7 @@ test('Should correctly separate discriminated union for layout', () => {
expect(schemaFields?.map((s) => s.key)).toEqual(['layout']);
});
-test('Should be able to update a discriminated union', async () => {
+test('Should be able to update a discriminated union', () => {
const file = readFileSync(
path.join(__dirname, 'snapshots', 'discriminated-union.tsx'),
'utf-8',
@@ -31,21 +30,6 @@ test('Should be able to update a discriminated union', async () => {
const nodePath = lineColumnToNodePath(ast, 3);
assert(nodePath, 'No node path found');
- const field: SchemaFieldInfo = {
- key: 'layout',
- description: 'Layout',
- typeName: 'enum',
- supported: true,
- rowHeight: 22,
- currentValue: 'none',
- fieldSchema: {
- type: 'enum',
- default: 'absolute-fill',
- description: 'Layout',
- variants: Internals.sequenceSchema.layout.variants,
- },
- };
-
const update = updateSequencePropsAst({
input: file,
nodePath,
@@ -53,7 +37,7 @@ test('Should be able to update a discriminated union', async () => {
{
key: 'layout',
value: 'none',
- defaultValue: field.fieldSchema.default,
+ defaultValue: Internals.sequenceSchema.layout.default,
},
],
});
@@ -75,3 +59,51 @@ test('Should be able to update a discriminated union', async () => {
}
}
});
+
+test('Should remove variant-specific props when switching enum value', () => {
+ const file = readFileSync(
+ path.join(__dirname, 'snapshots', 'discriminated-union-with-style.tsx'),
+ 'utf-8',
+ );
+
+ const ast = parseAst(file);
+
+ const nodePath = lineColumnToNodePath(ast, 3);
+ assert(nodePath, 'No node path found');
+
+ const update = updateSequencePropsAst({
+ input: file,
+ nodePath,
+ updates: [
+ {
+ key: 'layout',
+ value: 'none',
+ defaultValue: Internals.sequenceSchema.layout.default,
+ },
+ ],
+ schema: Internals.sequenceSchema,
+ });
+
+ console.log(update.serialized);
+
+ const expected = readFileSync(
+ path.join(
+ __dirname,
+ 'snapshots',
+ 'discriminated-union-with-style-expected.tsx',
+ ),
+ 'utf-8',
+ );
+ const actualLines = update.serialized.split('\n');
+ const expectedLines = expected.split('\n');
+ const maxLines = Math.max(actualLines.length, expectedLines.length);
+ for (let i = 0; i < maxLines; i++) {
+ if (actualLines[i] !== expectedLines[i]) {
+ console.log(update);
+ console.log(actualLines[i], expectedLines[i]);
+ throw new Error(
+ `Line ${i + 1} differs ${actualLines[i]} ${expectedLines[i]}`,
+ );
+ }
+ }
+});
diff --git a/packages/studio-server/src/test/snapshots/discriminated-union-expected.tsx b/packages/studio-server/src/test/snapshots/discriminated-union-expected.tsx
index 14b3b23492..b8204fa46c 100644
--- a/packages/studio-server/src/test/snapshots/discriminated-union-expected.tsx
+++ b/packages/studio-server/src/test/snapshots/discriminated-union-expected.tsx
@@ -1,12 +1,10 @@
export const ExperimentalControlsShowcase: React.FC = () => {
return (
- <Sequence
- style={{
+ <Sequence
+ style={{
scale: 1.74,
}}
- layout={'none'}
- >
- hi
- </Sequence>
- );
+ layout={"none"}>hi
+ </Sequence>
+ );
};
diff --git a/packages/studio-server/src/test/snapshots/discriminated-union-with-style-expected.tsx b/packages/studio-server/src/test/snapshots/discriminated-union-with-style-expected.tsx
new file mode 100644
index 0000000000..dde0d4d867
--- /dev/null
+++ b/packages/studio-server/src/test/snapshots/discriminated-union-with-style-expected.tsx
@@ -0,0 +1,6 @@
+export const ExperimentalControlsShowcase: React.FC = () => {
+ return (
+ <Sequence layout={"none"}>hi
+ </Sequence>
+ );
+};
diff --git a/packages/studio-server/src/test/snapshots/discriminated-union-with-style.tsx b/packages/studio-server/src/test/snapshots/discriminated-union-with-style.tsx
new file mode 100644
index 0000000000..b0089b46aa
--- /dev/null
+++ b/packages/studio-server/src/test/snapshots/discriminated-union-with-style.tsx
@@ -0,0 +1,12 @@
+export const ExperimentalControlsShowcase: React.FC = () => {
+ return (
+ <Sequence
+ layout="absolute-fill"
+ style={{
+ scale: 1.74,
+ }}
+ >
+ hi
+ </Sequence>
+ );
+};