From 46d5b779aef161f39eee7a065012b534fe1a78c0 Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Fri, 5 Jul 2019 14:50:05 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20unexpected=20image=20pos?= =?UTF-8?q?itions=20when=20re-ordering=20gallery=20images?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit no issue - the position that images were inserted did not always match the position indicator because of errors in the insert index calculations --- .../addon/components/koenig-card-gallery.js | 20 ++++++------------- .../services/koenig-drag-drop-handler.js | 1 - 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/ghost/admin/lib/koenig-editor/addon/components/koenig-card-gallery.js b/ghost/admin/lib/koenig-editor/addon/components/koenig-card-gallery.js index ce1a65d254..2df8baa441 100644 --- a/ghost/admin/lib/koenig-editor/addon/components/koenig-card-gallery.js +++ b/ghost/admin/lib/koenig-editor/addon/components/koenig-card-gallery.js @@ -406,26 +406,21 @@ export default Component.extend({ return {}; }, - _onDrop(draggableInfo, droppableElem, position) { + _onDrop(draggableInfo/*, droppableElem, position*/) { // do not allow dropping of non-images if (draggableInfo.type !== 'image' && draggableInfo.cardName !== 'image') { return false; } + let {insertIndex} = draggableInfo; let droppables = Array.from(this.element.querySelectorAll('[data-image]')); let draggableIndex = droppables.indexOf(draggableInfo.element); - if (this._isDropAllowed(draggableIndex, draggableInfo.insertIndex)) { + if (this._isDropAllowed(draggableIndex, insertIndex)) { if (draggableIndex === -1) { // external image being added let {payload} = draggableInfo; let img = draggableInfo.element.querySelector(`img[src="${payload.src}"]`); - let insertIndex = draggableInfo.insertIndex; - - // insert index needs adjusting because we're not shuffling - if (position && position.match(/right/)) { - insertIndex += 1; - } // image card payloads may not have all of the details we need but we can fill them in payload.width = payload.width || img.naturalWidth; @@ -440,8 +435,9 @@ export default Component.extend({ } else { // internal image being re-ordered let draggedImage = this.images.findBy('src', draggableInfo.payload.src); + let accountForRemoval = draggableIndex < insertIndex && insertIndex ? -1 : 0; this.images.removeObject(draggedImage); - this.images.insertAt(draggableInfo.insertIndex, draggedImage); + this.images.insertAt(insertIndex + accountForRemoval, draggedImage); } this._recalculateImageRows(); @@ -514,14 +510,10 @@ export default Component.extend({ } }); - if (position.match(/right/) && draggableIndex > insertIndex) { + if (position.match(/right/)) { insertIndex += 1; } - if (insertIndex >= this.images.length - 1) { - insertIndex = this.images.length - 1; - } - return { direction: 'horizontal', position: position.match(/left/) ? 'left' : 'right', diff --git a/ghost/admin/lib/koenig-editor/addon/services/koenig-drag-drop-handler.js b/ghost/admin/lib/koenig-editor/addon/services/koenig-drag-drop-handler.js index 0dc075f22f..79c1a9debe 100644 --- a/ghost/admin/lib/koenig-editor/addon/services/koenig-drag-drop-handler.js +++ b/ghost/admin/lib/koenig-editor/addon/services/koenig-drag-drop-handler.js @@ -353,7 +353,6 @@ export default Service.extend({ this._currentOverContainer.onDragLeaveDroppable(overDroppableElem); } this._currentOverDroppableElem = null; - this._currentOverDroppablePosition = null; } if (isOverDroppable) {