@@ -126,12 +126,11 @@ export const UncontrolledTreeEnvironment = React.forwardRef<
126
126
} }
127
127
onDrop = { async ( items , target ) => {
128
128
const promises : Promise < void > [ ] = [ ] ;
129
+ const itemsIndices = items . map ( i => i . index ) ;
130
+ let itemsPriorToInsertion = 0 ;
129
131
130
- // when dropped between items, items are injected at top of insertion point each
131
- const orderedItems =
132
- target . targetType === 'between-items' ? [ ...items ] . reverse ( ) : items ;
133
-
134
- for ( const item of orderedItems ) {
132
+ // move old items out
133
+ for ( const item of items ) {
135
134
const parent = Object . values ( currentItems ) . find ( potentialParent =>
136
135
potentialParent . children ?. includes ( item . index )
137
136
) ;
@@ -146,67 +145,73 @@ export const UncontrolledTreeEnvironment = React.forwardRef<
146
145
) ;
147
146
}
148
147
149
- if ( target . targetType === 'item' || target . targetType === 'root' ) {
150
- if ( target . targetItem === parent . index ) {
151
- // NOOP
152
- } else {
153
- promises . push (
154
- dataProvider . onChangeItemChildren (
155
- parent . index ,
156
- parent . children . filter ( child => child !== item . index )
157
- )
158
- ) ;
159
- promises . push (
160
- dataProvider . onChangeItemChildren ( target . targetItem , [
161
- ...( currentItems [ target . targetItem ] . children ?? [ ] ) ,
162
- item . index ,
163
- ] )
164
- ) ;
165
- }
166
- } else {
167
- const newParent = currentItems [ target . parentItem ] ;
168
- const newParentChildren = [ ...( newParent . children ?? [ ] ) ] . filter (
169
- child => child !== item . index
170
- ) ;
148
+ if (
149
+ target . targetType === 'between-items' &&
150
+ target . parentItem === item . index
151
+ ) {
152
+ // Trying to drop inside itself
153
+ return ;
154
+ }
171
155
172
- if ( target . parentItem === item . index ) {
173
- // Trying to drop inside itself
174
- return ;
175
- }
156
+ if (
157
+ ( target . targetType === 'item' || target . targetType === 'root' ) &&
158
+ target . targetItem !== parent . index
159
+ ) {
160
+ promises . push (
161
+ dataProvider . onChangeItemChildren (
162
+ parent . index ,
163
+ parent . children . filter ( child => child !== item . index )
164
+ )
165
+ ) ;
166
+ }
176
167
168
+ if ( target . targetType === 'between-items' ) {
177
169
if ( target . parentItem === parent . index ) {
170
+ const newParent = currentItems [ target . parentItem ] ;
178
171
const isOldItemPriorToNewItem =
179
172
( ( newParent . children ?? [ ] ) . findIndex (
180
173
child => child === item . index
181
174
) ?? Infinity ) < target . childIndex ;
182
- newParentChildren . splice (
183
- target . childIndex - ( isOldItemPriorToNewItem ? 1 : 0 ) ,
184
- 0 ,
185
- item . index
186
- ) ;
187
- promises . push (
188
- dataProvider . onChangeItemChildren (
189
- target . parentItem ,
190
- newParentChildren
191
- )
192
- ) ;
175
+ itemsPriorToInsertion += isOldItemPriorToNewItem ? 1 : 0 ;
193
176
} else {
194
- newParentChildren . splice ( target . childIndex , 0 , item . index ) ;
195
177
promises . push (
196
178
dataProvider . onChangeItemChildren (
197
179
parent . index ,
198
180
parent . children . filter ( child => child !== item . index )
199
181
)
200
182
) ;
201
- promises . push (
202
- dataProvider . onChangeItemChildren (
203
- target . parentItem ,
204
- newParentChildren
205
- )
206
- ) ;
207
183
}
208
184
}
209
185
}
186
+
187
+ // insert new items
188
+ if ( target . targetType === 'item' || target . targetType === 'root' ) {
189
+ promises . push (
190
+ dataProvider . onChangeItemChildren ( target . targetItem , [
191
+ ...( currentItems [ target . targetItem ] . children ?? [ ] ) . filter (
192
+ i => ! itemsIndices . includes ( i )
193
+ ) ,
194
+ ...itemsIndices ,
195
+ ] )
196
+ ) ;
197
+ } else {
198
+ const newParent = currentItems [ target . parentItem ] ;
199
+ const newParentChildren = [ ...( newParent . children ?? [ ] ) ] . filter (
200
+ c => ! itemsIndices . includes ( c )
201
+ ) ;
202
+ newParentChildren . splice (
203
+ target . childIndex - itemsPriorToInsertion ,
204
+ 0 ,
205
+ ...itemsIndices
206
+ ) ;
207
+ promises . push (
208
+ dataProvider . onChangeItemChildren (
209
+ target . parentItem ,
210
+ newParentChildren
211
+ )
212
+ ) ;
213
+ }
214
+
210
215
await Promise . all ( promises ) ;
211
216
props . onDrop ?.( items , target ) ;
212
217
} }
0 commit comments