Skip to content

Commit

Permalink
adjust drag position with scaling when acceptwidgets is true
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed Amdoun committed Dec 15, 2023
1 parent c1e78db commit c0abba2
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/gridstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2030,16 +2030,35 @@ export class GridStack {
// vars shared across all methods
let cellHeight: number, cellWidth: number;

// creates a reference element for tracking the right position after scaling
const testEl = document.createElement('div');
Utils.addElStyles(testEl, {
opacity: '0',
position: 'fixed',
top: 0 + 'px',
left: 0 + 'px',
width: '1px',
height: '1px',
zIndex: '-999999',
});

let onDrag = (event: DragEvent, el: GridItemHTMLElement, helper: GridItemHTMLElement) => {
let node = el.gridstackNode;
if (!node) return;

helper = helper || el;
helper.appendChild(testEl);
const testElPosition = testEl.getBoundingClientRect();
helper.removeChild(testEl);
const dragScale = {
x: 1 / testElPosition.width,
y: 1 / testElPosition.height,
}
let parent = this.el.getBoundingClientRect();
let {top, left} = helper.getBoundingClientRect();
left -= parent.left;
top -= parent.top;
let ui: DDUIData = {position: {top, left}};
let ui: DDUIData = {position: {top: top * dragScale.y, left: left * dragScale.x}};

if (node._temporaryRemoved) {
node.x = Math.max(0, Math.round(left / cellWidth));
Expand Down

0 comments on commit c0abba2

Please sign in to comment.