diff --git a/src/doc/DocAnnotator.js b/src/doc/DocAnnotator.js index c9ae3c49d..e2c8d1110 100644 --- a/src/doc/DocAnnotator.js +++ b/src/doc/DocAnnotator.js @@ -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); diff --git a/src/doc/__tests__/DocAnnotator-test.js b/src/doc/__tests__/DocAnnotator-test.js index 5da90078e..528019f5f 100644 --- a/src/doc/__tests__/DocAnnotator-test.js +++ b/src/doc/__tests__/DocAnnotator-test.js @@ -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'); diff --git a/src/doc/docUtil.js b/src/doc/docUtil.js index e3f54b6d8..861c3901c 100644 --- a/src/doc/docUtil.js +++ b/src/doc/docUtil.js @@ -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. *