Skip to content

Commit 02bb690

Browse files
committed
collsion: restored place across
fixed original #1094 again (moving 6 between 1 and 8 in web1.html)
1 parent 14ad7b8 commit 02bb690

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

spec/e2e/html/141_1534_swap.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ <h1>Swap collision demo</h1>
7979
};
8080

8181
load = function(i) {
82+
layout = i;
8283
grid.removeAll();
8384
grid.load(items[i]);
8485
}
@@ -103,9 +104,9 @@ <h1>Swap collision demo</h1>
103104
setSize(size);
104105
}
105106
setSize = function(size) {
106-
items.sort((a,b) => a.id - b.id);
107-
items.forEach((n,i) => {
108-
if (i<6) {
107+
items[layout].sort((a,b) => a.id - b.id);
108+
items[layout].forEach((n,i) => {
109+
if (layout === 0 && i<6) {
109110
n.w = n.h = size;
110111
n.y = i * size;
111112
if (n.x) n.x = size;
@@ -114,7 +115,7 @@ <h1>Swap collision demo</h1>
114115
}
115116
});
116117
grid.opts.maxRow = grid.engine.maxRow = grid.opts.maxRow ? (size === 1 ? 3 : 6) : 0;
117-
grid.load(items);
118+
grid.load(items[layout]);
118119
bigButton.innerHTML = 'Size: ' + size;
119120
maxButton.innerHTML = 'Max: ' + grid.opts.maxRow;
120121
}

src/gridstack-engine.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,19 @@ export class GridStackEngine {
9191
// (not handled with swap) that could take our place, move ourself past it instead
9292
// but remember that skip down so we only do this one (and push others otherwise).
9393
if (collide.locked || (node._moving && !node._skipDown && nn.y > node.y &&
94-
!this.float && !this.collide(collide, {...collide, y: node.y}, node)) &&
95-
Utils.isIntercepted(collide, {x: node.x-0.5, y: node.y-0.5, w: node.w+1, h: node.h+1})) {
94+
!this.float && !this.collide(collide, {...collide, y: node.y}, node)) && Utils.isTouching(node, collide)) {
9695

9796
node._skipDown = (node._skipDown || nn.y > node.y);
9897
moved = this.moveNode(node, {...nn, y: collide.y + collide.h, ...newOpt});
9998
if (collide.locked && moved) {
10099
Utils.copyPos(nn, node); // moving after lock become our new desired location
101100
} else if (moved && opt.pack && !collide.locked) {
102-
// we moved after and will pack: do it now and keep the original drop location to see what else we might push way
101+
// we moved after and will pack: do it now and keep the original drop location, but past the old collide to see what else we might push way
103102
this._packNodes();
103+
if (moved) {
104+
nn.y = collide.y + collide.h;
105+
Utils.copyPos(node, nn);
106+
}
104107
}
105108
didMove = didMove || moved;
106109
} else {
@@ -197,19 +200,15 @@ export class GridStackEngine {
197200
a._dirty = b._dirty = true;
198201
return true;
199202
}
200-
// make sure they at least touch (including corners which will skip below as unwanted)
201-
function _touching(): boolean {
202-
return Utils.isIntercepted(a, {x: b.x-0.5, y:b.y-0.5, w: b.w+1, h: b.h+1})
203-
}
204203
let touching: boolean; // remember if we called it (vs undefined)
205204

206205
// same size and same row or column, and touching
207-
if (a.w === b.w && a.h === b.h && (a.x === b.x || a.y === b.y) && (touching = _touching()))
206+
if (a.w === b.w && a.h === b.h && (a.x === b.x || a.y === b.y) && (touching = Utils.isTouching(a, b)))
208207
return _doSwap();
209208
if (touching === false) return; // ran test and fail, bail out
210209

211210
// check for taking same columns (but different height) and touching
212-
if (a.w === b.w && a.x === b.x && (touching || _touching())) {
211+
if (a.w === b.w && a.x === b.x && (touching || Utils.isTouching(a, b))) {
213212
if (b.y < a.y) { let t = a; a = b; b = t; } // swap a <-> b vars so a is first
214213
return _doSwap();
215214
}

src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ export class Utils {
9999
return !(a.y >= b.y + b.h || a.y + a.h <= b.y || a.x + a.w <= b.x || a.x >= b.x + b.w);
100100
}
101101

102+
/** returns true if a and b touch edges or corners */
103+
static isTouching(a: GridStackPosition, b: GridStackPosition): boolean {
104+
return Utils.isIntercepted(a, {x: b.x-0.5, y: b.y-0.5, w: b.w+1, h: b.h+1})
105+
}
102106
/**
103107
* Sorts array of nodes
104108
* @param nodes array to sort

0 commit comments

Comments
 (0)