-
Notifications
You must be signed in to change notification settings - Fork 44
/
DrawingModeController.js
438 lines (367 loc) · 14.3 KB
/
DrawingModeController.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// @flow
import AnnotationModeController from './AnnotationModeController';
import DocDrawingThread from '../doc/DocDrawingThread';
import { replaceHeader, enableElement, disableElement, clearCanvas, findClosestElWithClass, getPageEl } from '../util';
import AnnotationAPI from '../api/AnnotationAPI';
import {
TYPES,
STATES,
THREAD_EVENT,
SELECTOR_ANNOTATION_BUTTON_DRAW_CANCEL,
SELECTOR_ANNOTATION_BUTTON_DRAW_POST,
SELECTOR_ANNOTATION_BUTTON_DRAW_UNDO,
SELECTOR_ANNOTATION_BUTTON_DRAW_REDO,
SELECTOR_DRAW_MODE_HEADER,
CLASS_ANNOTATION_LAYER_DRAW,
CLASS_ANNOTATION_DRAW_MODE,
CLASS_ANNOTATION_POINT_MARKER,
ANNOTATOR_TYPE,
CLASS_ANNOTATION_LAYER_DRAW_IN_PROGRESS
} from '../constants';
// $FlowFixMe
import shell from './drawingShell.html';
class DrawingModeController extends AnnotationModeController {
/** @property {AnnotationThread} - The currently selected DrawingThread */
selectedThread: AnnotationThread;
/** @property {HTMLElement} - The button to cancel the pending drawing thread */
cancelButtonEl: HTMLElement;
/** @property {HTMLElement} - The button to commit the pending drawing thread */
postButtonEl: HTMLElement;
/** @property {HTMLElement} - The button to undo a stroke on the pending drawing thread */
undoButtonEl: HTMLElement;
/** @property {HTMLElement} - The button to redo a stroke on the pending drawing thread */
redoButtonEl: HTMLElement;
/** @property {AnnotationThread} */
currentThread: AnnotationThread;
/** @property {Function} */
locationFunction: Function;
/** @inheritdoc */
init(data: Object): void {
super.init(data);
// If the header coming from the preview options is not none (e.g.
// light, dark, or no value given), then we want to use our draw
// header. Otherwise we expect header UI to be handled by Preview’s
// consumer
if (data.options.header !== 'none' || this.headerElement) {
this.setupHeader(this.headerElement, shell);
}
}
/** @inheritdoc */
setupHeader(container: HTMLElement, header: HTMLElement): void {
super.setupHeader(container, header);
this.cancelButtonEl = this.getButton(SELECTOR_ANNOTATION_BUTTON_DRAW_CANCEL);
this.cancelButtonEl.textContent = this.localized.cancelButton;
this.postButtonEl = this.getButton(SELECTOR_ANNOTATION_BUTTON_DRAW_POST);
// TODO(@spramod): Remove '||' string, once doneButton is properly localized within Preview
this.postButtonEl.textContent = this.localized.doneButton || 'Done';
this.undoButtonEl = this.getButton(SELECTOR_ANNOTATION_BUTTON_DRAW_UNDO);
this.redoButtonEl = this.getButton(SELECTOR_ANNOTATION_BUTTON_DRAW_REDO);
}
/**
* Prevents click events from triggering other annotation types
*
* @param {Event} event - Mouse event
* @return {void}
*/
stopPropagation(event: Event): void {
const el = findClosestElWithClass(event.target, CLASS_ANNOTATION_POINT_MARKER);
if (el) {
event.stopPropagation();
}
}
/**
* Cancels drawing annotation
*
* @return {void}
*/
cancelDrawing(): void {
if (this.currentThread) {
this.destroyThread(this.currentThread);
}
this.exit();
}
/**
* Posts drawing annotation
*
* @return {void}
*/
postDrawing(): void {
if (
this.currentThread &&
this.currentThread.state === STATES.pending &&
this.currentThread.pathContainer &&
this.currentThread.pathContainer.undoStack.length > 0
) {
this.currentThread.save(TYPES.draw);
}
this.exit();
}
/**
* Undos last drawing
*
* @return {void}
*/
undoDrawing(): void {
if (this.currentThread) {
this.currentThread.undo();
}
}
/**
* Redos last undone drawing annotation
*
* @return {void}
*/
redoDrawing(): void {
if (this.currentThread) {
this.currentThread.redo();
}
}
/** @inheritdoc */
setupHandlers(): void {
/* eslint-disable require-jsdoc */
this.locationFunction = (event) => this.getLocation(event, TYPES.point);
this.locationFunction = this.locationFunction.bind(this);
/* eslint-enable require-jsdoc */
// $FlowFixMe
this.stopPropagation = this.stopPropagation.bind(this);
// $FlowFixMe
this.cancelDrawing = this.cancelDrawing.bind(this);
// $FlowFixMe
this.postDrawing = this.postDrawing.bind(this);
// $FlowFixMe
this.undoDrawing = this.undoDrawing.bind(this);
// $FlowFixMe
this.redoDrawing = this.redoDrawing.bind(this);
// $FlowFixMe
this.drawingStartHandler = this.drawingStartHandler.bind(this);
this.pushElementHandler(this.annotatedElement, 'click', this.stopPropagation, true);
this.pushElementHandler(this.cancelButtonEl, 'click', this.cancelDrawing);
this.pushElementHandler(this.postButtonEl, 'click', this.postDrawing);
this.pushElementHandler(this.undoButtonEl, 'click', this.undoDrawing);
this.pushElementHandler(this.redoButtonEl, 'click', this.redoDrawing);
// Mobile & Desktop listeners are bound for touch-enabled laptop edge cases
// $FlowFixMe
this.drawingStartHandler = this.drawingStartHandler.bind(this);
this.pushElementHandler(this.annotatedElement, ['mousedown', 'touchstart'], this.drawingStartHandler, true);
}
/**
* Start a drawing stroke
*
* @param {Event} event - DOM event
* @return {void}
*/
drawingStartHandler(event: Event): void {
// $FlowFixMe
if (event.target && event.target.nodeName === 'BUTTON') {
return;
}
event.stopPropagation();
event.preventDefault();
this.removeSelection();
// Get annotation location from click event, ignore click if location is invalid
const location = this.getLocation(event, TYPES.point);
if (!location) {
return;
}
if (this.currentThread) {
this.currentThread.handleStart(location);
return;
}
// Add initial drawing boundary based on starting location
location.minX = location.x;
location.maxX = location.x;
location.minY = location.y;
location.maxY = location.y;
// Create new thread with no annotations, show indicator, and show dialog
const thread = this.registerThread({
id: AnnotationAPI.generateID(),
type: this.mode,
location,
canAnnotate: true,
canDelete: true,
createdBy: this.api.user,
createdAt: new Date().toLocaleString(),
isPending: true,
comments: []
});
if (!thread) {
return;
}
this.currentThread = thread;
this.emit(THREAD_EVENT.pending, thread.getThreadEventData());
thread.bindDrawingListeners(this.locationFunction);
thread.addListener('threadevent', (data) => this.handleThreadEvents(thread, data));
thread.handleStart(location);
}
/** @inheritdoc */
resetMode() {
if (this.currentThread) {
this.currentThread.clearBoundary();
}
// Remove any visible boundaries
const boundaries = this.annotatedElement.querySelectorAll('.ba-drawing-boundary');
// $FlowFixMe
boundaries.forEach((boundaryEl) => boundaryEl.parentNode.removeChild(boundaryEl));
// Clear the in progress drawing canvases
const pageElements = this.annotatedElement.querySelectorAll('.page');
pageElements.forEach((pageEl) => clearCanvas(pageEl, CLASS_ANNOTATION_LAYER_DRAW_IN_PROGRESS));
this.annotatedElement.classList.remove(CLASS_ANNOTATION_DRAW_MODE);
}
/**
* Enables the specified annotation mode
*
* @return {void}
*/
enter(): void {
super.enter();
replaceHeader(this.headerElement, SELECTOR_DRAW_MODE_HEADER);
this.annotatedElement.classList.add(CLASS_ANNOTATION_DRAW_MODE);
}
/** @inheritdoc */
handleThreadEvents(thread: AnnotationThread, data: Object = {}): void {
const { eventData } = data;
switch (data.event) {
case THREAD_EVENT.save:
thread.removeListener('threadevent', this.handleThreadEvents);
thread.unbindDrawingListeners();
this.currentThread = undefined;
this.selectedThread = thread;
this.unbindListeners();
// Clear existing canvases
// eslint-disable-next-line
const pageEl = this.annotatedElement.querySelector(`[data-page-number="${thread.location.page}"]`);
clearCanvas(pageEl, CLASS_ANNOTATION_LAYER_DRAW_IN_PROGRESS);
// Do not bind when mode is exited
if (!this.annotatedElement.classList.contains(CLASS_ANNOTATION_DRAW_MODE)) {
return;
}
this.bindListeners();
// Reset undo/redo buttons
this.updateUndoRedoButtonEls();
// Given a location (page change) start drawing at the provided location
if (eventData && eventData.location) {
// $FlowFixMe
this.currentThread.handleStart(eventData.location);
}
break;
case THREAD_EVENT.delete:
if (!thread) {
return;
}
this.currentThread = undefined;
thread.removeListener('threadevent', this.handleThreadEvents);
thread.unbindDrawingListeners();
// Reset undo/redo buttons
this.updateUndoRedoButtonEls();
if (thread.state === STATES.pending) {
// Soft delete, in-progress thread doesn't require a redraw or a delete on the server
// Clear in-progress thread and restart drawing
thread.destroy();
this.unbindListeners();
this.bindListeners();
} else {
this.destroyThread(thread);
this.renderPage(thread.location.page);
}
break;
case 'availableactions':
this.updateUndoRedoButtonEls(eventData.undo, eventData.redo);
break;
default:
}
super.handleThreadEvents(thread, data);
}
/** @inheritdoc */
renderPage(pageNum: string): void {
// Clear existing canvases
const pageEl = getPageEl(this.annotatedElement, pageNum);
if (pageEl) {
clearCanvas(pageEl, CLASS_ANNOTATION_LAYER_DRAW);
clearCanvas(pageEl, CLASS_ANNOTATION_LAYER_DRAW_IN_PROGRESS);
}
super.renderPage(pageNum);
}
/**
* Selects a drawing thread given a pointer event. Randomly picks one if multiple drawings overlap
*
* @param {Event} event - The event object containing the pointer information
* @return {DrawingThread} Selected drawing annotation
*/
handleSelection(event: Event): DrawingThread {
let selected;
// NOTE: This is a workaround when buttons are not given precedence in the event chain
const hasPendingDrawing = this.currentThread && this.currentThread.state === STATES.pending;
// $FlowFixMe
if (!event || (event.target && event.target.nodeName === 'BUTTON') || hasPendingDrawing) {
return selected;
}
event.stopPropagation();
// Get the threads that correspond to the point that was clicked on
const location = this.getLocation(event, TYPES.point);
const intersectingThreads = this.getIntersectingThreads(event, location);
// Clear boundary on previously selected thread
this.removeSelection();
// Selected a region with no drawing threads, remove the reference to the previously selected thread
if (intersectingThreads.length === 0) {
this.selectedThread = undefined;
return selected;
}
// Randomly select a thread in case there are multiple overlapping threads (use canvas hitmap to avoid this)
const index = Math.floor(Math.random() * intersectingThreads.length);
selected = intersectingThreads[index];
this.select(selected);
return selected;
}
/**
* Deselect a saved and selected thread
*
* @return {void}
*/
removeSelection(): void {
if (!this.selectedThread) {
return;
}
this.selectedThread.hide();
this.selectedThread = undefined;
}
/**
* Select the indicated drawing thread. Deletes a drawing thread upon the second consecutive selection
*
* @param {DrawingThread} selectedDrawingThread - The drawing thread to select
* @return {void}
*/
select(selectedDrawingThread: DrawingThread): void {
selectedDrawingThread.show();
selectedDrawingThread.drawBoundary();
selectedDrawingThread.renderAnnotationPopover();
this.selectedThread = selectedDrawingThread;
}
/**
* Toggle the undo and redo buttons based on thenumber of actions available
*
* @param {number} undoCount - Thenumber of objects that can be undone
* @param {number} redoCount - Thenumber of objects that can be redone
* @return {void}
*/
updateUndoRedoButtonEls(undoCount: number = 0, redoCount: number = 0): void {
if (this.undoButtonEl) {
if (undoCount === 1) {
enableElement(this.undoButtonEl);
} else if (undoCount === 0) {
disableElement(this.undoButtonEl);
}
}
if (this.redoButtonEl) {
if (redoCount === 1) {
enableElement(this.redoButtonEl);
} else if (redoCount === 0) {
disableElement(this.redoButtonEl);
}
}
}
/** @inheritdoc */
instantiateThread(params: Object): DrawingThread {
return this.annotatorType === ANNOTATOR_TYPE.document ? new DocDrawingThread(params) : null;
}
}
export default DrawingModeController;