Skip to content

🎥 Make videos programmatically with React

Merge remote-tracking branch 'origin/main' into jonnyburger/create-pr-8141

Jonny Burger8a865fd671ca
diff --cc packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
index be24124d88,fe358869e7..e11fea5cda
--- a/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
@@@ -1,10 -1,10 +1,11 @@@
import {
EFFECT_DRAG_MIME_TYPE,
+ getRequiredPackageForEffectImportPath,
parseEffectDragData,
+ type ReorderSequencePosition,
} from '@remotion/studio-shared';
import React, {useCallback, useContext, useMemo, useState} from 'react';
-import type {TSequence} from 'remotion';
+import type {SequencePropsSubscriptionKey, TSequence} from 'remotion';
import {Internals} from 'remotion';
import {NoReactInternals} from 'remotion/no-react';
import {StudioServerConnectionCtx} from '../../helpers/client-id';
@@@ -249,18 -141,7 +252,19 @@@ export const TimelineSequenceItem: Reac
}, [originalLocation]);
const canDeleteFromSource = Boolean(nodePath && validatedLocation?.source);
+ const nodePathKey = useMemo(
+ () =>
+ nodePath ? Internals.makeSequencePropsSubscriptionKey(nodePath) : null,
+ [nodePath],
+ );
+ const parentId = sequence.parent ?? null;
+ const canReorderSequence =
+ SELECTION_ENABLED &&
+ previewConnected &&
+ Boolean(nodePath && nodePathKey && validatedLocation?.source) &&
+ nodePathInfo?.numberOfSequencesWithThisNodePath === 1;
+ const canHandleSequenceDrag = SELECTION_ENABLED && previewConnected;
+ const confirm = useConfirmationDialog();
const deleteDisabled = useMemo(
() => !previewConnected || !sequence.controls || !canDeleteFromSource,
@@@ -310,214 -196,8 +319,214 @@@
} catch (err) {
showNotification((err as Error).message, 4000);
}
- }, [nodePath, validatedLocation?.source, nodePathInfo]);
+ }, [confirm, nodePath, validatedLocation?.source, nodePathInfo]);
+ const getSequenceDropTarget = useCallback(
+ (e: React.DragEvent<HTMLDivElement>): SequenceDropTarget | null => {
+ const dragData = getSequenceReorderDragData(e.dataTransfer);
+ if (!dragData) {
+ return null;
+ }
+
+ if (!nodePath || !nodePathKey || !validatedLocation?.source) {
+ return {
+ type: 'invalid',
+ reason: 'This sequence cannot be reordered from source.',
+ };
+ }
+
+ if (dragData.nodePathKey === nodePathKey) {
+ return {
+ type: 'invalid',
+ reason: 'Drop onto another sequence to reorder.',
+ };
+ }
+
+ if (nodePathInfo?.numberOfSequencesWithThisNodePath !== 1) {
+ return {
+ type: 'invalid',
+ reason: 'Programmatically duplicated sequences cannot be reordered.',
+ };
+ }
+
+ if (
+ dragData.parentId !== parentId ||
+ dragData.fileName !== validatedLocation.source
+ ) {
+ return {
+ type: 'invalid',
+ reason: 'Sequences can only be reordered with direct JSX siblings.',
+ };
+ }
+
+ const rect = e.currentTarget.getBoundingClientRect();
+ const before = e.clientY < rect.top + rect.height / 2;
+ const insertionIndex = before ? trackIndex : trackIndex + 1;
+ const toIndex = getDestinationIndex({
+ fromIndex: dragData.trackIndex,
+ insertionIndex,
+ });
+
+ if (toIndex === dragData.trackIndex) {
+ return {
+ type: 'invalid',
+ reason: 'This sequence is already in that position.',
+ };
+ }
+
+ return {
+ type: 'valid',
+ dragData,
+ position: before ? ('before' as const) : ('after' as const),
+ };
+ },
+ [
+ nodePath,
+ nodePathInfo?.numberOfSequencesWithThisNodePath,
+ nodePathKey,
+ parentId,
+ trackIndex,
+ validatedLocation?.source,
+ ],
+ );
+
+ const onSequenceDragStart = useCallback(
+ (e: React.DragEvent<HTMLDivElement>) => {
+ if (
+ !canReorderSequence ||
+ !nodePath ||
+ !nodePathKey ||
+ !validatedLocation?.source
+ ) {
+ e.preventDefault();
+ return;
+ }
+
+ const dragData = {
+ nodePath,
+ nodePathKey,
+ trackIndex,
+ parentId,
+ fileName: validatedLocation.source,
+ };
+ currentSequenceDrag = dragData;
+ e.dataTransfer.effectAllowed = 'move';
+ e.dataTransfer.setData(
+ SEQUENCE_REORDER_MIME_TYPE,
+ JSON.stringify(dragData),
+ );
+ e.stopPropagation();
+ },
+ [
+ canReorderSequence,
+ nodePath,
+ nodePathKey,
+ parentId,
+ trackIndex,
+ validatedLocation?.source,
+ ],
+ );
+
+ const onSequenceDragEnd = useCallback(() => {
+ currentSequenceDrag = null;
+ setSequenceDropIndicator(null);
+ setSequenceDropRejection(null);
+ }, []);
+
+ const onSequenceDragOver = useCallback(
+ (e: React.DragEvent<HTMLDivElement>) => {
+ if (!hasSequenceReorderDragType(e.dataTransfer)) {
+ return;
+ }
+
+ const dropTarget = getSequenceDropTarget(e);
+ if (!dropTarget) {
+ setSequenceDropIndicator(null);
+ setSequenceDropRejection(null);
+ return;
+ }
+
+ if (dropTarget.type === 'invalid') {
+ setSequenceDropIndicator(null);
+ setSequenceDropRejection(dropTarget.reason);
+ e.dataTransfer.dropEffect = 'none';
+ return;
+ }
+
+ e.preventDefault();
+ e.stopPropagation();
+ e.dataTransfer.dropEffect = 'move';
+ setSequenceDropIndicator(dropTarget.position);
+ setSequenceDropRejection(null);
+ },
+ [getSequenceDropTarget],
+ );
+
+ const onSequenceDragLeave = useCallback(
+ (e: React.DragEvent<HTMLDivElement>) => {
+ if (e.currentTarget.contains(e.relatedTarget as Node | null)) {
+ return;
+ }
+
+ setSequenceDropIndicator(null);
+ setSequenceDropRejection(null);
+ },
+ [],
+ );
+
+ const onSequenceDrop = useCallback(
+ async (e: React.DragEvent<HTMLDivElement>) => {
+ if (
+ !canReorderSequence ||
+ previewServerState.type !== 'connected' ||
+ !nodePath ||
+ !validatedLocation?.source
+ ) {
+ return;
+ }
+
+ const dropTarget = getSequenceDropTarget(e);
+ if (!dropTarget || dropTarget.type === 'invalid') {
+ setSequenceDropIndicator(null);
+ setSequenceDropRejection(
+ dropTarget?.type === 'invalid' ? dropTarget.reason : null,
+ );
+ return;
+ }
+
+ e.preventDefault();
+ e.stopPropagation();
+ setSequenceDropIndicator(null);
+ setSequenceDropRejection(null);
+ currentSequenceDrag = null;
+
+ try {
+ const result = await callApi('/api/reorder-sequence', {
+ fileName: validatedLocation.source,
+ sourceNodePath: dropTarget.dragData.nodePath,
+ targetNodePath: nodePath,
+ position: dropTarget.position,
+ clientId: previewServerState.clientId,
+ });
+
+ if (result.success) {
+ showNotification('Reordered sequence', 2000);
+ } else {
+ showNotification(result.reason, 4000);
+ }
+ } catch (err) {
+ showNotification((err as Error).message, 4000);
+ }
+ },
+ [
+ canReorderSequence,
+ getSequenceDropTarget,
+ nodePath,
+ previewServerState,
+ validatedLocation?.source,
+ ],
+ );
+
const mediaSrc =
sequence.type === 'audio' ||
sequence.type === 'video' ||