Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Fix timeline start snapping for nested layers

Jonny Burger2ca8e6c7b118
diff --git a/packages/studio/src/components/AudioWaveform.tsx b/packages/studio/src/components/AudioWaveform.tsx
index 72679ffb24..3bed8237f6 100644
--- a/packages/studio/src/components/AudioWaveform.tsx
+++ b/packages/studio/src/components/AudioWaveform.tsx
@@ -32,13 +32,11 @@ const waveformCanvasStyle: React.CSSProperties = {
pointerEvents: 'none',
flexShrink: 0,
position: 'relative',
- zIndex: 1,
};
const volumeCanvasStyle: React.CSSProperties = {
pointerEvents: 'none',
position: 'absolute',
- zIndex: 0,
};
const AudioWaveformInner: React.FC<{
diff --git a/packages/studio/src/components/Timeline/Timeline.tsx b/packages/studio/src/components/Timeline/Timeline.tsx
index b1e57d1bb3..bf1f3b9941 100644
--- a/packages/studio/src/components/Timeline/Timeline.tsx
+++ b/packages/studio/src/components/Timeline/Timeline.tsx
@@ -64,6 +64,7 @@ import {
import {TimelineEdgeHighlightProvider} from './TimelineSequence';
import {TimelineSequenceMediaDurationDragLimitsProvider} from './TimelineSequenceRightEdgeDragHandle';
import {TimelineSlider} from './TimelineSlider';
+import {TimelineSnapIndicatorProvider} from './TimelineSnapIndicator';
import {
TimelineTimeIndicators,
TimelineTimePlaceholders,
@@ -628,7 +629,9 @@ export const Timeline: React.FC = () => {
return (
<TimelineEdgeHighlightProvider>
<TimelineSequenceMediaDurationDragLimitsProvider>
- <MemoizedTimelineInner />
+ <TimelineSnapIndicatorProvider>
+ <MemoizedTimelineInner />
+ </TimelineSnapIndicatorProvider>
</TimelineSequenceMediaDurationDragLimitsProvider>
</TimelineEdgeHighlightProvider>
);
diff --git a/packages/studio/src/components/Timeline/TimelineScrollable.tsx b/packages/studio/src/components/Timeline/TimelineScrollable.tsx
index 2d008a9636..a1c4bd2221 100644
--- a/packages/studio/src/components/Timeline/TimelineScrollable.tsx
+++ b/packages/studio/src/components/Timeline/TimelineScrollable.tsx
@@ -10,6 +10,7 @@ import {
TIMELINE_BACKGROUND,
useTimelineMarqueeSelection,
} from './TimelineSelection';
+import {TimelineSnapIndicator} from './TimelineSnapIndicator';
import {TimelineViewportProvider} from './TimelineViewport';
const outer: React.CSSProperties = {
@@ -54,6 +55,7 @@ export const TimelineScrollable: React.FC<{
<div style={containerStyle}>{children}</div>
</TimelineViewportProvider>
<TimelineAssetDropIndicator />
+ <TimelineSnapIndicator />
{marqueeRect === null ? null : (
<div
style={{
diff --git a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
index 1e451fa78b..3352fb26d7 100644
--- a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
@@ -57,6 +57,7 @@ import {
type TimelineSelection,
type TimelineSelectionInteraction,
} from './TimelineSelection';
+import {TimelineSnapIndicatorContext} from './TimelineSnapIndicator';
import {
TimelineTrimTooltip,
type TimelineTrimTooltipState,
@@ -714,7 +715,7 @@ export const getTimelineSequenceFromDragValue = ({
readonly deltaFrames: number;
}) => initialFrom + deltaFrames;
-export const getTimelineSequenceFromDragDelta = ({
+const getTimelineSequenceFromDragResult = ({
deltaFrames,
timelineDurationInFrames,
pxPerFrame,
@@ -738,13 +739,8 @@ export const getTimelineSequenceFromDragDelta = ({
continue;
}
- const nextFrom = getTimelineSequenceFromDragValue({
- initialFrom: target.initialFrom,
- deltaFrames: deltaFrames * target.parentPlaybackRate,
- });
- const distancePx = Math.abs(
- (nextFrom / target.parentPlaybackRate) * pxPerFrame,
- );
+ const nextTimelineStart = target.initialTimelineStart + deltaFrames;
+ const distancePx = Math.abs(nextTimelineStart * pxPerFrame);
if (
distancePx > timelineSequenceFromDragSnapThresholdPx ||
(closestSnap && closestSnap.distancePx <= distancePx)
@@ -753,7 +749,7 @@ export const getTimelineSequenceFromDragDelta = ({
}
closestSnap = {
- deltaFrames: -target.initialFrom / target.parentPlaybackRate,
+ deltaFrames: -target.initialTimelineStart,
distancePx,
};
}
@@ -767,12 +763,21 @@ export const getTimelineSequenceFromDragDelta = ({
),
);
// Clamp the shared delta after snapping so every selected track retains a frame.
- return Math.max(
+ const clampedDelta = Math.max(
minimumDelta,
Math.min(maximumDelta, closestSnap?.deltaFrames ?? deltaFrames),
);
+ return {
+ deltaFrames: clampedDelta,
+ snapFrame:
+ closestSnap && closestSnap.deltaFrames === clampedDelta ? 0 : null,
+ };
};
+export const getTimelineSequenceFromDragDelta = (
+ params: Parameters<typeof getTimelineSequenceFromDragResult>[0],
+) => getTimelineSequenceFromDragResult(params).deltaFrames;
+
export const getTimelineSequenceFromDragChanges = ({
targets,
deltaFrames,
@@ -1359,7 +1364,7 @@ export const getTimelineSequenceFromDragTargets = ({
originalSequence,
sequences,
),
- canSnapToTimelineStart: originalSequence.parent === null,
+ canSnapToTimelineStart: true,
minimumDeltaFrames:
(1 - originalSequence.duration - originalSequence.from) /
getParentSequencePlaybackRate(originalSequence, sequences),
@@ -2003,6 +2008,7 @@ export const useTimelineSequenceFromDrag = ({
const {previewServerState} = useContext(StudioServerConnectionCtx);
const currentSelection = useCurrentTimelineSelectionStateAsRef();
const {editorSnapping} = useContext(EditorSnappingContext);
+ const updateSnapFrameRef = useContext(TimelineSnapIndicatorContext);
const stopPointerSessionRef = useRef<(() => void) | null>(null);
const dragStateRef = useRef<{
@@ -2041,82 +2047,86 @@ export const useTimelineSequenceFromDrag = ({
onDragEnd,
};
- const finishDrag = useCallback((commit: boolean) => {
- const dragState = dragStateRef.current;
- if (!dragState) {
- return;
- }
-
- dragStateRef.current = null;
- latestRef.current.onDragEnd(dragState.didMove);
- document.body.style.userSelect = '';
- document.body.style.webkitUserSelect = '';
- const {
- setPropStatuses: latestSetPropStatuses,
- clearDragOverrides: latestClear,
- clearEffectDragOverrides: latestClearEffect,
- previewServerState: latestServerState,
- } = latestRef.current;
-
- const changes = getTimelineSequenceFromDragChanges({
- targets: dragState.targets,
- deltaFrames: dragState.latestDeltaFrames,
- });
- const keyframeMoves = getTimelineSequenceFromDragKeyframeMoves({
- targets: dragState.targets,
- deltaFrames: dragState.latestDeltaFrames,
- });
+ const finishDrag = useCallback(
+ (commit: boolean) => {
+ const dragState = dragStateRef.current;
+ if (!dragState) {
+ return;
+ }
- if (
- !commit ||
- latestServerState.type !== 'connected' ||
- (changes.length === 0 &&
- keyframeMoves.sequenceKeyframes.length === 0 &&
- keyframeMoves.effectKeyframes.length === 0)
- ) {
- clearFromDragOverrides({
+ updateSnapFrameRef?.current(null);
+ dragStateRef.current = null;
+ latestRef.current.onDragEnd(dragState.didMove);
+ document.body.style.userSelect = '';
+ document.body.style.webkitUserSelect = '';
+ const {
+ setPropStatuses: latestSetPropStatuses,
clearDragOverrides: latestClear,
clearEffectDragOverrides: latestClearEffect,
+ previewServerState: latestServerState,
+ } = latestRef.current;
+
+ const changes = getTimelineSequenceFromDragChanges({
targets: dragState.targets,
+ deltaFrames: dragState.latestDeltaFrames,
+ });
+ const keyframeMoves = getTimelineSequenceFromDragKeyframeMoves({
+ targets: dragState.targets,
+ deltaFrames: dragState.latestDeltaFrames,
});
- return;
- }
-
- const savePromise = saveSequenceProps({
- addedKeyframes: null,
- changes,
- movedKeyframes: {
- sequenceKeyframes: keyframeMoves.sequenceKeyframes,
- effectKeyframes: keyframeMoves.effectKeyframes,
- },
- setPropStatuses: latestSetPropStatuses,
- clientId: latestServerState.clientId,
- undoLabel:
- dragState.targets.length > 1
- ? 'Move selected sequences'
- : 'Move sequence',
- redoLabel:
- dragState.targets.length > 1
- ? 'Move selected sequences back'
- : 'Move sequence back',
- });
- savePromise
- .catch((err) => {
- Internals.Log.error(
- {logLevel: 'error', tag: null},
- 'Could not save from',
- err,
- );
- })
- .finally(() => {
+ if (
+ !commit ||
+ latestServerState.type !== 'connected' ||
+ (changes.length === 0 &&
+ keyframeMoves.sequenceKeyframes.length === 0 &&
+ keyframeMoves.effectKeyframes.length === 0)
+ ) {
clearFromDragOverrides({
clearDragOverrides: latestClear,
clearEffectDragOverrides: latestClearEffect,
targets: dragState.targets,
});
+ return;
+ }
+
+ const savePromise = saveSequenceProps({
+ addedKeyframes: null,
+ changes,
+ movedKeyframes: {
+ sequenceKeyframes: keyframeMoves.sequenceKeyframes,
+ effectKeyframes: keyframeMoves.effectKeyframes,
+ },
+ setPropStatuses: latestSetPropStatuses,
+ clientId: latestServerState.clientId,
+ undoLabel:
+ dragState.targets.length > 1
+ ? 'Move selected sequences'
+ : 'Move sequence',
+ redoLabel:
+ dragState.targets.length > 1
+ ? 'Move selected sequences back'
+ : 'Move sequence back',
});
- }, []);
+
+ savePromise
+ .catch((err) => {
+ Internals.Log.error(
+ {logLevel: 'error', tag: null},
+ 'Could not save from',
+ err,
+ );
+ })
+ .finally(() => {
+ clearFromDragOverrides({
+ clearDragOverrides: latestClear,
+ clearEffectDragOverrides: latestClearEffect,
+ targets: dragState.targets,
+ });
+ });
+ },
+ [updateSnapFrameRef],
+ );
const onPointerDown = useCallback(
(e: React.PointerEvent<HTMLDivElement>) => {
@@ -2176,7 +2186,7 @@ export const useTimelineSequenceFromDrag = ({
}
const dx = moveEvent.clientX - dragState.initialClientX;
- const deltaFrames = getTimelineSequenceFromDragDelta({
+ const {deltaFrames, snapFrame} = getTimelineSequenceFromDragResult({
timelineDurationInFrames,
deltaFrames: Math.round(dx / dragState.pxPerFrame),
pxPerFrame: dragState.pxPerFrame,
@@ -2192,6 +2202,8 @@ export const useTimelineSequenceFromDrag = ({
dragState.didMove = true;
}
+ updateSnapFrameRef?.current(dragState.didMove ? snapFrame : null);
+
if (deltaFrames === dragState.lastPreviewDeltaFrames) {
return;
}
@@ -2285,6 +2297,7 @@ export const useTimelineSequenceFromDrag = ({
propStatusesRef,
sequencesRef,
timelineDurationInFrames,
+ updateSnapFrameRef,
windowWidth,
],
);
diff --git a/packages/studio/src/components/Timeline/TimelineSnapIndicator.tsx b/packages/studio/src/components/Timeline/TimelineSnapIndicator.tsx
new file mode 100644
index 0000000000..8d5d645795
--- /dev/null
+++ b/packages/studio/src/components/Timeline/TimelineSnapIndicator.tsx
@@ -0,0 +1,86 @@
+import React, {useContext, useLayoutEffect, useRef, useState} from 'react';
+import {Internals} from 'remotion';
+import {SELECTED_OUTLINE_SNAP_COLOR} from '../../helpers/colors';
+import {getXPositionOfItemInTimelineImperatively} from '../../helpers/get-left-of-timeline-slider';
+import {EditorSnappingContext} from '../../state/editor-snapping';
+import {scrollableRef, sliderAreaRef} from './timeline-refs';
+import {TimelineWidthContext} from './TimelineWidthProvider';
+
+type UpdateTimelineSnapFrame = (frame: number | null) => void;
+
+export const TimelineSnapIndicatorContext =
+ React.createContext<React.RefObject<UpdateTimelineSnapFrame> | null>(null);
+
+export const TimelineSnapIndicatorProvider: React.FC<{
+ readonly children: React.ReactNode;
+}> = ({children}) => {
+ const updateSnapFrameRef = useRef<UpdateTimelineSnapFrame>(() => undefined);
+
+ return (
+ <TimelineSnapIndicatorContext.Provider value={updateSnapFrameRef}>
+ {children}
+ </TimelineSnapIndicatorContext.Provider>
+ );
+};
+
+const indicator: React.CSSProperties = {
+ backgroundColor: SELECTED_OUTLINE_SNAP_COLOR,
+ bottom: 0,
+ left: 0,
+ pointerEvents: 'none',
+ position: 'absolute',
+ top: 0,
+ width: 1,
+};
+
+export const TimelineSnapIndicator: React.FC = () => {
+ const updateSnapFrameRef = useContext(TimelineSnapIndicatorContext);
+ const [frame, setFrame] = useState<number | null>(null);
+ const {editorSnapping} = useContext(EditorSnappingContext);
+ const timelineWidth = useContext(TimelineWidthContext);
+ const videoConfig = Internals.useUnsafeVideoConfig();
+
+ useLayoutEffect(() => {
+ if (updateSnapFrameRef === null) {
+ return;
+ }
+
+ setFrame(null);
+ updateSnapFrameRef.current = setFrame;
+ return () => {
+ if (updateSnapFrameRef.current === setFrame) {
+ updateSnapFrameRef.current = () => undefined;
+ }
+ };
+ }, [updateSnapFrameRef, videoConfig?.id]);
+
+ if (
+ !editorSnapping ||
+ frame === null ||
+ timelineWidth === null ||
+ videoConfig === null
+ ) {
+ return null;
+ }
+
+ const left = getXPositionOfItemInTimelineImperatively(
+ frame,
+ videoConfig.durationInFrames,
+ sliderAreaRef.current?.clientWidth ?? timelineWidth,
+ );
+ const viewportLeft =
+ (scrollableRef.current?.getBoundingClientRect().left ?? 0) -
+ (scrollableRef.current?.scrollLeft ?? 0);
+ const alignedLeft =
+ Math.round((viewportLeft + left) * window.devicePixelRatio) /
+ window.devicePixelRatio -
+ viewportLeft;
+
+ return (
+ <div
+ aria-hidden="true"
+ data-timeline-snap-indicator="true"
+ style={{...indicator, transform: `translateX(${alignedLeft}px)`}}
+ />
+ );
+};