Skip to content

Commit

Permalink
Fix: if annotation click is outside the doc, ignore (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxiao6 authored and pramodsum committed May 31, 2018
1 parent 82bf717 commit b74484b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/doc/DocAnnotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,13 @@ class DocAnnotator extends Annotator {
clientEvent.clientX - pageDimensions.left,
clientEvent.clientY - pageDimensions.top - PAGE_PADDING_TOP
];
let [x, y] = browserCoordinates;

// If click is outside the page, ignore
if (docUtil.isCoordOutside(browserCoordinates, pageWidth, pageHeight)) {
return location;
}

let [x, y] = browserCoordinates;
// Do not create annotation if event doesn't have coordinates
if (Number.isNaN(x) || Number.isNaN(y)) {
this.emit(ANNOTATOR_EVENT.error, this.localized.createError);
Expand Down
17 changes: 17 additions & 0 deletions src/doc/__tests__/DocAnnotator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,23 @@ describe('doc/DocAnnotator', () => {
expect(docUtil.convertDOMSpaceToPDFSpace).to.not.be.called;
});

it('should not return a location if click event is outside the doc', () => {
stubs.selection.returns(false);
stubs.findClosest.returns('not-a-dialog');
annotator.hasTouch = true;
stubs.event = {
targetTouches: [
{
clientX: x + 1,
clientY: y,
target: annotator.annotatedEl
}
]
};

expect(annotator.getLocationFromEvent(stubs.event, TYPES.point)).to.be.null;
});

it('should return a valid point location if click is valid', () => {
stubs.selection.returns(false);
stubs.findClosest.returns('not-a-dialog');
Expand Down
17 changes: 17 additions & 0 deletions src/doc/docUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ export function isSelectionPresent() {
// Coordinate Utils
//------------------------------------------------------------------------------

/**
* Return false if the click is outside the doc
* @param {number[]} coordinates [x,y] coordinate location
* @param {number} pageWidth - Width of page in CSS pixels, needed to convert
* coordinate origin from bottom left (PDF) to top left (DOM)
* @param {number} pageHeight - Height of page in CSS pixels, needed to convert
* coordinate origin from bottom left (PDF) to top left (DOM)
* @return {boolean} True if outside
*/
export function isCoordOutside(coordinates, pageWidth, pageHeight) {
const [x, y] = coordinates;
if (x < 0 || x > pageWidth || y < 0 || y > pageHeight) {
return true;
}
return false;
}

/**
* Converts coordinates in PDF space to coordinates in DOM space.
*
Expand Down

0 comments on commit b74484b

Please sign in to comment.