@@ -92,13 +92,13 @@ function removeNode(node) {
92
92
node . parentElement . removeChild ( node ) ;
93
93
}
94
94
/**
95
- * @summary Inserts a `newChild` inside the `parent` at index number `position`
96
- * @param parent
97
- * @param newChild
98
- * @param position a number that is not negative
95
+ * Inserts the `newChild` node at the given index in a parent
96
+ * @param parent The parent HTML Element.
97
+ * @param newChild A HTML eement to add as a child of the parent.
98
+ * @param index index of the parent to place the new child in.
99
99
*/
100
- function insertNodeAt ( parent , newChild , position ) {
101
- var refChild = position === 0 ? parent . children [ 0 ] : parent . children [ position - 1 ] ;
100
+ function insertNodeAt ( parent , newChild , index ) {
101
+ var refChild = parent . children [ index ] || null ;
102
102
parent . insertBefore ( newChild , refChild ) ;
103
103
}
104
104
// todo:
@@ -134,7 +134,7 @@ var ReactSortable = /** @class */ (function (_super) {
134
134
var plugins = props . plugins ;
135
135
// mount plugins if any
136
136
if ( plugins ) {
137
- if ( plugins instanceof Array )
137
+ if ( Array . isArray ( plugins ) )
138
138
Sortable$1__default . mount . apply ( Sortable$1__default , __spread ( plugins ) ) ;
139
139
else
140
140
Sortable$1__default . mount ( plugins ) ;
@@ -185,13 +185,7 @@ var ReactSortable = /** @class */ (function (_super) {
185
185
enumerable : true ,
186
186
configurable : true
187
187
} ) ;
188
- /** const { plugins } = props;
189
- // mount plugins if any
190
- if (plugins) {
191
- if (plugins instanceof Array) Sortable.mount(...plugins);
192
- else Sortable.mount(plugins);
193
- }
194
- }Converts all the props from `ReactSortable` into the `options` object that `Sortable.create(el, [options])` can use. */
188
+ /** Converts all the props from `ReactSortable` into the `options` object that `Sortable.create(el, [options])` can use. */
195
189
ReactSortable . prototype . makeOptions = function ( ) {
196
190
var _this = this ;
197
191
var DOMHandlers = [
@@ -230,6 +224,7 @@ var ReactSortable = /** @class */ (function (_super) {
230
224
// call the component prop
231
225
_this . callOnHandlerProp ( evt , evtName ) ;
232
226
// calls state change
227
+ //@ts -ignore - until @types multidrag item is in
233
228
_this [ evtName ] ( evt ) ;
234
229
} ;
235
230
} ;
@@ -259,7 +254,6 @@ var ReactSortable = /** @class */ (function (_super) {
259
254
} ;
260
255
/** Called when an element is removed from the list into another list */
261
256
ReactSortable . prototype . onRemove = function ( evt ) {
262
- //@ts -ignore - pullMode not in types. Request made.
263
257
var item = evt . item , from = evt . from , oldIndex = evt . oldIndex , clone = evt . clone , pullMode = evt . pullMode ;
264
258
insertNodeAt ( from , item , oldIndex ) ;
265
259
var _a = this . props , list = _a . list , setList = _a . setList ;
@@ -277,14 +271,80 @@ var ReactSortable = /** @class */ (function (_super) {
277
271
} ;
278
272
/** Called when sorting is changed within the same list */
279
273
ReactSortable . prototype . onUpdate = function ( evt ) {
280
- var item = evt . item , from = evt . from , oldIndex = evt . oldIndex , newIndex = evt . newIndex ;
281
- removeNode ( item ) ;
282
- insertNodeAt ( from , item , oldIndex ) ;
283
- var _a = this . props , list = _a . list , setList = _a . setList ;
284
- var newState = __spread ( list ) ;
285
- var _b = __read ( newState . splice ( oldIndex , 1 ) , 1 ) , oldItem = _b [ 0 ] ;
286
- newState . splice ( newIndex , 0 , oldItem ) ;
287
- setList ( newState , this . sortable , store ) ;
274
+ var mode = ( function ( ) {
275
+ if ( evt . oldIndicies . length > 0 )
276
+ return "multidrag" ;
277
+ if ( evt . swapItem )
278
+ return "swap" ;
279
+ return "normal" ;
280
+ } ) ( ) ;
281
+ switch ( mode ) {
282
+ case "normal" : {
283
+ removeNode ( evt . item ) ;
284
+ insertNodeAt ( evt . from , evt . item , evt . oldIndex ) ;
285
+ var _a = this . props , list = _a . list , setList = _a . setList ;
286
+ var newState = __spread ( list ) ;
287
+ var _b = __read ( newState . splice ( evt . oldIndex , 1 ) , 1 ) , oldItem = _b [ 0 ] ;
288
+ newState . splice ( evt . newIndex , 0 , oldItem ) ;
289
+ return setList ( newState , this . sortable , store ) ;
290
+ }
291
+ case "swap" : {
292
+ // item that was dragged
293
+ removeNode ( evt . item ) ;
294
+ insertNodeAt ( evt . from , evt . item , evt . oldIndex ) ;
295
+ // item that was landed on for the swap
296
+ removeNode ( evt . swapItem ) ;
297
+ insertNodeAt ( evt . from , evt . swapItem , evt . newIndex ) ;
298
+ var _c = this . props , list = _c . list , setList = _c . setList ;
299
+ var newState_1 = __spread ( list ) ;
300
+ var customs = [
301
+ {
302
+ element : evt . item ,
303
+ oldIndex : evt . oldIndex ,
304
+ newIndex : evt . newIndex
305
+ } ,
306
+ {
307
+ element : evt . swapItem ,
308
+ oldIndex : evt . newIndex ,
309
+ newIndex : evt . oldIndex
310
+ }
311
+ ]
312
+ . map ( function ( curr ) { return ( __assign ( __assign ( { } , curr ) , { item : newState_1 [ curr . oldIndex ] } ) ) ; } )
313
+ . sort ( function ( a , b ) { return a . oldIndex - b . oldIndex ; } ) ;
314
+ // DOM element management
315
+ customs . forEach ( function ( curr ) { return removeNode ( curr . element ) ; } ) ;
316
+ customs . forEach ( function ( curr ) {
317
+ return insertNodeAt ( evt . from , curr . element , curr . oldIndex ) ;
318
+ } ) ;
319
+ customs . reverse ( ) . forEach ( function ( curr ) { return newState_1 . splice ( curr . oldIndex , 1 ) ; } ) ;
320
+ customs . forEach ( function ( curr ) { return newState_1 . splice ( curr . newIndex , 0 , curr . item ) ; } ) ;
321
+ return setList ( newState_1 , this . sortable , store ) ;
322
+ }
323
+ case "multidrag" : {
324
+ var newOldIndices = evt . oldIndicies . map ( function ( curr , index ) { return ( {
325
+ element : curr . multiDragElement ,
326
+ oldIndex : curr . index ,
327
+ newIndex : evt . newIndicies [ index ] . index
328
+ } ) ; } ) ;
329
+ // DOM element management
330
+ newOldIndices . forEach ( function ( curr ) { return removeNode ( curr . element ) ; } ) ;
331
+ newOldIndices . forEach ( function ( curr ) {
332
+ return insertNodeAt ( evt . from , curr . element , curr . oldIndex ) ;
333
+ } ) ;
334
+ var _d = this . props , list = _d . list , setList = _d . setList ;
335
+ var newState_2 = __spread ( list ) ;
336
+ newOldIndices
337
+ // remove old items in state, starting from the end.
338
+ . reverse ( )
339
+ . map ( function ( curr ) { return ( __assign ( __assign ( { } , curr ) , { item : newState_2 . splice ( curr . oldIndex , 1 ) . pop ( ) } ) ) ; } )
340
+ // insert new items, starting from the front.
341
+ . reverse ( )
342
+ . forEach ( function ( curr ) {
343
+ newState_2 . splice ( curr . newIndex , 0 , curr . item ) ;
344
+ } ) ;
345
+ return setList ( newState_2 , this . sortable , store ) ;
346
+ }
347
+ }
288
348
} ;
289
349
/** Called when the dragging starts */
290
350
ReactSortable . prototype . onStart = function ( evt ) {
@@ -307,7 +367,6 @@ var ReactSortable = /** @class */ (function (_super) {
307
367
/** @todo */
308
368
ReactSortable . prototype . onSelect = function ( evt ) {
309
369
var oldIndex = evt . oldIndex , newIndex = evt . newIndex ;
310
- console . log ( { oldIndex : oldIndex , newIndex : newIndex } ) ;
311
370
// append the class name the classes of the item
312
371
// do it on the item?
313
372
// a seperate state?
0 commit comments