1 |
4bc99591
|
Lukáš Vlček
|
import { AnnotationInfo } from '../api';
|
2 |
6973d036
|
Lukáš Vlček
|
import { DOCUMENT_TAG_ID_ATTRIBUTE_NAME } from '../constants';
|
3 |
4bc99591
|
Lukáš Vlček
|
|
4 |
|
|
export interface SelectionInfo {
|
5 |
|
|
startElementId: number;
|
6 |
|
|
endElementId: number;
|
7 |
|
|
startPositionOriginalDocument: number;
|
8 |
|
|
endPositionOriginalDocument: number;
|
9 |
|
|
selectionLengthOriginalDocument: number;
|
10 |
ba94b8c1
|
Lukáš Vlček
|
|
11 |
|
|
selectedText: string;
|
12 |
4bc99591
|
Lukáš Vlček
|
}
|
13 |
3f66b624
|
Lukáš Vlček
|
|
14 |
4bc99591
|
Lukáš Vlček
|
export function GetSelectionInfo(annotation: AnnotationInfo): SelectionInfo | null {
|
15 |
|
|
const selection = window.getSelection();
|
16 |
|
|
if (!selection) {
|
17 |
|
|
return null;
|
18 |
|
|
}
|
19 |
c5d8874c
|
Lukáš Vlček
|
if (selection.type.toLowerCase() != 'range') {
|
20 |
|
|
return null; // nothing is selected
|
21 |
|
|
}
|
22 |
|
|
|
23 |
4bc99591
|
Lukáš Vlček
|
if (!annotation?.tagStartPositions || !annotation.tagLengths) {
|
24 |
3f66b624
|
Lukáš Vlček
|
// invalid data provided from API
|
25 |
4bc99591
|
Lukáš Vlček
|
console.log('start or lengths not found');
|
26 |
|
|
return null;
|
27 |
|
|
}
|
28 |
|
|
|
29 |
3f66b624
|
Lukáš Vlček
|
let startTag = selection.anchorNode; // anchor node = node where selection started
|
30 |
|
|
let endTag = selection.focusNode; // focus node = node where selection ended
|
31 |
4bc99591
|
Lukáš Vlček
|
|
32 |
|
|
if (!startTag || !endTag) {
|
33 |
|
|
console.log('Selection not found');
|
34 |
|
|
return null;
|
35 |
|
|
}
|
36 |
|
|
|
37 |
|
|
if (startTag.nodeName.includes('#')) {
|
38 |
3f66b624
|
Lukáš Vlček
|
// it is not an element (usually #text)
|
39 |
4bc99591
|
Lukáš Vlček
|
startTag = startTag.parentNode;
|
40 |
|
|
}
|
41 |
|
|
if (endTag.nodeName.includes('#')) {
|
42 |
3f66b624
|
Lukáš Vlček
|
// it is not an element (usually #text)
|
43 |
4bc99591
|
Lukáš Vlček
|
endTag = endTag.parentNode;
|
44 |
|
|
}
|
45 |
3f66b624
|
Lukáš Vlček
|
|
46 |
4bc99591
|
Lukáš Vlček
|
if (!startTag || !endTag) {
|
47 |
|
|
console.log('Selection element not found');
|
48 |
|
|
return null;
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
if (!(startTag instanceof Element)) {
|
52 |
|
|
console.log('StartTag is not instance of Element');
|
53 |
|
|
return null;
|
54 |
|
|
}
|
55 |
|
|
if (!(endTag instanceof Element)) {
|
56 |
|
|
console.log('EndTag is not instance of Element');
|
57 |
|
|
return null;
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
let startElement = startTag as Element;
|
61 |
|
|
let endElement = endTag as Element;
|
62 |
|
|
|
63 |
6973d036
|
Lukáš Vlček
|
let startId: number =
|
64 |
|
|
Number(startElement.getAttribute(DOCUMENT_TAG_ID_ATTRIBUTE_NAME)) ?? -1;
|
65 |
|
|
let endId: number =
|
66 |
|
|
Number(endElement.getAttribute(DOCUMENT_TAG_ID_ATTRIBUTE_NAME)) ?? -1;
|
67 |
4bc99591
|
Lukáš Vlček
|
|
68 |
|
|
let startPosition =
|
69 |
|
|
annotation.tagStartPositions[startId] +
|
70 |
|
|
annotation.tagLengths[startId] +
|
71 |
|
|
selection.anchorOffset;
|
72 |
|
|
let endPosition =
|
73 |
|
|
annotation.tagStartPositions[endId] +
|
74 |
|
|
annotation.tagLengths[endId] +
|
75 |
|
|
selection.focusOffset -
|
76 |
|
|
1;
|
77 |
|
|
|
78 |
|
|
// need to switch start and end elements (selection was in the opposite way then expected)
|
79 |
|
|
if (startPosition > endPosition) {
|
80 |
|
|
let temp = startPosition;
|
81 |
|
|
startPosition = endPosition;
|
82 |
|
|
endPosition = temp;
|
83 |
|
|
|
84 |
|
|
temp = startId;
|
85 |
|
|
startId = endId;
|
86 |
|
|
endId = temp;
|
87 |
|
|
|
88 |
|
|
const tempElement = startElement;
|
89 |
|
|
startElement = endElement;
|
90 |
|
|
endElement = tempElement;
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
const length = endPosition - startPosition + 1;
|
94 |
ba94b8c1
|
Lukáš Vlček
|
const text = selection.toString();
|
95 |
4bc99591
|
Lukáš Vlček
|
|
96 |
|
|
return {
|
97 |
|
|
endElementId: endId,
|
98 |
|
|
startElementId: startId,
|
99 |
|
|
startPositionOriginalDocument: startPosition,
|
100 |
|
|
endPositionOriginalDocument: endPosition,
|
101 |
|
|
selectionLengthOriginalDocument: length,
|
102 |
ba94b8c1
|
Lukáš Vlček
|
selectedText: text,
|
103 |
4bc99591
|
Lukáš Vlček
|
};
|
104 |
|
|
}
|