Skip to content

🎥 Make videos programmatically with React

@remotion/studio: Offer new composition for Element drops

Igor Samokhovetsd32948e990c6
diff --git a/packages/example/e2e/studio-protocol.test.mts b/packages/example/e2e/studio-protocol.test.mts
index f6f252e8d1..e7e02ff551 100644
--- a/packages/example/e2e/studio-protocol.test.mts
+++ b/packages/example/e2e/studio-protocol.test.mts
@@ -504,37 +504,54 @@ const CloseupPlaceholder = () => {
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) => {
+ const dropElementOn = 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,
- }),
- );
+ for (const type of ['dragover', 'drop']) {
+ target.dispatchEvent(
+ new DragEvent(type, {
+ bubbles: true,
+ cancelable: true,
+ dataTransfer,
+ }),
+ );
+ }
}, elementDragData);
};
+ const expectNewCompositionFallback = async () => {
+ const fallbackDialog = studioPage.getByRole('dialog', {
+ name: 'Install Protocol Element',
+ });
+ await expect(fallbackDialog).toBeVisible();
+ await expect(
+ fallbackDialog.getByRole('button', {name: 'Current composition'}),
+ ).toBeDisabled();
+ await expect(
+ fallbackDialog.getByRole('button', {name: 'New composition'}),
+ ).toHaveAttribute('aria-pressed', 'true');
+ await expect(
+ fallbackDialog.getByText(
+ 'Studio could not find a safe place in “DirectCanvas” to insert the Element. Install it into a new composition instead.',
+ {exact: true},
+ ),
+ ).toBeVisible();
+ await expect(
+ fallbackDialog.getByText('Unverified drag-and-drop payload', {
+ exact: true,
+ }),
+ ).toBeVisible();
+ await fallbackDialog.getByRole('button', {name: 'Cancel'}).click();
+ await expect(fallbackDialog).toBeHidden();
+ };
- 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 dropElementOn('.remotion-studio-composition-container');
+ await expectNewCompositionFallback();
+ await dropElementOn('[data-timeline-scrollable="true"]');
+ await expectNewCompositionFallback();
await studioPage.getByText('MyComp', {exact: true}).first().click();
await senderPage.getByRole('button', {name: 'Install in Studio'}).click();
diff --git a/packages/studio/src/components/Canvas.tsx b/packages/studio/src/components/Canvas.tsx
index 3f06cc016b..fe2a43a486 100644
--- a/packages/studio/src/components/Canvas.tsx
+++ b/packages/studio/src/components/Canvas.tsx
@@ -67,6 +67,7 @@ import EditorGuides from './EditorGuides';
import {EditorRulers} from './EditorRuler';
import {useIsRulerVisible} from './EditorRuler/use-is-ruler-visible';
import {getEffectDragData} from './effect-drag-and-drop';
+import {getElementDragData, hasElementDragType} from './element-drag-and-drop';
import {prepareElementInstall} from './element-install-api';
import {
enqueueElementInstallRequest,
@@ -83,10 +84,7 @@ import {
type InsertElementDropPosition,
} from './import-assets';
import {SPACING_UNIT} from './layout';
-import {
- showCannotAddSequenceDropNotification,
- showNotification,
-} from './Notifications/NotificationCenter';
+import {showNotification} from './Notifications/NotificationCenter';
import {VideoPreview} from './Preview';
import {ResetZoomButton} from './ResetZoomButton';
import {useSvgImportDialog} from './SvgImportDialog';
@@ -264,8 +262,6 @@ 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,
);
@@ -1117,25 +1113,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;
+ const canDropElementIntoNewComposition =
+ cannotAddSequence &&
+ canReceiveElementInstallRequest &&
+ !isAddingAsset &&
+ hasElementDragType(event.dataTransfer);
+
if (
- shouldNotifyAboutUnsupportedDrop &&
- !unsupportedDropNotifiedRef.current
+ !canDropAssets &&
+ !canDropElementIntoNewComposition &&
+ !cannotAddSequence &&
+ !mayBeCanvasCapture
) {
- showCannotAddSequenceDropNotification();
- }
-
- unsupportedDropNotifiedRef.current = shouldNotifyAboutUnsupportedDrop;
-
- if (!canDropAssets && !cannotAddSequence && !mayBeCanvasCapture) {
setCompositionDropPreview(null);
return;
}
@@ -1143,7 +1138,11 @@ export const Canvas: React.FC<{
event.preventDefault();
if (event.dataTransfer) {
event.dataTransfer.dropEffect =
- canDropAssets || mayBeCanvasCapture ? 'copy' : 'none';
+ canDropAssets ||
+ canDropElementIntoNewComposition ||
+ mayBeCanvasCapture
+ ? 'copy'
+ : 'none';
}
if (
@@ -1227,8 +1226,10 @@ export const Canvas: React.FC<{
addFitPadding,
canDropAssets,
cannotAddSequence,
+ canReceiveElementInstallRequest,
contentDimensions,
editorSnapping,
+ isAddingAsset,
previewSize,
size,
],
@@ -1250,19 +1251,15 @@ 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)) {
@@ -1293,18 +1290,24 @@ export const Canvas: React.FC<{
}
}
- if (cannotAddSequence) {
+ const canDropElementIntoNewComposition =
+ cannotAddSequence &&
+ canReceiveElementInstallRequest &&
+ !isAddingAsset &&
+ getElementDragData(event.dataTransfer) !== null;
+
+ if (cannotAddSequence && !canDropElementIntoNewComposition) {
event.preventDefault();
event.stopPropagation();
- if (!unsupportedDropWasNotified) {
- showCannotAddSequenceDropNotification();
- }
-
+ showNotification(
+ 'Cannot insert this item: This composition component cannot accept a new <Sequence>.',
+ 3000,
+ );
return;
}
if (
- !canDropAssets ||
+ (!canDropAssets && !canDropElementIntoNewComposition) ||
compositionFile === null ||
currentCompositionId === null ||
config === null
@@ -1387,12 +1390,14 @@ export const Canvas: React.FC<{
addFitPadding,
canDropAssets,
cannotAddSequence,
+ canReceiveElementInstallRequest,
chooseSvgImportMode,
compositionFile,
config,
contentDimensions,
currentCompositionId,
editorSnapping,
+ isAddingAsset,
previewSize,
size,
setSelectedModal,
diff --git a/packages/studio/src/components/Notifications/NotificationCenter.tsx b/packages/studio/src/components/Notifications/NotificationCenter.tsx
index 4a89e86d28..be3906fbac 100644
--- a/packages/studio/src/components/Notifications/NotificationCenter.tsx
+++ b/packages/studio/src/components/Notifications/NotificationCenter.tsx
@@ -55,37 +55,6 @@ 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.ts
index 716676313c..e7b6c1cd5a 100644
--- a/packages/studio/src/components/Timeline/use-timeline-asset-drop.ts
+++ b/packages/studio/src/components/Timeline/use-timeline-asset-drop.ts
@@ -1,11 +1,4 @@
-import {
- useCallback,
- useContext,
- useEffect,
- useMemo,
- useRef,
- useState,
-} from 'react';
+import {useCallback, useContext, useEffect, useMemo, useState} from 'react';
import {Internals} from 'remotion';
import {StudioServerConnectionCtx} from '../../helpers/client-id';
import {isStudioInteractivityEnabled} from '../../helpers/interactivity-enabled';
@@ -14,8 +7,9 @@ import {SetSelectedModalContext} from '../../state/modals';
import {handleCanvasCaptureDrop} from '../canvas-capture-drop';
import {isFileDragEvent, isSupportedDropEvent} from '../drop-handler-data';
import {getEffectDragData} from '../effect-drag-and-drop';
+import {getElementDragData, hasElementDragType} from '../element-drag-and-drop';
import {handleDrop} from '../handle-drop';
-import {showCannotAddSequenceDropNotification} from '../Notifications/NotificationCenter';
+import {showNotification} from '../Notifications/NotificationCenter';
import {useSvgImportDialog} from '../SvgImportDialog';
import {getCurrentFrame} from './imperative-state';
import {scrollableRef, timelineVerticalScroll} from './timeline-refs';
@@ -39,7 +33,6 @@ 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;
@@ -61,14 +54,15 @@ export const useTimelineAssetDrop = () => {
const previewInteractive =
previewServerState.type === 'connected' && isStudioInteractivityEnabled();
- const canInsertAsset =
+ const canReceiveElementDrop =
previewInteractive &&
!window.remotion_isReadOnlyStudio &&
- compositionComponentInfo?.canAddSequence === true &&
currentCompositionId !== null &&
compositionFile !== null &&
videoConfig !== null &&
!isAddingAsset;
+ const canInsertAsset =
+ canReceiveElementDrop && compositionComponentInfo?.canAddSequence === true;
const getDropFrame = useCallback(
(event: DragEvent) => {
@@ -101,28 +95,19 @@ export const useTimelineAssetDrop = () => {
!isSupportedDropEvent(event) ||
!isEventTargetInsideElement(event, timeline)
) {
- unsupportedDropNotifiedRef.current = false;
setAssetDropFrame(null);
return;
}
- const mayBeCanvasCapture =
- isFileDragEvent(event) && !window.remotion_isReadOnlyStudio;
- const shouldNotifyAboutUnsupportedDrop =
+ const canDropElementIntoNewComposition =
compositionComponentInfo?.canAddSequence === false &&
- !mayBeCanvasCapture;
- if (
- shouldNotifyAboutUnsupportedDrop &&
- !unsupportedDropNotifiedRef.current
- ) {
- showCannotAddSequenceDropNotification();
- }
-
- unsupportedDropNotifiedRef.current = shouldNotifyAboutUnsupportedDrop;
+ canReceiveElementDrop &&
+ hasElementDragType(dataTransfer);
event.preventDefault();
event.stopPropagation();
- dataTransfer.dropEffect = canInsertAsset ? 'copy' : 'none';
+ dataTransfer.dropEffect =
+ canInsertAsset || canDropElementIntoNewComposition ? 'copy' : 'none';
const scrollable = scrollableRef.current;
const shouldShowDropFrame =
canInsertAsset &&
@@ -130,13 +115,16 @@ export const useTimelineAssetDrop = () => {
isEventTargetInsideElement(event, scrollable);
setAssetDropFrame(shouldShowDropFrame ? getDropFrame(event) : null);
},
- [canInsertAsset, compositionComponentInfo?.canAddSequence, getDropFrame],
+ [
+ canInsertAsset,
+ canReceiveElementDrop,
+ 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;
@@ -177,17 +165,21 @@ export const useTimelineAssetDrop = () => {
event.preventDefault();
event.stopPropagation();
+ const canDropElementIntoNewComposition =
+ compositionComponentInfo?.canAddSequence === false &&
+ canReceiveElementDrop &&
+ getElementDragData(dataTransfer) !== null;
if (
- !canInsertAsset ||
+ (!canInsertAsset && !canDropElementIntoNewComposition) ||
currentCompositionId === null ||
compositionFile === null ||
videoConfig === null
) {
- if (
- compositionComponentInfo?.canAddSequence === false &&
- !unsupportedDropWasNotified
- ) {
- showCannotAddSequenceDropNotification();
+ if (compositionComponentInfo?.canAddSequence === false) {
+ showNotification(
+ 'Cannot insert this item: This composition component cannot accept a new <Sequence>.',
+ 3000,
+ );
}
return;
@@ -224,6 +216,7 @@ export const useTimelineAssetDrop = () => {
},
[
canInsertAsset,
+ canReceiveElementDrop,
chooseSvgImportMode,
compositionComponentInfo?.canAddSequence,
compositionFile,
@@ -235,7 +228,6 @@ export const useTimelineAssetDrop = () => {
);
const clearAssetDropFrame = useCallback(() => {
- unsupportedDropNotifiedRef.current = false;
setAssetDropFrame(null);
}, []);