Skip to content

🎥 Make videos programmatically with React

nested schema editor

Jonny Burger4416a401ec7d
diff --git a/packages/cli/src/editor/components/RenderModal/RenderModalData.tsx b/packages/cli/src/editor/components/RenderModal/RenderModalData.tsx
index bfb894740f..fa68ca58e2 100644
--- a/packages/cli/src/editor/components/RenderModal/RenderModalData.tsx
+++ b/packages/cli/src/editor/components/RenderModal/RenderModalData.tsx
@@ -5,7 +5,7 @@ import {Spacing} from '../layout';
import type {SegmentedControlItem} from '../SegmentedControl';
import {SegmentedControl} from '../SegmentedControl';
import {RenderModalJSONInputPropsEditor} from './RenderModalJSONInputPropsEditor';
-import {SchemaEditor} from './SchemaEditor';
+import {SchemaEditor} from './SchemaEditor/SchemaEditor';
type Mode = 'json' | 'schema';
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/SchemaEditor.tsx
similarity index 79%
rename from packages/cli/src/editor/components/RenderModal/SchemaEditor.tsx
rename to packages/cli/src/editor/components/RenderModal/SchemaEditor/SchemaEditor.tsx
index e1d6869bfe..1b42027d53 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/SchemaEditor.tsx
@@ -1,6 +1,7 @@
import React from 'react';
import {z} from 'remotion';
-import {LIGHT_TEXT} from '../../helpers/colors';
+import {LIGHT_TEXT} from '../../../helpers/colors';
+import {ZodObjectEditor} from './ZodObjectEditor';
const errorExplanation: React.CSSProperties = {
fontSize: 14,
@@ -31,5 +32,9 @@ export const SchemaEditor: React.FC<{
);
}
+ if (typeName === z.ZodFirstPartyTypeKind.ZodObject) {
+ return <ZodObjectEditor jsonPath={[]} schema={schema} />;
+ }
+
return null;
};
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodEffectEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodEffectEditor.tsx
new file mode 100644
index 0000000000..64efdfb291
--- /dev/null
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodEffectEditor.tsx
@@ -0,0 +1,16 @@
+import {z} from 'remotion';
+import type {JSONPath} from './zod-types';
+import {ZodSwitch} from './ZodSwitch';
+
+export const ZodEffectEditor: React.FC<{
+ schema: z.ZodTypeAny;
+ jsonPath: JSONPath;
+}> = ({schema, jsonPath}) => {
+ const def = schema._def;
+ const typeName = def.typeName as z.ZodFirstPartyTypeKind;
+ if (typeName !== z.ZodFirstPartyTypeKind.ZodEffects) {
+ throw new Error('expected effect');
+ }
+
+ return <ZodSwitch jsonPath={jsonPath} schema={def.schema} />;
+};
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodObjectEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodObjectEditor.tsx
new file mode 100644
index 0000000000..456c0c3d40
--- /dev/null
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodObjectEditor.tsx
@@ -0,0 +1,47 @@
+import React from 'react';
+import {z} from 'remotion';
+import {label, optionRow} from '../layout';
+import type {JSONPath} from './zod-types';
+import {ZodSwitch} from './ZodSwitch';
+
+const container: React.CSSProperties = {
+ borderLeft: '1px solid white',
+};
+
+export const ZodObjectEditor: React.FC<{
+ schema: z.ZodTypeAny;
+ jsonPath: JSONPath;
+}> = ({schema, jsonPath}) => {
+ const def = schema._def;
+
+ const typeName = def.typeName as z.ZodFirstPartyTypeKind;
+ if (typeName !== z.ZodFirstPartyTypeKind.ZodObject) {
+ throw new Error('expected object');
+ }
+
+ const shape = def.shape();
+ const keys = Object.keys(shape);
+
+ const isRoot = jsonPath.length === 0;
+
+ return (
+ <div style={isRoot ? undefined : optionRow}>
+ <div>
+ {isRoot ? null : (
+ <div style={label}>{jsonPath[jsonPath.length - 1]}</div>
+ )}
+ <div style={isRoot ? undefined : container}>
+ {keys.map((key) => {
+ return (
+ <ZodSwitch
+ key={key}
+ jsonPath={[...jsonPath, key]}
+ schema={shape[key]}
+ />
+ );
+ })}
+ </div>
+ </div>
+ </div>
+ );
+};
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodStringEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodStringEditor.tsx
new file mode 100644
index 0000000000..63a02587c1
--- /dev/null
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodStringEditor.tsx
@@ -0,0 +1,26 @@
+import {useState} from 'react';
+import type {z} from 'remotion';
+import {RemotionInput} from '../../NewComposition/RemInput';
+import {label, optionRow} from '../layout';
+import type {JSONPath} from './zod-types';
+
+export const ZodStringEditor: React.FC<{
+ schema: z.ZodTypeAny;
+ jsonPath: JSONPath;
+}> = ({jsonPath}) => {
+ const [value, setValue] = useState('');
+
+ return (
+ <div style={optionRow}>
+ <div style={label}>{jsonPath[jsonPath.length - 1]}</div>
+ <RemotionInput
+ value={value}
+ status="ok"
+ placeholder={jsonPath.join('.')}
+ onChange={(e) => {
+ setValue(e.target.value);
+ }}
+ />
+ </div>
+ );
+};
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx
new file mode 100644
index 0000000000..190495cea1
--- /dev/null
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx
@@ -0,0 +1,31 @@
+import {z} from 'remotion';
+import type {JSONPath} from './zod-types';
+import {ZodEffectEditor} from './ZodEffectEditor';
+import {ZodObjectEditor} from './ZodObjectEditor';
+import {ZodStringEditor} from './ZodStringEditor';
+
+export const ZodSwitch: React.FC<{
+ schema: z.ZodTypeAny;
+ jsonPath: JSONPath;
+}> = ({schema, jsonPath}) => {
+ const def: z.ZodTypeDef = schema._def;
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ const typeName = (def as any).typeName as z.ZodFirstPartyTypeKind;
+ if (typeName === z.ZodFirstPartyTypeKind.ZodAny) {
+ return <div>any</div>;
+ }
+
+ if (typeName === z.ZodFirstPartyTypeKind.ZodObject) {
+ return <ZodObjectEditor jsonPath={jsonPath} schema={schema} />;
+ }
+
+ if (typeName === z.ZodFirstPartyTypeKind.ZodString) {
+ return <ZodStringEditor jsonPath={jsonPath} schema={schema} />;
+ }
+
+ if (typeName === z.ZodFirstPartyTypeKind.ZodEffects) {
+ return <ZodEffectEditor jsonPath={jsonPath} schema={schema} />;
+ }
+
+ return <div>unknown</div>;
+};
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/zod-types.ts b/packages/cli/src/editor/components/RenderModal/SchemaEditor/zod-types.ts
new file mode 100644
index 0000000000..d90525d602
--- /dev/null
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/zod-types.ts
@@ -0,0 +1 @@
+export type JSONPath = (string | number)[];
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index 0769a96496..85c6b563ae 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -1,6 +1,6 @@
import {alias} from 'lib/alias';
import React from 'react';
-import {Composition, Folder, getInputProps, Still} from 'remotion';
+import {Composition, Folder, getInputProps, Still, z} from 'remotion';
import {TwentyTwoKHzAudio} from './22KhzAudio';
import BetaText from './BetaText';
import {CancelRender} from './CancelRender';
@@ -664,6 +664,21 @@ export const Index: React.FC = () => {
width={1200}
height={630}
fps={30}
+ schema={z.object({
+ vehicle: z.string(),
+ other: z.string(),
+ abc: z.object({
+ xyz: z.string(),
+ jkl: z.string(),
+ def: z.object({
+ xyz: z.string(),
+ }),
+ }),
+ })}
+ defaultProps={{
+ vehicle: 'caa',
+ other: 'hi',
+ }}
durationInFrames={150}
/>
</Folder>