Skip to content

πŸŽ₯ Make videos programmatically with React

`remotion`: Fix discriminated union bug

Jonny Burger3130bb19f7d9
diff --git a/packages/core/src/delete-nested-key.ts b/packages/core/src/delete-nested-key.ts
new file mode 100644
index 0000000000..f8303c3eef
--- /dev/null
+++ b/packages/core/src/delete-nested-key.ts
@@ -0,0 +1,39 @@
+export const deleteNestedKey = (
+ obj: Record<string, unknown>,
+ keysToRemove: Set<string>,
+) => {
+ for (const key of keysToRemove) {
+ const parts = key.split('.');
+ const parents: Record<string, unknown>[] = [obj];
+ let current = obj;
+ for (let i = 0; i < parts.length - 1; i++) {
+ const part = parts[i];
+ const next = current[part];
+ if (next === undefined || next === null) {
+ current = null as unknown as Record<string, unknown>;
+ break;
+ }
+
+ current = next as Record<string, unknown>;
+ parents.push(current);
+ }
+
+ if (current === null) {
+ continue;
+ }
+
+ delete current[parts[parts.length - 1]];
+
+ for (let i = parents.length - 1; i > 0; i--) {
+ const parent = parents[i];
+ if (Object.keys(parent).length === 0) {
+ const parentKey = parts[i - 1];
+ delete parents[i - 1][parentKey];
+ } else {
+ break;
+ }
+ }
+ }
+
+ return obj;
+};
diff --git a/packages/core/src/test/delete-nested-key.test.ts b/packages/core/src/test/delete-nested-key.test.ts
new file mode 100644
index 0000000000..139a1c32a7
--- /dev/null
+++ b/packages/core/src/test/delete-nested-key.test.ts
@@ -0,0 +1,26 @@
+import {expect, test} from 'bun:test';
+import {deleteNestedKey} from '../delete-nested-key';
+
+test('removes parent object when it becomes empty', () => {
+ const obj = {
+ from: 0,
+ durationInFrames: 6,
+ style: {opacity: 0.6},
+ stack:
+ 'Error\n at Object.apply (http://localhost:3001/bundle.js:35234:18)\n at http://localhost:3001/src_Root_tsx.bundle.js:3772:86\n at Array.map (<anonymous>)\n at SlicedVideo (http://localhost:3001/src_Root_tsx.bundle.js:3770:47)\n at Object.react_stack_bottom_frame (http://localhost:3001/bundle.js:25913:20)\n at renderWithHooks (http://localhost:3001/bundle.js:7671:22)\n at updateFunctionComponent (http://localhost:3001/bundle.js:10175:19)\n at beginWork (http://localhost:3001/bundle.js:11787:18)\n at runWithFiberInDEV (http://localhost:3001/bundle.js:881:30)\n at performUnitOfWork (http://localhost:3001/bundle.js:17650:22)\n at workLoopSync (http://localhost:3001/bundle.js:17478:41)\n at renderRootSync (http://localhost:3001/bundle.js:17459:11)\n at performWorkOnRoot (http://localhost:3001/bundle.js:16513:11)\n at performWorkOnRootViaSchedulerTask (http://localhost:3001/bundle.js:18966:7)\n at MessagePort.performWorkUntilDeadline (http://localhost:3001/bundle.js:31124:48)',
+ layout: 'none',
+ };
+
+ const result = deleteNestedKey(
+ obj as unknown as Record<string, unknown>,
+ new Set(['style.opacity', 'style.rotate']),
+ );
+
+ expect(result).toEqual({
+ from: 0,
+ durationInFrames: 6,
+ stack: obj.stack,
+ layout: 'none',
+ } as unknown as typeof result);
+ expect('style' in result).toBe(false);
+});
diff --git a/packages/core/src/test/wrap-in-schema-helpers.test.ts b/packages/core/src/test/wrap-in-schema-helpers.test.ts
index 17957c7372..8ebd3cf797 100644
--- a/packages/core/src/test/wrap-in-schema-helpers.test.ts
+++ b/packages/core/src/test/wrap-in-schema-helpers.test.ts
@@ -78,6 +78,7 @@ test('mergeValues writes nested values back into props with dot keys', () => {
props,
valuesDotNotation: values,
schemaKeys: ['layout', 'style.scale'],
+ propsToDelete: new Set(),
});
expect(merged.layout).toBe('absolute-fill');
expect((merged.style as {scale: number}).scale).toBe(2);
@@ -92,6 +93,7 @@ test('end-to-end: layout=none drops style.scale from active props', () => {
props,
valuesDotNotation: values,
schemaKeys: activeKeys,
+ propsToDelete: new Set(),
});
expect(activeKeys).toEqual(['layout']);
// style.scale was not in activeKeys β†’ original style preserved, not overwritten
diff --git a/packages/core/src/use-schema.ts b/packages/core/src/use-schema.ts
index 1d53e39325..797bb2f74d 100644
--- a/packages/core/src/use-schema.ts
+++ b/packages/core/src/use-schema.ts
@@ -58,7 +58,7 @@ export const computeEffectiveSchemaValuesDotNotation = ({
currentValue: Record<string, unknown>;
overrideValues: Record<string, unknown>;
propStatus: Record<string, CanUpdateSequencePropStatus> | undefined;
-}): Record<string, unknown> => {
+}): {merged: Record<string, unknown>; propsToDelete: Set<string>} => {
const merged: Record<string, unknown> = {};
const propsToDelete = new Set<string>();
for (const key of Object.keys(currentValue)) {
@@ -69,13 +69,18 @@ export const computeEffectiveSchemaValuesDotNotation = ({
continue;
}
- merged[key] = getEffectiveVisualModeValue({
+ const value = getEffectiveVisualModeValue({
codeValue: codeValueStatus,
runtimeValue: currentValue[key],
dragOverrideValue: overrideValues[key],
defaultValue: field?.default,
shouldResortToDefaultValueIfUndefined: false,
});
+ if (value === undefined) {
+ propsToDelete.add(key);
+ }
+
+ merged[key] = value;
}
for (const key of Object.keys(overrideValues)) {
@@ -91,9 +96,5 @@ export const computeEffectiveSchemaValuesDotNotation = ({
}
}
- for (const propToDelete of propsToDelete) {
- delete merged[propToDelete];
- }
-
- return merged;
+ return {merged, propsToDelete};
};
diff --git a/packages/core/src/wrap-in-schema.ts b/packages/core/src/wrap-in-schema.ts
index f30fe08e07..066afec742 100644
--- a/packages/core/src/wrap-in-schema.ts
+++ b/packages/core/src/wrap-in-schema.ts
@@ -1,5 +1,6 @@
import React, {forwardRef, useState, useContext, useMemo} from 'react';
import type {SequenceControls} from './CompositionManager.js';
+import {deleteNestedKey} from './delete-nested-key.js';
import {
flattenActiveSchema,
getFlatSchemaWithAllKeys,
@@ -55,10 +56,12 @@ export const mergeValues = ({
props,
valuesDotNotation,
schemaKeys,
+ propsToDelete,
}: {
props: Record<string, unknown>;
valuesDotNotation: Record<string, unknown>;
schemaKeys: string[];
+ propsToDelete: Set<string>;
}): Record<string, unknown> => {
const merged = {...props};
@@ -68,6 +71,7 @@ export const mergeValues = ({
if (parts.length === 1) {
merged[key] = value;
+
continue;
}
@@ -88,6 +92,8 @@ export const mergeValues = ({
current[parts[parts.length - 1]] = value;
}
+ deleteNestedKey(merged, propsToDelete);
+
return merged;
};
@@ -191,7 +197,7 @@ export const wrapInSchema = <S extends SequenceSchema, Props extends object>(
// 3. Apply drag/code overrides on top of the runtime values.
// eslint-disable-next-line react-hooks/rules-of-hooks
- const valuesDotNotation = useMemo(() => {
+ const {merged: valuesDotNotation, propsToDelete} = useMemo(() => {
return computeEffectiveSchemaValuesDotNotation({
schema,
currentValue: currentRuntimeValueDotNotation,
@@ -213,6 +219,7 @@ export const wrapInSchema = <S extends SequenceSchema, Props extends object>(
props: props as Record<string, unknown>,
valuesDotNotation,
schemaKeys: activeKeys,
+ propsToDelete,
});
return React.createElement(Component, {
diff --git a/packages/studio-shared/src/schema-field-info.ts b/packages/studio-shared/src/schema-field-info.ts
index a76f60436e..19ee5c423b 100644
--- a/packages/studio-shared/src/schema-field-info.ts
+++ b/packages/studio-shared/src/schema-field-info.ts
@@ -47,12 +47,13 @@ export const getFieldsToShow = ({
getCodeValues: GetCodeValues;
nodePath: SequenceNodePath;
}): SchemaFieldInfo[] | null => {
- const valuesDotNotation = Internals.computeEffectiveSchemaValuesDotNotation({
- schema,
- currentValue: currentRuntimeValueDotNotation,
- overrideValues: getDragOverrides(nodePath),
- propStatus: getCodeValues(nodePath),
- });
+ const {merged: valuesDotNotation} =
+ Internals.computeEffectiveSchemaValuesDotNotation({
+ schema,
+ currentValue: currentRuntimeValueDotNotation,
+ overrideValues: getDragOverrides(nodePath),
+ propStatus: getCodeValues(nodePath),
+ });
const activeSchema = Internals.flattenActiveSchema(
schema,