Skip to content

Commit 9124ec1

Browse files
committed
fix(draggable plugin): wrong result when move downwards in same level
1 parent 2dcb413 commit 9124ec1

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

src/plugins/draggable/Draggable_vue.js

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,6 @@ export default {
116116
let index = 0
117117
for (const {value: el, index: index2} of hp.iterateAll(branchEl.parentElement.children)) {
118118
if (hp.hasClass(el, 'tree-branch') || hp.hasClass(el, 'tree-placeholder')) {
119-
if (el === store.dragBranchEl) {
120-
continue
121-
}
122119
if (el === branchEl) {
123120
break
124121
}
@@ -259,34 +256,20 @@ export default {
259256
},
260257
afterDrop: (store, t) => {
261258
if (store.pathChanged) {
262-
const {startTree, targetTree, startPath, targetPath, dragNode} = store
259+
const {startTree, targetTree, startPath, dragNode} = store
260+
let {targetPath} = store
263261
if (this.cloneWhenDrag !== true) {
264262
// remove from start position
265263
const startParentPath = hp.arrayWithoutEnd(startPath, 1)
266264
const startParent = startTree.getNodeByPath(startParentPath)
267265
const startSiblings = startParentPath.length === 0 ? startTree.treeData : startParent.children
268266
const startIndex = hp.arrayLast(startPath)
269267
startSiblings.splice(startIndex, 1)
270-
// update targetPath
271-
if (startTree === targetTree) {
272-
if (startPath.length <= targetPath.length) {
273-
const lenNoEnd = startPath.length - 1
274-
let same = true
275-
for (let i = 0; i < lenNoEnd; i++) {
276-
const s = startPath[i]
277-
const t = targetPath[i]
278-
if (s !== t) {
279-
same = false
280-
break
281-
}
282-
}
283-
if (same) {
284-
const endIndex = startPath.length - 1
285-
if (startPath[endIndex] < targetPath[endIndex]) {
286-
targetPath[endIndex] -= 1
287-
}
288-
}
289-
}
268+
// update targetPath if isDownwardsSameLevelMove
269+
if (store.isDownwardsSameLevelMove) {
270+
targetPath = targetPath.slice(0)
271+
const endIndex = startPath.length - 1
272+
targetPath[endIndex] -= 1
290273
}
291274
}
292275
// insert to target position

src/plugins/draggable/draggable.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ export default function makeTreeDraggable(treeEl, options = {}) {
466466
}
467467
//
468468
store.targetPath = options.getPathByBranchEl(placeholder)
469+
store.isDownwardsSameLevelMove = isDownwardsSameLevelMove()
469470
let pathChanged = isPathChanged()
470471
store.targetPathNotEqualToStartPath = pathChanged
471472
store.pathChangePrevented = false
@@ -495,9 +496,16 @@ export default function makeTreeDraggable(treeEl, options = {}) {
495496
}
496497
//
497498
function isPathChanged() {
498-
const {startTree, targetTree, startPath, targetPath} = store
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+
}
499503
return startTree !== targetTree || startPath.toString() !== targetPath.toString()
500504
}
505+
function isDownwardsSameLevelMove() {
506+
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)
508+
}
501509
},
502510
})
503511
return {destroy, options, optionsUpdated}

0 commit comments

Comments
 (0)