🎥 Make videos programmatically with React
Elements: Preserve initial props when inserting components
Igor Samokhovetsb47d440ab8ed
diff --git a/packages/browser-studio/src/browser-studio-operations.ts b/packages/browser-studio/src/browser-studio-operations.tsindex 5a7206e7e9..8c32aa3c31 100644--- a/packages/browser-studio/src/browser-studio-operations.ts+++ b/packages/browser-studio/src/browser-studio-operations.ts@@ -2107,19 +2107,22 @@ export const createBrowserStudioOperations = ({importName: plan.componentName,importPath: plan.importPath,position: componentOwnsSequence ? request.position : null,- props: componentOwnsSequence- ? [- ...(durationInFrames === null- ? []- : [- {- name: 'durationInFrames',- value: durationInFrames,- },- ]),- {name: 'name', value: request.element.displayName},- ]- : [],+ props: [+ ...Object.entries(request.element.initialProps ?? {}).map(+ ([name, value]) => ({name, value}),+ ),+ ...(componentOwnsSequence && durationInFrames !== null+ ? [+ {+ name: 'durationInFrames',+ value: durationInFrames,+ },+ ]+ : []),+ ...(componentOwnsSequence+ ? [{name: 'name', value: request.element.displayName}]+ : []),+ ],type: 'component',},from: componentOwnsSequence ? request.from : null,diff --git a/packages/browser-studio/src/test/browser-studio-project-operations.test.ts b/packages/browser-studio/src/test/browser-studio-project-operations.test.tsindex cc454506b3..7a01842187 100644--- a/packages/browser-studio/src/test/browser-studio-project-operations.test.ts+++ b/packages/browser-studio/src/test/browser-studio-project-operations.test.ts@@ -400,7 +400,7 @@ test('imports an Element with pinned Remotion dependencies as one undoable mutatdimensions: {width: 640, height: 180},displayName: 'Lower Third',durationInFrames: 90,- initialProps: null,+ initialProps: {label: 'Starter'},installationMode: 'wrapped' as const,slug: 'titles/lower-third',sourceCode: `import {Rect} from '@remotion/shapes';@@ -471,6 +471,9 @@ export const LowerThird = () => <Rect width={640} height={180} />;expect(project.files['/project/src/Composition.tsx']).toContain('name="Lower Third"',);+ expect(project.files['/project/src/Composition.tsx']).toContain(+ 'label="Starter"',+ );expect(project.files['/project/src/Composition.tsx']).toContain('translate: "24px 48px"',);@@ -555,6 +558,84 @@ export const LowerThird = () => <Rect width={640} height={180} />;);});+test('installs component-owned Element timing and initial props', async () => {+ let project = createBlankTemplateProject();+ const operations = createBrowserStudioOperations({+ dependencyVersions: {},+ getStaticFiles: null,+ getProject: () => project,+ initialElement: null,+ onProjectChange: (nextProject) => {+ project = nextProject;+ },+ resolveDependencies: null,+ });+ const element = {+ dependencies: [],+ dimensions: {width: 640, height: 180},+ displayName: 'Captions',+ durationInFrames: 90,+ initialProps: {+ captions: [{text: 'Starter', startMs: 0, endMs: 1000}],+ style: {+ color: 'red',+ position: 'relative',+ translate: '1px 2px',+ },+ width: 640,+ },+ installationMode: 'component-owned-sequence' as const,+ slug: 'captions',+ sourceCode: 'export const Captions = () => <div />;\n',+ } satisfies ElementDragData['element'];+ const preflight = await operations.prepareElementInstall({+ installationName: null,+ destination: {+ type: 'current-composition',+ compositionFile: '/project/src/Composition.tsx',+ compositionId: 'MyComp',+ },+ element,+ });+ if (!preflight.success) {+ throw new Error(preflight.reason);+ }++ const inserted = await operations.insertElement({+ installationName: null,+ compositionFile: '/project/src/Composition.tsx',+ compositionId: 'MyComp',+ element,+ expectedFileState: preflight.plan.expectedFileState,+ from: 30,+ overwriteExisting: false,+ position: {x: 24, y: 48},+ });+ if (!inserted.success) {+ throw new Error(+ inserted.type === 'error' ? inserted.reason : 'Unexpected file conflict',+ );+ }++ expect(project.files['/project/src/captions.element.tsx']).toBe(+ element.sourceCode,+ );+ const composition = project.files['/project/src/Composition.tsx'];+ expect(composition).not.toContain('<Sequence');+ expect(composition).toContain('<Captions');+ expect(composition).toContain('captions={[');+ expect(composition).toContain('text: "Starter"');+ expect(composition).toContain('width={640}');+ expect(composition).toContain('durationInFrames={90}');+ expect(composition).toContain('from={30}');+ expect(composition).toContain('name="Captions"');+ expect(composition).toContain('color: "red"');+ expect(composition).toContain('position: "absolute"');+ expect(composition).toContain('translate: "24px 48px"');+ expect(composition).not.toContain('translate: "1px 2px"');+ expect(composition.match(/\bstyle=/g)).toHaveLength(1);+});+test('installs packages as an undoable project mutation and reports structured failures', async () => {const initialProject = createBlankTemplateProject();let project = initialProject;