Skip to content

Commit 400eb8c

Browse files
authored
Merge pull request #3462 from febug/fix/overlapping-axis-drag-coverage
fix: allow dragging past overlapping edges
2 parents cadabd8 + 5d6e8df commit 400eb8c

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

‎spec/gridstack-engine-spec.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,36 @@ describe('gridstack engine:', () => {
4747
});
4848
});
4949

50+
describe('3447 dragging past an overlapping edge >', () => {
51+
[
52+
{name: 'up', target: {x: 0, y: 0, w: 10, h: 2}, start: {x: 8, y: 2, w: 4, h: 1}, mid: {x: 8, y: 1}, end: {x: 8, y: 0}, targetY: 1},
53+
{name: 'down', target: {x: 2, y: 1, w: 10, h: 2}, start: {x: 0, y: 0, w: 4, h: 1}, mid: {x: 0, y: 1}, end: {x: 0, y: 2}, targetY: 3},
54+
{name: 'left', target: {x: 0, y: 0, w: 2, h: 10}, start: {x: 2, y: 8, w: 1, h: 4}, mid: {x: 1, y: 8}, end: {x: 0, y: 8}, targetY: 12},
55+
{name: 'right', target: {x: 1, y: 2, w: 2, h: 10}, start: {x: 0, y: 0, w: 1, h: 4}, mid: {x: 1, y: 0}, end: {x: 2, y: 0}, targetY: 4},
56+
].forEach(({name, target, start, mid, end, targetY}) => {
57+
it(`can drag ${name} when the other axis already overlaps`, () => {
58+
e = new GridStackEngine({mode: 'float', column: 12});
59+
const other = e.addNode({...target, id: 'target'});
60+
const move = e.addNode({...start, id: 'move'});
61+
e.cleanNodes().beginUpdate(move);
62+
move._moving = true;
63+
e.cacheRects(50, 50, 0, 0, 0, 0);
64+
65+
// Exactly half of the target is still too little to push it.
66+
e.moveNodeCheck(move, {...mid, rect: {x: mid.x * 50, y: mid.y * 50, w: start.w * 50, h: start.h * 50}});
67+
expect(move).toEqual(expect.objectContaining(start));
68+
expect(other).toEqual(expect.objectContaining(target));
69+
70+
e.moveNodeCheck(move, {...end, rect: {x: end.x * 50, y: end.y * 50, w: start.w * 50, h: start.h * 50}});
71+
72+
expect(move).toEqual(expect.objectContaining(end));
73+
expect(other.y).toBe(targetY);
74+
expect(overlaps(e)).toEqual([]);
75+
e.endUpdate();
76+
});
77+
});
78+
});
79+
5080
describe('test constructor >', () => {
5181

5282
it('should be setup properly', () => {

‎src/gridstack-engine.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,16 @@ export class GridStackEngine {
239239
}
240240
const r2 = n._rect; // overlapping target
241241
let yOver = Number.MAX_VALUE, xOver = Number.MAX_VALUE;
242-
// depending on which side we started from, compute the overlap % of coverage
243-
// (ex: from above/below we only compute the max horizontal line coverage)
244-
if (r0.y! < r2.y!) { // from above
242+
// Only initially separated axes determine the entry direction. An already-overlapping
243+
// axis must not prevent crossing the target midpoint along the other axis.
244+
if (r0.y! + r0.h! <= r2.y!) { // from above
245245
yOver = ((r.y! + r.h!) - r2.y!) / r2.h!;
246-
} else if (r0.y! + r0.h! > r2.y! + r2.h!) { // from below
246+
} else if (r0.y! >= r2.y! + r2.h!) { // from below
247247
yOver = ((r2.y! + r2.h!) - r.y!) / r2.h!;
248248
}
249-
if (r0.x! < r2.x!) { // from the left
249+
if (r0.x! + r0.w! <= r2.x!) { // from the left
250250
xOver = ((r.x! + r.w!) - r2.x!) / r2.w!;
251-
} else if (r0.x! + r0.w! > r2.x! + r2.w!) { // from the right
251+
} else if (r0.x! >= r2.x! + r2.w!) { // from the right
252252
xOver = ((r2.x! + r2.w!) - r.x!) / r2.w!;
253253
}
254254
const over = Math.min(xOver, yOver);

0 commit comments

Comments
 (0)