Skip to content

🎥 Make videos programmatically with React

Fix wrap JSX consumers after codemod result change

Jonny Burger1e14f71108e8
diff --git a/packages/browser-studio/src/browser-studio-operations.ts b/packages/browser-studio/src/browser-studio-operations.ts
index 979bb13908..8e1d8b75d3 100644
--- a/packages/browser-studio/src/browser-studio-operations.ts
+++ b/packages/browser-studio/src/browser-studio-operations.ts
@@ -1511,7 +1511,7 @@ export const createBrowserStudioOperations = ({
undoRedoNavigation: null,
timelineSelection: null,
fileName,
- mutate: () => result.project,
+ mutate: (current) => applyCodemodChanges(current, result.changes),
nodePathMutationFiles: getNodePathMutationFiles(result),
});
if (nodePathMutation === null) {
diff --git a/packages/browser-studio/src/test/browser-studio-operations.test.ts b/packages/browser-studio/src/test/browser-studio-operations.test.ts
index d7cd4deafe..169e416921 100644
--- a/packages/browser-studio/src/test/browser-studio-operations.test.ts
+++ b/packages/browser-studio/src/test/browser-studio-operations.test.ts
@@ -3,6 +3,7 @@ import {
addSolid,
applyCodemodChanges,
CodemodsInternals,
+ getJsxNodes,
} from '@remotion/codemods';
import {createElementPayload} from '@remotion/studio-protocol';
import type {EventSourceEvent} from '@remotion/studio-shared';
@@ -938,6 +939,40 @@ const makeOperationsForProject = (project: VirtualProject) => {
return {operations, getProject: () => currentProject};
};
+test('wraps JSX as an undoable virtual project mutation', async () => {
+ const fileName = '/project/src/Composition.tsx';
+ const initialContents = `import {AbsoluteFill} from 'remotion';
+
+export const Component = () => <AbsoluteFill><div /></AbsoluteFill>;`;
+ const project: VirtualProject = {
+ rootDir: '/project',
+ entryPoint: fileName,
+ files: {[fileName]: initialContents},
+ };
+ const {operations, getProject} = makeOperationsForProject(project);
+ const nodePath = getJsxNodes({project, filePath: fileName}).find(
+ ({tagName}) => tagName === 'AbsoluteFill',
+ )?.nodePath;
+ if (!nodePath) {
+ throw new Error('Expected an AbsoluteFill node');
+ }
+
+ const result = await operations.wrapJsxNode({
+ fileName,
+ nodePath,
+ wrapper: 'Sequence',
+ width: null,
+ height: null,
+ });
+ expect(result.success).toBe(true);
+ expect(getProject().files[fileName]).toContain('<Sequence>');
+ expect(getProject().files[fileName]).toContain('<AbsoluteFill>');
+ expect(await operations.undo()).toMatchObject({success: true});
+ expect(getProject().files[fileName]).toBe(initialContents);
+ expect(await operations.redo()).toMatchObject({success: true});
+ expect(getProject().files[fileName]).toContain('<Sequence>');
+});
+
test('duplicates a JSX sequence as an undoable project mutation', async () => {
const fileName = '/project/src/Composition.tsx';
const initialContents = `import {Composition, Sequence} from 'remotion';
diff --git a/packages/studio-server/src/preview-server/routes/wrap-jsx-node.ts b/packages/studio-server/src/preview-server/routes/wrap-jsx-node.ts
index 16b21387e9..bf00132acb 100644
--- a/packages/studio-server/src/preview-server/routes/wrap-jsx-node.ts
+++ b/packages/studio-server/src/preview-server/routes/wrap-jsx-node.ts
@@ -59,7 +59,17 @@ export const wrapJsxNodeHandler: ApiHandler<
width: width ?? 0,
height: height ?? 0,
});
- const output = result.project.files[absolutePath];
+ const change = result.changes.find(
+ ({filePath}) => filePath === absolutePath,
+ );
+ if (
+ change?.previousContents !== fileContents ||
+ change.nextContents === null
+ ) {
+ throw new Error('Could not wrap JSX node');
+ }
+
+ const output = change.nextContents;
const remappings = result.nodePathRemappings.map(
({oldNodePath, newNodePath}) => ({oldNodePath, newNodePath}),
);