Skip to content

Commit 6ea7550

Browse files
authored
[Bugfix] Fix pen tool blocking non-annotation mouse down actions (#344)
1 parent 8ffad71 commit 6ea7550

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

src/UI/pen.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ let lines = [];
2222
function handleDocumentPointerdown(e) {
2323
path = null;
2424
lines = [];
25-
_candraw = true;
26-
/* if (!e.srcElement.classList.contains('annotationLayer')) {
25+
if (!findSVGAtPoint(e.clientX, e.clientY)) {
2726
return;
28-
} */
27+
}
28+
_candraw = true;
2929
e.preventDefault();
3030
}
3131

@@ -39,6 +39,9 @@ function handleDocumentPointerup(e) {
3939
}
4040

4141
function saveToStorage(x, y) {
42+
if (!_candraw) {
43+
return;
44+
}
4245
_candraw = false;
4346
let svg;
4447
if (lines.length > 1 && (svg = findSVGAtPoint(x, y))) {
@@ -64,12 +67,10 @@ function saveToStorage(x, y) {
6467
* @param {PointerEvent} e The DOM event to be handled
6568
*/
6669
function handleDocumentPointermove(e) {
67-
if (!e.srcElement.classList.contains('annotationLayer')) {
70+
if (!findSVGAtPoint(e.clientX, e.clientY) || !_candraw) {
6871
return;
6972
}
70-
if (_candraw) {
71-
savePoint(e.clientX, e.clientY);
72-
}
73+
savePoint(e.clientX, e.clientY);
7374
}
7475

7576
/**

test/UI/pen.spec.js

+45-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { equal, deepStrictEqual } from 'assert';
1+
import { deepStrictEqual, strictEqual } from 'assert';
22
import PDFJSAnnotate from '../../src/PDFJSAnnotate';
33
import { firePointerEvent } from '../fireEvent';
44
import mockAddAnnotation from '../mockAddAnnotation';
@@ -69,7 +69,7 @@ describe('UI::pen', function() {
6969
disablePen();
7070
simulateCreateDrawingAnnotation();
7171
setTimeout(function() {
72-
equal(addAnnotationSpy.called, false);
72+
strictEqual(addAnnotationSpy.called, false);
7373
done();
7474
}, 0);
7575
});
@@ -79,14 +79,49 @@ describe('UI::pen', function() {
7979
enablePen();
8080
simulateCreateDrawingAnnotation();
8181
setTimeout(function() {
82-
equal(addAnnotationSpy.called, true);
83-
let args = addAnnotationSpy.getCall(0).args;
84-
equal(args[0], 'test-document-id');
85-
equal(args[1], '1');
86-
equal(args[2].type, 'drawing');
87-
equal(args[2].width, 1);
88-
equal(args[2].color, '000000');
89-
equal(args[2].lines.length, 2);
82+
strictEqual(addAnnotationSpy.called, true);
83+
const args = addAnnotationSpy.getCall(0).args;
84+
strictEqual(args[0], 'test-document-id');
85+
strictEqual(args[1], 1);
86+
strictEqual(args[2].type, 'drawing');
87+
strictEqual(args[2].width, 1);
88+
strictEqual(args[2].color, '000000');
89+
strictEqual(args[2].lines.length, 2);
90+
done();
91+
}, 0);
92+
});
93+
94+
it('should not create annotation if started outside annotation layer', (done) => {
95+
disablePen();
96+
enablePen();
97+
setPen();
98+
99+
firePointerEvent(svg, 'pointerdown', {
100+
clientX: 2000,
101+
clientY: 10,
102+
pointerType: 'mouse'
103+
});
104+
105+
firePointerEvent(svg, 'pointermove', {
106+
clientX: 15,
107+
clientY: 15,
108+
pointerType: 'mouse'
109+
});
110+
111+
firePointerEvent(svg, 'pointermove', {
112+
clientX: 30,
113+
clientY: 30,
114+
pointerType: 'mouse'
115+
});
116+
117+
firePointerEvent(svg, 'pointerup', {
118+
clientX: 30,
119+
clientY: 30,
120+
pointerType: 'mouse'
121+
});
122+
123+
setTimeout(() => {
124+
strictEqual(addAnnotationSpy.called, false);
90125
done();
91126
}, 0);
92127
});

0 commit comments

Comments
 (0)