Skip to content

Commit dafac5e

Browse files
author
Jackson Nightingale
committed
Fix issue with item scramble on multidrag
1 parent 584b3be commit dafac5e

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/vuedraggable.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,16 @@ const draggableComponent = defineComponent({
256256
updatePositions(oldIndicies, newIndex) {
257257
/** @type {<T = any>(list: T[]) => T[]} */
258258
const updatePosition = list => {
259-
// get selected items with correct order
260-
// sort -> reverse (for prevent Array.splice side effect) -> splice -> reverse
259+
// Get selected items in the correct order
261260
const items = oldIndicies
262-
.sort()
263-
.reverse()
264-
.flatMap(oldIndex => list.splice(oldIndex, 1))
265-
.reverse();
266-
return list.splice(newIndex, 0, ...items);
261+
.sort((a, b) => b - a) // Sort in descending order
262+
.map(oldIndex => list.splice(oldIndex, 1)[0])
263+
.reverse(); // Remove the items
264+
265+
// Insert the items at the new index
266+
list.splice(newIndex, 0, ...items);
267+
268+
return list; // Return the updated list
267269
};
268270
this.alterList(updatePosition);
269271
},
@@ -318,7 +320,7 @@ const draggableComponent = defineComponent({
318320
// remove nodes
319321
evt.items.forEach(e => removeNode(e));
320322
// insert elements
321-
const newIndex = this.getVmIndexFromDomIndex(evt.newIndex);
323+
const newIndex = this.getVmIndexFromDomIndex(evt.newIndicies[0].index);
322324
this.spliceList(newIndex, 0, ...elements);
323325
// emit change
324326
const added = elements.map((element, index) => ({
@@ -441,7 +443,7 @@ const draggableComponent = defineComponent({
441443
);
442444
// move items
443445
const oldIndicies = itemsWithIndex.map(({ index }) => index - headerSize);
444-
const newIndex = this.getVmIndexFromDomIndex(evt.newIndex);
446+
const newIndex = this.getVmIndexFromDomIndex(evt.newIndicies[0].index);
445447
// note: Array.from = prevent sort change side effect
446448
this.updatePositions(Array.from(oldIndicies), newIndex);
447449
// emit change

0 commit comments

Comments
 (0)