@@ -5,10 +5,9 @@ import type { UniqueIdentifier } from "../../../types";
55import {
66 applyOffset ,
77 arraysEqual ,
8- centerAxis ,
98 type Direction ,
9+ doesOverlapOnAxis ,
1010 moveArrayIndex ,
11- overlapsAxis ,
1211 type Rectangle ,
1312} from "../../../utils" ;
1413
@@ -31,9 +30,10 @@ export const useDraggableSort = ({
3130 initialOrder = [ ] ,
3231 onOrderChange,
3332 onOrderUpdate,
34- shouldSwapWorklet,
33+ shouldSwapWorklet = doesOverlapOnAxis ,
3534} : UseDraggableSortOptions ) => {
36- const { draggableActiveId, draggableActiveLayout, draggableOffsets, draggableLayouts } = useDndContext ( ) ;
35+ const { draggableIds, draggableActiveId, draggableActiveLayout, draggableOffsets, draggableLayouts } =
36+ useDndContext ( ) ;
3737 const direction = horizontal ? "horizontal" : "vertical" ;
3838
3939 const draggablePlaceholderIndex = useSharedValue ( - 1 ) ;
@@ -65,25 +65,46 @@ export const useDraggableSort = ({
6565 y : offsets [ itemId ] . y . value ,
6666 } ) ;
6767
68- if ( shouldSwapWorklet ) {
69- if ( shouldSwapWorklet ( activeLayout , itemLayout , direction ) ) {
70- // console.log(`Found placeholder index ${itemIndex} using custom shouldSwapWorklet!`);
71- return itemIndex ;
72- }
73- continue ;
74- }
75-
76- // Default to center axis
77- const itemCenterAxis = centerAxis ( itemLayout , direction ) ;
78- if ( overlapsAxis ( activeLayout , itemCenterAxis , direction ) ) {
68+ if ( shouldSwapWorklet ( activeLayout , itemLayout , direction ) ) {
69+ // console.log(`Found placeholder index ${itemIndex} using custom shouldSwapWorklet!`);
7970 return itemIndex ;
8071 }
72+ continue ;
8173 }
8274 // Fallback to current index
83- // console.log(`Fallback to current index ${activeIndex}`);
8475 return activeIndex ;
8576 } ;
8677
78+ // Track added/removed draggable items and update the sort order
79+ useAnimatedReaction (
80+ ( ) => draggableIds . value ,
81+ ( next , prev ) => {
82+ if ( prev === null || prev . length === 0 ) {
83+ return ;
84+ }
85+
86+ // Handle removed draggable items
87+ const removedIds = prev . filter ( ( id ) => ! next . includes ( id ) ) ;
88+ if ( removedIds . length > 0 ) {
89+ draggableSortOrder . value = draggableSortOrder . get ( ) . filter ( ( itemId ) => ! removedIds . includes ( itemId ) ) ;
90+ }
91+
92+ // Handle added draggable items by inserting them at the correct index
93+ const layouts = draggableLayouts . get ( ) ;
94+ const addedIds = next . filter ( ( id ) => ! prev . includes ( id ) ) ;
95+ addedIds . forEach ( ( id ) => {
96+ const index = Object . entries ( layouts )
97+ . sort ( ( [ , a ] , [ , b ] ) => a . get ( ) [ horizontal ? "x" : "y" ] - b . get ( ) [ horizontal ? "x" : "y" ] )
98+ . findIndex ( ( [ key ] ) => key === id ) ;
99+ const nextOrder = draggableSortOrder . value . slice ( ) ;
100+ nextOrder . splice ( index , 0 , id ) ;
101+ // draggableLastOrder.value = draggableSortOrder.value.slice();
102+ draggableSortOrder . value = nextOrder ;
103+ } ) ;
104+ } ,
105+ [ ] ,
106+ ) ;
107+
87108 // Track active layout changes and update the placeholder index
88109 useAnimatedReaction (
89110 ( ) => [ draggableActiveId . value , draggableActiveLayout . value ] as const ,
0 commit comments