-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dashboard): constrain drag start and endpoint in grid
- Loading branch information
Showing
10 changed files
with
248 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { moveSelectionBox } from '~/util/moveSelectionBox'; | ||
|
||
import { DashboardState } from '~/store/state'; | ||
|
||
const grid = { | ||
width: 100, | ||
height: 100, | ||
cellSize: 1, | ||
} as DashboardState['grid']; | ||
describe('moveSelectionBox', () => { | ||
const widget1 = { x: 0, y: 0, width: 10, height: 10 }; | ||
|
||
it('should move selection box', () => { | ||
const selectionBox = widget1; | ||
const vector = { x: 10, y: 10 }; | ||
const expected = { x: 10, y: 10, width: 10, height: 10 }; | ||
expect(moveSelectionBox({ selectionBox, vector, grid })).toEqual(expected); | ||
}); | ||
|
||
it('should not move selection box if it is out of bounds', () => { | ||
const selectionBox = widget1; | ||
const vector = { x: 100, y: 100 }; | ||
const expected = { x: 90, y: 90, width: 10, height: 10 }; | ||
expect(moveSelectionBox({ selectionBox, vector, grid })).toEqual(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Position, Rect } from '~/types'; | ||
import { DashboardState } from '~/store/state'; | ||
|
||
export const moveSelectionBox: (params: { | ||
selectionBox: Rect; | ||
vector: Position; | ||
grid: DashboardState['grid']; | ||
}) => Rect = ({ selectionBox, vector, grid }) => { | ||
const newRect = { ...selectionBox }; | ||
if (newRect.x + vector.x < 0) { | ||
vector.x = 0 - newRect.x; | ||
} | ||
|
||
if (newRect.x + newRect.width + vector.x > grid.width) { | ||
vector.x = grid.width - newRect.x - newRect.width; | ||
} | ||
|
||
if (newRect.y + vector.y < 0) { | ||
vector.y = 0 - newRect.y; | ||
} | ||
|
||
if (newRect.y + newRect.height + vector.y > grid.height) { | ||
vector.y = grid.height - newRect.y - newRect.height; | ||
} | ||
|
||
newRect.x += vector.x; | ||
newRect.y += vector.y; | ||
|
||
return newRect; | ||
}; |
Oops, something went wrong.