Skip to content

Commit 77e0481

Browse files
committed
#1942: Check if ghost is first
1 parent a12fb2b commit 77e0481

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/Sortable.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ Sortable.prototype = /** @lends Sortable.prototype */ {
11751175
return completed(false);
11761176
}
11771177

1178-
// assign target only if condition is true
1178+
// if there is a last element, it is the target
11791179
if (elLastChild && el === evt.target) {
11801180
target = elLastChild;
11811181
}
@@ -1193,6 +1193,23 @@ Sortable.prototype = /** @lends Sortable.prototype */ {
11931193
return completed(true);
11941194
}
11951195
}
1196+
else if (elLastChild && _ghostIsFirst(evt, vertical, this)) {
1197+
let firstChild = getChild(el, 0, options, true);
1198+
if (firstChild === dragEl) {
1199+
return completed(false);
1200+
}
1201+
target = firstChild;
1202+
targetRect = getRect(target);
1203+
1204+
if (onMove(rootEl, el, dragEl, dragRect, target, targetRect, evt, false) !== false) {
1205+
capture();
1206+
el.insertBefore(dragEl, firstChild);
1207+
parentEl = el; // actualization
1208+
1209+
changed();
1210+
return completed(true);
1211+
}
1212+
}
11961213
else if (target.parentNode === el) {
11971214
targetRect = getRect(target);
11981215
let direction = 0,
@@ -1762,6 +1779,14 @@ function _unsilent() {
17621779
_silent = false;
17631780
}
17641781

1782+
function _ghostIsFirst(evt, vertical, sortable) {
1783+
let rect = getRect(getChild(sortable.el, 0, sortable.options));
1784+
const spacer = 10;
1785+
1786+
return vertical ?
1787+
((evt.clientX < rect.left - spacer) || (evt.clientY < rect.top && evt.clientX < rect.right)) :
1788+
((evt.clientY < rect.top - spacer) || (evt.clientY < rect.bottom && evt.clientX < rect.left))
1789+
}
17651790

17661791
function _ghostIsLast(evt, vertical, sortable) {
17671792
let rect = getRect(lastChild(sortable.el, sortable.options.draggable));

src/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ function isScrolledPast(el, elSide, parentSide) {
296296
* @param {Object} options Parent Sortable's options
297297
* @return {HTMLElement} The child at index childNum, or null if not found
298298
*/
299-
function getChild(el, childNum, options) {
299+
function getChild(el, childNum, options, includeDragEl) {
300300
let currentChild = 0,
301301
i = 0,
302302
children = el.children;
@@ -305,7 +305,7 @@ function getChild(el, childNum, options) {
305305
if (
306306
children[i].style.display !== 'none' &&
307307
children[i] !== Sortable.ghost &&
308-
children[i] !== Sortable.dragged &&
308+
(includeDragEl || children[i] !== Sortable.dragged) &&
309309
closest(children[i], options.draggable, el, false)
310310
) {
311311
if (currentChild === childNum) {

0 commit comments

Comments
 (0)