Skip to content

Commit a8fb1a1

Browse files
committed
fix: update DOM to correctly reflect the state prior to vue.draggable functions
1 parent f4bf376 commit a8fb1a1

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/vuedraggable.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,10 @@ const draggableComponent = defineComponent({
334334
if (element === undefined) {
335335
return;
336336
}
337-
const swapItem = swapMode ? evt.to.children[evt.newIndex] : null;
337+
const swapItem = swapMode ? evt.from.children[evt.oldIndex] : null;
338338
removeNode(evt.item);
339339
let newIndex = this.getVmIndexFromDomIndex(evt.newIndex);
340-
if (evt.swap) newIndex -= 1;
340+
if (swapMode) newIndex = newIndex === 0 ? 0 : newIndex - 1;
341341
// @ts-ignore
342342
this.spliceList(newIndex, swapMode ? 1 : 0, element);
343343
const added = { element, newIndex };

tests/unit/vuedraggable.swap.spec.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ describe("draggable.vue with swap plugin", () => {
9494
onStart(startEvent);
9595
await nextTick();
9696

97-
// console.log(Array.from(vm.targetDomElement.children))
97+
const temp = vm.targetDomElement.children[0]
98+
vm.targetDomElement.replaceChild(vm.targetDomElement.children[2], vm.targetDomElement.children[0])
99+
vm.targetDomElement.replaceChild(temp, vm.targetDomElement.children[2])
98100
// drop to last item
99101
const updateEvent = {
100102
from: wrapper.element,
@@ -131,7 +133,9 @@ describe("draggable.vue with swap plugin", () => {
131133
onStart(startEvent);
132134
await nextTick();
133135

134-
// console.log(Array.from(vm.targetDomElement.children))
136+
const temp = vm.targetDomElement.children[0]
137+
vm.targetDomElement.replaceChild(vm.targetDomElement.children[3], vm.targetDomElement.children[0])
138+
vm.targetDomElement.appendChild(temp)
135139
// drop to last item
136140
const updateEvent = {
137141
from: wrapper.element,
@@ -191,6 +195,9 @@ describe("draggable.vue with swap plugin", () => {
191195
await nextTick();
192196

193197
// console.log(Array.from(vm.targetDomElement.children))
198+
const temp = vm.targetDomElement.children[1]
199+
vm.targetDomElement.replaceChild(vm.targetDomElement.children[3], vm.targetDomElement.children[1])
200+
vm.targetDomElement.replaceChild(temp, vm.targetDomElement.children[3])
194201
// drop to last item
195202
const updateEvent = {
196203
from: wrapper.element,
@@ -228,6 +235,9 @@ describe("draggable.vue with swap plugin", () => {
228235
await nextTick();
229236

230237
// console.log(Array.from(vm.targetDomElement.children))
238+
const temp = vm.targetDomElement.children[1]
239+
vm.targetDomElement.replaceChild(vm.targetDomElement.children[4], vm.targetDomElement.children[1])
240+
vm.targetDomElement.appendChild(temp)
231241
// drop to last item
232242
const updateEvent = {
233243
from: wrapper.element,
@@ -290,6 +300,9 @@ describe("draggable.vue with swap plugin", () => {
290300
await nextTick();
291301

292302
// console.log(Array.from(vm.targetDomElement.children))
303+
const temp = vm.targetDomElement.children[0]
304+
vm.targetDomElement.replaceChild(vm.targetDomElement.children[2], vm.targetDomElement.children[0])
305+
vm.targetDomElement.replaceChild(temp, vm.targetDomElement.children[2])
293306
// drop to last item
294307
const updateEvent = {
295308
from: wrapper.element,
@@ -327,6 +340,9 @@ describe("draggable.vue with swap plugin", () => {
327340
await nextTick();
328341

329342
// console.log(Array.from(vm.targetDomElement.children))
343+
const temp = vm.targetDomElement.children[0]
344+
vm.targetDomElement.replaceChild(vm.targetDomElement.children[3], vm.targetDomElement.children[0])
345+
vm.targetDomElement.appendChild(temp)
330346
// drop to last item
331347
const updateEvent = {
332348
from: wrapper.element,
@@ -389,6 +405,9 @@ describe("draggable.vue with swap plugin", () => {
389405
group1.onStart(startEvent);
390406
await nextTick();
391407

408+
const temp = group2.vm.targetDomElement.children[0]
409+
group2.vm.targetDomElement.replaceChild(group1.vm.targetDomElement.children[0], group2.vm.targetDomElement.children[0])
410+
group1.vm.targetDomElement.insertBefore(temp, group1.vm.targetDomElement.children[0])
392411
const addEvent = {
393412
from: group1.wrapper.element,
394413
to: group2.wrapper.element,
@@ -483,6 +502,9 @@ describe("draggable.vue with swap plugin", () => {
483502
group1.onStart(startEvent);
484503
await nextTick();
485504

505+
const temp = group2.vm.targetDomElement.children[0]
506+
group2.vm.targetDomElement.replaceChild(group1.vm.targetDomElement.children[0], group2.vm.targetDomElement.children[0])
507+
group1.vm.targetDomElement.insertBefore(temp, group1.vm.targetDomElement.children[0])
486508
const addEvent = {
487509
from: group1.wrapper.element,
488510
to: group2.wrapper.element,

0 commit comments

Comments
 (0)