Skip to content

Commit 38e4d87

Browse files
committed
#2339: Use child containing rect for ghostIsLast & ghostIsFirst
1 parent 1b7575f commit 38e4d87

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/Sortable.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
scrollBy,
3636
clone,
3737
expando,
38-
getContentRect
38+
getChildContainingRectFromElement
3939
} from './utils.js';
4040

4141

@@ -1790,22 +1790,22 @@ function _unsilent() {
17901790

17911791
function _ghostIsFirst(evt, vertical, sortable) {
17921792
let firstElRect = getRect(getChild(sortable.el, 0, sortable.options, true));
1793-
const sortableContentRect = getContentRect(sortable.el);
1793+
const childContainingRect = getChildContainingRectFromElement(sortable.el, sortable.options, ghostEl);
17941794
const spacer = 10;
17951795

17961796
return vertical ?
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)
1797+
(evt.clientX < childContainingRect.left - spacer || evt.clientY < firstElRect.top && evt.clientX < firstElRect.right) :
1798+
(evt.clientY < childContainingRect.top - spacer || evt.clientY < firstElRect.bottom && evt.clientX < firstElRect.left)
17991799
}
18001800

18011801
function _ghostIsLast(evt, vertical, sortable) {
18021802
const lastElRect = getRect(lastChild(sortable.el, sortable.options.draggable));
1803-
const sortableContentRect = getContentRect(sortable.el);
1803+
const childContainingRect = getChildContainingRectFromElement(sortable.el, sortable.options, ghostEl);
18041804
const spacer = 10;
18051805

18061806
return vertical ?
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);
1807+
(evt.clientX > childContainingRect.right + spacer || evt.clientY > lastElRect.bottom && evt.clientX > lastElRect.left) :
1808+
(evt.clientY > childContainingRect.bottom + spacer || evt.clientX > lastElRect.right && evt.clientY > lastElRect.top);
18091809
}
18101810

18111811
function _getSwapDirection(evt, target, targetRect, vertical, swapThreshold, invertedSwapThreshold, invertSwap, isLastTarget) {
@@ -1944,23 +1944,23 @@ if (documentExists) {
19441944

19451945
// Export utils
19461946
Sortable.utils = {
1947-
on: on,
1948-
off: off,
1949-
css: css,
1950-
find: find,
1947+
on,
1948+
off,
1949+
css,
1950+
find,
19511951
is: function (el, selector) {
19521952
return !!closest(el, selector, el, false);
19531953
},
1954-
extend: extend,
1955-
throttle: throttle,
1956-
closest: closest,
1957-
toggleClass: toggleClass,
1958-
clone: clone,
1959-
index: index,
1954+
extend,
1955+
throttle,
1956+
closest,
1957+
toggleClass,
1958+
clone,
1959+
index,
19601960
nextTick: _nextTick,
19611961
cancelNextTick: _cancelNextTick,
19621962
detectDirection: _detectDirection,
1963-
getChild: getChild
1963+
getChild
19641964
};
19651965

19661966

src/utils.js

+18
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,23 @@ function unsetRect(el) {
541541
css(el, 'height', '');
542542
}
543543

544+
function getChildContainingRectFromElement(container, options, ghostEl) {
545+
const rect = {};
546+
547+
Array.from(container.children).forEach(child => {
548+
if (!closest(child, options.draggable, container, false) || child.animated || child === ghostEl) return;
549+
const childRect = getRect(child);
550+
rect.left = Math.min(rect.left ?? Infinity, childRect.left);
551+
rect.top = Math.min(rect.top ?? Infinity, childRect.top);
552+
rect.right = Math.max(rect.right ?? -Infinity, childRect.right);
553+
rect.bottom = Math.max(rect.bottom ?? -Infinity, childRect.bottom);
554+
});
555+
rect.width = rect.right - rect.left;
556+
rect.height = rect.bottom - rect.top;
557+
rect.x = rect.left;
558+
rect.y = rect.top;
559+
return rect;
560+
}
544561

545562
const expando = 'Sortable' + (new Date).getTime();
546563

@@ -573,5 +590,6 @@ export {
573590
setRect,
574591
unsetRect,
575592
getContentRect,
593+
getChildContainingRectFromElement,
576594
expando
577595
};

0 commit comments

Comments
 (0)