55 resolveTimelineMinDuration ,
66} from "./timelineGroupEditing" ;
77import type { TimelineElement } from "../store/playerStore" ;
8- import { clampToHostStart } from "../store/timelineElement" ;
8+ import { clampToHostStart , savedClipEdges } from "../store/timelineElement" ;
99import {
1010 CLIP_Y ,
1111 TRACK_H ,
@@ -34,7 +34,6 @@ import { heldAudioShiftRange, heldPartnerVideoBounds } from "./audioClipLink";
3434type BuildSnapTargets = (
3535 excludeElementKey : string | null ,
3636 includeBeats : boolean ,
37- includePlayhead ?: boolean ,
3837) => TimelineSnapTarget [ ] ;
3938
4039export interface DragPreviewContext {
@@ -56,6 +55,8 @@ export interface DragPreviewContext {
5655 * on demand from `elements`, so the result is identical either way.
5756 */
5857 audioTracks ?: ReadonlySet < number > ;
58+ /** Seconds between the ruler's lines; 0 or absent when snapping is off. */
59+ gridStep ?: number ;
5960}
6061
6162export function createKeyboardClipDrag (
@@ -248,6 +249,7 @@ export function computeDragPreview(
248249 // Relaxed clamp: allow the snapped start past the content, up to the
249250 // rendered extent (see dragMaxStart) — the composition grows on commit.
250251 dragMaxStart + drag . element . duration ,
252+ ctx . gridStep ,
251253 ) ;
252254 // A group moves rigidly: the grabbed clip stops where any mover would cross its host's start.
253255 const group = resolveGroupDrag ( drag , ctx ) ;
@@ -266,6 +268,11 @@ export function computeDragPreview(
266268 return { ...drag , started : true } ;
267269 }
268270 const { track : previewTrack , insertRow } = placement ;
271+ const target =
272+ placement . start === snap . start && snap . snapTime !== null && snap . snapType !== null
273+ ? { time : snap . snapTime , type : snap . snapType }
274+ : null ;
275+ const guide = guideIfSaved ( drag . element , { start : snap . start } , target , pps ) ;
269276 return {
270277 ...drag ,
271278 started : true ,
@@ -277,11 +284,24 @@ export function computeDragPreview(
277284 // tell a deliberate vertical lane change from a horizontal drag.
278285 desiredTrack : nextMove . track ,
279286 insertRow,
280- snapTime : placement . start === snap . start ? snap . snapTime : null ,
281- snapType : placement . start === snap . start ? snap . snapType : null ,
287+ snapTime : guide ?. time ?? null ,
288+ snapType : guide ?. type ?? null ,
282289 } ;
283290}
284291
292+ /** A snap's guide, kept only when the clip's edge as its file will save it is within a pixel of it. */
293+ export function guideIfSaved (
294+ element : TimelineElement ,
295+ clip : { start : number ; duration ?: number } ,
296+ target : TimelineSnapTarget | null ,
297+ pps : number ,
298+ ) : TimelineSnapTarget | null {
299+ if ( ! target ) return null ;
300+ const saved = savedClipEdges ( element , clip . start , clip . duration ) ;
301+ const off = Math . min ( Math . abs ( saved . start - target . time ) , Math . abs ( saved . end - target . time ) ) ;
302+ return off * pps < 1 ? target : null ;
303+ }
304+
285305/** One frame: the last visible frame of a clip sits just before its end time. */
286306const TRIM_END_FRAME_LEAD_S = 1 / STUDIO_PREVIEW_FPS ;
287307
@@ -296,6 +316,8 @@ export interface ResizePreviewContext {
296316 buildSnapTargets : BuildSnapTargets ;
297317 elements ?: readonly TimelineElement [ ] ;
298318 gestureKeys ?: ReadonlySet < string > ;
319+ /** Seconds between the ruler's lines; 0 or absent when snapping is off. */
320+ gridStep ?: number ;
299321}
300322
301323export interface ResizePreviewResult {
@@ -362,21 +384,19 @@ export function computeResizePreview(
362384 effectiveClientX ,
363385 ) ;
364386
365- // Snap to beats and clip edges, never the playhead (the dragged edge drives
366- // its own preview seek, so that would be circular). Stay inside the same
367- // limits resolveTimelineResize enforces. The music track defines the
368- // beats, so it must not snap to them, but still snaps to clip edges.
387+ // Snap within the same limits resolveTimelineResize enforces. The music
388+ // track defines the beats, so it must not snap to them.
369389 const trimTargets = buildSnapTargets (
370390 resize . element . key ?? resize . element . id ,
371391 ! isMusicTrack ( resize . element ) ,
372- false ,
373392 ) ;
393+ const gridStep = ctx . gridStep ?? 0 ;
374394 let snap : TimelineSnapTarget | null = null ;
375- if ( trimTargets . length > 0 ) {
395+ if ( trimTargets . length > 0 || gridStep > 0 ) {
376396 const snapSecs = TIMELINE_SNAP_PX / Math . max ( pps , 1 ) ;
377397 if ( resize . edge === "end" ) {
378398 const edgeTime = nextResize . start + nextResize . duration ;
379- const { time : snapped , target } = snapTimelineTime ( edgeTime , trimTargets , snapSecs ) ;
399+ const { time : snapped , target } = snapTimelineTime ( edgeTime , trimTargets , snapSecs , gridStep ) ;
380400 // Stay within [start+minDuration, maxEnd] so the snap can't create a
381401 // degenerate clip or run past the source/composition limit.
382402 const snappedDuration = Math . round ( ( snapped - nextResize . start ) * 1000 ) / 1000 ;
@@ -387,16 +407,21 @@ export function computeResizePreview(
387407 ) {
388408 // An edge already on the target still owns the guide; only move it when off.
389409 if ( snapped !== edgeTime ) nextResize = { ...nextResize , duration : snappedDuration } ;
390- snap = target ;
410+ snap = guideIfSaved ( resize . element , nextResize , target , pps ) ;
391411 }
392412 } else {
393- const { time : snapped , target } = snapTimelineTime ( nextResize . start , trimTargets , snapSecs ) ;
413+ const { time : snapped , target } = snapTimelineTime (
414+ nextResize . start ,
415+ trimTargets ,
416+ snapSecs ,
417+ gridStep ,
418+ ) ;
394419 const clip = { ...nextResize , playbackRate : resize . element . playbackRate } ;
395420 const delta = snapped - nextResize . start ;
396421 const bounds = clipStartTrimDeltaBounds ( clip , minStart , resolveTimelineMinDuration ( ) ) ;
397422 if ( target && delta >= bounds . minDelta - 1e-6 && delta <= bounds . maxDelta + 1e-6 ) {
398423 if ( snapped !== nextResize . start ) nextResize = applyClipStartTrimDelta ( clip , delta ) ;
399- snap = target ;
424+ snap = guideIfSaved ( resize . element , nextResize , target , pps ) ;
400425 }
401426 }
402427 }
0 commit comments