Skip to content

Commit

Permalink
fix(draggable plugin): wrong result when move downwards in same level
Browse files Browse the repository at this point in the history
  • Loading branch information
phphe committed Apr 12, 2021
1 parent 2dcb413 commit 9124ec1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
31 changes: 7 additions & 24 deletions src/plugins/draggable/Draggable_vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ export default {
let index = 0
for (const {value: el, index: index2} of hp.iterateAll(branchEl.parentElement.children)) {
if (hp.hasClass(el, 'tree-branch') || hp.hasClass(el, 'tree-placeholder')) {
if (el === store.dragBranchEl) {
continue
}
if (el === branchEl) {
break
}
Expand Down Expand Up @@ -259,34 +256,20 @@ export default {
},
afterDrop: (store, t) => {
if (store.pathChanged) {
const {startTree, targetTree, startPath, targetPath, dragNode} = store
const {startTree, targetTree, startPath, dragNode} = store
let {targetPath} = store
if (this.cloneWhenDrag !== true) {
// remove from start position
const startParentPath = hp.arrayWithoutEnd(startPath, 1)
const startParent = startTree.getNodeByPath(startParentPath)
const startSiblings = startParentPath.length === 0 ? startTree.treeData : startParent.children
const startIndex = hp.arrayLast(startPath)
startSiblings.splice(startIndex, 1)
// update targetPath
if (startTree === targetTree) {
if (startPath.length <= targetPath.length) {
const lenNoEnd = startPath.length - 1
let same = true
for (let i = 0; i < lenNoEnd; i++) {
const s = startPath[i]
const t = targetPath[i]
if (s !== t) {
same = false
break
}
}
if (same) {
const endIndex = startPath.length - 1
if (startPath[endIndex] < targetPath[endIndex]) {
targetPath[endIndex] -= 1
}
}
}
// update targetPath if isDownwardsSameLevelMove
if (store.isDownwardsSameLevelMove) {
targetPath = targetPath.slice(0)
const endIndex = startPath.length - 1
targetPath[endIndex] -= 1
}
}
// insert to target position
Expand Down
10 changes: 9 additions & 1 deletion src/plugins/draggable/draggable.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ export default function makeTreeDraggable(treeEl, options = {}) {
}
//
store.targetPath = options.getPathByBranchEl(placeholder)
store.isDownwardsSameLevelMove = isDownwardsSameLevelMove()
let pathChanged = isPathChanged()
store.targetPathNotEqualToStartPath = pathChanged
store.pathChangePrevented = false
Expand Down Expand Up @@ -495,9 +496,16 @@ export default function makeTreeDraggable(treeEl, options = {}) {
}
//
function isPathChanged() {
const {startTree, targetTree, startPath, targetPath} = store
const {startTree, targetTree, startPath, targetPath, isDownwardsSameLevelMove} = store
if (isDownwardsSameLevelMove) {
return hp.arrayLast(startPath) < hp.arrayLast(targetPath) - 1 // if equal, not moved
}
return startTree !== targetTree || startPath.toString() !== targetPath.toString()
}
function isDownwardsSameLevelMove() {
const {startTree, targetTree, startPath, targetPath} = store
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)
}
},
})
return {destroy, options, optionsUpdated}
Expand Down

0 comments on commit 9124ec1

Please sign in to comment.