Skip to content

Commit 337a09b

Browse files
committed
fix(draggable plugin): fix node insert error after drop
fix #51
1 parent 9675d19 commit 337a09b

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

src/plugins/draggable/Draggable.vue

+22-5
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,28 @@ export default {
264264
const startSiblings = startParentPath.length === 0 ? startTree.treeData : startParent.children
265265
const startIndex = hp.arrayLast(startPath)
266266
startSiblings.splice(startIndex, 1)
267-
// update targetPath if isDownwardsSameLevelMove
268-
if (store.isDownwardsSameLevelMove) {
269-
targetPath = targetPath.slice(0)
270-
const endIndex = startPath.length - 1
271-
targetPath[endIndex] -= 1
267+
// remove node from the starting position may affect the target path.
268+
// example
269+
// startPath targetPath
270+
// [0] [1]
271+
// [0] [1, 0]
272+
// [3, 1] [3, 3]
273+
// [3, 1] [3, 3, 5]
274+
// above targetPaths should be transformed to [0], [0, 0] [3, 2] [3, 2, 5]
275+
if (startTree === targetTree) {
276+
if (startPath.length <= targetPath.length) {
277+
const sw = startPath.slice(0, startPath.length - 1) // without end
278+
const tw = targetPath.slice(0, sw.length) // same length with sw
279+
if (sw.toString() === tw.toString()) {
280+
const endIndex = sw.length
281+
if (startPath[endIndex] < targetPath[endIndex]) {
282+
targetPath = targetPath.slice(0) // create a copy of targetPath
283+
targetPath[endIndex] -= 1
284+
} else if (startPath[endIndex] === targetPath[endIndex]) {
285+
console.error('Draggable.afterDrop: That is impossible!');
286+
}
287+
}
288+
}
272289
}
273290
}
274291
// insert to target position

src/plugins/draggable/draggable.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,6 @@ export default function makeTreeDraggable(treeEl, options = {}) {
466466
}
467467
//
468468
store.targetPath = options.getPathByBranchEl(placeholder)
469-
store.isDownwardsSameLevelMove = isDownwardsSameLevelMove()
470469
let pathChanged = isPathChanged()
471470
store.targetPathNotEqualToStartPath = pathChanged
472471
store.pathChangePrevented = false
@@ -496,15 +495,21 @@ export default function makeTreeDraggable(treeEl, options = {}) {
496495
}
497496
//
498497
function isPathChanged() {
499-
const {startTree, targetTree, startPath, targetPath, isDownwardsSameLevelMove} = store
500-
if (isDownwardsSameLevelMove) {
501-
return hp.arrayLast(startPath) < hp.arrayLast(targetPath) - 1 // if equal, not moved
502-
}
503-
return startTree !== targetTree || startPath.toString() !== targetPath.toString()
504-
}
505-
function isDownwardsSameLevelMove() {
506498
const {startTree, targetTree, startPath, targetPath} = store
507-
return startTree === targetTree && startPath.length === targetPath.length && startPath.slice(0, startPath.length - 1).toString() === targetPath.slice(0, targetPath.length - 1).toString() && hp.arrayLast(startPath) < hp.arrayLast(targetPath)
499+
if (startTree === targetTree && startPath.length === targetPath.length) {
500+
if (startPath.toString() === targetPath.toString()) {
501+
return false
502+
} else {
503+
// downward same-level move, the end of targetPath is 1 more than real value
504+
// 同级向下移动时, targetPath的末位比真实值大1
505+
const t = startPath.slice(0)
506+
t[t.length - 1] ++
507+
if (t.toString() === targetPath.toString()) {
508+
return false
509+
}
510+
}
511+
}
512+
return true
508513
}
509514
},
510515
})

0 commit comments

Comments
 (0)