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