From 77e048187b9e2e0370bc67b3b25281a5656277d3 Mon Sep 17 00:00:00 2001 From: Owen Mills Date: Sun, 2 May 2021 11:14:22 -0400 Subject: [PATCH] #1942: Check if ghost is first --- src/Sortable.js | 27 ++++++++++++++++++++++++++- src/utils.js | 4 ++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Sortable.js b/src/Sortable.js index 9cf665dbd..219a64300 100644 --- a/src/Sortable.js +++ b/src/Sortable.js @@ -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; } @@ -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, @@ -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)); diff --git a/src/utils.js b/src/utils.js index 09a3ce02e..cdb8cc774 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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; @@ -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) {