Skip to content

🎥 Make videos programmatically with React

Merge remote-tracking branch 'origin/main' into codex/fix-studio-sequence-mutation-remapping

Jonny Burger97e405195082
diff --cc packages/browser-studio/src/browser-studio-operations.ts
index fc5f8dd443,336595cc01..f770bc031b
--- a/packages/browser-studio/src/browser-studio-operations.ts
+++ b/packages/browser-studio/src/browser-studio-operations.ts
@@@ -1292,32 -1293,55 +1292,54 @@@ export const createBrowserStudioOperati
};
const duplicateJsxNode: BrowserStudioOperations['duplicateJsxNode'] = async ({
- fileName,
- nodePath,
+ nodes,
}) => {
try {
+ if (nodes.length === 0) {
+ throw new Error('No JSX nodes were specified for duplication');
+ }
+
const project = getProject();
- const absolutePath = findProjectFile({
- filePath: fileName,
- project,
- });
- const result = await duplicateJsxNodeCodemod({
- input: project.files[absolutePath],
- nodePath,
- formatFile: formatCodemodFile,
- });
+ const nodesByFile = new Map<
+ string,
+ (typeof nodes)[number]['nodePath'][]
+ >();
+ for (const node of nodes) {
+ const fileName = findProjectFile({
+ filePath: node.fileName,
+ project,
+ });
+ const fileNodes = nodesByFile.get(fileName) ?? [];
+ fileNodes.push(node.nodePath);
+ nodesByFile.set(fileName, fileNodes);
+ }
+
+ const updates = await Promise.all(
+ [...nodesByFile].map(async ([fileName, nodePaths]) => ({
+ fileName,
+ result: await duplicateJsxNodesCodemod({
+ input: project.files[fileName],
+ nodePaths,
+ formatFile: formatCodemodFile,
+ }),
+ })),
+ );
+ const nextProject = {
+ ...project,
+ files: {
+ ...project.files,
+ ...Object.fromEntries(
+ updates.map(({fileName, result}) => [fileName, result.output]),
+ ),
+ },
+ };
const nodePathMutation = controller.applyMutation({
- fileName: absolutePath,
- mutate: () => ({
- ...project,
- files: {...project.files, [absolutePath]: result.output},
- }),
- nodePathMutationFiles: [
- {
- absolutePath,
- remappings: result.nodePathRemappings,
- },
- ],
+ fileName: updates.map(({fileName}) => fileName).join(', '),
+ mutate: () => nextProject,
+ nodePathMutationFiles: updates.map(({fileName, result}) => ({
+ absolutePath: fileName,
+ remappings: result.nodePathRemappings,
- restoredNodePaths: [],
+ })),
});
if (nodePathMutation === null) {
throw new Error('Could not duplicate JSX node');
diff --cc packages/studio-server/src/preview-server/routes/duplicate-jsx-node.ts
index a69c830673,af1be9c4b4..68ce824bba
--- a/packages/studio-server/src/preview-server/routes/duplicate-jsx-node.ts
+++ b/packages/studio-server/src/preview-server/routes/duplicate-jsx-node.ts
@@@ -24,40 -24,76 +24,75 @@@ import
export const duplicateJsxNodeHandler: ApiHandler<
DuplicateJsxNodeRequest,
DuplicateJsxNodeResponse
- > = ({input: {fileName, nodePath}, remotionRoot, logLevel}) => {
+ > = ({input: {nodes}, remotionRoot, logLevel}) => {
return withSourceFileWriteQueue(async () => {
try {
+ if (nodes.length === 0) {
+ throw new Error('No JSX nodes were specified for duplication');
+ }
+
RenderInternals.Log.trace(
{indent: false, logLevel},
- `[duplicate-jsx-node] Received request for fileName="${fileName}"`,
+ `[duplicate-jsx-node] Received request to duplicate ${nodes.length} JSX node${nodes.length === 1 ? '' : 's'}`,
);
- const {absolutePath, fileRelativeToRoot} = resolveFileInsideProject({
- remotionRoot,
- fileName,
- action: 'modify',
- });
- const fileContents = readFileSync(absolutePath, 'utf-8');
+ const itemsByFileName = new Map<string, typeof nodes>();
+ for (const item of nodes) {
+ const fileItems = itemsByFileName.get(item.fileName) ?? [];
+ fileItems.push(item);
+ itemsByFileName.set(item.fileName, fileItems);
+ }
+
+ const updates = await Promise.all(
+ [...itemsByFileName.entries()].map(async ([fileName, fileItems]) => {
+ const {absolutePath, fileRelativeToRoot} = resolveFileInsideProject({
+ remotionRoot,
+ fileName,
+ action: 'modify',
+ });
+ const fileContents = readFileSync(absolutePath, 'utf-8');
+ const {output, formatted, nodeLabels, logLines, nodePathRemappings} =
+ await duplicateJsxNodes({
+ input: fileContents,
+ nodePaths: fileItems.map((item) => item.nodePath),
+ });
- const {output, formatted, nodeLabel, logLine, nodePathRemappings} =
- await duplicateJsxNode({input: fileContents, nodePath});
- const nodePathMutation = broadcastSequenceNodePathMutation([
- {
- absolutePath,
- remappings: nodePathRemappings,
- },
- ]);
+ return {
+ absolutePath,
+ fileRelativeToRoot,
+ fileContents,
+ formatted,
+ logLine: Math.min(...logLines),
+ nodeLabels,
+ nodePathRemappings,
+ output,
+ };
+ }),
+ );
+ const nodePathMutation = broadcastSequenceNodePathMutation(
+ updates.map((update) => ({
+ absolutePath: update.absolutePath,
+ remappings: update.nodePathRemappings,
- restoredNodePaths: [],
+ })),
+ );
+ const duplicatedNodeDescription =
+ nodes.length === 1
+ ? updates[0].nodeLabels[0]
+ : `${nodes.length} JSX nodes`;
- pushToUndoStack({
- filePath: absolutePath,
- oldContents: fileContents,
- newContents: null,
+ pushTransactionToUndoStack({
+ snapshots: updates.map((update) => ({
+ filePath: update.absolutePath,
+ oldContents: update.fileContents,
+ newContents: null,
+ logLine: update.logLine,
+ nodePathRemappings: update.nodePathRemappings,
+ })),
logLevel,
remotionRoot,
- logLine,
description: {
- undoMessage: `↩️ Duplication of ${nodeLabel}`,
- redoMessage: `↪️ Duplication of ${nodeLabel}`,
+ undoMessage: `↩️ Duplication of ${duplicatedNodeDescription}`,
+ redoMessage: `↪️ Duplication of ${duplicatedNodeDescription}`,
},
entryType: 'duplicate-jsx-node',
suppressHmrOnFileRestore: false,
diff --cc packages/studio-server/src/test/jsx-node-path-mutation-routes.test.ts
index fb88d056dd,e4e74d58f9..04e2e15e6c
--- a/packages/studio-server/src/test/jsx-node-path-mutation-routes.test.ts
+++ b/packages/studio-server/src/test/jsx-node-path-mutation-routes.test.ts
@@@ -168,13 -157,8 +176,15 @@@ test('JSX structure routes broadcast an
}
assertMutation({before, mutation: duplicateResponse.nodePathMutation});
+ forwardMutationFiles.push(duplicateResponse.nodePathMutation.files);
+ expect(
+ duplicateResponse.nodePathMutation.files[0].remappings.some(
+ (remapping) =>
+ remapping.oldNodePath === null && remapping.newNodePath !== null,
+ ),
+ ).toBe(true);
+ expect(readFileSync(filePath, 'utf-8')).toContain('name="b-copy"');
+ expect(readFileSync(filePath, 'utf-8')).toContain('name="c-copy"');
before = readFileSync(filePath, 'utf-8');
const splitResponse = await splitJsxSequenceHandler({