Skip to content

Commit b74484b

Browse files
mingzexiao6pramodsum
authored andcommitted
Fix: if annotation click is outside the doc, ignore (#192)
1 parent 82bf717 commit b74484b

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/doc/DocAnnotator.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,13 @@ class DocAnnotator extends Annotator {
167167
clientEvent.clientX - pageDimensions.left,
168168
clientEvent.clientY - pageDimensions.top - PAGE_PADDING_TOP
169169
];
170-
let [x, y] = browserCoordinates;
171170

171+
// If click is outside the page, ignore
172+
if (docUtil.isCoordOutside(browserCoordinates, pageWidth, pageHeight)) {
173+
return location;
174+
}
175+
176+
let [x, y] = browserCoordinates;
172177
// Do not create annotation if event doesn't have coordinates
173178
if (Number.isNaN(x) || Number.isNaN(y)) {
174179
this.emit(ANNOTATOR_EVENT.error, this.localized.createError);

src/doc/__tests__/DocAnnotator-test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,23 @@ describe('doc/DocAnnotator', () => {
228228
expect(docUtil.convertDOMSpaceToPDFSpace).to.not.be.called;
229229
});
230230

231+
it('should not return a location if click event is outside the doc', () => {
232+
stubs.selection.returns(false);
233+
stubs.findClosest.returns('not-a-dialog');
234+
annotator.hasTouch = true;
235+
stubs.event = {
236+
targetTouches: [
237+
{
238+
clientX: x + 1,
239+
clientY: y,
240+
target: annotator.annotatedEl
241+
}
242+
]
243+
};
244+
245+
expect(annotator.getLocationFromEvent(stubs.event, TYPES.point)).to.be.null;
246+
});
247+
231248
it('should return a valid point location if click is valid', () => {
232249
stubs.selection.returns(false);
233250
stubs.findClosest.returns('not-a-dialog');

src/doc/docUtil.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,23 @@ export function isSelectionPresent() {
116116
// Coordinate Utils
117117
//------------------------------------------------------------------------------
118118

119+
/**
120+
* Return false if the click is outside the doc
121+
* @param {number[]} coordinates [x,y] coordinate location
122+
* @param {number} pageWidth - Width of page in CSS pixels, needed to convert
123+
* coordinate origin from bottom left (PDF) to top left (DOM)
124+
* @param {number} pageHeight - Height of page in CSS pixels, needed to convert
125+
* coordinate origin from bottom left (PDF) to top left (DOM)
126+
* @return {boolean} True if outside
127+
*/
128+
export function isCoordOutside(coordinates, pageWidth, pageHeight) {
129+
const [x, y] = coordinates;
130+
if (x < 0 || x > pageWidth || y < 0 || y > pageHeight) {
131+
return true;
132+
}
133+
return false;
134+
}
135+
119136
/**
120137
* Converts coordinates in PDF space to coordinates in DOM space.
121138
*

0 commit comments

Comments
 (0)