Skip to content

🎥 Make videos programmatically with React

updater

Jonny Burger96cd8d146ea5
diff --git a/packages/cli/src/codemods/stringify-with-path.ts b/packages/cli/src/codemods/stringify-with-path.ts
index 17ce405fe8..77c1d43e5b 100644
--- a/packages/cli/src/codemods/stringify-with-path.ts
+++ b/packages/cli/src/codemods/stringify-with-path.ts
@@ -1,4 +1,5 @@
import type {EnumPath} from '../editor/components/RenderModal/SchemaEditor/extract-enum-json-paths';
+import {FILE_TOKEN} from '../editor/components/RenderModal/SchemaEditor/input-props-serialization';
const doesMatchPath = (path1: EnumPath, enumPaths: EnumPath[]) => {
return enumPaths.some((p) =>
@@ -31,8 +32,12 @@ export const stringifyDefaultProps = ({
return `__REMOVEQUOTE__new Date('${item.toISOString()}')__REMOVEQUOTE__`;
}
- if (typeof this[key] === 'string' && doesMatchPath(path, enumPaths)) {
- return `${this[key]}__ADD_AS_CONST__`;
+ if (typeof item === 'string' && doesMatchPath(path, enumPaths)) {
+ return `${item}__ADD_AS_CONST__`;
+ }
+
+ if (typeof item === 'string' && item.startsWith(FILE_TOKEN)) {
+ return `__REMOVEQUOTE____WRAP_IN_STATIC_FILE_START__${item}__WRAP_IN_STATIC_FILE_END____REMOVEQUOTE__`;
}
return value;
@@ -40,7 +45,9 @@ export const stringifyDefaultProps = ({
)
.replace(/"__REMOVEQUOTE__/g, '')
.replace(/__REMOVEQUOTE__"/g, '')
- .replace(/__ADD_AS_CONST__"/g, '" as const');
+ .replace(/__ADD_AS_CONST__"/g, '" as const')
+ .replace(/__WRAP_IN_STATIC_FILE_START__/g, 'staticFile("')
+ .replace(/__WRAP_IN_STATIC_FILE_END__/g, '")');
function replacerWithPath(
replacer: (
diff --git a/packages/cli/src/codemods/update-default-props.ts b/packages/cli/src/codemods/update-default-props.ts
index 538a17c856..aa461e719a 100644
--- a/packages/cli/src/codemods/update-default-props.ts
+++ b/packages/cli/src/codemods/update-default-props.ts
@@ -141,6 +141,7 @@ export const updateDefaultProps = async ({
input.substring(0, startPos) +
stringifyDefaultProps({props: newDefaultProps, enumPaths}) +
input.substring(endPos);
+ console.log(newFile);
const configFilePath = await resolveConfigFile();
if (!configFilePath) {
diff --git a/packages/cli/src/editor/components/NewComposition/ComboBox.tsx b/packages/cli/src/editor/components/NewComposition/ComboBox.tsx
index 6faf65b4ee..8f0906eeb0 100644
--- a/packages/cli/src/editor/components/NewComposition/ComboBox.tsx
+++ b/packages/cli/src/editor/components/NewComposition/ComboBox.tsx
@@ -180,7 +180,9 @@ export const Combobox: React.FC<{
};
}, [opened, size]);
- const selected = values.find((v) => v.id === selectedId) as SelectionItem;
+ const selected = values.find((v) => v.id === selectedId) as
+ | SelectionItem
+ | undefined;
const style = useMemo((): React.CSSProperties => {
return {
@@ -209,14 +211,16 @@ export const Combobox: React.FC<{
style={style}
className={MENU_INITIATOR_CLASSNAME}
>
- <div
- title={
- typeof selected.label === 'string' ? selected.label : undefined
- }
- style={label}
- >
- {selected?.label}
- </div>
+ {selected ? (
+ <div
+ title={
+ typeof selected.label === 'string' ? selected.label : undefined
+ }
+ style={label}
+ >
+ {selected?.label}
+ </div>
+ ) : null}
<Spacing x={1} /> <CaretDown />
</button>
{portalStyle
@@ -232,7 +236,7 @@ export const Combobox: React.FC<{
onHide={onHide}
leaveLeftSpace
preselectIndex={values.findIndex(
- (v) => v.id === selected.id
+ (v) => selected && v.id === selected.id
)}
topItemCanBeUnselected={false}
/>
diff --git a/packages/cli/src/editor/components/NewComposition/MenuContent.tsx b/packages/cli/src/editor/components/NewComposition/MenuContent.tsx
index f8a4000ba1..90e9d1802e 100644
--- a/packages/cli/src/editor/components/NewComposition/MenuContent.tsx
+++ b/packages/cli/src/editor/components/NewComposition/MenuContent.tsx
@@ -51,7 +51,7 @@ export const MenuContent: React.FC<{
}
const [selectedItem, setSelectedItem] = useState<string | null>(
- typeof preselectIndex === 'number'
+ typeof preselectIndex === 'number' && preselectIndex >= 0
? (values[preselectIndex].id as string)
: null
);
diff --git a/packages/cli/src/editor/components/RenderModal/RenderModalData.tsx b/packages/cli/src/editor/components/RenderModal/RenderModalData.tsx
index 47a9f2e2aa..6a572de7bb 100644
--- a/packages/cli/src/editor/components/RenderModal/RenderModalData.tsx
+++ b/packages/cli/src/editor/components/RenderModal/RenderModalData.tsx
@@ -27,7 +27,7 @@ import {RenderModalJSONPropsEditor} from './RenderModalJSONPropsEditor';
import {extractEnumJsonPaths} from './SchemaEditor/extract-enum-json-paths';
import type {SerializedJSONWithCustomFields} from './SchemaEditor/input-props-serialization';
import {
- deserializeJSONWithDate,
+ deserializeJSONWithCustomFields,
serializeJSONWithDate,
} from './SchemaEditor/input-props-serialization';
import {SchemaEditor} from './SchemaEditor/SchemaEditor';
@@ -96,7 +96,7 @@ const persistanceKey = 'remotion.show-render-modalwarning';
const parseJSON = (str: string): State => {
try {
- const value = deserializeJSONWithDate(str);
+ const value = deserializeJSONWithCustomFields(str);
return {str, value, validJSON: true};
} catch (e) {
return {str, validJSON: false, error: (e as Error).message};
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/SchemaErrorMessages.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/SchemaErrorMessages.tsx
index b9dc105994..55380f5e6f 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/SchemaErrorMessages.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/SchemaErrorMessages.tsx
@@ -125,8 +125,8 @@ export const InvalidSchema: React.FC<{
<div style={errorExplanation}>Fix the schema using the JSON editor.</div>
<Spacing y={1} block />
<div style={errorExplanation}>
- Alternatively, reset the data to the <code>defaultProps</code> that you
- have defined.
+ Alternatively, reset the data to the{' '}
+ <code style={codeSnippet}>defaultProps</code> that you have defined.
</div>
<Spacing y={1} block />
<Button onClick={reset}>Reset props</Button>
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
index efa669fb55..7307263a0d 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
@@ -16,6 +16,7 @@ import {createZodValues} from './create-zod-values';
import {SchemaFieldsetLabel} from './SchemaLabel';
import type {JSONPath} from './zod-types';
import {ZodArrayItemEditor} from './ZodArrayItemEditor';
+import type {UpdaterFunction} from './ZodSwitch';
const container: React.CSSProperties = {
width: '100%',
@@ -37,8 +38,8 @@ export const ZodArrayEditor: React.FC<{
jsonPath: JSONPath;
value: unknown[];
defaultValue: unknown[];
- setValue: React.Dispatch<React.SetStateAction<unknown[]>>;
- onSave: (updater: (oldState: unknown[]) => unknown[]) => void;
+ setValue: UpdaterFunction<unknown[]>;
+ onSave: UpdaterFunction<unknown[]>;
compact: boolean;
showSaveButton: boolean;
onRemove: null | (() => void);
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodBooleanEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodBooleanEditor.tsx
index 3680ce4802..9e9c9b893e 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodBooleanEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodBooleanEditor.tsx
@@ -3,6 +3,7 @@ import {Checkbox} from '../../Checkbox';
import {narrowOption, optionRow} from '../layout';
import {SchemaLabel} from './SchemaLabel';
import type {JSONPath} from './zod-types';
+import type {UpdaterFunction} from './ZodSwitch';
const fullWidth: React.CSSProperties = {
width: '100%',
@@ -11,7 +12,7 @@ const fullWidth: React.CSSProperties = {
export const ZodBooleanEditor: React.FC<{
jsonPath: JSONPath;
value: boolean;
- setValue: React.Dispatch<React.SetStateAction<boolean>>;
+ setValue: UpdaterFunction<boolean>;
compact: boolean;
defaultValue: boolean;
onSave: (updater: (oldNum: unknown) => boolean) => void;
@@ -31,7 +32,7 @@ export const ZodBooleanEditor: React.FC<{
}) => {
const onValueChange = useCallback(
(newValue: boolean) => {
- setValue(newValue);
+ setValue(() => newValue);
},
[setValue]
);
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodColorEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodColorEditor.tsx
index ece06140e7..9b07b74f50 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodColorEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodColorEditor.tsx
@@ -13,6 +13,7 @@ import {ValidationMessage} from '../../NewComposition/ValidationMessage';
import {narrowOption, optionRow} from '../layout';
import {SchemaLabel} from './SchemaLabel';
import type {JSONPath} from './zod-types';
+import type {UpdaterFunction} from './ZodSwitch';
type LocalState = {
value: string;
@@ -28,7 +29,7 @@ export const ZodColorEditor: React.FC<{
jsonPath: JSONPath;
value: string;
defaultValue: string;
- setValue: React.Dispatch<React.SetStateAction<string>>;
+ setValue: UpdaterFunction<string>;
onSave: (updater: (oldNum: unknown) => string) => void;
onRemove: null | (() => void);
compact: boolean;
@@ -72,7 +73,7 @@ export const ZodColorEditor: React.FC<{
};
setLocalValue(newLocalState);
if (safeParse.success) {
- setValue(newValue);
+ setValue(() => newValue);
}
},
[schema, setValue]
@@ -96,7 +97,7 @@ export const ZodColorEditor: React.FC<{
};
setLocalValue(newLocalState);
if (safeParse.success) {
- setValue(newColor);
+ setValue(() => newColor);
}
},
[a, schema, setValue, zodTypes]
@@ -112,7 +113,7 @@ export const ZodColorEditor: React.FC<{
};
setLocalValue(newLocalState);
if (safeParse.success) {
- setValue(newValue);
+ setValue(() => newValue);
}
},
[schema, setValue]
@@ -154,7 +155,7 @@ export const ZodColorEditor: React.FC<{
};
setLocalValue(newLocalState);
if (safeParse.success) {
- setValue(newColor);
+ setValue(() => newColor);
}
},
[localValue.value, schema, setValue, zodTypes]
@@ -175,7 +176,7 @@ export const ZodColorEditor: React.FC<{
};
setLocalValue(newLocalState);
if (safeParse.success) {
- setValue(newColor);
+ setValue(() => newColor);
}
},
[localValue.value, schema, setValue, zodTypes]
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodDateEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodDateEditor.tsx
index f9def48494..71992ff768 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodDateEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodDateEditor.tsx
@@ -7,6 +7,7 @@ import {ValidationMessage} from '../../NewComposition/ValidationMessage';
import {narrowOption, optionRow} from '../layout';
import {SchemaLabel} from './SchemaLabel';
import type {JSONPath} from './zod-types';
+import type {UpdaterFunction} from './ZodSwitch';
type LocalState = {
value: Date;
@@ -58,7 +59,7 @@ export const ZodDateEditor: React.FC<{
jsonPath: JSONPath;
value: Date;
defaultValue: Date;
- setValue: React.Dispatch<React.SetStateAction<Date>>;
+ setValue: UpdaterFunction<Date>;
onSave: (updater: (oldNum: unknown) => Date) => void;
onRemove: null | (() => void);
compact: boolean;
@@ -92,7 +93,7 @@ export const ZodDateEditor: React.FC<{
};
setLocalValue(newLocalState);
if (safeParse.success) {
- setValue(newValue);
+ setValue(() => newValue);
}
},
[schema, setValue]
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodEffectEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodEffectEditor.tsx
index bc399da00c..5fc9e214c9 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodEffectEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodEffectEditor.tsx
@@ -19,7 +19,7 @@ export const ZodEffectEditor: React.FC<{
schema: z.ZodTypeAny;
jsonPath: JSONPath;
value: unknown;
- setValue: (value: unknown) => void;
+ setValue: (updater: (oldState: unknown) => unknown) => void;
compact: boolean;
defaultValue: unknown;
onSave: (updater: (oldState: unknown) => unknown) => void;
@@ -57,8 +57,9 @@ export const ZodEffectEditor: React.FC<{
}
const onChange = useCallback(
- (newValue: unknown) => {
- setLocalValue(() => {
+ (updater: (oldValue: unknown) => unknown) => {
+ setLocalValue((oldLocalState) => {
+ const newValue = updater(oldLocalState.value);
const safeParse = schema.safeParse(newValue);
if (safeParse.success) {
updateValue(() => newValue);
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodEnumEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodEnumEditor.tsx
index b39111484a..df4f4603e6 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodEnumEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodEnumEditor.tsx
@@ -9,6 +9,7 @@ import {ValidationMessage} from '../../NewComposition/ValidationMessage';
import {narrowOption, optionRow} from '../layout';
import {SchemaLabel} from './SchemaLabel';
import type {JSONPath} from './zod-types';
+import type {UpdaterFunction} from './ZodSwitch';
const container: React.CSSProperties = {
width: '100%',
@@ -24,8 +25,8 @@ export const ZodEnumEditor: React.FC<{
jsonPath: JSONPath;
value: string;
defaultValue: string;
- setValue: React.Dispatch<React.SetStateAction<string>>;
- onSave: (updater: (oldState: string) => string) => void;
+ setValue: UpdaterFunction<string>;
+ onSave: UpdaterFunction<string>;
compact: boolean;
showSaveButton: boolean;
onRemove: null | (() => void);
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodNullableEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodNullableEditor.tsx
index 020b829e2e..b108e15604 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodNullableEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodNullableEditor.tsx
@@ -9,7 +9,7 @@ export const ZodNullableEditor: React.FC<{
value: unknown;
defaultValue: unknown;
schema: z.ZodTypeAny;
- setValue: React.Dispatch<React.SetStateAction<unknown>>;
+ setValue: (updater: (oldNum: unknown) => unknown) => void;
onSave: (updater: (oldNum: unknown) => unknown) => void;
onRemove: null | (() => void);
saving: boolean;
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodNumberEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodNumberEditor.tsx
index 8f177b04ee..940a09fb8f 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodNumberEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodNumberEditor.tsx
@@ -6,6 +6,7 @@ import {ValidationMessage} from '../../NewComposition/ValidationMessage';
import {narrowOption, optionRow} from '../layout';
import {SchemaLabel} from './SchemaLabel';
import type {JSONPath} from './zod-types';
+import type {UpdaterFunction} from './ZodSwitch';
type LocalState = {
value: string;
@@ -74,7 +75,7 @@ export const ZodNumberEditor: React.FC<{
schema: z.ZodTypeAny;
jsonPath: JSONPath;
value: number;
- setValue: React.Dispatch<React.SetStateAction<number>>;
+ setValue: UpdaterFunction<number>;
compact: boolean;
defaultValue: number;
onSave: (updater: (oldNum: unknown) => number) => void;
@@ -109,7 +110,7 @@ export const ZodNumberEditor: React.FC<{
};
setLocalValue(newLocalState);
if (safeParse.success) {
- setValue(Number(newValue));
+ setValue(() => Number(newValue));
}
},
[schema, setValue]
@@ -124,7 +125,7 @@ export const ZodNumberEditor: React.FC<{
};
setLocalValue(newLocalState);
if (safeParse.success) {
- setValue(newValue);
+ setValue(() => newValue);
}
},
[schema, setValue]
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodObjectEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodObjectEditor.tsx
index 07f98d547c..088fc9bd84 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodObjectEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodObjectEditor.tsx
@@ -26,7 +26,9 @@ export const ZodObjectEditor: React.FC<{
jsonPath: JSONPath;
value: unknown;
defaultValue: unknown;
- setValue: React.Dispatch<React.SetStateAction<unknown>>;
+ setValue: (
+ updater: (oldState: Record<string, unknown>) => Record<string, unknown>
+ ) => void;
compact: boolean;
onSave: (
updater: (oldVal: Record<string, unknown>) => Record<string, unknown>
@@ -94,7 +96,7 @@ export const ZodObjectEditor: React.FC<{
((defaultValue as Record<string, string>) ?? value)[key]
}
setValue={(val) => {
- setValue((oldVal: Record<string, string>) => {
+ setValue((oldVal) => {
return {
...oldVal,
[key]:
@@ -103,7 +105,7 @@ export const ZodObjectEditor: React.FC<{
});
}}
onSave={(val) => {
- onSave((oldVal: Record<string, unknown>) => {
+ onSave((oldVal) => {
return {
...oldVal,
[key]:
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOptionalEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOptionalEditor.tsx
index 6b31a5e18a..e6c8b1bb94 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOptionalEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOptionalEditor.tsx
@@ -9,7 +9,7 @@ export const ZodOptionalEditor: React.FC<{
value: unknown;
defaultValue: unknown;
schema: z.ZodTypeAny;
- setValue: React.Dispatch<React.SetStateAction<unknown>>;
+ setValue: (updater: (oldNum: unknown) => unknown) => void;
onSave: (updater: (oldNum: unknown) => unknown) => void;
onRemove: null | (() => void);
saving: boolean;
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
index 4162309207..1f0e43d1b5 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
@@ -37,7 +37,7 @@ export const ZodOrNullishEditor: React.FC<{
value: unknown;
defaultValue: unknown;
schema: z.ZodTypeAny;
- setValue: React.Dispatch<React.SetStateAction<unknown>>;
+ setValue: (updater: (oldState: unknown) => unknown) => void;
onSave: (updater: (oldNum: unknown) => unknown) => void;
onRemove: null | (() => void);
nullishValue: null | undefined;
@@ -66,7 +66,7 @@ export const ZodOrNullishEditor: React.FC<{
const onValueChange = useCallback(
(newValue: unknown) => {
- setValue(newValue);
+ setValue(() => newValue);
},
[setValue]
);
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodStaticFileEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodStaticFileEditor.tsx
new file mode 100644
index 0000000000..8c1c54c100
--- /dev/null
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodStaticFileEditor.tsx
@@ -0,0 +1,141 @@
+import React, {useCallback, useMemo, useState} from 'react';
+import {getStaticFiles} from 'remotion';
+import type {z} from 'zod';
+import {Checkmark} from '../../../icons/Checkmark';
+import {useZodIfPossible} from '../../get-zod-if-possible';
+import {Spacing} from '../../layout';
+import type {ComboboxValue} from '../../NewComposition/ComboBox';
+import {Combobox} from '../../NewComposition/ComboBox';
+import {ValidationMessage} from '../../NewComposition/ValidationMessage';
+import {narrowOption, optionRow} from '../layout';
+import {SchemaLabel} from './SchemaLabel';
+import type {JSONPath} from './zod-types';
+import type {UpdaterFunction} from './ZodSwitch';
+
+const container: React.CSSProperties = {
+ width: '100%',
+};
+
+type LocalState = {
+ value: string;
+ zodValidation: z.SafeParseReturnType<unknown, unknown>;
+};
+
+export const ZodStaticFileEditor: React.FC<{
+ schema: z.ZodTypeAny;
+ jsonPath: JSONPath;
+ value: string;
+ defaultValue: string;
+ setValue: UpdaterFunction<string>;
+ onSave: (updater: (oldState: string) => string) => void;
+ compact: boolean;
+ showSaveButton: boolean;
+ onRemove: null | (() => void);
+ saving: boolean;
+}> = ({
+ schema,
+ jsonPath,
+ compact,
+ setValue: updateValue,
+ defaultValue,
+ value,
+ onSave,
+ showSaveButton,
+ onRemove,
+ saving,
+}) => {
+ const z = useZodIfPossible();
+ if (!z) {
+ throw new Error('expected zod');
+ }
+
+ const [localValue, setLocalValue] = useState<LocalState>(() => {
+ return {
+ value,
+ zodValidation: schema.safeParse(value),
+ revision: 0,
+ };
+ });
+
+ const def = schema._def;
+
+ const typeName = def.typeName as z.ZodFirstPartyTypeKind;
+ if (typeName !== z.ZodFirstPartyTypeKind.ZodString) {
+ throw new Error('expected enum');
+ }
+
+ const isRoot = jsonPath.length === 0;
+
+ const onChange = useCallback(
+ (updater: (oldV: string) => string) => {
+ setLocalValue((oldLocalState) => {
+ const newValue = updater(oldLocalState.value);
+ const safeParse = schema.safeParse(newValue);
+ if (safeParse.success) {
+ updateValue(updater);
+ }
+
+ return {
+ value: newValue,
+ zodValidation: safeParse,
+ };
+ });
+ },
+ [schema, updateValue]
+ );
+
+ const reset = useCallback(() => {
+ onChange(() => defaultValue);
+ }, [defaultValue, onChange]);
+
+ const comboBoxValues = useMemo(() => {
+ return getStaticFiles().map((option): ComboboxValue => {
+ return {
+ value: option.src,
+ label: option.name,
+ id: option.src,
+ keyHint: null,
+ leftItem: option.src === value ? <Checkmark /> : null,
+ onClick: (id: string) => {
+ onChange(() => id);
+ },
+ quickSwitcherLabel: null,
+ subMenu: null,
+ type: 'item',
+ };
+ });
+ }, [onChange, value]);
+
+ const save = useCallback(() => {
+ onSave(() => value);
+ }, [onSave, value]);
+
+ return (
+ <div style={compact ? narrowOption : optionRow}>
+ <SchemaLabel
+ onSave={save}
+ showSaveButton={showSaveButton}
+ isDefaultValue={value === defaultValue}
+ compact={compact}
+ onReset={reset}
+ jsonPath={jsonPath}
+ onRemove={onRemove}
+ saving={saving}
+ />
+
+ <div style={isRoot ? undefined : container}>
+ <Combobox values={comboBoxValues} selectedId={value} title={value} />
+ </div>
+ {!localValue.zodValidation.success && (
+ <>
+ <Spacing x={1} />
+ <ValidationMessage
+ align="flex-start"
+ message={localValue.zodValidation.error.format()._errors[0]}
+ type="error"
+ />
+ </>
+ )}
+ </div>
+ );
+};
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodStringEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodStringEditor.tsx
index 4da8cba117..43e6082ae4 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodStringEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodStringEditor.tsx
@@ -7,6 +7,7 @@ import {ValidationMessage} from '../../NewComposition/ValidationMessage';
import {narrowOption, optionRow} from '../layout';
import {SchemaLabel} from './SchemaLabel';
import type {JSONPath} from './zod-types';
+import type {UpdaterFunction} from './ZodSwitch';
type LocalState = {
value: string;
@@ -22,7 +23,7 @@ export const ZodStringEditor: React.FC<{
jsonPath: JSONPath;
value: string;
defaultValue: string;
- setValue: React.Dispatch<React.SetStateAction<string>>;
+ setValue: UpdaterFunction<string>;
onSave: (updater: (oldNum: unknown) => string) => void;
onRemove: null | (() => void);
compact: boolean;
@@ -61,7 +62,7 @@ export const ZodStringEditor: React.FC<{
};
setLocalValue(newLocalState);
if (safeParse.success) {
- setValue(newValue);
+ setValue(() => newValue);
}
},
[schema, setValue]
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx
index 7edd36d800..4169a8b4f3 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx
@@ -16,15 +16,18 @@ import {ZodNullableEditor} from './ZodNullableEditor';
import {ZodNumberEditor} from './ZodNumberEditor';
import {ZodObjectEditor} from './ZodObjectEditor';
import {ZodOptionalEditor} from './ZodOptionalEditor';
+import {ZodStaticFileEditor} from './ZodStaticFileEditor';
import {ZodStringEditor} from './ZodStringEditor';
import {ZodUnionEditor} from './ZodUnionEditor';
+export type UpdaterFunction<T> = (updater: (oldValue: T) => T) => void;
+
export const ZodSwitch: React.FC<{
schema: z.ZodTypeAny;
jsonPath: JSONPath;
value: unknown;
defaultValue: unknown;
- setValue: React.Dispatch<React.SetStateAction<unknown>>;
+ setValue: UpdaterFunction<unknown>;
onSave: (newValue: (oldVal: unknown) => unknown) => void;
compact: boolean;
showSaveButton: boolean;
@@ -58,19 +61,13 @@ export const ZodSwitch: React.FC<{
if (typeName === z.ZodFirstPartyTypeKind.ZodObject) {
return (
<ZodObjectEditor
- setValue={setValue}
+ setValue={setValue as UpdaterFunction<Record<string, unknown>>}
value={value}
defaultValue={defaultValue}
jsonPath={jsonPath}
schema={schema}
compact={compact}
- onSave={
- onSave as (
- newValue: (
- oldVal: Record<string, unknown>
- ) => Record<string, unknown>
- ) => void
- }
+ onSave={onSave as UpdaterFunction<Record<string, unknown>>}
showSaveButton={showSaveButton}
onRemove={onRemove}
saving={saving}
@@ -79,10 +76,27 @@ export const ZodSwitch: React.FC<{
}
if (typeName === z.ZodFirstPartyTypeKind.ZodString) {
+ if ((value as string).startsWith(window.remotion_staticBase)) {
+ return (
+ <ZodStaticFileEditor
+ setValue={setValue as UpdaterFunction<string>}
+ value={value as string}
+ jsonPath={jsonPath}
+ schema={schema}
+ compact={compact}
+ defaultValue={defaultValue as string}
+ onSave={onSave as (newValue: (oldVal: string) => string) => void}
+ showSaveButton={showSaveButton}
+ onRemove={onRemove}
+ saving={saving}
+ />
+ );
+ }
+
return (
<ZodStringEditor
value={value as string}
- setValue={setValue as React.Dispatch<React.SetStateAction<string>>}
+ setValue={setValue as UpdaterFunction<string>}
jsonPath={jsonPath}
schema={schema}
compact={compact}
@@ -99,7 +113,7 @@ export const ZodSwitch: React.FC<{
return (
<ZodDateEditor
value={value as Date}
- setValue={setValue as React.Dispatch<React.SetStateAction<Date>>}
+ setValue={setValue as UpdaterFunction<Date>}
jsonPath={jsonPath}
schema={schema}
compact={compact}
@@ -116,7 +130,7 @@ export const ZodSwitch: React.FC<{
return (
<ZodNumberEditor
value={value as number}
- setValue={setValue as React.Dispatch<React.SetStateAction<unknown>>}
+ setValue={setValue as UpdaterFunction<number>}
jsonPath={jsonPath}
schema={schema}
compact={compact}
@@ -133,7 +147,7 @@ export const ZodSwitch: React.FC<{
return (
<ZodBooleanEditor
value={value as boolean}
- setValue={setValue as React.Dispatch<React.SetStateAction<unknown>>}
+ setValue={setValue as UpdaterFunction<boolean>}
jsonPath={jsonPath}
compact={compact}
defaultValue={defaultValue as boolean}
@@ -208,13 +222,13 @@ export const ZodSwitch: React.FC<{
if (typeName === z.ZodFirstPartyTypeKind.ZodArray) {
return (
<ZodArrayEditor
- setValue={setValue as React.Dispatch<React.SetStateAction<unknown[]>>}
+ setValue={setValue as UpdaterFunction<unknown[]>}
value={value as unknown[]}
jsonPath={jsonPath}
schema={schema}
compact={compact}
defaultValue={defaultValue as unknown[]}
- onSave={onSave as (newValue: (oldVal: unknown[]) => unknown[]) => void}
+ onSave={onSave as UpdaterFunction<unknown[]>}
showSaveButton={showSaveButton}
onRemove={onRemove}
saving={saving}
@@ -225,13 +239,13 @@ export const ZodSwitch: React.FC<{
if (typeName === z.ZodFirstPartyTypeKind.ZodEnum) {
return (
<ZodEnumEditor
- setValue={setValue as React.Dispatch<React.SetStateAction<string>>}
+ setValue={setValue as UpdaterFunction<string>}
value={value as string}
jsonPath={jsonPath}
schema={schema}
compact={compact}
defaultValue={defaultValue as string}
- onSave={onSave as (newValue: (oldVal: string) => string) => void}
+ onSave={onSave as UpdaterFunction<string>}
showSaveButton={showSaveButton}
onRemove={onRemove}
saving={saving}
@@ -248,7 +262,7 @@ export const ZodSwitch: React.FC<{
return (
<ZodColorEditor
value={value as string}
- setValue={setValue as React.Dispatch<React.SetStateAction<string>>}
+ setValue={setValue as UpdaterFunction<string>}
jsonPath={jsonPath}
schema={schema}
compact={compact}
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodUnionEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodUnionEditor.tsx
index 14b1e6b0a9..6ef1072e0c 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodUnionEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodUnionEditor.tsx
@@ -43,7 +43,7 @@ export const ZodUnionEditor: React.FC<{
value: unknown;
defaultValue: unknown;
schema: z.ZodTypeAny;
- setValue: React.Dispatch<React.SetStateAction<unknown>>;
+ setValue: (updater: (oldState: unknown) => unknown) => void;
onSave: (updater: (oldNum: unknown) => unknown) => void;
onRemove: null | (() => void);
saving: boolean;
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/input-props-serialization.ts b/packages/cli/src/editor/components/RenderModal/SchemaEditor/input-props-serialization.ts
index 77cd44018b..38e415cca9 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/input-props-serialization.ts
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/input-props-serialization.ts
@@ -1,5 +1,3 @@
-import {staticFile} from 'remotion';
-
export type SerializedJSONWithCustomFields = {
serializedString: string;
customDateUsed: boolean;
@@ -7,7 +5,7 @@ export type SerializedJSONWithCustomFields = {
};
const DATE_TOKEN = 'remotion-date:';
-const FILE_TOKEN = 'remotion-file:';
+export const FILE_TOKEN = 'remotion-file:';
export const serializeJSONWithDate = (
data: unknown,
@@ -43,16 +41,12 @@ export const serializeJSONWithDate = (
return {serializedString, customDateUsed, customFileUsed};
};
-export const deserializeJSONWithDate = (data: string) => {
+export const deserializeJSONWithCustomFields = (data: string) => {
return JSON.parse(data, (_, value) => {
if (typeof value === 'string' && value.startsWith(DATE_TOKEN)) {
return new Date(value.replace(DATE_TOKEN, ''));
}
- if (typeof value === 'string' && value.startsWith(FILE_TOKEN)) {
- return staticFile(value.replace(FILE_TOKEN, ''));
- }
-
return value;
});
};
diff --git a/packages/cli/src/preview-server/routes/update-default-props.ts b/packages/cli/src/preview-server/routes/update-default-props.ts
index 3c72359bb8..7f0fd47379 100644
--- a/packages/cli/src/preview-server/routes/update-default-props.ts
+++ b/packages/cli/src/preview-server/routes/update-default-props.ts
@@ -1,6 +1,6 @@
import {readFileSync, writeFileSync} from 'node:fs';
import {updateDefaultProps} from '../../codemods/update-default-props';
-import {deserializeJSONWithDate} from '../../editor/components/RenderModal/SchemaEditor/input-props-serialization';
+import {deserializeJSONWithCustomFields} from '../../editor/components/RenderModal/SchemaEditor/input-props-serialization';
import type {ApiHandler} from '../api-types';
import {getProjectInfo} from '../project-info';
import type {UpdateDefaultPropsRequest} from '../render-queue/job';
@@ -22,7 +22,7 @@ export const updateDefaultPropsHandler: ApiHandler<
const updated = await updateDefaultProps({
compositionId,
input: readFileSync(projectInfo.videoFile, 'utf-8'),
- newDefaultProps: deserializeJSONWithDate(defaultProps),
+ newDefaultProps: deserializeJSONWithCustomFields(defaultProps),
enumPaths,
});
diff --git a/packages/cli/src/test/date-serialization.test.ts b/packages/cli/src/test/date-serialization.test.ts
index 2f258dbf13..c4be467568 100644
--- a/packages/cli/src/test/date-serialization.test.ts
+++ b/packages/cli/src/test/date-serialization.test.ts
@@ -1,6 +1,6 @@
import {expect, test} from 'vitest';
import {
- deserializeJSONWithDate,
+ deserializeJSONWithCustomFields,
serializeJSONWithDate,
} from '../editor/components/RenderModal/SchemaEditor/input-props-serialization';
@@ -10,7 +10,7 @@ test('date serialization', () => {
const {serializedString, customDateUsed} = serializeJSONWithDate(date, 2);
expect(customDateUsed).toEqual(true);
- const deserialized = deserializeJSONWithDate(serializedString);
+ const deserialized = deserializeJSONWithCustomFields(serializedString);
expect(deserialized.data).toBeInstanceOf(Date);
});
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index 020eac87d0..5cc590d525 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -784,7 +784,7 @@ export const Index: React.FC = () => {
color: '#eb3a60',
nullable: null,
optional: 'hihi',
- filePath: 'remotion-file:jpeg.png',
+ filePath: staticFile('remotion-file:nested/mp3.png'),
}}
durationInFrames={150}
/>