Skip to content

Commit

Permalink
#1942: Check if ghost is first
Browse files Browse the repository at this point in the history
  • Loading branch information
owen-m1 committed May 2, 2021
1 parent a12fb2b commit 77e0481
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
27 changes: 26 additions & 1 deletion src/Sortable.js
Expand Up @@ -1175,7 +1175,7 @@ Sortable.prototype = /** @lends Sortable.prototype */ {
return completed(false);
}

// assign target only if condition is true
// if there is a last element, it is the target
if (elLastChild && el === evt.target) {
target = elLastChild;
}
Expand All @@ -1193,6 +1193,23 @@ Sortable.prototype = /** @lends Sortable.prototype */ {
return completed(true);
}
}
else if (elLastChild && _ghostIsFirst(evt, vertical, this)) {
let firstChild = getChild(el, 0, options, true);
if (firstChild === dragEl) {
return completed(false);
}
target = firstChild;
targetRect = getRect(target);

if (onMove(rootEl, el, dragEl, dragRect, target, targetRect, evt, false) !== false) {
capture();
el.insertBefore(dragEl, firstChild);
parentEl = el; // actualization

changed();
return completed(true);
}
}
else if (target.parentNode === el) {
targetRect = getRect(target);
let direction = 0,
Expand Down Expand Up @@ -1762,6 +1779,14 @@ function _unsilent() {
_silent = false;
}

function _ghostIsFirst(evt, vertical, sortable) {
let rect = getRect(getChild(sortable.el, 0, sortable.options));
const spacer = 10;

return vertical ?
((evt.clientX < rect.left - spacer) || (evt.clientY < rect.top && evt.clientX < rect.right)) :
((evt.clientY < rect.top - spacer) || (evt.clientY < rect.bottom && evt.clientX < rect.left))
}

function _ghostIsLast(evt, vertical, sortable) {
let rect = getRect(lastChild(sortable.el, sortable.options.draggable));
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Expand Up @@ -296,7 +296,7 @@ function isScrolledPast(el, elSide, parentSide) {
* @param {Object} options Parent Sortable's options
* @return {HTMLElement} The child at index childNum, or null if not found
*/
function getChild(el, childNum, options) {
function getChild(el, childNum, options, includeDragEl) {
let currentChild = 0,
i = 0,
children = el.children;
Expand All @@ -305,7 +305,7 @@ function getChild(el, childNum, options) {
if (
children[i].style.display !== 'none' &&
children[i] !== Sortable.ghost &&
children[i] !== Sortable.dragged &&
(includeDragEl || children[i] !== Sortable.dragged) &&
closest(children[i], options.draggable, el, false)
) {
if (currentChild === childNum) {
Expand Down

0 comments on commit 77e0481

Please sign in to comment.