Skip to content

Commit 6f3d2e2

Browse files
committed
swap items of different width if they are the same row/height
1 parent 5c44e11 commit 6f3d2e2

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Change log
6363
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
6464

6565
## 4.2.7-dev (TBD)
66+
* you can now swap items of different width if they are the same row/height. Thanks to [spektrummedia](http://spektrummedia.com) for sponsoring it.
6667
* fix [#1860](https://github.com/gridstack/gridstack.js/issues/1860) nested grid save inf loop fix
6768
* use latest `dart-sass`, updated comments
6869

spec/e2e/html/141_1534_swap.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ <h1>Swap collision demo</h1>
3737
let maxButton = document.getElementById('max');
3838
let bigButton = document.getElementById('big');
3939
let size = 1;
40-
let layout = 3;
40+
let layout = 5;
4141

4242
let opt = {
4343
float: false,
@@ -66,6 +66,10 @@ <h1>Swap collision demo</h1>
6666
// {x:0, y:0, w:1, h:1, content:'drag down past 2'}, {x:1, y:0, w:2, h:1}, {x:0, y:3, w:2, h:1}, {x:0, y:5, w:1, h:1}, // BETTER but quick flicker to end
6767
], [ // load 4
6868
{x:0, y:0, w:6, h:2, content:'drag down past 2, re-orders 3'}, {x:6, y:0, w:6, h:2}, {x:0, y:3, w:8, h:1}, {x:0, y:5, w:4, h:1},
69+
],[ // load 5 swap different size
70+
{x:0, y:0}, {x:1, y:0, w: 2}, {x:3, y:0, w: 2},
71+
{x:0, y:1}, {x:1, y:1}, {x:2, y:1, w: 2}, {x:4, y:1},
72+
{x:1, y:2, h: 2}, {x:1, y:4}
6973
],[ // web1.html demo
7074
{x:0, y:0, w:4, h:2},
7175
{x:4, y:0, w:4, h:4, id: 'big'},

src/gridstack-engine.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ export class GridStackEngine {
201201
b.x = a.x; b.y = a.y; // b -> a position
202202
if (a.h != b.h) {
203203
a.x = x; a.y = b.y + b.h; // a -> goes after b
204+
} else if (a.w != b.w) {
205+
a.x = b.x + b.w; a.y = y; // a -> goes after b
204206
} else {
205207
a.x = x; a.y = y; // a -> old b position
206208
}
@@ -212,19 +214,20 @@ export class GridStackEngine {
212214
// same size and same row or column, and touching
213215
if (a.w === b.w && a.h === b.h && (a.x === b.x || a.y === b.y) && (touching = Utils.isTouching(a, b)))
214216
return _doSwap();
215-
if (touching === false) return; // ran test and fail, bail out
217+
if (touching === false) return; // IFF ran test and fail, bail out
216218

217219
// check for taking same columns (but different height) and touching
218-
if (a.w === b.w && a.x === b.x && (touching || Utils.isTouching(a, b))) {
220+
if (a.w === b.w && a.x === b.x && (touching || (touching = Utils.isTouching(a, b)))) {
219221
if (b.y < a.y) { let t = a; a = b; b = t; } // swap a <-> b vars so a is first
220222
return _doSwap();
221223
}
224+
if (touching === false) return;
222225

223-
/* different X will be weird (expect vertical swap) and different height overlap, so too complex. user regular layout instead
224-
// else check if swapping would not collide with anything else (requiring a re-layout)
225-
if (!this.collide(a, {x: a.x, y: a.y, w: b.w, h: b.h}, b) &&
226-
!this.collide(a, {x: b.x, y: b.y, w: a.w, h: a.h}, b))
227-
return _doSwap(); */
226+
// check if taking same row (but different width) and touching
227+
if (a.h === b.h && a.y === b.y && (touching || (touching = Utils.isTouching(a, b)))) {
228+
if (b.x < a.x) { let t = a; a = b; b = t; } // swap a <-> b vars so a is first
229+
return _doSwap();
230+
}
228231
return false;
229232
}
230233

0 commit comments

Comments
 (0)