Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions spec/gridstack-engine-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,36 @@ describe('gridstack engine:', () => {
});
});

describe('3447 dragging past an overlapping edge >', () => {
[
{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},
{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},
{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},
{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},
].forEach(({name, target, start, mid, end, targetY}) => {
it(`can drag ${name} when the other axis already overlaps`, () => {
e = new GridStackEngine({mode: 'float', column: 12});
const other = e.addNode({...target, id: 'target'});
const move = e.addNode({...start, id: 'move'});
e.cleanNodes().beginUpdate(move);
move._moving = true;
e.cacheRects(50, 50, 0, 0, 0, 0);

// Exactly half of the target is still too little to push it.
e.moveNodeCheck(move, {...mid, rect: {x: mid.x * 50, y: mid.y * 50, w: start.w * 50, h: start.h * 50}});
expect(move).toEqual(expect.objectContaining(start));
expect(other).toEqual(expect.objectContaining(target));

e.moveNodeCheck(move, {...end, rect: {x: end.x * 50, y: end.y * 50, w: start.w * 50, h: start.h * 50}});

expect(move).toEqual(expect.objectContaining(end));
expect(other.y).toBe(targetY);
expect(overlaps(e)).toEqual([]);
e.endUpdate();
});
});
});

describe('test constructor >', () => {

it('should be setup properly', () => {
Expand Down
12 changes: 6 additions & 6 deletions src/gridstack-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,16 @@ export class GridStackEngine {
}
const r2 = n._rect; // overlapping target
let yOver = Number.MAX_VALUE, xOver = Number.MAX_VALUE;
// depending on which side we started from, compute the overlap % of coverage
// (ex: from above/below we only compute the max horizontal line coverage)
if (r0.y! < r2.y!) { // from above
// Only initially separated axes determine the entry direction. An already-overlapping
// axis must not prevent crossing the target midpoint along the other axis.
if (r0.y! + r0.h! <= r2.y!) { // from above
yOver = ((r.y! + r.h!) - r2.y!) / r2.h!;
} else if (r0.y! + r0.h! > r2.y! + r2.h!) { // from below
} else if (r0.y! >= r2.y! + r2.h!) { // from below
yOver = ((r2.y! + r2.h!) - r.y!) / r2.h!;
}
if (r0.x! < r2.x!) { // from the left
if (r0.x! + r0.w! <= r2.x!) { // from the left
xOver = ((r.x! + r.w!) - r2.x!) / r2.w!;
} else if (r0.x! + r0.w! > r2.x! + r2.w!) { // from the right
} else if (r0.x! >= r2.x! + r2.w!) { // from the right
xOver = ((r2.x! + r2.w!) - r.x!) / r2.w!;
}
const over = Math.min(xOver, yOver);
Expand Down