Skip to content

Commit 630d2a6

Browse files
author
Mohamed Amdoun
committed
dragtransform only calculated on start in gridstack.ts
1 parent baf8cd0 commit 630d2a6

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed

src/dd-resizable.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ export class DDResizable extends DDBaseImplement implements HTMLElementExtendOpt
226226
this.parentOriginStylePosition = this.el.parentElement.style.position;
227227

228228
const parent = this.el.parentElement;
229-
const transformValues = Utils.getValuesFromTransformedElement(parent);
229+
const dragTransform = Utils.getValuesFromTransformedElement(parent);
230230
this.rectScale = {
231-
x: transformValues.xScale,
232-
y: transformValues.yScale
231+
x: dragTransform.xScale,
232+
y: dragTransform.yScale
233233
};
234234

235235
if (getComputedStyle(this.el.parentElement).position.match(/static/)) {

src/gridstack.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ export class GridStack {
259259
protected _extraDragRow = 0;
260260
/** @internal true if nested grid should get column count from our width */
261261
protected _autoColumn?: boolean;
262+
/** @internal meant to store the scale of the active grid */
263+
protected dragTransform: DragTransform = { xScale: 1, yScale: 1, xOffset: 0, yOffset: 0 };
262264
private _skipInitialResize: boolean;
263265

264266
/**
@@ -2035,41 +2037,27 @@ export class GridStack {
20352037
if (!node) return;
20362038

20372039
helper = helper || el;
2038-
let transformValues: DragTransform;
2039-
// if we are dragging an element in and out that is coming from a grid
2040-
// we get the transform values by using the helper attached to the grid
2041-
if (node.grid?.el) {
2042-
transformValues = Utils.getValuesFromTransformedElement(helper)
2043-
}
2044-
// if the element is being dragged from outside (not from any grid)
2045-
// we use the grid as the transformation reference, since the helper is not subject to transformation
2046-
else if (this._placeholder && this._placeholder.closest('.grid-stack')) {
2047-
const gridEl = this._placeholder.closest('.grid-stack') as HTMLElement;
2048-
transformValues = Utils.getValuesFromTransformedElement(gridEl);
2049-
// if the element is being dragged from outside, scale it down to match the grid's scale
2050-
helper.style.transform = `scale(${1 / transformValues.xScale},${1 / transformValues.yScale})`;
2040+
2041+
// if the element is being dragged from outside, scale it down to match the grid's scale
2042+
// and slightly adjust its position relative to the mouse
2043+
if (!node.grid?.el) {
2044+
// this scales the helper down
2045+
helper.style.transform = `scale(${1 / this.dragTransform.xScale},${1 / this.dragTransform.yScale})`;
20512046
// this makes it so that the helper is well positioned relative to the mouse after scaling
20522047
const helperRect = helper.getBoundingClientRect();
2053-
helper.style.left = helperRect.x + (transformValues.xScale - 1) * (event.clientX - helperRect.x) / transformValues.xScale + 'px';
2054-
helper.style.top = helperRect.y + (transformValues.yScale - 1) * (event.clientY - helperRect.y) / transformValues.yScale + 'px';
2048+
helper.style.left = helperRect.x + (this.dragTransform.xScale - 1) * (event.clientX - helperRect.x) / this.dragTransform.xScale + 'px';
2049+
helper.style.top = helperRect.y + (this.dragTransform.yScale - 1) * (event.clientY - helperRect.y) / this.dragTransform.yScale + 'px';
20552050
helper.style.transformOrigin = `0px 0px`
2056-
} // if all else fails, we might want to use the default transform value
2057-
else {
2058-
transformValues = {
2059-
xScale: 1,
2060-
xOffset: 0,
2061-
yScale: 1,
2062-
yOffset: 0,
2063-
}
20642051
}
2052+
20652053
let parent = this.el.getBoundingClientRect();
20662054
let {top, left} = helper.getBoundingClientRect();
20672055
left -= parent.left;
20682056
top -= parent.top;
20692057
let ui: DDUIData = {
20702058
position: {
2071-
top: top * transformValues.xScale,
2072-
left: left * transformValues.yScale
2059+
top: top * this.dragTransform.xScale,
2060+
left: left * this.dragTransform.yScale
20732061
}
20742062
};
20752063

@@ -2427,6 +2415,27 @@ export class GridStack {
24272415
this.el.appendChild(this.placeholder);
24282416
// console.log('_onStartMoving placeholder') // TEST
24292417

2418+
// if the element is inside a grid, it has already been scaled
2419+
// we can use that as a scale reference
2420+
if (node.grid?.el) {
2421+
this.dragTransform = Utils.getValuesFromTransformedElement(el);
2422+
}
2423+
// if the element is being dragged from outside (not from any grid)
2424+
// we use the grid as the transformation reference, since the helper is not subject to transformation
2425+
else if (this.placeholder && this.placeholder.closest('.grid-stack')) {
2426+
const gridEl = this.placeholder.closest('.grid-stack') as HTMLElement;
2427+
this.dragTransform = Utils.getValuesFromTransformedElement(gridEl);
2428+
}
2429+
// Fallback
2430+
else {
2431+
this.dragTransform = {
2432+
xScale: 1,
2433+
xOffset: 0,
2434+
yScale: 1,
2435+
yOffset: 0,
2436+
}
2437+
}
2438+
24302439
node.el = this.placeholder;
24312440
node._lastUiPosition = ui.position;
24322441
node._prevYPix = ui.position.top;

0 commit comments

Comments
 (0)