@@ -92,13 +92,13 @@ function removeNode(node) {
9292 node . parentElement . removeChild ( node ) ;
9393}
9494/**
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.
9999 */
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 ;
102102 parent . insertBefore ( newChild , refChild ) ;
103103}
104104// todo:
@@ -134,7 +134,7 @@ var ReactSortable = /** @class */ (function (_super) {
134134 var plugins = props . plugins ;
135135 // mount plugins if any
136136 if ( plugins ) {
137- if ( plugins instanceof Array )
137+ if ( Array . isArray ( plugins ) )
138138 Sortable$1__default . mount . apply ( Sortable$1__default , __spread ( plugins ) ) ;
139139 else
140140 Sortable$1__default . mount ( plugins ) ;
@@ -185,13 +185,7 @@ var ReactSortable = /** @class */ (function (_super) {
185185 enumerable : true ,
186186 configurable : true
187187 } ) ;
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. */
195189 ReactSortable . prototype . makeOptions = function ( ) {
196190 var _this = this ;
197191 var DOMHandlers = [
@@ -230,6 +224,7 @@ var ReactSortable = /** @class */ (function (_super) {
230224 // call the component prop
231225 _this . callOnHandlerProp ( evt , evtName ) ;
232226 // calls state change
227+ //@ts -ignore - until @types multidrag item is in
233228 _this [ evtName ] ( evt ) ;
234229 } ;
235230 } ;
@@ -259,7 +254,6 @@ var ReactSortable = /** @class */ (function (_super) {
259254 } ;
260255 /** Called when an element is removed from the list into another list */
261256 ReactSortable . prototype . onRemove = function ( evt ) {
262- //@ts -ignore - pullMode not in types. Request made.
263257 var item = evt . item , from = evt . from , oldIndex = evt . oldIndex , clone = evt . clone , pullMode = evt . pullMode ;
264258 insertNodeAt ( from , item , oldIndex ) ;
265259 var _a = this . props , list = _a . list , setList = _a . setList ;
@@ -277,14 +271,80 @@ var ReactSortable = /** @class */ (function (_super) {
277271 } ;
278272 /** Called when sorting is changed within the same list */
279273 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+ }
288348 } ;
289349 /** Called when the dragging starts */
290350 ReactSortable . prototype . onStart = function ( evt ) {
@@ -307,7 +367,6 @@ var ReactSortable = /** @class */ (function (_super) {
307367 /** @todo */
308368 ReactSortable . prototype . onSelect = function ( evt ) {
309369 var oldIndex = evt . oldIndex , newIndex = evt . newIndex ;
310- console . log ( { oldIndex : oldIndex , newIndex : newIndex } ) ;
311370 // append the class name the classes of the item
312371 // do it on the item?
313372 // a seperate state?
0 commit comments