Skip to content

Commit 8ec3dc9

Browse files
authored
Merge pull request #3241 from KokXinTan/fix/resize-position-drift
fix: preserve position during SE/S/E resize (prevent rounding drift)
2 parents f177f71 + 6d62b86 commit 8ec3dc9

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/dd-resizable.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ export class DDResizable extends DDBaseImplement implements HTMLElementExtendOpt
184184
this.temporalRect = this._getChange(event, dir);
185185
this._applyChange();
186186
const ev = Utils.initEvent<MouseEvent>(event, { type: 'resize', target: this.el });
187+
(ev as any).resizeDir = dir; // expose handle direction so _dragOrResize can avoid position drift
187188
if (this.option.resize) {
188189
this.option.resize(ev, this._ui());
189190
}

src/gridstack.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2928,11 +2928,16 @@ export class GridStack {
29282928
if (node.w === p.w && node.h === p.h) return;
29292929
if (node._lastTried && node._lastTried.w === p.w && node._lastTried.h === p.h) return; // skip one we tried (but failed)
29302930

2931-
// if we size on left/top side this might move us, so get possible new position as well
2932-
const left = ui.position.left + mLeft;
2933-
const top = ui.position.top + mTop;
2934-
p.x = Math.round(left / cellWidth);
2935-
p.y = Math.round(top / cellHeight);
2931+
// only recalculate position for handles that move the top-left corner (N/W).
2932+
// for SE/S/E handles the top-left is anchored — recalculating from pixels causes
2933+
// rounding drift on fine grids where cellWidth/cellHeight are only a few pixels. #385 #1356
2934+
const dir = ((event as any).resizeDir || '') as string;
2935+
if (dir.includes('w') || dir.includes('n')) {
2936+
const left = ui.position.left + mLeft;
2937+
const top = ui.position.top + mTop;
2938+
if (dir.includes('w')) p.x = Math.round(left / cellWidth);
2939+
if (dir.includes('n')) p.y = Math.round(top / cellHeight);
2940+
}
29362941

29372942
resizing = true;
29382943
}

0 commit comments

Comments
 (0)