Skip to content

Commit 0ba780e

Browse files
committed
Fix ghost getting bigger and bigger
1 parent 0a22000 commit 0ba780e

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/eventHandlers/mousemove.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { VNode } from '@cycle/dom';
22
import { select } from 'snabbdom-selector';
33
import { EventHandler, MouseOffset, ItemDimensions, Intersection } from '../definitions';
44

5-
import { getGhostStyle, findParent, getIntersection, getArea, addAttributes, replaceNode } from '../helpers';
5+
import { updateGhostStyle, findParent, getIntersection, getArea, addAttributes, replaceNode } from '../helpers';
66

77
/**
88
* Used to adjust the position of the ghost and swap the items if needed
@@ -34,7 +34,7 @@ export const mousemoveHandler : EventHandler = (node, event, options) => {
3434
(-itemIntersection > maxArea - itemArea ? maxIntersection[1] : itemIndex);
3535

3636
const ghostAttrs : { [attr : string]: string } = {
37-
'style': getGhostStyle(event, mouseOffset, ghost.elm as Element),
37+
'style': updateGhostStyle(event, mouseOffset, ghost.elm as Element),
3838
'data-itemindex': newIndex.toString()
3939
};
4040

src/helpers.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function applyDefaults(options : SortableOptions, root : VNode) : Sortabl
3131
* @param {key} can be used to specify a inital key
3232
* @return {VNode} a new VNode with the keys
3333
*/
34-
export function addKeys(node : VNode, key : string = 'key') : VNode
34+
export function addKeys(node : VNode, key : string = Math.random().toString()) : VNode
3535
{
3636
if (!node.children) {
3737
return { ...node, key };
@@ -102,11 +102,22 @@ export function replaceNode(root : VNode, selector : string, replacement : VNode
102102
export function getGhostStyle(event : MouseEvent, mouseOffset : MouseOffset, item : Element) : string
103103
{
104104
const itemRect : ClientRect = item.getBoundingClientRect();
105-
const body : Element = findParent(item, 'body');
106105
return 'z-index: 5; margin: 0; pointer-events: none; position: absolute; width: '
107106
+ itemRect.width + 'px; ' + 'height: ' + itemRect.height + 'px; top: '
108-
+ (event.clientY + mouseOffset.y + body.scrollTop) + 'px; left: '
109-
+ (event.clientX + mouseOffset.x + body.scrollLeft) + 'px;';
107+
+ (event.clientY + mouseOffset.y + document.body.scrollTop) + 'px; left: '
108+
+ (event.clientX + mouseOffset.x + document.body.scrollLeft) + 'px;';
109+
}
110+
111+
/**
112+
* Returns the updated style for this ghost element
113+
*/
114+
export function updateGhostStyle(event : MouseEvent, mouseOffset : MouseOffset, ghost : Element) : string
115+
{
116+
const prevStyle : string = ghost.getAttribute('style');
117+
118+
return prevStyle.substring(0, prevStyle.indexOf(' top:')) + ' top: '
119+
+ (event.clientY + mouseOffset.y + document.body.scrollTop) + 'px; left: '
120+
+ (event.clientX + mouseOffset.x + document.body.scrollLeft) + 'px;';
110121
}
111122

112123
/**

0 commit comments

Comments
 (0)