Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Add left timeline edge dragging (#8609)

Jonny Burgerc5c3b06ab1de
diff --git a/packages/example/src/TrimBeforeSupportTest.tsx b/packages/example/src/TrimBeforeSupportTest.tsx
index 8f41fe651d..3e80fc258c 100644
--- a/packages/example/src/TrimBeforeSupportTest.tsx
+++ b/packages/example/src/TrimBeforeSupportTest.tsx
@@ -1,3 +1,4 @@
+import {Audio, Video} from '@remotion/media';
import {RemotionRiveCanvas} from '@remotion/rive';
import {
Arrow,
@@ -16,7 +17,6 @@ import React from 'react';
import {
AbsoluteFill,
AnimatedImage,
- Audio,
CanvasImage,
Html5Audio,
Html5Video,
@@ -26,12 +26,10 @@ import {
OffthreadVideo,
Sequence,
Solid,
- Video,
staticFile,
useCurrentFrame,
} from 'remotion';
-const TRIM_BEFORE = 20;
const TILE_WIDTH = 220;
const TILE_HEIGHT = 140;
const GAP = 18;
@@ -117,13 +115,14 @@ const TrimmedSequenceBox: React.FC<{
return (
<Sequence
name={name}
- trimBefore={TRIM_BEFORE}
- durationInFrames={120}
+ trimBefore={20}
+ durationInFrames={109}
style={{
...baseTileStyle,
backgroundColor,
transform: getTileTransform(index),
}}
+ from={3}
>
<Counter />
</Sequence>
@@ -153,13 +152,14 @@ const TrimBeforeSupportTest: React.FC = () => {
element: (
<Interactive.Div
name="<Interactive.Div>"
- trimBefore={TRIM_BEFORE}
- durationInFrames={120}
+ trimBefore={20}
+ durationInFrames={103}
style={{
...baseTileStyle,
backgroundColor: '#0b7a75',
transform: getTileTransform(1),
}}
+ from={17}
>
<Counter />
</Interactive.Div>
@@ -173,9 +173,10 @@ const TrimBeforeSupportTest: React.FC = () => {
color="#e11d48"
width={TILE_WIDTH}
height={TILE_HEIGHT}
- trimBefore={TRIM_BEFORE}
- durationInFrames={120}
+ trimBefore={20}
+ durationInFrames={103}
style={{transform: getTileTransform(2)}}
+ from={17}
/>
),
},
@@ -186,12 +187,13 @@ const TrimBeforeSupportTest: React.FC = () => {
name="<HtmlInCanvas>"
width={TILE_WIDTH}
height={TILE_HEIGHT}
- trimBefore={TRIM_BEFORE}
- durationInFrames={120}
+ trimBefore={20}
+ durationInFrames={101}
style={{
...baseTileStyle,
transform: getTileTransform(3),
}}
+ from={19}
>
<div
style={{
@@ -217,13 +219,14 @@ const TrimBeforeSupportTest: React.FC = () => {
<Img
name="<Img>"
src={img}
- trimBefore={TRIM_BEFORE}
- durationInFrames={120}
+ trimBefore={20}
+ durationInFrames={105}
style={{
...baseTileStyle,
objectFit: 'cover',
transform: getTileTransform(4),
}}
+ from={15}
/>
),
},
@@ -234,12 +237,13 @@ const TrimBeforeSupportTest: React.FC = () => {
name="<AnimatedImage>"
src={gif}
fit="cover"
- trimBefore={TRIM_BEFORE}
- durationInFrames={120}
+ trimBefore={20}
+ durationInFrames={113}
style={{
...baseTileStyle,
transform: getTileTransform(5),
}}
+ from={7}
/>
),
},
@@ -252,12 +256,13 @@ const TrimBeforeSupportTest: React.FC = () => {
width={TILE_WIDTH}
height={TILE_HEIGHT}
fit="cover"
- trimBefore={TRIM_BEFORE}
- durationInFrames={120}
+ trimBefore={20}
+ durationInFrames={111}
style={{
...baseTileStyle,
transform: getTileTransform(6),
}}
+ from={9}
/>
),
},
@@ -267,7 +272,7 @@ const TrimBeforeSupportTest: React.FC = () => {
<Video
name="<Video>"
src={video}
- trimBefore={TRIM_BEFORE}
+ trimBefore={20}
muted
style={{
...baseTileStyle,
@@ -282,7 +287,7 @@ const TrimBeforeSupportTest: React.FC = () => {
<Html5Video
name="<Html5Video>"
src={video}
- trimBefore={TRIM_BEFORE}
+ trimBefore={20}
muted
style={{
...baseTileStyle,
@@ -298,7 +303,7 @@ const TrimBeforeSupportTest: React.FC = () => {
<OffthreadVideo
name="<OffthreadVideo>"
src={video}
- trimBefore={TRIM_BEFORE}
+ trimBefore={20}
muted
style={{
...baseTileStyle,
@@ -311,12 +316,7 @@ const TrimBeforeSupportTest: React.FC = () => {
{
name: 'Audio',
element: (
- <Audio
- name="<Audio>"
- src={audio}
- trimBefore={TRIM_BEFORE}
- volume={0.15}
- />
+ <Audio name="<Audio>" src={audio} trimBefore={20} volume={0.15} />
),
},
{
@@ -325,7 +325,7 @@ const TrimBeforeSupportTest: React.FC = () => {
<Html5Audio
name="<Html5Audio>"
src={audio}
- trimBefore={TRIM_BEFORE}
+ trimBefore={20}
volume={0.15}
/>
),
@@ -336,7 +336,7 @@ const TrimBeforeSupportTest: React.FC = () => {
<RemotionRiveCanvas
name="<RemotionRiveCanvas>"
src={rive}
- trimBefore={TRIM_BEFORE}
+ trimBefore={20}
durationInFrames={120}
style={{
...baseTileStyle,
@@ -355,7 +355,7 @@ const TrimBeforeSupportTest: React.FC = () => {
headLength={48}
shaftWidth={30}
fill="#38bdf8"
- trimBefore={TRIM_BEFORE}
+ trimBefore={20}
durationInFrames={120}
style={shapeStyle(13)}
/>
@@ -373,7 +373,7 @@ const TrimBeforeSupportTest: React.FC = () => {
pointerPosition={0.5}
pointerDirection="down"
fill="#fde047"
- trimBefore={TRIM_BEFORE}
+ trimBefore={20}
durationInFrames={120}
style={shapeStyle(14)}
/>
@@ -386,7 +386,7 @@ const TrimBeforeSupportTest: React.FC = () => {
name="<Circle>"
radius={48}
fill="#4ade80"
- trimBefore={TRIM_BEFORE}
+ trimBefore={20}
durationInFrames={120}
style={shapeStyle(15)}
/>
@@ -400,9 +400,10 @@ const TrimBeforeSupportTest: React.FC = () => {
rx={62}
ry={36}
fill="#f97316"
- trimBefore={TRIM_BEFORE}
+ trimBefore={20}
durationInFrames={120}
style={shapeStyle(16)}
+ from={5}
/>
),
},
@@ -414,9 +415,10 @@ const TrimBeforeSupportTest: React.FC = () => {
height={96}
aspectRatio={1.1}
fill="#fb7185"
- trimBefore={TRIM_BEFORE}
- durationInFrames={120}
+ trimBefore={20}
+ durationInFrames={121}
style={shapeStyle(17)}
+ from={4}
/>
),
},
@@ -431,9 +433,10 @@ const TrimBeforeSupportTest: React.FC = () => {
counterClockwise={false}
rotation={0}
fill="#a78bfa"
- trimBefore={TRIM_BEFORE}
- durationInFrames={120}
+ trimBefore={20}
+ durationInFrames={99}
style={shapeStyle(18)}
+ from={8}
/>
),
},
@@ -445,9 +448,10 @@ const TrimBeforeSupportTest: React.FC = () => {
points={6}
radius={58}
fill="#22d3ee"
- trimBefore={TRIM_BEFORE}
- durationInFrames={120}
+ trimBefore={20}
+ durationInFrames={121}
style={shapeStyle(19)}
+ from={-1}
/>
),
},
@@ -459,9 +463,10 @@ const TrimBeforeSupportTest: React.FC = () => {
width={104}
height={76}
fill="#60a5fa"
- trimBefore={TRIM_BEFORE}
- durationInFrames={120}
+ trimBefore={20}
+ durationInFrames={105}
style={shapeStyle(20)}
+ from={15}
/>
),
},
@@ -473,9 +478,10 @@ const TrimBeforeSupportTest: React.FC = () => {
width={84}
height={104}
fill="#facc15"
- trimBefore={TRIM_BEFORE}
- durationInFrames={120}
+ trimBefore={20}
+ durationInFrames={106}
style={shapeStyle(21)}
+ from={14}
/>
),
},
@@ -488,9 +494,10 @@ const TrimBeforeSupportTest: React.FC = () => {
innerRadius={28}
outerRadius={62}
fill="#f472b6"
- trimBefore={TRIM_BEFORE}
- durationInFrames={120}
+ trimBefore={20}
+ durationInFrames={110}
style={shapeStyle(22)}
+ from={15}
/>
),
},
@@ -502,9 +509,10 @@ const TrimBeforeSupportTest: React.FC = () => {
length={100}
direction="up"
fill="#34d399"
- trimBefore={TRIM_BEFORE}
- durationInFrames={120}
+ trimBefore={20}
+ durationInFrames={100}
style={shapeStyle(23)}
+ from={20}
/>
),
},
@@ -514,12 +522,13 @@ const TrimBeforeSupportTest: React.FC = () => {
<Video
name="<Video> remote"
src="https://remotion.media/video.mp4"
- trimBefore={TRIM_BEFORE}
+ trimBefore={20}
muted
style={{
...baseTileStyle,
transform: getTileTransform(24),
}}
+ from={2}
/>
),
},
diff --git a/packages/studio/src/components/Timeline/Timeline.tsx b/packages/studio/src/components/Timeline/Timeline.tsx
index b8fce93d90..ea0006227d 100644
--- a/packages/studio/src/components/Timeline/Timeline.tsx
+++ b/packages/studio/src/components/Timeline/Timeline.tsx
@@ -18,6 +18,7 @@ import {SplitterHandle} from '../Splitter/SplitterHandle';
import {MAX_TIMELINE_TRACKS} from './MaxTimelineTracks';
import {SequencePropsObserver} from './SequencePropsObserver';
import {shouldShowTrackInTimeline} from './should-show-track-in-timeline';
+import {shouldSubscribeToSequenceProps} from './should-subscribe-to-sequence-props';
import {SubscribeToNodePaths} from './SubscribeToNodePaths';
import {timelineVerticalScroll} from './timeline-refs';
import {TimelineDragHandler} from './TimelineDragHandler';
@@ -252,7 +253,7 @@ const TimelineInner: React.FC = () => {
return (
<TimelineContextMenuArea>
{sequences.map((sequence) => {
- if (!sequence.controls || !previewConnected || !sequence.getStack()) {
+ if (!shouldSubscribeToSequenceProps(sequence, previewConnected)) {
return null;
}
diff --git a/packages/studio/src/components/Timeline/TimelineSequence.tsx b/packages/studio/src/components/Timeline/TimelineSequence.tsx
index a752c1d59e..f339a9f007 100644
--- a/packages/studio/src/components/Timeline/TimelineSequence.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequence.tsx
@@ -35,6 +35,7 @@ import {
} from './TimelineSelection';
import {TimelineSequenceFrame} from './TimelineSequenceFrame';
import {
+ TimelineSequenceLeftEdgeDragHandle,
TimelineSequenceRightEdgeDragHandle,
useTimelineSequenceFromDrag,
} from './TimelineSequenceRightEdgeDragHandle';
@@ -266,6 +267,9 @@ const TimelineSequenceInner: React.FC<{
const fromCanUpdate = Boolean(
propStatusesForOverride?.from?.status === 'static',
);
+ const trimBeforeCanUpdate = Boolean(
+ propStatusesForOverride?.trimBefore?.status === 'static',
+ );
const {previewServerState} = useContext(StudioServerConnectionCtx);
const previewConnected = previewServerState.type === 'connected';
const {setPropStatuses} = useContext(Internals.VisualModeSettersContext);
@@ -507,6 +511,18 @@ const TimelineSequenceInner: React.FC<{
nodePath !== null &&
validatedLocation !== null &&
durationCanUpdate;
+ const showLeftEdgeDragHandle =
+ (s.type === 'sequence' ||
+ s.type === 'image' ||
+ s.type === 'audio' ||
+ s.type === 'video') &&
+ !s.isInsideSeries &&
+ nodePath !== null &&
+ validatedLocation !== null &&
+ Boolean(s.controls) &&
+ fromCanUpdate &&
+ durationCanUpdate &&
+ trimBeforeCanUpdate;
if (maxMediaDuration === null && !s.loopDisplay) {
return null;
@@ -560,6 +576,13 @@ const TimelineSequenceInner: React.FC<{
{s.loopDisplay === undefined ? null : (
<LoopedTimelineIndicator loops={s.loopDisplay.numberOfTimes} />
)}
+ {showLeftEdgeDragHandle && nodePathInfo && validatedLocation ? (
+ <TimelineSequenceLeftEdgeDragHandle
+ nodePathInfo={nodePathInfo}
+ windowWidth={windowWidth}
+ timelineDurationInFrames={video.durationInFrames ?? 1}
+ />
+ ) : null}
{showRightEdgeDragHandle && nodePathInfo && validatedLocation ? (
<TimelineSequenceRightEdgeDragHandle
nodePathInfo={nodePathInfo}
diff --git a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
index c61f4d33d9..9ff67c0a99 100644
--- a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
@@ -14,6 +14,7 @@ import type {
PropStatuses,
SequencePropsSubscriptionKey,
TSequence,
+ InteractivitySchema,
} from 'remotion';
import {Internals} from 'remotion';
import {NoReactInternals} from 'remotion/no-react';
@@ -53,6 +54,15 @@ export type TimelineSequenceDurationDragTarget = {
readonly nodePath: SequencePropsSubscriptionKey;
};
+export type TimelineSequenceLeftEdgeDragTarget = {
+ readonly fileName: string;
+ readonly initialDuration: number;
+ readonly initialFrom: number;
+ readonly initialTrimBefore: number;
+ readonly nodePath: SequencePropsSubscriptionKey;
+ readonly schema: InteractivitySchema;
+};
+
export type TimelineSequenceFromDragTarget = {
readonly fileName: string;
readonly initialFrom: number;
@@ -85,6 +95,19 @@ const canUpdateFrom = ({
return status === 'static';
};
+const canUpdateTrimBefore = ({
+ propStatuses,
+ nodePath,
+}: {
+ readonly propStatuses: PropStatuses;
+ readonly nodePath: SequencePropsSubscriptionKey;
+}) => {
+ const status = Internals.getPropStatusesCtx(propStatuses, nodePath)
+ ?.trimBefore?.status;
+
+ return status === 'static';
+};
+
const isDurationDraggableSequence = (sequence: TSequence) => {
return (
(sequence.type === 'sequence' || sequence.type === 'image') &&
@@ -94,6 +117,17 @@ const isDurationDraggableSequence = (sequence: TSequence) => {
);
};
+const isLeftEdgeDraggableSequence = (sequence: TSequence) => {
+ return (
+ !sequence.isInsideSeries &&
+ Boolean(sequence.controls) &&
+ (sequence.type === 'sequence' ||
+ sequence.type === 'image' ||
+ sequence.type === 'audio' ||
+ sequence.type === 'video')
+ );
+};
+
const isFromDraggableSequence = (sequence: TSequence) => {
return (
!sequence.loopDisplay &&
@@ -110,6 +144,98 @@ export const getTimelineSequenceDurationDragValue = ({
readonly deltaFrames: number;
}) => Math.max(1, initialDuration + deltaFrames);
+export const getTimelineSequenceLeftEdgeDragDelta = ({
+ initialDuration,
+ initialTrimBefore,
+ deltaFrames,
+}: {
+ readonly initialDuration: number;
+ readonly initialTrimBefore: number;
+ readonly deltaFrames: number;
+}) => {
+ const minDeltaFrames = 0 - initialTrimBefore;
+ const maxDeltaFrames = initialDuration - 1;
+
+ return Math.max(minDeltaFrames, Math.min(deltaFrames, maxDeltaFrames));
+};
+
+export const getTimelineSequenceLeftEdgeDragValues = ({
+ initialDuration,
+ initialFrom,
+ initialTrimBefore,
+ deltaFrames,
+}: {
+ readonly initialDuration: number;
+ readonly initialFrom: number;
+ readonly initialTrimBefore: number;
+ readonly deltaFrames: number;
+}) => {
+ const clampedDeltaFrames = getTimelineSequenceLeftEdgeDragDelta({
+ initialDuration,
+ initialTrimBefore,
+ deltaFrames,
+ });
+
+ return {
+ durationInFrames: initialDuration - clampedDeltaFrames,
+ from: initialFrom + clampedDeltaFrames,
+ trimBefore: initialTrimBefore + clampedDeltaFrames,
+ };
+};
+
+export const getTimelineSequenceLeftEdgeDragChanges = ({
+ targets,
+ deltaFrames,
+}: {
+ readonly targets: readonly TimelineSequenceLeftEdgeDragTarget[];
+ readonly deltaFrames: number;
+}): SaveSequencePropChange[] => {
+ return targets.flatMap((target) => {
+ const nextValues = getTimelineSequenceLeftEdgeDragValues({
+ initialDuration: target.initialDuration,
+ initialFrom: target.initialFrom,
+ initialTrimBefore: target.initialTrimBefore,
+ deltaFrames,
+ });
+ const changes: SaveSequencePropChange[] = [];
+
+ if (nextValues.from !== target.initialFrom) {
+ changes.push({
+ fileName: target.fileName,
+ nodePath: target.nodePath,
+ fieldKey: 'from',
+ value: nextValues.from,
+ defaultValue: '0',
+ schema: target.schema,
+ });
+ }
+
+ if (nextValues.durationInFrames !== target.initialDuration) {
+ changes.push({
+ fileName: target.fileName,
+ nodePath: target.nodePath,
+ fieldKey: 'durationInFrames',
+ value: nextValues.durationInFrames,
+ defaultValue: null,
+ schema: target.schema,
+ });
+ }
+
+ if (nextValues.trimBefore !== target.initialTrimBefore) {
+ changes.push({
+ fileName: target.fileName,
+ nodePath: target.nodePath,
+ fieldKey: 'trimBefore',
+ value: nextValues.trimBefore,
+ defaultValue: '0',
+ schema: target.schema,
+ });
+ }
+
+ return changes;
+ });
+};
+
export const getTimelineSequenceDurationDragChanges = ({
targets,
deltaFrames,
@@ -273,6 +399,94 @@ export const getTimelineSequenceDurationDragTargets = ({
return [...targets.values()];
};
+export const getTimelineSequenceLeftEdgeDragTargets = ({
+ draggedNodePathInfo,
+ selectedItems,
+ sequences,
+ overrideIdsToNodePaths,
+ propStatuses,
+}: {
+ readonly draggedNodePathInfo: SequenceNodePathInfo;
+ readonly selectedItems: readonly TimelineSelection[];
+ readonly sequences: TSequence[];
+ readonly overrideIdsToNodePaths: OverrideIdToNodePaths;
+ readonly propStatuses: PropStatuses;
+}): TimelineSequenceLeftEdgeDragTarget[] | null => {
+ const draggedSelectionKey =
+ getTimelineSequenceSelectionKey(draggedNodePathInfo);
+ const selectedSequenceItems = selectedItems.filter(
+ (item): item is TimelineSelection & {type: 'sequence'} =>
+ item.type === 'sequence',
+ );
+ const draggedItemIsSelected = selectedSequenceItems.some(
+ (item) =>
+ getTimelineSequenceSelectionKey(item.nodePathInfo) ===
+ draggedSelectionKey,
+ );
+
+ if (
+ draggedItemIsSelected &&
+ selectedSequenceItems.length !== selectedItems.length
+ ) {
+ return null;
+ }
+
+ const targetNodePathInfos =
+ draggedItemIsSelected && selectedSequenceItems.length > 1
+ ? selectedSequenceItems.map((item) => item.nodePathInfo)
+ : [draggedNodePathInfo];
+ const tracks = calculateTimeline({sequences, overrideIdsToNodePaths});
+ const targets = new Map<string, TimelineSequenceLeftEdgeDragTarget>();
+
+ for (const nodePathInfo of targetNodePathInfos) {
+ const track = findSequenceTrack({tracks, nodePathInfo});
+ const originalSequence = track
+ ? sequences.find((sequence) => sequence.id === track.sequence.id)
+ : null;
+ if (
+ !track ||
+ !track.nodePathInfo ||
+ !originalSequence ||
+ !isLeftEdgeDraggableSequence(originalSequence)
+ ) {
+ return null;
+ }
+
+ const nodePath = track.nodePathInfo.sequenceSubscriptionKey;
+ const trimsMedia =
+ originalSequence.type === 'audio' || originalSequence.type === 'video';
+ if (
+ !canUpdateFrom({propStatuses, nodePath}) ||
+ !canUpdateDurationInFrames({propStatuses, nodePath}) ||
+ !canUpdateTrimBefore({propStatuses, nodePath})
+ ) {
+ return null;
+ }
+
+ const {controls} = originalSequence;
+ if (!controls) {
+ return null;
+ }
+
+ const key = stringifySequenceSubscriptionKey(nodePath);
+ if (!targets.has(key)) {
+ targets.set(key, {
+ fileName: nodePath.absolutePath,
+ initialDuration: originalSequence.duration,
+ initialFrom: originalSequence.from,
+ initialTrimBefore: trimsMedia
+ ? (originalSequence.trimBefore ??
+ Math.max(0, originalSequence.startMediaFrom))
+ : (originalSequence.trimBefore ?? 0),
+ nodePath,
+ schema: controls.schema,
+ });
+ }
+ }
+
+ return [...targets.values()];
+};
+
export const getTimelineSequenceFromDragTargets = ({
draggedNodePathInfo,
selectedItems,
@@ -344,6 +558,18 @@ export const getTimelineSequenceFromDragTargets = ({
return [...targets.values()];
};
+const clearLeftEdgeDragOverrides = ({
+ clearDragOverrides,
+ targets,
+}: {
+ readonly clearDragOverrides: (nodePath: SequencePropsSubscriptionKey) => void;
+ readonly targets: readonly TimelineSequenceLeftEdgeDragTarget[];
+}) => {
+ for (const target of targets) {
+ clearDragOverrides(target.nodePath);
+ }
+};
+
const clearDurationDragOverrides = ({
clearDragOverrides,
targets,
@@ -368,6 +594,259 @@ const clearFromDragOverrides = ({
}
};
+export const TimelineSequenceLeftEdgeDragHandle: React.FC<{
+ readonly nodePathInfo: SequenceNodePathInfo;
+ readonly windowWidth: number;
+ readonly timelineDurationInFrames: number;
+}> = ({nodePathInfo, windowWidth, timelineDurationInFrames}) => {
+ const {setPropStatuses, setDragOverrides, clearDragOverrides} = useContext(
+ Internals.VisualModeSettersContext,
+ );
+ const propStatusesRef = useContext(
+ Internals.VisualModePropStatusesRefContext,
+ );
+ const sequencesRef = useContext(Internals.SequenceManagerRefContext);
+ const {overrideIdToNodePathMappings} = useContext(
+ Internals.OverrideIdsToNodePathsGettersContext,
+ );
+ const {previewServerState} = useContext(StudioServerConnectionCtx);
+ const currentSelection = useCurrentTimelineSelectionStateAsRef();
+
+ const [dragging, setDragging] = useState(false);
+ const dragStateRef = useRef<{
+ initialClientX: number;
+ latestDeltaFrames: number;
+ pxPerFrame: number;
+ pointerId: number;
+ targets: readonly TimelineSequenceLeftEdgeDragTarget[];
+ } | null>(null);
+
+ const latestRef = useRef({
+ nodePathInfo,
+ setPropStatuses,
+ setDragOverrides,
+ clearDragOverrides,
+ previewServerState,
+ overrideIdToNodePathMappings,
+ });
+ latestRef.current = {
+ nodePathInfo,
+ setPropStatuses,
+ setDragOverrides,
+ clearDragOverrides,
+ previewServerState,
+ overrideIdToNodePathMappings,
+ };
+
+ const finishDrag = useCallback((commit: boolean) => {
+ const dragState = dragStateRef.current;
+ dragStateRef.current = null;
+ document.body.style.userSelect = '';
+ document.body.style.webkitUserSelect = '';
+ stopForcingSpecificCursor();
+ setDragging(false);
+
+ if (!dragState) {
+ return;
+ }
+
+ const {
+ setPropStatuses: latestSetPropStatuses,
+ clearDragOverrides: latestClear,
+ previewServerState: latestServerState,
+ } = latestRef.current;
+
+ const changes = getTimelineSequenceLeftEdgeDragChanges({
+ targets: dragState.targets,
+ deltaFrames: dragState.latestDeltaFrames,
+ });
+
+ if (
+ !commit ||
+ latestServerState.type !== 'connected' ||
+ changes.length === 0
+ ) {
+ clearLeftEdgeDragOverrides({
+ clearDragOverrides: latestClear,
+ targets: dragState.targets,
+ });
+ return;
+ }
+
+ const savePromise = saveSequenceProps({
+ changes,
+ setPropStatuses: latestSetPropStatuses,
+ clientId: latestServerState.clientId,
+ undoLabel:
+ dragState.targets.length > 1
+ ? 'Resize selected sequences'
+ : 'Resize sequence',
+ redoLabel:
+ dragState.targets.length > 1
+ ? 'Resize selected sequences back'
+ : 'Resize sequence back',
+ });
+
+ savePromise
+ .catch((err) => {
+ Internals.Log.error(
+ {logLevel: 'error', tag: null},
+ 'Could not save left edge drag',
+ err,
+ );
+ })
+ .finally(() => {
+ clearLeftEdgeDragOverrides({
+ clearDragOverrides: latestClear,
+ targets: dragState.targets,
+ });
+ });
+ }, []);
+
+ const onPointerDown = useCallback(
+ (e: React.PointerEvent<HTMLDivElement>) => {
+ if (e.button !== 0) {
+ return;
+ }
+
+ const pxPerFrame =
+ timelineDurationInFrames > 0
+ ? (windowWidth - TIMELINE_PADDING * 2) / timelineDurationInFrames
+ : 0;
+ if (pxPerFrame <= 0) {
+ return;
+ }
+
+ const {
+ nodePathInfo: latestNodePathInfo,
+ overrideIdToNodePathMappings: latestOverrideIdsToNodePaths,
+ } = latestRef.current;
+ const {selectedItems: latestSelectedItems} = currentSelection.current;
+ const targets = getTimelineSequenceLeftEdgeDragTargets({
+ draggedNodePathInfo: latestNodePathInfo,
+ selectedItems: latestSelectedItems,
+ sequences: sequencesRef.current,
+ overrideIdsToNodePaths: latestOverrideIdsToNodePaths,
+ propStatuses: propStatusesRef.current,
+ });
+ if (targets === null || targets.length === 0) {
+ return;
+ }
+
+ e.stopPropagation();
+ e.preventDefault();
+ dragStateRef.current = {
+ initialClientX: e.clientX,
+ latestDeltaFrames: 0,
+ pxPerFrame,
+ pointerId: e.pointerId,
+ targets,
+ };
+ document.body.style.userSelect = 'none';
+ document.body.style.webkitUserSelect = 'none';
+ forceSpecificCursor('ew-resize');
+ setDragging(true);
+ },
+ [
+ currentSelection,
+ propStatusesRef,
+ sequencesRef,
+ timelineDurationInFrames,
+ windowWidth,
+ ],
+ );
+
+ useEffect(() => {
+ if (!dragging) {
+ return;
+ }
+
+ const onMove = (e: PointerEvent) => {
+ const dragState = dragStateRef.current;
+ if (!dragState || e.pointerId !== dragState.pointerId) {
+ return;
+ }
+
+ const dx = e.clientX - dragState.initialClientX;
+ const deltaFrames = Math.round(dx / dragState.pxPerFrame);
+ dragState.latestDeltaFrames = deltaFrames;
+ for (const target of dragState.targets) {
+ const nextValues = getTimelineSequenceLeftEdgeDragValues({
+ initialDuration: target.initialDuration,
+ initialFrom: target.initialFrom,
+ initialTrimBefore: target.initialTrimBefore,
+ deltaFrames,
+ });
+ latestRef.current.setDragOverrides(
+ target.nodePath,
+ 'from',
+ Internals.makeStaticDragOverride(nextValues.from),
+ );
+ latestRef.current.setDragOverrides(
+ target.nodePath,
+ 'durationInFrames',
+ Internals.makeStaticDragOverride(nextValues.durationInFrames),
+ );
+ latestRef.current.setDragOverrides(
+ target.nodePath,
+ 'trimBefore',
+ Internals.makeStaticDragOverride(nextValues.trimBefore),
+ );
+ }
+ };
+
+ const onUp = (e: PointerEvent) => {
+ const dragState = dragStateRef.current;
+ if (!dragState || e.pointerId !== dragState.pointerId) {
+ return;
+ }
+
+ finishDrag(true);
+ };
+
+ const onCancel = (e: PointerEvent) => {
+ const dragState = dragStateRef.current;
+ if (!dragState || e.pointerId !== dragState.pointerId) {
+ return;
+ }
+
+ finishDrag(false);
+ };
+
+ const onWindowBlur = () => {
+ finishDrag(false);
+ };
+
+ window.addEventListener('pointermove', onMove);
+ window.addEventListener('pointerup', onUp);
+ window.addEventListener('pointercancel', onCancel);
+ window.addEventListener('blur', onWindowBlur);
+
+ return () => {
+ window.removeEventListener('pointermove', onMove);
+ window.removeEventListener('pointerup', onUp);
+ window.removeEventListener('pointercancel', onCancel);
+ window.removeEventListener('blur', onWindowBlur);
+ };
+ }, [dragging, finishDrag]);
+
+ const style: React.CSSProperties = {
+ ...baseStyle,
+ left: 0,
+ background: 'transparent',
+ };
+
+ return (
+ <div
+ role="separator"
+ aria-orientation="vertical"
+ title="Drag to trim start"
+ style={style}
+ onPointerDown={onPointerDown}
+ />
+ );
+};
+
export const useTimelineSequenceFromDrag = ({
nodePathInfo,
windowWidth,
diff --git a/packages/studio/src/components/Timeline/should-subscribe-to-sequence-props.ts b/packages/studio/src/components/Timeline/should-subscribe-to-sequence-props.ts
new file mode 100644
index 0000000000..b89efe92b2
--- /dev/null
+++ b/packages/studio/src/components/Timeline/should-subscribe-to-sequence-props.ts
@@ -0,0 +1,15 @@
+import type {TSequence} from 'remotion';
+
+export const shouldSubscribeToSequenceProps = (
+ sequence: TSequence,
+ previewConnected: boolean,
+): sequence is TSequence & {
+ readonly controls: NonNullable<TSequence['controls']>;
+} => {
+ return (
+ sequence.showInTimeline &&
+ Boolean(sequence.controls) &&
+ previewConnected &&
+ Boolean(sequence.getStack())
+ );
+};
diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.ts
index 771d6d068b..a1e1dd6b14 100644
--- a/packages/studio/src/test/timeline-selection.test.ts
+++ b/packages/studio/src/test/timeline-selection.test.ts
@@ -66,6 +66,7 @@ import {
isDuplicatableSequenceRowSelection,
} from '../components/Timeline/duplicate-selected-timeline-item';
import {getTimelinePropResetTargets} from '../components/Timeline/reset-selected-timeline-props';
+import {shouldSubscribeToSequenceProps} from '../components/Timeline/should-subscribe-to-sequence-props';
import {
getEasingClipboardDataFromSelection,
getEffectPropClipboardDataFromSelection,
@@ -99,6 +100,9 @@ import {
getTimelineSequenceFromDragChanges,
getTimelineSequenceFromDragTargets,
getTimelineSequenceFromDragValue,
+ getTimelineSequenceLeftEdgeDragChanges,
+ getTimelineSequenceLeftEdgeDragTargets,
+ getTimelineSequenceLeftEdgeDragValues,
} from '../components/Timeline/TimelineSequenceRightEdgeDragHandle';
import {
parsedTransformOriginToUv,
@@ -180,6 +184,7 @@ const makeTimelineSequence = ({
refForOutline = null,
duration = 100,
from = 0,
+ startMediaFrom = 0,
type = 'sequence',
showInTimeline = true,
}: {
@@ -191,6 +196,7 @@ const makeTimelineSequence = ({
readonly refForOutline?: RefObject<HTMLElement | null> | null;
readonly duration?: number;
readonly from?: number;
+ readonly startMediaFrom?: number;
readonly type?: TSequence['type'];
readonly showInTimeline?: boolean;
}): TSequence =>
@@ -222,6 +228,7 @@ const makeTimelineSequence = ({
isInsideSeries: false,
effects,
frozenFrame: null,
+ startMediaFrom,
}) as TSequence;
const makeDurationPropStatuses = (
@@ -258,6 +265,33 @@ const makeFromPropStatuses = (
return propStatuses;
};
+const makeLeftEdgePropStatuses = (
+ nodePaths: readonly SequencePropsSubscriptionKey[],
+ includeTrimBefore = false,
+ includeTimelineRange = true,
+): PropStatuses => {
+ const propStatuses: PropStatuses = {};
+ for (const nodePath of nodePaths) {
+ propStatuses[Internals.makeSequencePropsSubscriptionKey(nodePath)] = {
+ canUpdate: true,
+ props: {
+ ...(includeTimelineRange
+ ? {
+ durationInFrames: {status: 'static' as const, codeValue: 100},
+ from: {status: 'static' as const, codeValue: 0},
+ }
+ : {}),
+ ...(includeTrimBefore
+ ? {trimBefore: {status: 'static' as const, codeValue: 0}}
+ : {}),
+ },
+ effects: [],
+ };
+ }
+
+ return propStatuses;
+};
+
test('timeline marquee rectangle intersection detects overlapping targets', () => {
expect(
timelineMarqueeRectsIntersect(
@@ -273,6 +307,21 @@ test('timeline marquee rectangle intersection detects overlapping targets', () =
).toBe(false);
});
+test('Timeline skips prop subscriptions for hidden timeline sequences', () => {
+ const schema = {} satisfies InteractivitySchema;
+ const visibleSequence = {
+ ...makeTimelineSequence({schema}),
+ getStack: () => 'stack',
+ };
+ const hiddenSequence = {
+ ...visibleSequence,
+ showInTimeline: false,
+ };
+
+ expect(shouldSubscribeToSequenceProps(visibleSequence, true)).toBe(true);
+ expect(shouldSubscribeToSequenceProps(hiddenSequence, true)).toBe(false);
+});
+
test('timeline marquee points are clamped to the track bounds', () => {
expect(
getClampedTimelineMarqueePoint({
@@ -1163,6 +1212,177 @@ test('Timeline duration drag ignores selection if dragged sequence is not select
]);
});
+test('Timeline left edge drag adjusts from, duration and trimBefore for selected sequences', () => {
+ const schema = {} satisfies InteractivitySchema;
+ const firstNodePathInfo = makeNodePathInfo(['body', 0], []);
+ const secondNodePathInfo = makeNodePathInfo(['body', 1], []);
+ const targets = getTimelineSequenceLeftEdgeDragTargets({
+ draggedNodePathInfo: firstNodePathInfo,
+ selectedItems: [
+ {type: 'sequence', nodePathInfo: firstNodePathInfo},
+ {type: 'sequence', nodePathInfo: secondNodePathInfo},
+ ],
+ sequences: [
+ makeTimelineSequence({
+ schema,
+ id: 'first',
+ overrideId: 'first',
+ duration: 40,
+ from: 5,
+ }),
+ makeTimelineSequence({
+ schema,
+ id: 'second',
+ overrideId: 'second',
+ duration: 15,
+ from: 10,
+ type: 'image',
+ }),
+ ],
+ overrideIdsToNodePaths: {
+ first: firstNodePathInfo.sequenceSubscriptionKey,
+ second: secondNodePathInfo.sequenceSubscriptionKey,
+ },
+ propStatuses: makeLeftEdgePropStatuses(
+ [
+ firstNodePathInfo.sequenceSubscriptionKey,
+ secondNodePathInfo.sequenceSubscriptionKey,
+ ],
+ true,
+ ),
+ });
+
+ expect(
+ getTimelineSequenceLeftEdgeDragChanges({
+ targets: targets ?? [],
+ deltaFrames: 6,
+ }).map((change) => [change.fieldKey, change.value]),
+ ).toEqual([
+ ['from', 11],
+ ['durationInFrames', 34],
+ ['trimBefore', 6],
+ ['from', 16],
+ ['durationInFrames', 9],
+ ['trimBefore', 6],
+ ]);
+});
+
+test('Timeline left edge drag clamps trimBefore to the visible range', () => {
+ expect(
+ getTimelineSequenceLeftEdgeDragValues({
+ initialDuration: 4,
+ initialFrom: 20,
+ initialTrimBefore: 2,
+ deltaFrames: 10,
+ }),
+ ).toEqual({
+ durationInFrames: 1,
+ from: 23,
+ trimBefore: 5,
+ });
+ expect(
+ getTimelineSequenceLeftEdgeDragValues({
+ initialDuration: 4,
+ initialFrom: 20,
+ initialTrimBefore: 2,
+ deltaFrames: -10,
+ }),
+ ).toEqual({
+ durationInFrames: 6,
+ from: 18,
+ trimBefore: 0,
+ });
+});
+
+test('Timeline left edge drag adjusts and clamps media trimBefore', () => {
+ const schema = {} satisfies InteractivitySchema;
+ const nodePathInfo = makeNodePathInfo(['body', 0], []);
+ const targets = getTimelineSequenceLeftEdgeDragTargets({
+ draggedNodePathInfo: nodePathInfo,
+ selectedItems: [{type: 'sequence', nodePathInfo}],
+ sequences: [
+ makeTimelineSequence({
+ schema,
+ duration: 40,
+ from: 12,
+ startMediaFrom: 8,
+ type: 'video',
+ }),
+ ],
+ overrideIdsToNodePaths: {
+ override: nodePathInfo.sequenceSubscriptionKey,
+ },
+ propStatuses: makeLeftEdgePropStatuses(
+ [nodePathInfo.sequenceSubscriptionKey],
+ true,
+ ),
+ });
+
+ expect(targets?.map((target) => target.initialTrimBefore)).toEqual([8]);
+ expect(
+ getTimelineSequenceLeftEdgeDragChanges({
+ targets: targets ?? [],
+ deltaFrames: -20,
+ }).map((change) => [change.fieldKey, change.value]),
+ ).toEqual([
+ ['from', 4],
+ ['durationInFrames', 48],
+ ['trimBefore', 0],
+ ]);
+});
+
+test('Timeline left edge drag is blocked without timeline range props', () => {
+ const schema = {} satisfies InteractivitySchema;
+ const nodePathInfo = makeNodePathInfo(['body', 0], []);
+ const targets = getTimelineSequenceLeftEdgeDragTargets({
+ draggedNodePathInfo: nodePathInfo,
+ selectedItems: [{type: 'sequence', nodePathInfo}],
+ sequences: [
+ makeTimelineSequence({
+ schema,
+ duration: 40,
+ from: 0,
+ startMediaFrom: 8,
+ type: 'video',
+ }),
+ ],
+ overrideIdsToNodePaths: {
+ override: nodePathInfo.sequenceSubscriptionKey,
+ },
+ propStatuses: makeLeftEdgePropStatuses(
+ [nodePathInfo.sequenceSubscriptionKey],
+ true,
+ false,
+ ),
+ });
+
+ expect(targets).toBe(null);
+});
+
+test('Timeline left edge drag is blocked if trimBefore cannot update', () => {
+ const schema = {} satisfies InteractivitySchema;
+ const nodePathInfo = makeNodePathInfo(['body', 0], []);
+
+ expect(
+ getTimelineSequenceLeftEdgeDragTargets({
+ draggedNodePathInfo: nodePathInfo,
+ selectedItems: [{type: 'sequence', nodePathInfo}],
+ sequences: [
+ makeTimelineSequence({
+ schema,
+ type: 'audio',
+ }),
+ ],
+ overrideIdsToNodePaths: {
+ override: nodePathInfo.sequenceSubscriptionKey,
+ },
+ propStatuses: makeLeftEdgePropStatuses([
+ nodePathInfo.sequenceSubscriptionKey,
+ ]),
+ }),
+ ).toBe(null);
+});
+
test('Timeline from drag applies the same delta to selected sequences', () => {
const schema = {} satisfies InteractivitySchema;
const firstNodePathInfo = makeNodePathInfo(['body', 0], []);