Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(highlight): Ignore empty rects when rendering highlight canvas rects #697

Merged
merged 5 commits into from
Aug 2, 2022
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
6 changes: 6 additions & 0 deletions src/highlight/HighlightCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ export default class HighlightCanvas extends React.PureComponent<Props> {

shapesArray.forEach(rect => {
const { height, isActive, isHover, width, x, y } = rect;

// Ignore empty rects with a width or height of 0
if (width === 0 || height === 0) {
karelee7 marked this conversation as resolved.
Show resolved Hide resolved
return;
}

const rectHeight = (height / 100) * canvasHeight;
const rectWidth = (width / 100) * canvasWidth;
const x1 = (x / 100) * canvasWidth;
Expand Down
23 changes: 23 additions & 0 deletions src/highlight/__mocks__/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ export const rect: Rect = {
y: 5,
};

export const noWidthRect: Rect = {
type: 'rect' as const,
height: 10,
width: 0,
x: 5,
y: 5,
};

export const noHeightRect: Rect = {
type: 'rect' as const,
height: 0,
width: 20,
x: 5,
y: 5,
};

export const target: TargetHighlight = {
location: {
type: 'page' as const,
Expand Down Expand Up @@ -55,3 +71,10 @@ export const selection = {
},
],
};

export const canvasContext = {
fillRect: jest.fn(),
restore: jest.fn(),
save: jest.fn(),
strokeRect: jest.fn(),
};
20 changes: 19 additions & 1 deletion src/highlight/__tests__/HighlightCanvas-test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { shallow, ShallowWrapper } from 'enzyme';
import HighlightCanvas, { Props } from '../HighlightCanvas';
import { rect as mockRect } from '../__mocks__/data';
import { canvasContext, rect as mockRect, noWidthRect, noHeightRect } from '../__mocks__/data';

describe('HighlightCanvas', () => {
const defaults: Props = {
Expand Down Expand Up @@ -56,5 +56,23 @@ describe('HighlightCanvas', () => {
const wrapper = getWrapper();
expect(wrapper.find('canvas').hasClass('ba-HighlightCanvas')).toBe(true);
});

test('should render, excluding 0 width and height values', () => {
const wrapper = getWrapper({ shapes: [mockRect, noWidthRect, noHeightRect] });
const instance = wrapper.instance() as HighlightCanvas;

instance.canvasRef = {
current: {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
karelee7 marked this conversation as resolved.
Show resolved Hide resolved
getContext: () => canvasContext,
},
};

instance.renderRects();

expect(canvasContext.fillRect).toHaveBeenCalledTimes(1);
expect(canvasContext.strokeRect).toHaveBeenCalledTimes(1);
});
});
});
40 changes: 40 additions & 0 deletions src/highlight/__tests__/highlightUtil-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,46 @@ describe('highlightUtil', () => {
expect(result[1]).toEqual(rects[3]);
expect(result[2]).toEqual(rects[4]);
});

test('should combine rects by row, excluding 0 width and height values', () => {
const validRect1 = {
height: 20,
width: 100,
x: 100,
y: 200,
};

const validRect2 = {
height: 21,
width: 100,
x: 500,
y: 200,
};

const noHeightRect = {
height: 0,
width: 100,
x: 200,
y: 200,
};

const noWidthRect = {
height: 23,
width: 0,
x: 400,
y: 300,
};

const result = combineRects([validRect1, noHeightRect, validRect2, noWidthRect]);
expect(result).toHaveLength(2);
expect(result[0]).toEqual({
karelee7 marked this conversation as resolved.
Show resolved Hide resolved
height: 20,
width: 100,
x: 100,
y: 200,
});
expect(result[1]).toEqual(validRect2);
});
});

describe('getShapeRelativeToContainer', () => {
Expand Down
6 changes: 6 additions & 0 deletions src/highlight/highlightUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ export const combineRects = (rects: Shape[]): Shape[] => {

dedupeRects(rects).forEach(rect => {
const { height, width, x, y } = rect;

// Ignore empty rects with a width or height of 0
if (width === 0 || height === 0) {
return;
}

const lastRect = result.pop();

if (!lastRect) {
Expand Down