@@ -12,6 +12,7 @@ import {
1212 snapMoveToTargets ,
1313 snapTimelineTime ,
1414 type TimelineSnapTarget ,
15+ type TimelineSnapType ,
1516} from "./timelineSnapping" ;
1617import { resolveInsertRow , resolveZoneDropPlacement } from "./timelineCollision" ;
1718import {
@@ -21,11 +22,13 @@ import {
2122import { clampGroupMoveDelta } from "./timelineMultiDragPreview" ;
2223import type { DraggedClipState , ResizingClipState } from "./timelineClipDragTypes" ;
2324import { resolveDragLandingStart } from "./timelineDragLanding" ;
25+ import { STUDIO_PREVIEW_FPS } from "../lib/time" ;
2426
2527/** Snap-target builder closure supplied by the hook (closes over refs + store). */
2628type BuildSnapTargets = (
2729 excludeElementKey : string | null ,
2830 includeBeats : boolean ,
31+ includePlayhead ?: boolean ,
2932) => TimelineSnapTarget [ ] ;
3033
3134export interface DragPreviewContext {
@@ -223,6 +226,14 @@ export function computeDragPreview(
223226 } ;
224227}
225228
229+ /** One frame: the last visible frame of a clip sits just before its end time. */
230+ const TRIM_END_FRAME_LEAD_S = 1 / STUDIO_PREVIEW_FPS ;
231+
232+ /** The composition time whose frame a trim shows: the edge being dragged. */
233+ export function trimPreviewTime ( edge : "start" | "end" , start : number , duration : number ) : number {
234+ return edge === "start" ? start : Math . max ( start , start + duration - TRIM_END_FRAME_LEAD_S ) ;
235+ }
236+
226237export interface ResizePreviewContext {
227238 scroll : HTMLDivElement | null ;
228239 pps : number ;
@@ -234,6 +245,9 @@ export interface ResizePreviewResult {
234245 previewStart : number ;
235246 previewDuration : number ;
236247 previewPlaybackStart ?: number ;
248+ /** The target the trimmed edge snapped to; null when the edge is free. */
249+ snapTime : number | null ;
250+ snapType : TimelineSnapType | null ;
237251}
238252
239253/** Compute the trim preview for a pointer x (pure — the hook applies the state). */
@@ -287,28 +301,31 @@ export function computeResizePreview(
287301 effectiveClientX ,
288302 ) ;
289303
290- // Snap edge to unified targets (beats + clip edges + playhead) when available.
291- // The snap must stay inside the same limits resolveTimelineResize enforces, or
292- // it would push the edge past the available source media / composition end.
293- // The music track defines the beats, so it must not snap to them — but it
294- // still snaps to the playhead and other clip edges.
304+ // Snap to beats and clip edges, never the playhead (the dragged edge drives
305+ // its own preview seek, so that would be circular). Stay inside the same
306+ // limits resolveTimelineResize enforces. The music track defines the
307+ // beats, so it must not snap to them, but still snaps to clip edges.
295308 const trimTargets = buildSnapTargets (
296309 resize . element . key ?? resize . element . id ,
297310 ! isMusicTrack ( resize . element ) ,
311+ false ,
298312 ) ;
313+ let snap : TimelineSnapTarget | null = null ;
299314 if ( trimTargets . length > 0 ) {
300315 const snapSecs = TIMELINE_SNAP_PX / Math . max ( pps , 1 ) ;
301316 if ( resize . edge === "end" ) {
302317 const edgeTime = nextResize . start + nextResize . duration ;
303- const snapped = snapTimelineTime ( edgeTime , trimTargets , snapSecs ) . time ;
318+ const { time : snapped , target } = snapTimelineTime ( edgeTime , trimTargets , snapSecs ) ;
304319 // Stay within [start+minDuration, maxEnd] so the snap can't create a
305320 // degenerate clip or run past the source/composition limit.
306321 const snappedDuration = Math . round ( ( snapped - nextResize . start ) * 1000 ) / 1000 ;
307- if ( snapped !== edgeTime && snapped <= maxEnd + 1e-6 && snappedDuration >= 0.05 ) {
308- nextResize = { ...nextResize , duration : snappedDuration } ;
322+ if ( target && snapped <= maxEnd + 1e-6 && snappedDuration >= 0.05 ) {
323+ // An edge already on the target still owns the guide; only move it when off.
324+ if ( snapped !== edgeTime ) nextResize = { ...nextResize , duration : snappedDuration } ;
325+ snap = target ;
309326 }
310327 } else {
311- const snapped = snapTimelineTime ( nextResize . start , trimTargets , snapSecs ) . time ;
328+ const { time : snapped , target } = snapTimelineTime ( nextResize . start , trimTargets , snapSecs ) ;
312329 const delta = nextResize . start - snapped ; // >0 when snapping left
313330 // Leftward snap reveals more source; cap so playbackStart can't go < 0.
314331 const maxLeftDelta =
@@ -318,22 +335,20 @@ export function computeResizePreview(
318335 // Also require the resulting duration to stay >= minDuration so a rightward
319336 // snap (delta < 0) can't collapse the clip to zero/negative.
320337 const snappedDuration = Math . round ( ( nextResize . duration + delta ) * 1000 ) / 1000 ;
321- if (
322- snapped !== nextResize . start &&
323- snapped >= 0 &&
324- delta <= maxLeftDelta + 1e-6 &&
325- snappedDuration >= 0.05
326- ) {
327- nextResize = {
328- ...nextResize ,
329- start : snapped ,
330- duration : snappedDuration ,
331- playbackStart :
332- nextResize . playbackStart != null
333- ? Math . round ( Math . max ( 0 , nextResize . playbackStart - delta * playbackRate ) * 1000 ) /
334- 1000
335- : undefined ,
336- } ;
338+ if ( target && snapped >= 0 && delta <= maxLeftDelta + 1e-6 && snappedDuration >= 0.05 ) {
339+ if ( snapped !== nextResize . start ) {
340+ nextResize = {
341+ ...nextResize ,
342+ start : snapped ,
343+ duration : snappedDuration ,
344+ playbackStart :
345+ nextResize . playbackStart != null
346+ ? Math . round ( Math . max ( 0 , nextResize . playbackStart - delta * playbackRate ) * 1000 ) /
347+ 1000
348+ : undefined ,
349+ } ;
350+ }
351+ snap = target ;
337352 }
338353 }
339354 }
@@ -343,6 +358,8 @@ export function computeResizePreview(
343358 previewStart : nextResize . start ,
344359 previewDuration : nextResize . duration ,
345360 previewPlaybackStart : nextResize . playbackStart ,
361+ snapTime : snap ?. time ?? null ,
362+ snapType : snap ?. type ?? null ,
346363 } ;
347364}
348365
@@ -359,11 +376,18 @@ export function previewGroupResize(
359376 ) => void ,
360377) : void {
361378 const grabbedChange = applyTimelineGroupResizePreview ( session , next ) ;
379+ const previewStart = grabbedChange ?. start ?? next . previewStart ;
380+ const previewDuration = grabbedChange ?. duration ?? next . previewDuration ;
381+ // A member clamp can pull the grabbed edge off the raw snap target; then no guide.
382+ const edgeTime = session . edge === "end" ? previewStart + previewDuration : previewStart ;
383+ const stillSnapped = next . snapTime != null && Math . abs ( edgeTime - next . snapTime ) < 1e-3 ;
362384 setResizeState ( {
363385 originScrollLeft : next . originScrollLeft ,
364- previewStart : grabbedChange ?. start ?? next . previewStart ,
365- previewDuration : grabbedChange ?. duration ?? next . previewDuration ,
386+ previewStart,
387+ previewDuration,
366388 previewPlaybackStart : grabbedChange ?. playbackStart ?? next . previewPlaybackStart ,
389+ snapTime : stillSnapped ? next . snapTime : null ,
390+ snapType : stillSnapped ? next . snapType : null ,
367391 groupPreview : session . changes ,
368392 } ) ;
369393}
0 commit comments