Skip to content

🎥 Make videos programmatically with React

Address composition reorder review feedback

Jonny Burger2b489189abb5
diff --git a/packages/example/e2e/studio.test.mts b/packages/example/e2e/studio.test.mts
index 145b8bac6b..a503312e23 100644
--- a/packages/example/e2e/studio.test.mts
+++ b/packages/example/e2e/studio.test.mts
@@ -35,12 +35,14 @@ const dragCompositionSelectorItem = async ({
sourceTitle,
targetTitle,
position,
+ dropAtPosition,
drop,
}: {
page: Page;
sourceTitle: string;
targetTitle: string;
position: 'before' | 'inside' | 'after';
+ dropAtPosition: 'before' | 'inside' | 'after' | null;
drop: boolean;
}) => {
return page.evaluate(
@@ -48,6 +50,7 @@ const dragCompositionSelectorItem = async ({
sourceTitle: source,
targetTitle: target,
position: dropPosition,
+ dropAtPosition: finalDropPosition,
drop: shouldDrop,
}) => {
const items = Array.from(
@@ -72,12 +75,13 @@ const dragCompositionSelectorItem = async ({
}),
);
const rect = targetElement.getBoundingClientRect();
- const clientY =
- dropPosition === 'before'
+ const getClientY = (position: 'before' | 'inside' | 'after') =>
+ position === 'before'
? rect.top + 1
- : dropPosition === 'after'
+ : position === 'after'
? rect.bottom - 1
: rect.top + rect.height / 2;
+ const clientY = getClientY(dropPosition);
const dragOver = new DragEvent('dragover', {
bubbles: true,
cancelable: true,
@@ -86,7 +90,73 @@ const dragCompositionSelectorItem = async ({
});
targetElement.dispatchEvent(dragOver);
if (shouldDrop) {
+ const dropClientY = getClientY(finalDropPosition ?? dropPosition);
targetElement.dispatchEvent(
+ new DragEvent('drop', {
+ bubbles: true,
+ cancelable: true,
+ clientY: dropClientY,
+ dataTransfer,
+ }),
+ );
+ sourceElement.dispatchEvent(
+ new DragEvent('dragend', {bubbles: true, dataTransfer}),
+ );
+ }
+
+ return dragOver.defaultPrevented;
+ },
+ {sourceTitle, targetTitle, position, dropAtPosition, drop},
+ );
+};
+
+const dragCompositionSelectorItemToRoot = async ({
+ page,
+ sourceTitle,
+ drop,
+}: {
+ page: Page;
+ sourceTitle: string;
+ drop: boolean;
+}) => {
+ return page.evaluate(
+ ({sourceTitle: source, drop: shouldDrop}) => {
+ const sourceElement = Array.from(
+ document.querySelectorAll<HTMLElement>(
+ '.__remotion-composition-selector-item',
+ ),
+ ).find((element) => element.title === source);
+ const root = Array.from(
+ document.querySelectorAll<HTMLElement>(
+ '.__remotion-vertical-scrollbar',
+ ),
+ ).find((element) =>
+ element.querySelector('.__remotion-composition-selector-item'),
+ );
+ if (!sourceElement || !root) {
+ throw new Error(`Could not find drag source ${source} or root list`);
+ }
+
+ const dataTransfer = new DataTransfer();
+ sourceElement.dispatchEvent(
+ new DragEvent('dragstart', {
+ bubbles: true,
+ cancelable: true,
+ dataTransfer,
+ }),
+ );
+ const rect = root.getBoundingClientRect();
+ root.scrollTop = root.scrollHeight;
+ const clientY = rect.bottom - 1;
+ const dragOver = new DragEvent('dragover', {
+ bubbles: true,
+ cancelable: true,
+ clientY,
+ dataTransfer,
+ });
+ root.dispatchEvent(dragOver);
+ if (shouldDrop) {
+ root.dispatchEvent(
new DragEvent('drop', {
bubbles: true,
cancelable: true,
@@ -101,7 +171,7 @@ const dragCompositionSelectorItem = async ({
return dragOver.defaultPrevented;
},
- {sourceTitle, targetTitle, position, drop},
+ {sourceTitle, drop},
);
};
@@ -1217,6 +1287,7 @@ test.describe('visual mode', () => {
sourceTitle: firstItemTitle!,
targetTitle: firstItemTitle!,
position: 'before',
+ dropAtPosition: null,
drop: true,
}),
).toBe(false);
@@ -1226,6 +1297,7 @@ test.describe('visual mode', () => {
sourceTitle: firstItemTitle!,
targetTitle: firstItemTitle!,
position: 'after',
+ dropAtPosition: null,
drop: true,
}),
).toBe(false);
@@ -1248,6 +1320,7 @@ test.describe('visual mode', () => {
sourceTitle: 'AnimatedBarChart',
targetTitle: 'Schema',
position: 'inside',
+ dropAtPosition: null,
drop: false,
}),
).toBe(true);
@@ -1262,6 +1335,7 @@ test.describe('visual mode', () => {
sourceTitle: 'AnimatedBarChart',
targetTitle: 'Schema',
position: 'before',
+ dropAtPosition: null,
drop: false,
}),
).toBe(true);
@@ -1286,6 +1360,7 @@ test.describe('visual mode', () => {
sourceTitle: 'AnimatedBarChart',
targetTitle: 'Schema',
position: 'before',
+ dropAtPosition: 'inside',
drop: true,
});
await expect
@@ -1319,6 +1394,7 @@ test.describe('visual mode', () => {
sourceTitle: 'AnimatedBarChart',
targetTitle: 'Schema',
position: 'before',
+ dropAtPosition: null,
drop: true,
}),
).toBe(false);
@@ -1330,6 +1406,7 @@ test.describe('visual mode', () => {
sourceTitle: 'Schema',
targetTitle: 'use-current-scale-on-load',
position: 'before',
+ dropAtPosition: null,
drop: true,
});
await expect
@@ -1348,6 +1425,7 @@ test.describe('visual mode', () => {
sourceTitle: 'AnimatedBarChart',
targetTitle: 'Schema',
position: 'inside',
+ dropAtPosition: null,
drop: true,
});
await expect
@@ -1359,9 +1437,71 @@ test.describe('visual mode', () => {
return composition > folderStart && composition < folderEnd;
})
.toBe(true);
+ const afterCompositionNested = fs.readFileSync(rootFile, 'utf8');
+ await expect
+ .poll(async () => {
+ const nestedCompositionBox = await page
+ .getByTitle('AnimatedBarChart', {exact: true})
+ .boundingBox();
+ const schemaCompositionBox = await schemaComposition.boundingBox();
+ return (
+ nestedCompositionBox !== null &&
+ schemaCompositionBox !== null &&
+ nestedCompositionBox.y > schemaCompositionBox.y
+ );
+ })
+ .toBe(true);
+
+ expect(
+ await dragCompositionSelectorItemToRoot({
+ page,
+ sourceTitle: 'AnimatedBarChart',
+ drop: false,
+ }),
+ ).toBe(true);
+ const rootReorderLine = page.locator(
+ '[data-composition-root-reorder-line]',
+ );
+ await expect(rootReorderLine).toBeVisible();
+ await expect(rootReorderLine).toHaveCSS('height', '2px');
+ await expect(rootReorderLine).toHaveCSS(
+ 'background-color',
+ 'rgb(11, 132, 255)',
+ );
+ const rootLineBox = await rootReorderLine.boundingBox();
+ const lastItemBox = await page
+ .locator('.__remotion-composition-selector-item')
+ .last()
+ .boundingBox();
+ expect(rootLineBox).not.toBeNull();
+ expect(lastItemBox).not.toBeNull();
+ expect(rootLineBox!.y).toBeGreaterThanOrEqual(
+ lastItemBox!.y + lastItemBox!.height - 2,
+ );
+
+ expect(
+ await dragCompositionSelectorItemToRoot({
+ page,
+ sourceTitle: 'AnimatedBarChart',
+ drop: true,
+ }),
+ ).toBe(true);
+ await expect
+ .poll(() => {
+ const contents = fs.readFileSync(rootFile, 'utf8');
+ const folderStart = contents.indexOf('<Folder name="Schema">');
+ const folderEnd = contents.indexOf('</Folder>', folderStart);
+ const composition = contents.indexOf('id="AnimatedBarChart"');
+ return composition > folderEnd;
+ })
+ .toBe(true);
const undoButton = page.getByRole('button', {name: /^Undo/});
await undoButton.click();
+ await expect
+ .poll(() => fs.readFileSync(rootFile, 'utf8'))
+ .toBe(afterCompositionNested);
+ await undoButton.click();
await expect
.poll(() => fs.readFileSync(rootFile, 'utf8'))
.toBe(afterFolderReorder);
diff --git a/packages/studio-codemods/src/recast-mods.ts b/packages/studio-codemods/src/recast-mods.ts
index 8c971a8045..8d0a9b8121 100644
--- a/packages/studio-codemods/src/recast-mods.ts
+++ b/packages/studio-codemods/src/recast-mods.ts
@@ -461,44 +461,42 @@ const appendElementToFolder = ({
const appendElementToRoot = ({
element,
- file,
+ returnStatement,
}: {
element: JSXElement;
- file: File;
+ returnStatement: ReturnStatement;
}) => {
- let appended = false;
-
- recast.types.visit(file, {
- visitReturnStatement(astPath) {
- if (appended) {
- return false;
- }
-
- const {argument} = astPath.node;
- if (argument?.type !== 'JSXFragment' && argument?.type !== 'JSXElement') {
- this.traverse(astPath);
- return undefined;
- }
+ const {argument} = returnStatement;
+ if (argument?.type !== 'JSXFragment' && argument?.type !== 'JSXElement') {
+ throw new Error('Could not find a root JSX element');
+ }
- if (argument.type === 'JSXFragment') {
- (argument.children as JSXFragment['children']).push(
- stripParenthesizedExtra(element),
- );
- } else {
- astPath.node.argument = wrapInJsxFragment([
- argument as unknown as JSXElement,
- element,
- ]) as never;
- }
+ if (argument.type === 'JSXFragment') {
+ (argument.children as JSXFragment['children']).push(
+ stripParenthesizedExtra(element),
+ );
+ } else {
+ returnStatement.argument = wrapInJsxFragment([
+ argument as unknown as JSXElement,
+ element,
+ ]) as never;
+ }
+};
- appended = true;
- return false;
- },
- });
+const getEnclosingReturnStatement = (path: recast.types.NodePath) => {
+ let currentPath: recast.types.NodePath | null = path;
+ while (currentPath !== null) {
+ if (
+ (currentPath.node as unknown as {type?: string} | null)?.type ===
+ 'ReturnStatement'
+ ) {
+ return currentPath.node as unknown as ReturnStatement;
+ }
- if (!appended) {
- throw new Error('Could not find a root JSX element');
+ currentPath = currentPath.parentPath ?? null;
}
+
+ throw new Error('Could not find a root JSX element');
};
const getCompositionOrFolderLabel = (item: CompositionOrFolder) => {
@@ -713,10 +711,11 @@ const moveCompositionOrFolder = ({
}
}
+ const sourceReturnStatement = getEnclosingReturnStatement(sourceItem.path);
deleteJsxElementAtPath(sourceItem.path);
const element = stripParenthesizedExtra(sourceItem.node);
if (transformation.destination.type === 'root') {
- appendElementToRoot({element, file});
+ appendElementToRoot({element, returnStatement: sourceReturnStatement});
} else if (transformation.destination.type === 'folder') {
appendElementToFolder({
element,
@@ -833,9 +832,13 @@ const moveCompositionToFolder = ({
const compositionElement = (sourcePath as recast.types.NodePath)
.node as JSXElement;
+ const sourceReturnStatement = getEnclosingReturnStatement(sourcePath);
deleteJsxElementAtPath(sourcePath);
if (transformation.folderName === null) {
- appendElementToRoot({element: compositionElement, file});
+ appendElementToRoot({
+ element: compositionElement,
+ returnStatement: sourceReturnStatement,
+ });
changesMade.push({description: 'Moved composition to root'});
} else {
if (targetFolder === null) {
diff --git a/packages/studio-server/src/test/apply-codemod.test.ts b/packages/studio-server/src/test/apply-codemod.test.ts
index 669d2b6f53..4706e0db55 100644
--- a/packages/studio-server/src/test/apply-codemod.test.ts
+++ b/packages/studio-server/src/test/apply-codemod.test.ts
@@ -155,6 +155,31 @@ export const RemotionRoot: React.FC = () => {
};
`;
+const folderRootWithEarlierComponentContents = `import React from 'react';
+import {Composition, Folder} from 'remotion';
+
+const Component = () => {
+ return <><div>Content</div></>;
+};
+
+export const RemotionRoot: React.FC = () => {
+ return (
+ <>
+ <Folder name="Parent">
+ <Composition
+ id="Nested"
+ component={Component}
+ durationInFrames={120}
+ fps={30}
+ width={1280}
+ height={720}
+ />
+ </Folder>
+ </>
+ );
+};
+`;
+
const clearUndoRedoStacks = () => {
(getUndoStack() as unknown as unknown[]).length = 0;
(getRedoStack() as unknown as unknown[]).length = 0;
@@ -983,6 +1008,26 @@ test('moves a composition to root', () => {
expect(newContents.match(/id="NestedA"/g)?.length).toBe(1);
});
+test('moves a composition to its registration root when another component appears first', () => {
+ const {changesMade, newContents} = parseAndApplyCodemod({
+ input: folderRootWithEarlierComponentContents,
+ codeMod: {
+ type: 'move-composition-or-folder',
+ source: {type: 'composition', compositionId: 'Nested'},
+ destination: {type: 'root'},
+ },
+ });
+
+ expect(changesMade).toHaveLength(1);
+ expect(newContents.match(/id="Nested"/g)).toHaveLength(1);
+ expect(newContents.indexOf('id="Nested"')).toBeGreaterThan(
+ newContents.indexOf('export const RemotionRoot'),
+ );
+ expect(newContents.indexOf('id="Nested"')).toBeGreaterThan(
+ newContents.indexOf('</Folder>'),
+ );
+});
+
test('visually reorders compositions and folders', () => {
const compositionBeforeFolder = parseAndApplyCodemod({
input: selfClosingFolderRootContents,
diff --git a/packages/studio/src/components/CompositionSelector.tsx b/packages/studio/src/components/CompositionSelector.tsx
index 49d090cbe4..c2a5b5a3aa 100644
--- a/packages/studio/src/components/CompositionSelector.tsx
+++ b/packages/studio/src/components/CompositionSelector.tsx
@@ -8,7 +8,7 @@ import React, {
} from 'react';
import {Internals} from 'remotion';
import {StudioServerConnectionCtx} from '../helpers/client-id';
-import {BACKGROUND, WHITE_ALPHA_12} from '../helpers/colors';
+import {BACKGROUND, TIMELINE_BLUE} from '../helpers/colors';
import {
createFolderTree,
sortFolderTreeAlphabetically,
@@ -97,6 +97,21 @@ const container: React.CSSProperties = {
backgroundColor: BACKGROUND,
};
+const list: React.CSSProperties = {
+ flex: 1,
+ overflowY: 'auto',
+ paddingTop: 4,
+ paddingBottom: 4,
+ backgroundColor: BACKGROUND,
+};
+
+const rootReorderLine: React.CSSProperties = {
+ backgroundColor: TIMELINE_BLUE,
+ height: 2,
+ pointerEvents: 'none',
+ width: '100%',
+};
+
const autoScrollThreshold = 70;
const maxAutoScrollSpeed = 18;
@@ -170,16 +185,6 @@ export const CompositionSelector: React.FC = () => {
: tree;
}, [compositionSortOrder, compositions, folders, foldersExpanded]);
- const list: React.CSSProperties = useMemo(() => {
- return {
- flex: 1,
- overflowY: 'auto',
- paddingTop: 4,
- paddingBottom: 4,
- backgroundColor: rootDragHovered ? WHITE_ALPHA_12 : BACKGROUND,
- };
- }, [rootDragHovered]);
-
const toggleFolder = useCallback(
(folderName: string, parentName: string | null) => {
Internals.compositionSelectorRef.current?.toggleFolder(
@@ -409,6 +414,9 @@ export const CompositionSelector: React.FC = () => {
/>
);
})}
+ {rootDragHovered ? (
+ <div data-composition-root-reorder-line style={rootReorderLine} />
+ ) : null}
</div>
</div>
);
diff --git a/packages/studio/src/components/CompositionSelectorItem.tsx b/packages/studio/src/components/CompositionSelectorItem.tsx
index e433230088..914761f4b9 100644
--- a/packages/studio/src/components/CompositionSelectorItem.tsx
+++ b/packages/studio/src/components/CompositionSelectorItem.tsx
@@ -248,6 +248,7 @@ export const CompositionSelectorItem: React.FC<{
}, [item, currentComposition]);
const [isDragging, setIsDragging] = useState(false);
const [dropPosition, setDropPosition] = useState<DropPosition | null>(null);
+ const dropPositionRef = useRef<DropPosition | null>(null);
const folderExpansionTimer = useRef<number | null>(null);
const folderExpansionRequested = useRef(false);
@@ -257,6 +258,10 @@ export const CompositionSelectorItem: React.FC<{
folderExpansionTimer.current = null;
}
}, []);
+ const updateDropPosition = useCallback((position: DropPosition | null) => {
+ dropPositionRef.current = position;
+ setDropPosition(position);
+ }, []);
useEffect(() => {
return cancelFolderExpansion;
@@ -425,8 +430,8 @@ export const CompositionSelectorItem: React.FC<{
activeDragRef.current = null;
cancelFolderExpansion();
setIsDragging(false);
- setDropPosition(null);
- }, [activeDragRef, cancelFolderExpansion]);
+ updateDropPosition(null);
+ }, [activeDragRef, cancelFolderExpansion, updateDropPosition]);
const getDropPosition = useCallback(
(event: DragEvent<HTMLElement>): DropPosition | null => {
@@ -558,7 +563,7 @@ export const CompositionSelectorItem: React.FC<{
const position = getDropPosition(event);
if (position === null) {
cancelFolderExpansion();
- setDropPosition(null);
+ updateDropPosition(null);
event.stopPropagation();
return;
}
@@ -573,20 +578,21 @@ export const CompositionSelectorItem: React.FC<{
event.stopPropagation();
event.dataTransfer.dropEffect = 'move';
clearRootDragHover();
- setDropPosition(position);
+ updateDropPosition(position);
},
[
cancelFolderExpansion,
clearRootDragHover,
getDropPosition,
scheduleFolderExpansion,
+ updateDropPosition,
],
);
const onRowDragLeave = useCallback(() => {
cancelFolderExpansion();
- setDropPosition(null);
- }, [cancelFolderExpansion]);
+ updateDropPosition(null);
+ }, [cancelFolderExpansion, updateDropPosition]);
const moveItem = useCallback(
async ({
@@ -645,7 +651,7 @@ export const CompositionSelectorItem: React.FC<{
async (event: DragEvent<HTMLElement>) => {
cancelFolderExpansion();
const dragData = parseCompositionSelectorDragData(event.dataTransfer);
- const position = getDropPosition(event);
+ const position = dropPositionRef.current;
if (dragData === null) {
return;
}
@@ -658,7 +664,7 @@ export const CompositionSelectorItem: React.FC<{
event.preventDefault();
event.stopPropagation();
clearRootDragHover();
- setDropPosition(null);
+ updateDropPosition(null);
await moveItem({
dragData,
destination:
@@ -677,9 +683,9 @@ export const CompositionSelectorItem: React.FC<{
[
cancelFolderExpansion,
clearRootDragHover,
- getDropPosition,
item,
moveItem,
+ updateDropPosition,
],
);
diff --git a/packages/studio/src/test/composition-selector-drag-data.test.ts b/packages/studio/src/test/composition-selector-drag-data.test.ts
new file mode 100644
index 0000000000..bb23a33141
--- /dev/null
+++ b/packages/studio/src/test/composition-selector-drag-data.test.ts
@@ -0,0 +1,134 @@
+import {expect, test} from 'bun:test';
+import {
+ compositionSelectorDragDataToSymbolicatedStack,
+ hasCompositionSelectorDragData,
+ makeCompositionSelectorDragData,
+ parseCompositionSelectorDragData,
+} from '../components/composition-selector-drag-data';
+
+const composition = makeCompositionSelectorDragData({
+ item: {type: 'composition', compositionId: 'MyVideo'},
+ sourceFile: 'src/Root.tsx',
+});
+
+const folder = makeCompositionSelectorDragData({
+ item: {type: 'folder', folderName: 'Nested', parentName: 'Parent'},
+ sourceFile: null,
+});
+
+const makeDataTransfer = ({
+ mimeType,
+ payload,
+}: {
+ readonly mimeType: string;
+ readonly payload: string;
+}) => ({
+ types: ['text/plain', mimeType],
+ getData: (requestedMimeType: string) =>
+ requestedMimeType === mimeType ? payload : '',
+});
+
+test('constructs and parses composition selector drag data', () => {
+ expect(composition.mimeType).toBe(
+ 'application/remotion-composition-selector-reorder',
+ );
+ expect(hasCompositionSelectorDragData(['text/plain'])).toBe(false);
+ expect(
+ hasCompositionSelectorDragData(['text/plain', composition.mimeType]),
+ ).toBe(true);
+ expect(
+ parseCompositionSelectorDragData(makeDataTransfer(composition)),
+ ).toEqual(composition.data);
+ expect(parseCompositionSelectorDragData(makeDataTransfer(folder))).toEqual(
+ folder.data,
+ );
+});
+
+test('rejects malformed composition selector drag data', () => {
+ expect(
+ parseCompositionSelectorDragData({
+ types: ['text/plain'],
+ getData: () => composition.payload,
+ }),
+ ).toBe(null);
+
+ const validComposition = {
+ type: 'remotion-composition-selector',
+ version: 1,
+ item: {type: 'composition', compositionId: 'MyVideo'},
+ sourceFile: 'src/Root.tsx',
+ };
+ const validFolder = {
+ ...validComposition,
+ item: {type: 'folder', folderName: 'Nested', parentName: 'Parent'},
+ };
+
+ for (const payload of [
+ '',
+ '{',
+ JSON.stringify({...validComposition, type: 'other'}),
+ JSON.stringify({...validComposition, version: 2}),
+ JSON.stringify({...validComposition, item: null}),
+ JSON.stringify({
+ ...validComposition,
+ item: {type: 'other', compositionId: 'MyVideo'},
+ }),
+ JSON.stringify({
+ ...validComposition,
+ item: {type: 'composition', compositionId: ''},
+ }),
+ JSON.stringify({
+ ...validComposition,
+ item: {type: 'composition', compositionId: 'a'.repeat(501)},
+ }),
+ JSON.stringify({
+ ...validComposition,
+ item: {type: 'composition', compositionId: 'Invalid\0id'},
+ }),
+ JSON.stringify({
+ ...validFolder,
+ item: {...validFolder.item, folderName: ''},
+ }),
+ JSON.stringify({
+ ...validFolder,
+ item: {...validFolder.item, folderName: 'a'.repeat(501)},
+ }),
+ JSON.stringify({
+ ...validFolder,
+ item: {...validFolder.item, parentName: 'a'.repeat(2001)},
+ }),
+ JSON.stringify({
+ ...validFolder,
+ item: {type: 'folder', folderName: 'Nested'},
+ }),
+ JSON.stringify({...validComposition, sourceFile: undefined}),
+ JSON.stringify({...validComposition, sourceFile: ''}),
+ JSON.stringify({...validComposition, sourceFile: 'a'.repeat(2001)}),
+ JSON.stringify({...validComposition, sourceFile: '/src/Root.tsx'}),
+ JSON.stringify({...validComposition, sourceFile: '../Root.tsx'}),
+ JSON.stringify({...validComposition, sourceFile: 'src/../Root.tsx'}),
+ JSON.stringify({...validComposition, sourceFile: 'src\\Root.tsx'}),
+ JSON.stringify({...validComposition, sourceFile: 'src/Root\0.tsx'}),
+ ]) {
+ expect(
+ parseCompositionSelectorDragData(
+ makeDataTransfer({mimeType: composition.mimeType, payload}),
+ ),
+ ).toBe(null);
+ }
+});
+
+test('converts composition selector source locations to symbolicated stacks', () => {
+ expect(
+ compositionSelectorDragDataToSymbolicatedStack(composition.data),
+ ).toEqual({
+ originalColumnNumber: null,
+ originalFileName: 'src/Root.tsx',
+ originalFunctionName: null,
+ originalLineNumber: null,
+ originalScriptCode: null,
+ });
+ expect(compositionSelectorDragDataToSymbolicatedStack(folder.data)).toBe(
+ null,
+ );
+});