🎥 Make videos programmatically with React
`@remotion/studio`: Fix dragging local assets onto the canvas (#10682)
Jonny Burger6fd5c7739764
diff --git a/packages/browser-studio/e2e/browser-studio.test.ts b/packages/browser-studio/e2e/browser-studio.test.tsindex aea7763956..52bd7260aa 100644--- a/packages/browser-studio/e2e/browser-studio.test.ts+++ b/packages/browser-studio/e2e/browser-studio.test.ts@@ -1,9 +1,74 @@-import {expect, test} from '@playwright/test';+import {fileURLToPath} from 'node:url';+import {+ expect,+ test,+ type FrameLocator,+ type Locator,+ type Page,+} from '@playwright/test';import {createElementPayload,StudioProtocolInternals,} from '@remotion/studio-protocol';+const localImagePath = fileURLToPath(+ new URL('../../codex-plugin/assets/logo.png', import.meta.url),+);++const dropLocalFile = async ({+ filePath,+ page,+ target,+}: {+ filePath: string;+ page: Page;+ target: Locator;+}) => {+ const box = await target.boundingBox();+ if (box === null) {+ throw new Error('Browser Studio drop target has no bounding box');+ }++ const client = await page.context().newCDPSession(page);+ const dragData = {+ dragOperationsMask: 1,+ files: [filePath],+ items: [],+ };+ const coordinates = {x: box.x + box.width / 2, y: box.y + box.height / 2};+ await client.send('Input.dispatchDragEvent', {+ ...coordinates,+ data: dragData,+ type: 'dragEnter',+ });+ await client.send('Input.dispatchDragEvent', {+ ...coordinates,+ data: dragData,+ type: 'dragOver',+ });+ await client.send('Input.dispatchDragEvent', {+ ...coordinates,+ data: dragData,+ type: 'drop',+ });+};++const waitForBrowserStudioOperations = async (studio: FrameLocator) => {+ await expect+ .poll(() =>+ studio.locator('body').evaluate(() =>+ Boolean(+ (+ window as typeof window & {+ remotion_browserStudio?: unknown;+ }+ ).remotion_browserStudio,+ ),+ ),+ )+ .toBe(true);+};+test('loads Browser Studio, opens external links, and can add, delete, and duplicate', async ({page,}) => {@@ -232,6 +297,121 @@ test('loads Browser Studio from one immutable release artifact set', async ({expect(remoteRemotionRequests).toEqual([]);});+test('drops a local image onto the canvas and imports it into the virtual project', async ({+ page,+}) => {+ const studioApiRequests: string[] = [];+ page.on('request', (request) => {+ const requestUrl = new URL(request.url());+ if (+ requestUrl.pathname.startsWith('/api/') &&+ requestUrl.origin === new URL(page.url()).origin+ ) {+ studioApiRequests.push(requestUrl.pathname);+ }+ });++ await page.goto('/');+ const studio = page.frameLocator('iframe');+ await expect(studio.getByTitle('/project').getByText('MyComp')).toBeVisible();+ await waitForBrowserStudioOperations(studio);+ await studio.locator('[data-compname="MyComp"]').click();+ const canvas = studio.locator('.remotion-studio-composition-container');+ await expect(canvas).toBeVisible();+ await dropLocalFile({+ filePath: localImagePath,+ page,+ target: canvas,+ });+ await expect(studio.getByText('logo.png', {exact: true})).toBeVisible();+ await expect(studio.getByText('<CanvasImage>', {exact: true})).toBeVisible();++ await expect+ .poll(() =>+ page.evaluate(() => {+ const browserWindow = window as typeof window & {+ __browserStudioProject: {+ files: Record<string, string>;+ publicFiles?: Record<string, Uint8Array | string>;+ };+ };+ const contents =+ browserWindow.__browserStudioProject.publicFiles?.['logo.png'];+ return {+ composition:+ browserWindow.__browserStudioProject.files[+ '/project/src/Composition.tsx'+ ],+ publicFileSize:+ typeof contents === 'string'+ ? new TextEncoder().encode(contents).byteLength+ : contents?.byteLength,+ };+ }),+ )+ .toMatchObject({+ composition: expect.stringContaining('logo.png'),+ publicFileSize: expect.any(Number),+ });+ expect(studioApiRequests).toEqual([]);+});++test('drops a local file into the virtual Assets folder', async ({page}) => {+ const studioApiRequests: string[] = [];+ page.on('request', (request) => {+ const requestUrl = new URL(request.url());+ if (+ requestUrl.pathname.startsWith('/api/') &&+ requestUrl.origin === new URL(page.url()).origin+ ) {+ studioApiRequests.push(requestUrl.pathname);+ }+ });++ await page.goto('/');+ const studio = page.frameLocator('iframe');+ await expect(studio.getByTitle('/project').getByText('MyComp')).toBeVisible();+ await waitForBrowserStudioOperations(studio);+ await studio.getByRole('button', {name: 'Assets', exact: true}).click();+ const assetSelector = studio.locator('[data-asset-selector]');+ await expect(assetSelector).toBeVisible();+ await dropLocalFile({+ filePath: localImagePath,+ page,+ target: assetSelector,+ });++ await expect(studio.getByText('logo.png', {exact: true})).toBeVisible();+ await expect+ .poll(() =>+ page.evaluate(() => {+ const browserWindow = window as typeof window & {+ __browserStudioProject: {+ files: Record<string, string>;+ publicFiles?: Record<string, Uint8Array | string>;+ };+ };+ const contents =+ browserWindow.__browserStudioProject.publicFiles?.['logo.png'];+ return {+ composition:+ browserWindow.__browserStudioProject.files[+ '/project/src/Composition.tsx'+ ],+ publicFileSize:+ typeof contents === 'string'+ ? new TextEncoder().encode(contents).byteLength+ : contents?.byteLength,+ };+ }),+ )+ .toMatchObject({+ composition: expect.not.stringContaining('logo.png'),+ publicFileSize: expect.any(Number),+ });+ expect(studioApiRequests).toEqual([]);+});+test('installs packages without a server API and preserves undo, redo, and HMR', async ({page,}) => {@@ -249,6 +429,7 @@ test('installs packages without a server API and preserves undo, redo, and HMR',await page.goto('/');const studio = page.frameLocator('iframe');await expect(studio.getByTitle('/project').getByText('MyComp')).toBeVisible();+ await waitForBrowserStudioOperations(studio);await studio.locator('body').evaluate(() => {(window as typeof window & {diff --git a/packages/studio/src/components/Canvas.tsx b/packages/studio/src/components/Canvas.tsxindex d169055ef8..e3e4f2795b 100644--- a/packages/studio/src/components/Canvas.tsx+++ b/packages/studio/src/components/Canvas.tsx@@ -1137,14 +1137,17 @@ export const Canvas: React.FC<{return;}- if (isFileDragEvent(event) && !window.remotion_isReadOnlyStudio) {+ const localFiles = isFileDragEvent(event)+ ? Array.from(event.dataTransfer?.files ?? [])+ : null;++ if (localFiles !== null && !window.remotion_isReadOnlyStudio) {event.preventDefault();event.stopPropagation();- const files = Array.from(event.dataTransfer?.files ?? []);- if (files.length === 1) {+ if (localFiles.length === 1) {setIsAddingAsset(true);try {- const canvasCapture = await getCanvasCaptureImport(files[0]);+ const canvasCapture = await getCanvasCaptureImport(localFiles[0]);if (canvasCapture !== null) {setSelectedModal({type: 'new-comp',@@ -1244,6 +1247,7 @@ export const Canvas: React.FC<{event,fps: config.fps,from: getCurrentFrame(),+ localFiles,preferCompositionStart: true,});} finally {diff --git a/packages/studio/src/components/Timeline/use-timeline-asset-drop.ts b/packages/studio/src/components/Timeline/use-timeline-asset-drop.tsindex cccb232559..f16df93a2a 100644--- a/packages/studio/src/components/Timeline/use-timeline-asset-drop.ts+++ b/packages/studio/src/components/Timeline/use-timeline-asset-drop.ts@@ -172,6 +172,7 @@ export const useTimelineAssetDrop = () => {event,fps: videoConfig.fps,from: frame,+ localFiles: null,preferCompositionStart: false,});} finally {diff --git a/packages/studio/src/components/handle-drop.ts b/packages/studio/src/components/handle-drop.tsindex 1e7a5c3ebe..c21412aea4 100644--- a/packages/studio/src/components/handle-drop.ts+++ b/packages/studio/src/components/handle-drop.ts@@ -35,6 +35,7 @@ export const handleDrop = async ({event,fps,from,+ localFiles,preferCompositionStart,}: {chooseSvgImportMode: () => Promise<SvgImportMode | null>;@@ -45,10 +46,11 @@ export const handleDrop = async ({event: DragEvent;fps: number;from: number | null;+ localFiles: File[] | null;preferCompositionStart: boolean;}) => {- if (isFileDragEvent(event)) {- const files = Array.from(event.dataTransfer?.files ?? []);+ if (localFiles !== null || isFileDragEvent(event)) {+ const files = localFiles ?? Array.from(event.dataTransfer?.files ?? []);if (files.length === 0) {return;}