Skip to content

Commit

Permalink
fix: wrong offset in ondrag for dropPosition calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
v1r0x committed Jul 12, 2018
1 parent fae420b commit 68b0ec9
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ export function getNodeFromPath<T>(rootData: TreeData<T>[], path: number[]) {
return node
}

function getGlobalOffset(dropTarget: HTMLElement) {
let offset = 0
let currentElem = dropTarget
while (currentElem) {
offset += currentElem.offsetTop
currentElem = currentElem.offsetParent
}
return offset
}

function getDropPosition(pageY: number, offsetTop: number, offsetHeight: number) {
const top = pageY - offsetTop
if (top < offsetHeight / 3) {
Expand Down Expand Up @@ -252,7 +262,8 @@ export function ondrag<T>(pageY: number, dragTarget: HTMLElement | null, dropTar
const targetPath = dropTargetPathString.split(',').map(s => +s)
const targetData = getNodeFromPath(data, targetPath)!
const sourceData = getNodeFromPath(data, sourcePath)!
const position = getDropPosition(pageY, dropTarget.offsetTop, dropTarget.offsetHeight)
const offsetTop = getGlobalOffset(dropTarget)
const position = getDropPosition(pageY, offsetTop, dropTarget.offsetHeight)
if (targetData.state.dropPosition !== position) {
targetData.state.dropPosition = position
const dropData: DropData<T> = {
Expand Down

0 comments on commit 68b0ec9

Please sign in to comment.