Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Allow adding and removing sequence props

Jonny Burgerbeffae74f4ca
diff --git a/packages/media/src/audio/audio-for-preview.tsx b/packages/media/src/audio/audio-for-preview.tsx
index aa3bd8ce4c..4a6deed11d 100644
--- a/packages/media/src/audio/audio-for-preview.tsx
+++ b/packages/media/src/audio/audio-for-preview.tsx
@@ -508,7 +508,7 @@ type InnerAudioProps = {
};
const audioSchema = z.object({
- volume: z.number().min(0).multipleOf(0.01),
+ volume: z.number().min(0).max(20).multipleOf(0.01).default(1),
playbackRate: z.number().min(0).multipleOf(0.01),
trimBefore: z.number(),
trimAfter: z.number(),
diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsx
index 35d09f7913..594726a3b5 100644
--- a/packages/media/src/video/video-for-preview.tsx
+++ b/packages/media/src/video/video-for-preview.tsx
@@ -529,7 +529,7 @@ const VideoForPreviewAssertedShowing: React.FC<
};
const videoSchema = z.object({
- volume: z.number().min(0).multipleOf(0.01),
+ volume: z.number().min(0).max(20).multipleOf(0.01).default(1),
playbackRate: z.number().min(0).multipleOf(0.01),
trimBefore: z.number(),
trimAfter: z.number(),
diff --git a/packages/studio-server/src/codemods/update-sequence-props.ts b/packages/studio-server/src/codemods/update-sequence-props.ts
index 9ce3797849..ff3a01c0c1 100644
--- a/packages/studio-server/src/codemods/update-sequence-props.ts
+++ b/packages/studio-server/src/codemods/update-sequence-props.ts
@@ -10,17 +10,23 @@ export const updateSequenceProps = async ({
key,
value,
enumPaths,
+ defaultValue,
}: {
input: string;
targetLine: number;
key: string;
value: unknown;
enumPaths: EnumPath[];
+ defaultValue: unknown | null;
}): Promise<{output: string; oldValueString: string}> => {
const ast = parseAst(input);
let found = false;
let oldValueString = '';
+ const isDefault =
+ defaultValue !== null &&
+ JSON.stringify(value) === JSON.stringify(defaultValue);
+
recast.types.visit(ast, {
visitJSXOpeningElement(path) {
const {node} = path;
@@ -29,7 +35,7 @@ export const updateSequenceProps = async ({
return this.traverse(path);
}
- const attr = node.attributes?.find((a) => {
+ const attrIndex = node.attributes?.findIndex((a) => {
if (a.type === 'JSXSpreadAttribute') {
return false;
}
@@ -41,10 +47,18 @@ export const updateSequenceProps = async ({
return a.name.name === key;
});
- if (!attr || attr.type === 'JSXSpreadAttribute') {
- throw new Error(
- `Could not find attribute "${key}" on the JSX element at line ${targetLine}`,
- );
+ const attr =
+ attrIndex !== undefined && attrIndex !== -1
+ ? node.attributes?.[attrIndex]
+ : undefined;
+
+ if (isDefault) {
+ if (attr && attr.type !== 'JSXSpreadAttribute' && node.attributes) {
+ node.attributes.splice(attrIndex!, 1);
+ }
+
+ found = true;
+ return this.traverse(path);
}
if (attr.value) {
@@ -63,7 +77,21 @@ export const updateSequenceProps = async ({
).expression as AssignmentExpression
).right as ExpressionKind;
- attr.value = recast.types.builders.jsxExpressionContainer(parsed);
+ if (!attr || attr.type === 'JSXSpreadAttribute') {
+ const newAttr = recast.types.builders.jsxAttribute(
+ recast.types.builders.jsxIdentifier(key),
+ recast.types.builders.jsxExpressionContainer(parsed),
+ );
+
+ if (!node.attributes) {
+ node.attributes = [];
+ }
+
+ node.attributes.push(newAttr);
+ } else {
+ attr.value = recast.types.builders.jsxExpressionContainer(parsed);
+ }
+
found = true;
return this.traverse(path);
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.ts
index 56160bceca..2daeaee3b4 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
@@ -15,8 +15,7 @@ import {parseAst} from '../../codemods/parse-ast';
type CanUpdatePropStatus =
| {canUpdate: true; codeValue: unknown}
- | {canUpdate: false; reason: 'computed'}
- | {canUpdate: false; reason: 'not-set'};
+ | {canUpdate: false; reason: 'computed'};
export const isStaticValue = (node: Expression): boolean => {
switch (node.type) {
@@ -214,7 +213,7 @@ export const computeSequencePropsStatus = ({
if (key in allProps) {
filteredProps[key] = allProps[key];
} else {
- filteredProps[key] = {canUpdate: false, reason: 'not-set'};
+ filteredProps[key] = {canUpdate: true, codeValue: undefined};
}
}
diff --git a/packages/studio-server/src/preview-server/routes/save-sequence-props.ts b/packages/studio-server/src/preview-server/routes/save-sequence-props.ts
index f0cf4e0ef0..8c18644379 100644
--- a/packages/studio-server/src/preview-server/routes/save-sequence-props.ts
+++ b/packages/studio-server/src/preview-server/routes/save-sequence-props.ts
@@ -14,7 +14,7 @@ export const saveSequencePropsHandler: ApiHandler<
SaveSequencePropsRequest,
SaveSequencePropsResponse
> = async ({
- input: {fileName, line, column, key, value, enumPaths},
+ input: {fileName, line, column, key, value, enumPaths, defaultValue},
remotionRoot,
logLevel,
}) => {
@@ -33,6 +33,7 @@ export const saveSequencePropsHandler: ApiHandler<
key,
value: JSON.parse(value),
enumPaths,
+ defaultValue: defaultValue !== null ? JSON.parse(defaultValue) : null,
});
suppressHmrForFile(absolutePath);
diff --git a/packages/studio-server/src/test/update-sequence-props.test.ts b/packages/studio-server/src/test/update-sequence-props.test.ts
index 149248f66b..3754b5a103 100644
--- a/packages/studio-server/src/test/update-sequence-props.test.ts
+++ b/packages/studio-server/src/test/update-sequence-props.test.ts
@@ -80,7 +80,7 @@ test('canUpdateSequenceProps should flag computed props', () => {
expect(result.props.hueShift).toEqual({canUpdate: true, codeValue: 30});
expect(result.props.seed).toEqual({canUpdate: false, reason: 'computed'});
expect(result.props.nonExistentProp).toEqual({
- canUpdate: false,
- reason: 'not-set',
+ canUpdate: true,
+ codeValue: undefined,
});
});
diff --git a/packages/studio-shared/src/api-requests.ts b/packages/studio-shared/src/api-requests.ts
index 4d01052c3f..b317fe5401 100644
--- a/packages/studio-shared/src/api-requests.ts
+++ b/packages/studio-shared/src/api-requests.ts
@@ -217,8 +217,7 @@ export type UnsubscribeFromSequencePropsRequest = {
export type CanUpdateSequencePropStatus =
| {canUpdate: true; codeValue: unknown}
- | {canUpdate: false; reason: 'computed'}
- | {canUpdate: false; reason: 'not-set'};
+ | {canUpdate: false; reason: 'computed'};
export type CanUpdateSequencePropsResponse =
| {
@@ -237,6 +236,7 @@ export type SaveSequencePropsRequest = {
key: string;
value: string;
enumPaths: EnumPath[];
+ defaultValue: string | null;
};
export type SaveSequencePropsResponse =
diff --git a/packages/studio/src/components/Timeline/TimelineExpandedSection.tsx b/packages/studio/src/components/Timeline/TimelineExpandedSection.tsx
index 7d44a95099..90f05167ca 100644
--- a/packages/studio/src/components/Timeline/TimelineExpandedSection.tsx
+++ b/packages/studio/src/components/Timeline/TimelineExpandedSection.tsx
@@ -20,6 +20,7 @@ import {
getExpandedTrackHeight,
getSchemaFields,
} from '../../helpers/timeline-layout';
+import {getDefaultValue} from '../RenderModal/SchemaEditor/zod-schema-type';
import {callApi} from '../call-api';
import {
TimelineFieldSavingSpinner,
@@ -62,10 +63,13 @@ const TimelineFieldRow: React.FC<{
readonly onDragEnd: () => void;
readonly propStatus: CanUpdateSequencePropStatus | null;
}> = ({field, onSave, onDragValueChange, onDragEnd, propStatus}) => {
+ const effectiveCodeValue = propStatus?.canUpdate
+ ? (propStatus.codeValue ?? field.currentValue)
+ : undefined;
const saving =
propStatus !== null &&
propStatus.canUpdate &&
- JSON.stringify(field.currentValue) !== JSON.stringify(propStatus.codeValue);
+ JSON.stringify(field.currentValue) !== JSON.stringify(effectiveCodeValue);
return (
<div style={{...fieldRow, height: field.rowHeight}}>
@@ -244,6 +248,12 @@ export const TimelineExpandedSection: React.FC<{
return Promise.reject(new Error('Cannot save'));
}
+ const field = schemaFields?.find((f) => f.key === key);
+ const defaultValue =
+ field && field.typeName === 'default'
+ ? JSON.stringify(getDefaultValue(field.fieldSchema))
+ : null;
+
return callApi('/api/save-sequence-props', {
fileName: validatedLocation.source,
line: validatedLocation.line,
@@ -251,9 +261,10 @@ export const TimelineExpandedSection: React.FC<{
key,
value: JSON.stringify(value),
enumPaths: [],
+ defaultValue,
}).then(() => undefined);
},
- [propStatuses, validatedLocation],
+ [propStatuses, validatedLocation, schemaFields],
);
const overrideId = sequence.controls?.overrideId ?? sequence.id;
diff --git a/packages/studio/src/components/Timeline/TimelineSchemaField.tsx b/packages/studio/src/components/Timeline/TimelineSchemaField.tsx
index f6258be375..31f380a395 100644
--- a/packages/studio/src/components/Timeline/TimelineSchemaField.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSchemaField.tsx
@@ -115,9 +115,7 @@ export const TimelineFieldValue: React.FC<{
}
if (propStatus !== null && !propStatus.canUpdate) {
- const label =
- propStatus.reason === 'not-set' ? 'not set' : propStatus.reason;
- return <span style={unsupportedLabel}>{label}</span>;
+ return <span style={unsupportedLabel}>{propStatus.reason}</span>;
}
if (propStatus === null) {
@@ -128,12 +126,14 @@ export const TimelineFieldValue: React.FC<{
);
}
+ const effectiveCodeValue = propStatus.codeValue ?? field.currentValue;
+
if (field.typeName === 'number') {
return (
<span style={wrapperStyle}>
<TimelineNumberField
field={field}
- codeValue={propStatus.codeValue}
+ codeValue={effectiveCodeValue}
canUpdate={canUpdate}
onSave={onSave}
onDragValueChange={onDragValueChange}
@@ -145,7 +145,7 @@ export const TimelineFieldValue: React.FC<{
return (
<span style={{...unsupportedLabel, fontStyle: 'normal'}}>
- {String(propStatus.codeValue)}
+ {String(effectiveCodeValue)}
</span>
);
};