Skip to content

Commit 82c4fe7

Browse files
authored
fix(region): Add overflow protection for regions drawn near page edge (#502)
1 parent 775d12c commit 82c4fe7

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/region/RegionCreator.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,20 @@ export default function RegionCreator({ className, onStart, onStop }: Props): JS
5454
}
5555

5656
const { height, width } = creatorEl.getBoundingClientRect();
57+
const MAX_HEIGHT = height - MIN_SIZE;
58+
const MAX_WIDTH = width - MIN_SIZE;
5759
const MAX_X = Math.max(0, width - MIN_X);
5860
const MAX_Y = Math.max(0, height - MIN_Y);
5961

60-
// Set the origin x/y to the lowest value and the target x/y to the highest to avoid negative height/width
61-
const originX = Math.min(Math.max(MIN_X, x1 < x2 ? x1 : x2), MAX_X);
62-
const originY = Math.min(Math.max(MIN_Y, y1 < y2 ? y1 : y2), MAX_Y);
63-
const targetX = Math.min(Math.max(MIN_X, x2 > x1 ? x2 : x1), MAX_X);
64-
const targetY = Math.min(Math.max(MIN_Y, y2 > y1 ? y2 : y1), MAX_Y);
62+
// Add a buffer to the origin to avoid exceeding the dimensions of the page
63+
const initialX = x1 >= MAX_WIDTH && x2 >= MAX_WIDTH ? x1 - MIN_SIZE : x1;
64+
const initialY = y1 >= MAX_HEIGHT && y2 >= MAX_HEIGHT ? y1 - MIN_SIZE : y1;
65+
66+
// Set the origin to the lowest value and the target to the highest to avoid negative height/width
67+
const originX = Math.min(Math.max(MIN_X, initialX < x2 ? initialX : x2), MAX_X);
68+
const originY = Math.min(Math.max(MIN_Y, initialY < y2 ? initialY : y2), MAX_Y);
69+
const targetX = Math.min(Math.max(MIN_X, x2 > initialX ? x2 : initialX), MAX_X);
70+
const targetY = Math.min(Math.max(MIN_Y, y2 > initialY ? y2 : initialY), MAX_Y);
6571

6672
// Derive the shape height/width from the two coordinates
6773
const shapeHeight = Math.max(MIN_SIZE, targetY - originY);

src/region/__tests__/RegionCreator-test.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,18 @@ describe('RegionCreator', () => {
6666
});
6767

6868
test.each`
69-
x1 | y1 | x2 | y2 | result | comment
70-
${5} | ${5} | ${100} | ${100} | ${{ height: 9.5, width: 9.5, x: 0.5, y: 0.5 }} | ${'standard dimensions'}
71-
${50} | ${50} | ${100} | ${100} | ${{ height: 5, width: 5, x: 5, y: 5 }} | ${'standard dimensions'}
72-
${100} | ${100} | ${50} | ${50} | ${{ height: 5, width: 5, x: 5, y: 5 }} | ${'standard dimensions'}
73-
${100} | ${100} | ${105} | ${105} | ${{ height: 1, width: 1, x: 10, y: 10 }} | ${'minimum size'}
74-
${1500} | ${1500} | ${50} | ${50} | ${{ height: 95, width: 95, x: 5, y: 5 }} | ${'maximum size'}
75-
${1500} | ${1500} | ${1500} | ${1500} | ${{ height: 1, width: 1, x: 100, y: 100 }} | ${'maximum position'}
76-
${-1} | ${-1} | ${10} | ${10} | ${{ height: 1, width: 1, x: 0, y: 0 }} | ${'minimum position'}
69+
x1 | y1 | x2 | y2 | result | comment
70+
${0} | ${0} | ${1000} | ${1000} | ${{ height: 100, width: 100, x: 0, y: 0 }} | ${'maximum size'}
71+
${0} | ${0} | ${1500} | ${1500} | ${{ height: 100, width: 100, x: 0, y: 0 }} | ${'maximum size (over)'}
72+
${100} | ${100} | ${105} | ${105} | ${{ height: 1, width: 1, x: 10, y: 10 }} | ${'minimum size'}
73+
${50} | ${50} | ${100} | ${100} | ${{ height: 5, width: 5, x: 5, y: 5 }} | ${'standard dimensions'}
74+
${100} | ${100} | ${50} | ${50} | ${{ height: 5, width: 5, x: 5, y: 5 }} | ${'standard dimensions'}
75+
${152} | ${152} | ${245} | ${245} | ${{ height: 9.3, width: 9.3, x: 15.2, y: 15.2 }} | ${'standard dimensions'}
76+
${1000} | ${100} | ${1000} | ${150} | ${{ height: 5, width: 1, x: 99, y: 10 }} | ${'maximum position (x)'}
77+
${100} | ${1000} | ${150} | ${1000} | ${{ height: 1, width: 5, x: 10, y: 99 }} | ${'maximum position (y)'}
78+
${1000} | ${1000} | ${1000} | ${1000} | ${{ height: 1, width: 1, x: 99, y: 99 }} | ${'maximum position (both)'}
79+
${1500} | ${1500} | ${1500} | ${1500} | ${{ height: 1, width: 1, x: 99, y: 99 }} | ${'maximum position (over)'}
80+
${-1} | ${-1} | ${10} | ${10} | ${{ height: 1, width: 1, x: 0, y: 0 }} | ${'minimum position'}
7781
`('should update the rendered rect when the user draws $comment', ({ result, x1, x2, y1, y2 }) => {
7882
const wrapper = getWrapper();
7983

0 commit comments

Comments
 (0)