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