/
CreateHighlightDialog.js
286 lines (250 loc) · 8.78 KB
/
CreateHighlightDialog.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
// @flow
import React from 'react';
import noop from 'lodash/noop';
import { render, unmountComponentAtNode } from 'react-dom';
import EventEmitter from 'events';
import AnnotationPopover from '../components/AnnotationPopover';
import {
repositionCaret,
getPageInfo,
findElement,
getPopoverLayer,
isInElement,
getPageEl,
shouldDisplayMobileUI
} from '../util';
import { getDialogCoordsFromRange } from './docUtil';
import {
CREATE_EVENT,
TYPES,
PAGE_PADDING_TOP,
PAGE_PADDING_BOTTOM,
SELECTOR_CLASS_ANNOTATION_POPOVER,
INLINE_POPOVER_HEIGHT
} from '../constants';
class CreateHighlightDialog extends EventEmitter {
/** @property {Object} - Position, on the DOM, to align the dialog to the end of a highlight. */
position = {
x: 0,
y: 0
};
/** @property {boolean} - Whether or not we're on a mobile device. */
isMobile: boolean;
/** @property {boolean} - Whether or not we support touch. */
hasTouch: boolean;
/** @property {boolean} - Whether or not to allow plain highlight interaction. */
allowHighlight: boolean;
/** @property {boolean} - Whether or not to allow comment interactions. */
allowComment: boolean;
/** @property {HTMLElement} - Preview container DOM element */
container: HTMLElement;
/**
* A dialog used to create plain and comment highlights.
*
* [constructor]
*
* @param {HTMLElement} annotatedElement - Parent element
* @param {Object} [config] - For configuring the dialog.
* @param {boolean} [config.hasTouch] - True to add touch events.
* @param {boolean} [config.isMobile] - True if on a mobile device.
* @return {CreateHighlightDialog} CreateHighlightDialog instance
*/
constructor(annotatedElement: HTMLElement, config: Object = {}) {
super();
this.annotatedElement = annotatedElement;
this.container = config.container;
this.hasTouch = !!config.hasTouch || false;
this.allowHighlight = config.allowHighlight || false;
this.allowComment = config.allowComment || false;
this.headerHeight = config.headerHeight;
}
/** @inheritdoc */
destroy() {
this.unmountPopover();
}
/**
* Resets and unmounts the annotation popover
*
* @return {void}
*/
unmountPopover() {
if (!this.isVisible) {
return;
}
this.isVisible = false;
const popoverLayers = this.container.querySelectorAll('.ba-dialog-layer');
if (!this.createPopoverComponent || popoverLayers.length === 0) {
return;
}
popoverLayers.forEach(unmountComponentAtNode);
this.createPopoverComponent = null;
}
/**
* Render the popover
*
* @param {Selection} selection Current text selection
* @param {AnnotationType} type - highlight type
* @return {void}
*/
show(selection: ?Selection, type: AnnotationType = TYPES.highlight) {
if (!selection) {
return;
}
// Select page of first node selected
this.selection = selection;
// $FlowFixMe
this.pageInfo = getPageInfo(this.selection.anchorNode);
if (!this.pageInfo.pageEl) {
return;
}
if (shouldDisplayMobileUI(this.container)) {
this.position = { x: 0, y: 0 };
} else {
this.setPosition(this.selection);
}
this.renderAnnotationPopover(type);
}
/**
* Shows the appropriate annotation dialog for this thread.
*
* @param {string} type - annotation type
* @param {HTMLElement} pageEl - Page DOM Element
* @return {void}
*/
renderAnnotationPopover = (type: AnnotationType = TYPES.highlight) => {
const pageEl = shouldDisplayMobileUI(this.container)
? this.container
: getPageEl(this.annotatedElement, this.pageInfo.page);
const popoverLayer = getPopoverLayer(pageEl);
this.createPopoverComponent = render(
<AnnotationPopover
type={type}
canAnnotate={true}
canComment={this.allowComment}
canDelete={true}
position={this.updatePosition}
onDelete={noop}
onCancel={this.onCommentCancel}
onCreate={this.onCreate}
onCommentClick={this.onCommentClick}
isPending={true}
isMobile={shouldDisplayMobileUI(this.container)}
headerHeight={this.headerHeight}
/>,
popoverLayer
);
this.isVisible = true;
};
/**
* @param {Event} event - Mouse event
* @return {boolean} Whether or not the click event occured over a highlight in the canvas
*/
isInHighlight = (event: Event) => {
if (!this.selection || !this.selection.rangeCount) {
return false;
}
const lastRange = this.selection.getRangeAt(this.selection.rangeCount - 1);
return isInElement(event, lastRange);
};
/** @inheritdoc */
setPosition(selection: HTMLElement) {
if (!this.selection || !this.selection.rangeCount) {
return;
}
// $FlowFixMe
const lastRange = selection.getRangeAt(selection.rangeCount - 1);
const coords = getDialogCoordsFromRange(lastRange);
// Select page of first node selected
// $FlowFixMe
this.pageInfo = getPageInfo(selection.anchorNode);
const { pageEl } = this.pageInfo;
if (!pageEl) {
return;
}
const popoverEl = findElement(
this.annotatedElement,
SELECTOR_CLASS_ANNOTATION_POPOVER,
this.renderAnnotationPopover
);
const popoverDimensions = popoverEl.getBoundingClientRect();
const pageDimensions = pageEl.getBoundingClientRect();
const pageLeft = pageDimensions.left;
const pageTop = pageDimensions.top - popoverDimensions.height;
this.position = {
x: coords.x - pageLeft,
y: coords.y - pageTop
};
this.updatePosition();
}
/**
* Update the position styling for the dialog so that the chevron points to
* the desired location.
*
* @return {void}
*/
updatePosition = () => {
if (shouldDisplayMobileUI(this.container)) {
return;
}
// Position it below lower right corner or center of the highlight - we need
// to reposition every time since the DOM could have changed from
// zooming
const { pageEl } = this.pageInfo;
const pageDimensions = pageEl.getBoundingClientRect();
const pageHeight = pageDimensions.height - PAGE_PADDING_TOP - PAGE_PADDING_BOTTOM;
const BOUNDARY_PADDING = 5;
const popoverEl = findElement(
this.annotatedElement,
SELECTOR_CLASS_ANNOTATION_POPOVER,
this.renderAnnotationPopover
);
const dialogDimensions = popoverEl.getBoundingClientRect();
const dialogWidth = dialogDimensions.width;
let dialogX = this.position.x - dialogWidth / 2;
let dialogY = this.position.y - INLINE_POPOVER_HEIGHT + BOUNDARY_PADDING;
// Only reposition if one side is past page boundary - if both are,
// just center the dialog and cause scrolling since there is nothing
// else we can do
dialogX = repositionCaret(popoverEl, dialogX, dialogWidth, this.position.x, pageDimensions.width);
if (dialogY < 0) {
dialogY = 0;
} else if (dialogY > pageHeight) {
dialogY = pageHeight - dialogDimensions.height;
}
popoverEl.style.left = `${dialogX}px`;
popoverEl.style.top = `${dialogY}px`;
};
/**
* Fire an event notifying that the plain highlight button has been clicked.
*
* @param {AnnotationType} type - Annotation type
* @param {string} message - Annotation message
* @return {void}
*/
onCreate = (type: AnnotationType, message: String) => {
if (!message) {
this.emit(CREATE_EVENT.plain);
return;
}
this.emit(CREATE_EVENT.commentPost, message);
};
/**
* Fire an event notifying that the comment button has been clicked. Also
* show the comment box, and give focus to the text area conatined by it.
*
* @return {void}
*/
onCommentClick = () => {
this.emit(CREATE_EVENT.comment);
this.renderAnnotationPopover(TYPES.highlight_comment);
};
/**
* Cancels adding a comment to the highlight annotation by rendering a plain highlight popover
*
* @return {void}
*/
onCommentCancel = () => {
this.renderAnnotationPopover(TYPES.highlight);
};
}
export default CreateHighlightDialog;