Skip to content

Commit c05940c

Browse files
committed
Improve eraser in deleting annotations along its path
1 parent a5685d3 commit c05940c

File tree

2 files changed

+74
-15
lines changed

2 files changed

+74
-15
lines changed

src/UI/eraser.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import {
55
} from './utils';
66

77
let _canerase = false;
8+
let previousPoint = null;
89

910
/**
1011
*
1112
* @param {PointerEvent} e
1213
*/
1314
function handleDocumentDown(e) {
1415
_canerase = true;
16+
previousPoint = [e.clientX, e.clientY];
1517
}
1618

1719
/**
@@ -20,14 +22,41 @@ function handleDocumentDown(e) {
2022
*/
2123
function handleDocumentUp(e) {
2224
_canerase = false;
25+
erase(findAnnotationAtPoint(e.clientX, e.clientY));
2326
}
2427

2528
/**
2629
*
2730
* @param {PointerEvent} e
2831
*/
2932
function handleDocumentMouseMove(e) {
30-
erase(findAnnotationAtPoint(e.clientX, e.clientY));
33+
if (!_canerase) {
34+
return;
35+
}
36+
let check = [];
37+
let diffX = Math.abs(previousPoint[0] - e.clientX);
38+
let diffY = Math.abs(previousPoint[1] - e.clientY);
39+
if (diffX >= 1 || diffY >= 1) {
40+
let maxSteps = Math.round(Math.max(diffX, diffY));
41+
let subStepSize = Math.min(diffX, diffY) / maxSteps;
42+
let smallerTest = diffX < diffY;
43+
let startPoint = [
44+
Math.min(previousPoint[0], e.clientX),
45+
Math.min(previousPoint[1], e.clientY)
46+
];
47+
for (let i = 0; i < maxSteps; i++) {
48+
if (smallerTest) {
49+
check.push([Math.round(startPoint[0] + (subStepSize * i)), Math.round(startPoint[1] + i)]);
50+
}
51+
else {
52+
check.push([Math.round(startPoint[0] + i), Math.round(startPoint[1] + (subStepSize * i))]);
53+
}
54+
}
55+
}
56+
for (let point of check) {
57+
erase(findAnnotationAtPoint(point[0], point[1]));
58+
}
59+
previousPoint = [e.clientX, e.clientY];
3160
}
3261

3362
function erase(target) {

test/UI/eraser.spec.js

+44-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { equal } from 'assert';
22
import PDFJSAnnotate from '../../src/PDFJSAnnotate';
33
import { firePointerEvent } from '../fireEvent';
4-
import mockDeleteAnnotation from "../mockDeleteAnnotation";
5-
import mockGetAnnotations from "../mockGetAnnotations";
4+
import mockDeleteAnnotation from '../mockDeleteAnnotation';
5+
import mockGetAnnotations from '../mockGetAnnotations';
66
import mockSVGContainer from '../mockSVGContainer';
77
import mockTextAnnotation from '../mockTextAnnotation';
88
import { enableEraser, disableEraser } from '../../src/UI/eraser';
@@ -17,30 +17,30 @@ function simulateEraserMovement() {
1717
firePointerEvent(svg, 'pointerdown', {
1818
clientX: 10,
1919
clientY: 10,
20-
pointerType: "mouse"
20+
pointerType: 'mouse'
2121
});
2222

2323
firePointerEvent(svg, 'pointermove', {
2424
clientX: 25,
2525
clientY: 15,
26-
pointerType: "mouse"
26+
pointerType: 'mouse'
2727
});
2828

2929
firePointerEvent(svg, 'pointermove', {
3030
clientX: 30,
3131
clientY: 30,
32-
pointerType: "mouse"
32+
pointerType: 'mouse'
3333
});
3434

3535
firePointerEvent(svg, 'pointerup', {
3636
clientX: 30,
3737
clientY: 30,
38-
pointerType: "mouse"
38+
pointerType: 'mouse'
3939
});
4040
}
4141

42-
describe('UI::eraser', function () {
43-
beforeEach(function () {
42+
describe('UI::eraser', function() {
43+
beforeEach(function() {
4444
svg = mockSVGContainer();
4545
svg.style.width = '100px';
4646
svg.style.height = '100px';
@@ -53,34 +53,64 @@ describe('UI::eraser', function () {
5353
PDFJSAnnotate.__storeAdapter.getAnnotations = mockGetAnnotations();
5454
});
5555

56-
afterEach(function () {
56+
afterEach(function() {
5757
if (svg.parentNode) {
5858
svg.parentNode.removeChild(svg);
5959
}
6060

6161
disableEraser();
6262
});
6363

64-
after(function () {
64+
after(function() {
6565
PDFJSAnnotate.__storeAdapter.deleteAnnotation = __deleteAnnotation;
6666
PDFJSAnnotate.__storeAdapter.getAnnotations = __getAnnotations;
6767
});
6868

69-
it('should do nothing when disabled', function (done) {
69+
it('should do nothing when disabled', function(done) {
7070
enableEraser();
7171
disableEraser();
7272
simulateEraserMovement();
73-
setTimeout(function () {
73+
setTimeout(function() {
7474
equal(deleteAnnotationSpy.called, false);
7575
done();
7676
}, 0);
7777
});
7878

79-
it('should create an annotation when enabled', function (done) {
79+
it('should delete an annotation when enabled', function(done) {
8080
disableEraser();
8181
enableEraser();
8282
simulateEraserMovement();
83-
setTimeout(function () {
83+
setTimeout(function() {
84+
equal(deleteAnnotationSpy.called, true);
85+
let args = deleteAnnotationSpy.getCall(0).args;
86+
equal(args[0], 'test-document-id');
87+
equal(args[1], text.getAttribute('data-pdf-annotate-id'));
88+
done();
89+
}, 0);
90+
});
91+
92+
it('should delete annotation between pointermove points', function(done) {
93+
disableEraser();
94+
enableEraser();
95+
firePointerEvent(svg, 'pointerdown', {
96+
clientX: 0,
97+
clientY: 0,
98+
pointerType: 'mouse'
99+
});
100+
101+
firePointerEvent(svg, 'pointermove', {
102+
clientX: 25,
103+
clientY: 20,
104+
pointerType: 'mouse'
105+
});
106+
107+
firePointerEvent(svg, 'pointerup', {
108+
clientX: 25,
109+
clientY: 20,
110+
pointerType: 'mouse'
111+
});
112+
113+
setTimeout(function() {
84114
equal(deleteAnnotationSpy.called, true);
85115
let args = deleteAnnotationSpy.getCall(0).args;
86116
equal(args[0], 'test-document-id');

0 commit comments

Comments
 (0)