Skip to content

fix: fixed 'childNodes' index error #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/useDraggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export function useDraggable<T>(
*/
export function useDraggable<T>(...args: any[]): UseDraggableReturn {
const vm = getCurrentInstance()?.proxy

let currentNodes: Node[] | null = null
const el = args[0]
let [, list, options] = args

Expand All @@ -153,10 +153,12 @@ export function useDraggable<T>(...args: any[]): UseDraggableReturn {
* @param {DraggableEvent} evt - DraggableEvent
*/
function onStart(evt: DraggableEvent) {
const data = unref(unref(list)?.[evt.oldIndex!])
const { from, oldIndex, item } = evt
currentNodes = Array.from(from.childNodes)
const data = unref(unref(list)?.[oldIndex!])
const clonedData = clone(data)
setCurrentData(data, clonedData)
evt.item[CLONE_ELEMENT_KEY] = clonedData
item[CLONE_ELEMENT_KEY] = clonedData
}

/**
Expand Down Expand Up @@ -214,9 +216,32 @@ export function useDraggable<T>(...args: any[]): UseDraggableReturn {
moveArrayElement(unref(list), oldIndex!, newIndex!)
}

function onEnd() {
function onEnd(e: DraggableEvent) {
const { newIndex, oldIndex, from, to } = e
let error: Error | null = null
const isSameIndex = newIndex === oldIndex && from === to
try {
//region #202
if (isSameIndex) {
let oldNode: Node | null = null
currentNodes?.some((node, index) => {
if (oldNode && currentNodes?.length !== to.childNodes.length) {
from.insertBefore(oldNode, node.nextSibling)
return true
}
const _node = to.childNodes[index]
oldNode = to?.replaceChild(node, _node)
})
}
//endregion
} catch (e) {
error = e
} finally {
currentNodes = null
}
nextTick(() => {
setCurrentData()
if (error) throw error
})
}

Expand Down