@@ -191,6 +191,7 @@ export function useElementInteraction({
191191 const [ isPendingDrag , setIsPendingDrag ] = useState ( false ) ;
192192 const pendingDragRef = useRef < PendingDragState | null > ( null ) ;
193193 const moveGroupRef = useRef < MoveGroup | null > ( null ) ;
194+ const newTrackIdsRef = useRef < string [ ] > ( [ ] ) ;
194195 const groupMoveResultRef = useRef < GroupMoveResult | null > ( null ) ;
195196 const lastMouseXRef = useRef ( 0 ) ;
196197 const mouseDownLocationRef = useRef < { x : number ; y : number } | null > ( null ) ;
@@ -227,6 +228,7 @@ export function useElementInteraction({
227228
228229 const endDrag = useCallback ( ( ) => {
229230 moveGroupRef . current = null ;
231+ newTrackIdsRef . current = [ ] ;
230232 groupMoveResultRef . current = null ;
231233 setDragState ( initialDragState ) ;
232234 setDragDropTarget ( null ) ;
@@ -262,7 +264,7 @@ export function useElementInteraction({
262264 target : {
263265 kind : "newTracks" ,
264266 anchorInsertIndex : dropTarget . trackIndex ,
265- newTrackIds : group . members . map ( ( ) => generateUUID ( ) ) ,
267+ newTrackIds : newTrackIdsRef . current ,
266268 } ,
267269 } ) ;
268270 }
@@ -297,7 +299,7 @@ export function useElementInteraction({
297299 target : {
298300 kind : "newTracks" ,
299301 anchorInsertIndex : dropTarget . trackIndex ,
300- newTrackIds : group . members . map ( ( ) => generateUUID ( ) ) ,
302+ newTrackIds : newTrackIdsRef . current ,
301303 } ,
302304 } ) ;
303305 } ,
@@ -386,18 +388,61 @@ export function useElementInteraction({
386388 }
387389
388390 moveGroupRef . current = moveGroup ;
389- groupMoveResultRef . current = null ;
391+ newTrackIdsRef . current = moveGroup . members . map ( ( ) => generateUUID ( ) ) ;
390392 const dragTimeOffsets : Record < string , number > = { } ;
391393 for ( const member of moveGroup . members ) {
392394 dragTimeOffsets [ member . elementId ] = member . timeOffset ;
393395 }
396+ const {
397+ snappedTime : initialSnappedTime ,
398+ snapPoint : initialSnapPoint ,
399+ } = getDragSnapResult ( {
400+ frameSnappedTime : snappedTime ,
401+ group : moveGroup ,
402+ } ) ;
403+ const verticalDragDirection = getVerticalDragDirection ( {
404+ startMouseY : pendingDragRef . current . startMouseY ,
405+ currentMouseY : clientY ,
406+ } ) ;
407+ const anchorDropTarget = getDragDropTarget ( {
408+ clientX,
409+ clientY,
410+ elementId : pendingDragRef . current . elementId ,
411+ trackId : pendingDragRef . current . trackId ,
412+ tracks : sceneTracks ,
413+ tracksContainerRef,
414+ tracksScrollRef,
415+ headerRef,
416+ zoomLevel,
417+ snappedTime : initialSnappedTime ,
418+ verticalDragDirection,
419+ } ) ;
420+ const nextGroupMoveResult =
421+ anchorDropTarget != null
422+ ? resolveGroupDragMove ( {
423+ group : moveGroup ,
424+ snappedTime : initialSnappedTime ,
425+ dropTarget : anchorDropTarget ,
426+ } )
427+ : null ;
428+ groupMoveResultRef . current = nextGroupMoveResult ;
429+ setDragDropTarget (
430+ anchorDropTarget &&
431+ ( anchorDropTarget . isNewTrack || ! nextGroupMoveResult )
432+ ? {
433+ ...anchorDropTarget ,
434+ isNewTrack : true ,
435+ }
436+ : null ,
437+ ) ;
394438 startDrag ( {
395439 ...pendingDragRef . current ,
396440 dragElementIds : moveGroup . members . map ( ( member ) => member . elementId ) ,
397441 dragTimeOffsets,
398- initialCurrentTime : snappedTime ,
442+ initialCurrentTime : initialSnappedTime ,
399443 initialCurrentMouseY : clientY ,
400444 } ) ;
445+ onSnapPointChange ?.( initialSnapPoint ) ;
401446 startedDragThisEvent = true ;
402447 pendingDragRef . current = null ;
403448 setIsPendingDrag ( false ) ;
@@ -533,40 +578,14 @@ export function useElementInteraction({
533578 }
534579 }
535580
536- const dropTarget = getDragDropTarget ( {
537- clientX,
538- clientY,
539- elementId : dragState . elementId ,
540- trackId : dragState . trackId ,
541- tracks : sceneTracks ,
542- tracksContainerRef,
543- tracksScrollRef,
544- headerRef,
545- zoomLevel,
546- snappedTime : dragState . currentTime ,
547- verticalDragDirection : getVerticalDragDirection ( {
548- startMouseY : dragState . startMouseY ,
549- currentMouseY : clientY ,
550- } ) ,
551- } ) ;
552- if ( ! dropTarget ) {
553- endDrag ( ) ;
554- onSnapPointChange ?.( null ) ;
555- return ;
556- }
557- const snappedTime = dragState . currentTime ;
558581 const moveGroup = moveGroupRef . current ;
559582 if ( ! moveGroup ) {
560583 endDrag ( ) ;
561584 onSnapPointChange ?.( null ) ;
562585 return ;
563586 }
564587
565- const groupMoveResult = resolveGroupDragMove ( {
566- group : moveGroup ,
567- snappedTime,
568- dropTarget,
569- } ) ;
588+ const groupMoveResult = groupMoveResultRef . current ;
570589 if ( ! groupMoveResult ) {
571590 endDrag ( ) ;
572591 onSnapPointChange ?.( null ) ;
@@ -604,18 +623,10 @@ export function useElementInteraction({
604623 dragState . isDragging ,
605624 dragState . elementId ,
606625 dragState . startElementTime ,
607- dragState . startMouseY ,
608626 dragState . trackId ,
609- dragState . currentTime ,
610- zoomLevel ,
611627 endDrag ,
612628 onSnapPointChange ,
613629 editor . timeline ,
614- tracksContainerRef ,
615- tracksScrollRef ,
616- headerRef ,
617- resolveGroupDragMove ,
618- sceneTracks ,
619630 ] ) ;
620631
621632 useEffect ( ( ) => {
0 commit comments