Skip to content

Commit 21ab175

Browse files
committed
collision: Resizable: {handles:'w/sw'} fix
fix #1679 * 4.x broke the left side size handle (as it needs to move item as well as resize) now works again and should be more solid too.
1 parent 33db0cf commit 21ab175

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Change log
5252
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
5353
## 4.0.1-dev
5454

55+
- fix [#1679](https://github.com/gridstack/gridstack.js/issues/1679) `Resizable: {handles:'w/sw'}` work again in 4.x
5556
- fix [#1658](https://github.com/gridstack/gridstack.js/issues/1658) `enableMove(T/F)` not working correctly
5657
- fix `helper: myFunction` now working for H5 case for `dragInOptions` & `setupDragIn()`
5758
## 4.0.1 (2021-3-20)

spec/e2e/html/141_1534_swap.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ <h1>Swap collision demo</h1>
3737
let size = 1;
3838
let layout = 0;
3939

40-
let grid = GridStack.init({float: false, cellHeight: 70, maxRow: 0});
40+
let grid = GridStack.init({float: false, cellHeight: 70, maxRow: 0, resizable: {handles: 'sw,w,e,se'}});
4141
addEvents(grid);
4242

4343
let items = [[ // load 0

src/gridstack-dd.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -532,22 +532,23 @@ GridStack.prototype._leave = function(node: GridStackNode, el: GridItemHTMLEleme
532532

533533
/** @internal called when item is being dragged/resized */
534534
GridStack.prototype._dragOrResize = function(el: GridItemHTMLElement, event: Event, ui: DDUIData, node: GridStackNode, cellWidth: number, cellHeight: number) {
535-
// calculate the place where we're landing by offsetting margin so actual edge crosses mid point
536-
let left = ui.position.left + (ui.position.left > node._lastUiPosition.left ? -this.opts.marginRight : this.opts.marginLeft);
537-
let top = ui.position.top + (ui.position.top > node._lastUiPosition.top ? -this.opts.marginBottom : this.opts.marginTop);
538-
let x = Math.round(left / cellWidth);
539-
let y = Math.round(top / cellHeight);
540-
let w = node.w;
541-
let h = node.h;
535+
let p = {...node._orig};
542536
let resizing: boolean;
543537

544538
if (event.type === 'drag') {
545539
if (node._isCursorOutside) return; // handled by dropover
546540
let distance = ui.position.top - node._prevYPix;
547541
node._prevYPix = ui.position.top;
548542
Utils.updateScrollPosition(el, ui.position, distance);
543+
544+
// get new position taking into account the margin in the direction we are moving! (need to pass mid point by margin)
545+
let left = ui.position.left + (ui.position.left > node._lastUiPosition.left ? -this.opts.marginRight : this.opts.marginLeft);
546+
let top = ui.position.top + (ui.position.top > node._lastUiPosition.top ? -this.opts.marginBottom : this.opts.marginTop);
547+
p.x = Math.round(left / cellWidth);
548+
p.y = Math.round(top / cellHeight);
549+
549550
// if inTrash or outside of the bounds (but not external which is handled by 'dropout' event), temporarily remove it from us
550-
if (node._isAboutToRemove || (!node._isExternal && this.engine.isOutside(x, y, node))) {
551+
if (node._isAboutToRemove || (!node._isExternal && this.engine.isOutside(p.x, p.y, node))) {
551552
this._leave(node, event.target);
552553
} else {
553554
if (node._temporaryRemoved) {
@@ -558,28 +559,35 @@ GridStack.prototype._dragOrResize = function(el: GridItemHTMLElement, event: Eve
558559
delete node._temporaryRemoved;
559560
}
560561
}
561-
if (node.x === x && node.y === y) return; // skip same
562+
if (node.x === p.x && node.y === p.y) return; // skip same
562563
// DON'T skip one we tried as we might have failed because of coverage <50% before
563564
// if (node._lastTried && node._lastTried.x === x && node._lastTried.y === y) return;
564565
} else if (event.type === 'resize') {
565-
if (x < 0) return;
566+
if (p.x < 0) return;
566567
// Scrolling page if needed
567568
Utils.updateScrollResize(event as MouseEvent, el, cellHeight);
568-
w = Math.round(ui.size.width / cellWidth);
569-
h = Math.round(ui.size.height / cellHeight);
570-
if (node.w === w && node.h === h) return;
571-
if (node._lastTried && node._lastTried.w === w && node._lastTried.h === h) return; // skip one we tried (but failed)
569+
570+
// get new size
571+
p.w = Math.round((ui.size.width - this.opts.marginLeft) / cellWidth);
572+
p.h = Math.round((ui.size.height - this.opts.marginTop) / cellHeight);
573+
if (node.w === p.w && node.h === p.h) return;
574+
if (node._lastTried && node._lastTried.w === p.w && node._lastTried.h === p.h) return; // skip one we tried (but failed)
575+
576+
// if we changed sizing on left side, move the item as well. Note: we don't support TOP resizing
577+
if (Math.round(ui.position.left) < Math.round(node._orig.x * cellWidth)) { // use round or we can get slightly off compare
578+
p.x = node._orig.x + node._orig.w - p.w;
579+
}
572580
resizing = true;
573581
}
574582

575-
node._lastTried = {x, y, w, h}; // set as last tried (will nuke if we go there)
583+
node._lastTried = p; // set as last tried (will nuke if we go there)
576584
let rect: GridStackPosition = { // screen pix of the dragged box
577585
x: ui.position.left + this.opts.marginLeft,
578586
y: ui.position.top + this.opts.marginTop,
579587
w: (ui.size ? ui.size.width : node.w * cellWidth) - this.opts.marginLeft - this.opts.marginRight,
580588
h: (ui.size ? ui.size.height : node.h * cellHeight) - this.opts.marginTop - this.opts.marginBottom
581589
};
582-
if (this.engine.moveNodeCheck(node, {x, y, w, h, cellWidth, cellHeight, rect})) {
590+
if (this.engine.moveNodeCheck(node, {...p, cellWidth, cellHeight, rect})) {
583591
node._lastUiPosition = ui.position;
584592
this.engine.cacheRects(cellWidth, cellHeight, this.opts.marginTop, this.opts.marginRight, this.opts.marginBottom, this.opts.marginLeft);
585593
delete node._skipDown;

0 commit comments

Comments
 (0)