🎥 Make videos programmatically with React
`@remotion/studio`: Show feedback for unsupported Element drops (#11321)
Igor Samokhovets7908240a3811
diff --git a/packages/example/e2e/studio-protocol.test.mts b/packages/example/e2e/studio-protocol.test.mtsindex da0ba9d0ed..0a5d4370b6 100644--- a/packages/example/e2e/studio-protocol.test.mts+++ b/packages/example/e2e/studio-protocol.test.mts@@ -72,6 +72,14 @@ export const MyComposition = () => {width={1280}height={720}/>+ <Composition+ id="DirectCanvas"+ component={DirectCanvas}+ durationInFrames={60}+ fps={30}+ width={1280}+ height={720}+ /><CloseupFolder /></>);@@ -80,6 +88,10 @@ export const MyComposition = () => {export const MyComponent = () => {return <AbsoluteFill></AbsoluteFill>;};++export const DirectCanvas = () => {+ return <canvas width={1280} height={720} />;+};`,);const closeupDirectory = path.join(temporaryProject, 'src', 'closeups');@@ -171,7 +183,7 @@ const CloseupPlaceholder = () => {<p id="environment"></p><p id="status"></p><script type="module">- import {addElementLibraryToStudio, createElementPayload, installInStudio, isInsideStudio, StudioProtocolInternals} from '/protocol.js';+ import {addElementLibraryToStudio, createElementPayload, installInStudio, isInsideStudio, setStudioDragData, StudioProtocolInternals} from '/protocol.js';document.querySelector('#environment').textContent = isInsideStudio() ? 'Inside Remotion Studio' : 'Outside Remotion Studio';const payload = createElementPayload({displayName: 'Protocol Element',@@ -181,6 +193,14 @@ const CloseupPlaceholder = () => {dimensions: {width: 640, height: 120},durationInFrames: 30,});+ const dragHandle = document.createElement('div');+ dragHandle.id = 'drag-element';+ dragHandle.draggable = true;+ dragHandle.textContent = 'Drag into Studio';+ document.body.appendChild(dragHandle);+ dragHandle.ondragstart = (event) => {+ setStudioDragData({dataTransfer: event.dataTransfer, payload});+ };document.querySelector('#install').onclick = async () => {document.querySelector('#status').textContent = '';const result = await installInStudio({payload});@@ -421,6 +441,75 @@ const CloseupPlaceholder = () => {await studioPage.mouse.click(500, 300);const senderPage = await context.newPage();await senderPage.goto(senderUrl);++ const elementDragData = await senderPage+ .locator('#drag-element')+ .evaluate((dragHandle) => {+ const dataTransfer = new DataTransfer();+ dragHandle.dispatchEvent(+ new DragEvent('dragstart', {+ bubbles: true,+ cancelable: true,+ dataTransfer,+ }),+ );+ return Array.from(dataTransfer.types, (type) => ({+ data: dataTransfer.getData(type),+ type,+ }));+ });+ expect(+ elementDragData.some(({type}) =>+ type.startsWith('application/vnd.remotion.drag+json;v=1;type=element'),+ ),+ ).toBe(true);++ await studioPage.bringToFront();+ const directCanvasInfo = studioPage.waitForResponse(+ (response) =>+ new URL(response.url()).pathname ===+ '/api/composition-component-info' &&+ response.request().postDataJSON().compositionId === 'DirectCanvas',+ );+ await studioPage.getByText('DirectCanvas', {exact: true}).first().click();+ expect(await (await directCanvasInfo).json()).toMatchObject({+ data: {canAddSequence: false},+ success: true,+ });++ const cannotInsertNotification = studioPage.getByText(+ 'Cannot insert this item: This composition component cannot accept a new <Sequence>.',+ {exact: true},+ );+ const dragElementOver = async (selector: string) => {+ await studioPage.locator(selector).evaluate((target, dragData) => {+ const dataTransfer = new DataTransfer();+ for (const {data, type} of dragData) {+ dataTransfer.setData(type, data);+ }++ target.dispatchEvent(+ new DragEvent('dragover', {+ bubbles: true,+ cancelable: true,+ dataTransfer,+ }),+ );+ }, elementDragData);+ };++ await dragElementOver('.remotion-studio-composition-container');+ await expect(cannotInsertNotification).toBeVisible();+ await expect(cannotInsertNotification).toHaveCount(0, {timeout: 5000});++ await dragElementOver('[data-timeline-scrollable="true"]');+ await expect(cannotInsertNotification).toBeVisible();+ await dragElementOver('.remotion-studio-composition-container');+ await dragElementOver('[data-timeline-scrollable="true"]');+ await expect(cannotInsertNotification).toHaveCount(1);+ await expect(cannotInsertNotification).toHaveCount(0, {timeout: 5000});++ await studioPage.getByText('MyComp', {exact: true}).first().click();await senderPage.getByRole('button', {name: 'Install in Studio'}).click();await studioPage.bringToFront();const newCompositionDialog = studioPage.getByRole('dialog', {diff --git a/packages/studio/src/components/Canvas.tsx b/packages/studio/src/components/Canvas.tsxindex 532c71d4a4..3f06cc016b 100644--- a/packages/studio/src/components/Canvas.tsx+++ b/packages/studio/src/components/Canvas.tsx@@ -83,7 +83,10 @@ import {type InsertElementDropPosition,} from './import-assets';import {SPACING_UNIT} from './layout';-import {showNotification} from './Notifications/NotificationCenter';+import {+ showCannotAddSequenceDropNotification,+ showNotification,+} from './Notifications/NotificationCenter';import {VideoPreview} from './Preview';import {ResetZoomButton} from './ResetZoomButton';import {useSvgImportDialog} from './SvgImportDialog';@@ -261,6 +264,7 @@ export const Canvas: React.FC<{const lastFocusedAtRef = useRef<number | null>(typeof document === 'undefined' || document.hasFocus() ? Date.now() : null,);+ const unsupportedDropNotifiedRef = useRef(false);const [assetResolution, setAssetResolution] = useState<AssetMetadata | null>(null,@@ -1113,12 +1117,24 @@ export const Canvas: React.FC<{const onDragOver = useCallback((event: DragEvent) => {if (!isSupportedDropEvent(event) || !isDragEventInsideCanvas(event)) {+ unsupportedDropNotifiedRef.current = false;setCompositionDropPreview(null);return;}const mayBeCanvasCapture =isFileDragEvent(event) && !window.remotion_isReadOnlyStudio;+ const shouldNotifyAboutUnsupportedDrop =+ cannotAddSequence && !mayBeCanvasCapture;+ if (+ shouldNotifyAboutUnsupportedDrop &&+ !unsupportedDropNotifiedRef.current+ ) {+ showCannotAddSequenceDropNotification();+ }++ unsupportedDropNotifiedRef.current = shouldNotifyAboutUnsupportedDrop;+if (!canDropAssets && !cannotAddSequence && !mayBeCanvasCapture) {setCompositionDropPreview(null);return;@@ -1234,15 +1250,19 @@ export const Canvas: React.FC<{}}+ unsupportedDropNotifiedRef.current = false;setCompositionDropPreview(null);}, []);const onDragEnd = useCallback(() => {+ unsupportedDropNotifiedRef.current = false;setCompositionDropPreview(null);}, []);const onDrop = useCallback(async (event: DragEvent) => {+ const unsupportedDropWasNotified = unsupportedDropNotifiedRef.current;+ unsupportedDropNotifiedRef.current = false;setCompositionDropPreview(null);if (!isSupportedDropEvent(event) || !isDragEventInsideCanvas(event)) {@@ -1276,10 +1296,10 @@ export const Canvas: React.FC<{if (cannotAddSequence) {event.preventDefault();event.stopPropagation();- showNotification(- 'Cannot insert items into this composition component',- 3000,- );+ if (!unsupportedDropWasNotified) {+ showCannotAddSequenceDropNotification();+ }+return;}diff --git a/packages/studio/src/components/Notifications/NotificationCenter.tsx b/packages/studio/src/components/Notifications/NotificationCenter.tsxindex be3906fbac..4a89e86d28 100644--- a/packages/studio/src/components/Notifications/NotificationCenter.tsx+++ b/packages/studio/src/components/Notifications/NotificationCenter.tsx@@ -55,6 +55,37 @@ export const showNotification = (});};+const CANNOT_ADD_SEQUENCE_DROP_NOTIFICATION =+ 'Cannot insert this item: This composition component cannot accept a new <Sequence>.';+const CANNOT_ADD_SEQUENCE_NOTIFICATION_DURATION = 3000;+let cannotAddSequenceNotification: CreatedNotification | null = null;+let cannotAddSequenceDismissTimeout: ReturnType<typeof setTimeout> | null =+ null;++export const showCannotAddSequenceDropNotification = () => {+ if (cannotAddSequenceNotification === null) {+ cannotAddSequenceNotification = showNotification(+ CANNOT_ADD_SEQUENCE_DROP_NOTIFICATION,+ null,+ );+ } else {+ cannotAddSequenceNotification.replaceContent(+ CANNOT_ADD_SEQUENCE_DROP_NOTIFICATION,+ null,+ );+ }++ if (cannotAddSequenceDismissTimeout !== null) {+ clearTimeout(cannotAddSequenceDismissTimeout);+ }++ cannotAddSequenceDismissTimeout = setTimeout(() => {+ cannotAddSequenceNotification?.dismiss();+ cannotAddSequenceNotification = null;+ cannotAddSequenceDismissTimeout = null;+ }, CANNOT_ADD_SEQUENCE_NOTIFICATION_DURATION);+};+export const NotificationCenter: React.FC = () => {const [notifications, setNotifications] = useState<TNotification[]>([]);const {currentZIndex} = useZIndex();diff --git a/packages/studio/src/components/Timeline/use-timeline-asset-drop.ts b/packages/studio/src/components/Timeline/use-timeline-asset-drop.tsindex c345f5f580..716676313c 100644--- a/packages/studio/src/components/Timeline/use-timeline-asset-drop.ts+++ b/packages/studio/src/components/Timeline/use-timeline-asset-drop.ts@@ -1,4 +1,11 @@-import {useCallback, useContext, useEffect, useMemo, useState} from 'react';+import {+ useCallback,+ useContext,+ useEffect,+ useMemo,+ useRef,+ useState,+} from 'react';import {Internals} from 'remotion';import {StudioServerConnectionCtx} from '../../helpers/client-id';import {isStudioInteractivityEnabled} from '../../helpers/interactivity-enabled';@@ -8,7 +15,7 @@ import {handleCanvasCaptureDrop} from '../canvas-capture-drop';import {isFileDragEvent, isSupportedDropEvent} from '../drop-handler-data';import {getEffectDragData} from '../effect-drag-and-drop';import {handleDrop} from '../handle-drop';-import {showNotification} from '../Notifications/NotificationCenter';+import {showCannotAddSequenceDropNotification} from '../Notifications/NotificationCenter';import {useSvgImportDialog} from '../SvgImportDialog';import {getCurrentFrame} from './imperative-state';import {scrollableRef, timelineVerticalScroll} from './timeline-refs';@@ -32,6 +39,7 @@ export const useTimelineAssetDrop = () => {const {previewServerState} = useContext(StudioServerConnectionCtx);const [isAddingAsset, setIsAddingAsset] = useState(false);const [assetDropFrame, setAssetDropFrame] = useState<number | null>(null);+ const unsupportedDropNotifiedRef = useRef(false);const currentCompositionId =canvasContent?.type === 'composition' ? canvasContent.compositionId : null;@@ -93,10 +101,25 @@ export const useTimelineAssetDrop = () => {!isSupportedDropEvent(event) ||!isEventTargetInsideElement(event, timeline)) {+ unsupportedDropNotifiedRef.current = false;setAssetDropFrame(null);return;}+ const mayBeCanvasCapture =+ isFileDragEvent(event) && !window.remotion_isReadOnlyStudio;+ const shouldNotifyAboutUnsupportedDrop =+ compositionComponentInfo?.canAddSequence === false &&+ !mayBeCanvasCapture;+ if (+ shouldNotifyAboutUnsupportedDrop &&+ !unsupportedDropNotifiedRef.current+ ) {+ showCannotAddSequenceDropNotification();+ }++ unsupportedDropNotifiedRef.current = shouldNotifyAboutUnsupportedDrop;+event.preventDefault();event.stopPropagation();dataTransfer.dropEffect = canInsertAsset ? 'copy' : 'none';@@ -107,11 +130,13 @@ export const useTimelineAssetDrop = () => {isEventTargetInsideElement(event, scrollable);setAssetDropFrame(shouldShowDropFrame ? getDropFrame(event) : null);},- [canInsertAsset, getDropFrame],+ [canInsertAsset, compositionComponentInfo?.canAddSequence, getDropFrame],);const onAssetDrop = useCallback(async (event: DragEvent) => {+ const unsupportedDropWasNotified = unsupportedDropNotifiedRef.current;+ unsupportedDropNotifiedRef.current = false;setAssetDropFrame(null);const timeline = timelineVerticalScroll.current;const {dataTransfer} = event;@@ -158,11 +183,11 @@ export const useTimelineAssetDrop = () => {compositionFile === null ||videoConfig === null) {- if (compositionComponentInfo?.canAddSequence === false) {- showNotification(- 'Cannot insert items into this composition component',- 3000,- );+ if (+ compositionComponentInfo?.canAddSequence === false &&+ !unsupportedDropWasNotified+ ) {+ showCannotAddSequenceDropNotification();}return;@@ -210,6 +235,7 @@ export const useTimelineAssetDrop = () => {);const clearAssetDropFrame = useCallback(() => {+ unsupportedDropNotifiedRef.current = false;setAssetDropFrame(null);}, []);