Skip to content

Commit 70bf738

Browse files
committed
SortableJS#1686: Use parent rect in ghostIsLast & ghostIsFirst
1 parent c5a8822 commit 70bf738

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/Sortable.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ import {
3434
throttle,
3535
scrollBy,
3636
clone,
37-
expando
37+
expando,
38+
getContentRect
3839
} from './utils.js';
3940

4041

@@ -1788,21 +1789,23 @@ function _unsilent() {
17881789
}
17891790

17901791
function _ghostIsFirst(evt, vertical, sortable) {
1791-
let rect = getRect(getChild(sortable.el, 0, sortable.options, true));
1792+
let firstElRect = getRect(getChild(sortable.el, 0, sortable.options, true));
1793+
const sortableContentRect = getContentRect(sortable.el);
17921794
const spacer = 10;
17931795

17941796
return vertical ?
1795-
((evt.clientX < rect.left - spacer) || (evt.clientY < rect.top && evt.clientX < rect.right)) :
1796-
((evt.clientY < rect.top - spacer) || (evt.clientY < rect.bottom && evt.clientX < rect.left))
1797+
(evt.clientX < sortableContentRect.left - spacer || evt.clientY < firstElRect.top && evt.clientX < firstElRect.right) :
1798+
(evt.clientY < sortableContentRect.top - spacer || evt.clientY < firstElRect.bottom && evt.clientX < firstElRect.left)
17971799
}
17981800

17991801
function _ghostIsLast(evt, vertical, sortable) {
1800-
let rect = getRect(lastChild(sortable.el, sortable.options.draggable));
1802+
const lastElRect = getRect(lastChild(sortable.el, sortable.options.draggable));
1803+
const sortableContentRect = getContentRect(sortable.el);
18011804
const spacer = 10;
18021805

18031806
return vertical ?
1804-
(evt.clientX > rect.right + spacer || evt.clientX <= rect.right && evt.clientY > rect.bottom && evt.clientX >= rect.left) :
1805-
(evt.clientX > rect.right && evt.clientY > rect.top || evt.clientX <= rect.right && evt.clientY > rect.bottom + spacer);
1807+
(evt.clientX > sortableContentRect.right + spacer || evt.clientY > lastElRect.bottom && evt.clientX > lastElRect.left) :
1808+
(evt.clientY > sortableContentRect.bottom + spacer || evt.clientX > lastElRect.right && evt.clientY > lastElRect.top);
18061809
}
18071810

18081811
function _getSwapDirection(evt, target, targetRect, vertical, swapThreshold, invertedSwapThreshold, invertSwap, isLastTarget) {

src/utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,26 @@ function getRect(el, relativeToContainingBlock, relativeToNonStaticParent, undoS
254254
};
255255
}
256256

257+
/**
258+
* Returns the content rect of the element (bounding rect minus border and padding)
259+
* @param {HTMLElement} el
260+
*/
261+
function getContentRect(el) {
262+
let rect = getRect(el);
263+
const paddingLeft = parseInt(css(el, 'padding-left')),
264+
paddingTop = parseInt(css(el, 'padding-top')),
265+
paddingRight = parseInt(css(el, 'padding-right')),
266+
paddingBottom = parseInt(css(el, 'padding-bottom'));
267+
rect.top += paddingTop + parseInt(css(el, 'border-top-width'));
268+
rect.left += paddingLeft + parseInt(css(el, 'border-left-width'));
269+
// Client Width/Height includes padding only
270+
rect.width = el.clientWidth - paddingLeft - paddingRight;
271+
rect.height = el.clientHeight - paddingTop - paddingBottom;
272+
rect.bottom = rect.top + rect.height;
273+
rect.right = rect.left + rect.width;
274+
return rect;
275+
}
276+
257277
/**
258278
* Checks if a side of an element is scrolled past a side of its parents
259279
* @param {HTMLElement} el The element who's side being scrolled out of view is in question
@@ -552,5 +572,6 @@ export {
552572
clone,
553573
setRect,
554574
unsetRect,
575+
getContentRect,
555576
expando
556577
};

0 commit comments

Comments
 (0)