-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy patharrow.js
191 lines (165 loc) · 4.24 KB
/
arrow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import PDFJSAnnotate from '../PDFJSAnnotate';
import { appendChild } from '../render/appendChild';
import {
convertToScreenPoint,
convertToSvgPoint,
disableUserSelect,
enableUserSelect,
findAnnotationAtPoint,
findSVGAtPoint,
findSVGContainer,
getMetadata
} from './utils';
let _enabled = false;
let _penSize;
let _penColor;
let path;
let lines;
let originY;
let originX;
/**
* Handle document.mousedown event
*
* @param {Event} e The DOM event to be handled
*/
function handleDocumentMousedown(e) {
let target = findAnnotationAtPoint(e.clientX, e.clientY);
if (target === null) {
return;
}
let type = target.getAttribute('data-pdf-annotate-type');
if (type !== 'circle' && type !== 'fillcircle' && type !== 'emptycircle') {
return;
}
let svg = findSVGContainer(target);
let { documentId } = getMetadata(svg);
let annotationId = target.getAttribute('data-pdf-annotate-id');
PDFJSAnnotate.getStoreAdapter()
.getAnnotation(documentId, annotationId)
.then((annotation) => {
if (annotation) {
path = null;
lines = [];
let point = convertToScreenPoint([annotation.cx, annotation.cy], svg);
let rect = svg.getBoundingClientRect();
originX = point[0] + rect.left;
originY = point[1] + rect.top;
document.addEventListener('mousemove', handleDocumentMousemove);
document.addEventListener('mouseup', handleDocumentMouseup);
}
});
}
/**
* Handle document.mouseup event
*
* @param {Event} e The DOM event to be handled
*/
function handleDocumentMouseup(e) {
let svg;
if (lines.length > 1 && (svg = findSVGAtPoint(e.clientX, e.clientY))) {
let { documentId, pageNumber } = getMetadata(svg);
PDFJSAnnotate.getStoreAdapter()
.addAnnotation(documentId, pageNumber, {
type: 'arrow',
width: _penSize,
color: _penColor,
lines
})
.then((annotation) => {
if (path) {
svg.removeChild(path);
}
appendChild(svg, annotation);
});
}
document.removeEventListener('mousemove', handleDocumentMousemove);
document.removeEventListener('mouseup', handleDocumentMouseup);
}
/**
* Handle document.mousemove event
*
* @param {Event} e The DOM event to be handled
*/
function handleDocumentMousemove(e) {
let x = lines.length === 0 ? originX : e.clientX;
let y = lines.length === 0 ? originY : e.clientY;
savePoint(x, y);
}
/**
* Handle document.keyup event
*
* @param {Event} e The DOM event to be handled
*/
function handleDocumentKeyup(e) {
// Cancel rect if Esc is pressed
if (e.keyCode === 27) {
lines = null;
path.parentNode.removeChild(path);
document.removeEventListener('mousemove', handleDocumentMousemove);
document.removeEventListener('mouseup', handleDocumentMouseup);
}
}
/**
* Save a point to the line being drawn.
*
* @param {Number} x The x coordinate of the point
* @param {Number} y The y coordinate of the point
*/
function savePoint(x, y) {
let svg = findSVGAtPoint(x, y);
if (!svg) {
return;
}
let rect = svg.getBoundingClientRect();
let point = convertToSvgPoint([x - rect.left, y - rect.top], svg);
if (lines.length < 2) {
lines.push(point);
return;
}
else {
lines[1] = point; // update end point
}
if (path) {
svg.removeChild(path);
}
path = appendChild(svg, {
type: 'arrow',
color: _penColor,
width: _penSize,
lines
});
}
/**
* Set the attributes of the pen.
*
* @param {Number} penSize The size of the lines drawn by the pen
* @param {String} penColor The color of the lines drawn by the pen
*/
export function setArrow(penSize = 10, penColor = '0000FF') {
_penSize = parseInt(penSize, 10);
_penColor = penColor;
}
/**
* Enable the pen behavior
*/
export function enableArrow() {
if (_enabled) {
return;
}
_enabled = true;
document.addEventListener('mousedown', handleDocumentMousedown);
document.addEventListener('keyup', handleDocumentKeyup);
disableUserSelect();
}
/**
* Disable the pen behavior
*/
export function disableArrow() {
if (!_enabled) {
return;
}
_enabled = false;
document.removeEventListener('mousedown', handleDocumentMousedown);
document.removeEventListener('keyup', handleDocumentKeyup);
enableUserSelect();
}