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
|
console.log(selection.type);
|
22
|
|
23
|
if (!annotation?.tagStartPositions || !annotation.tagLengths) {
|
24
|
// invalid data provided from API
|
25
|
console.log('start or lengths not found');
|
26
|
return null;
|
27
|
}
|
28
|
|
29
|
let startTag = selection.anchorNode; // anchor node = node where selection started
|
30
|
let endTag = selection.focusNode; // focus node = node where selection ended
|
31
|
|
32
|
if (!startTag || !endTag) {
|
33
|
console.log('Selection not found');
|
34
|
return null;
|
35
|
}
|
36
|
|
37
|
if (startTag.nodeName.includes('#')) {
|
38
|
// it is not an element (usually #text)
|
39
|
startTag = startTag.parentNode;
|
40
|
}
|
41
|
if (endTag.nodeName.includes('#')) {
|
42
|
// it is not an element (usually #text)
|
43
|
endTag = endTag.parentNode;
|
44
|
}
|
45
|
|
46
|
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
|
let startId: number = Number(startElement.getAttribute(idAttributeName)) ?? -1;
|
64
|
let endId: number = Number(endElement.getAttribute(idAttributeName)) ?? -1;
|
65
|
|
66
|
let startPosition =
|
67
|
annotation.tagStartPositions[startId] +
|
68
|
annotation.tagLengths[startId] +
|
69
|
selection.anchorOffset;
|
70
|
let endPosition =
|
71
|
annotation.tagStartPositions[endId] +
|
72
|
annotation.tagLengths[endId] +
|
73
|
selection.focusOffset -
|
74
|
1;
|
75
|
|
76
|
// need to switch start and end elements (selection was in the opposite way then expected)
|
77
|
if (startPosition > endPosition) {
|
78
|
let temp = startPosition;
|
79
|
startPosition = endPosition;
|
80
|
endPosition = temp;
|
81
|
|
82
|
temp = startId;
|
83
|
startId = endId;
|
84
|
endId = temp;
|
85
|
|
86
|
const tempElement = startElement;
|
87
|
startElement = endElement;
|
88
|
endElement = tempElement;
|
89
|
}
|
90
|
|
91
|
const length = endPosition - startPosition + 1;
|
92
|
|
93
|
return {
|
94
|
endElementId: endId,
|
95
|
startElementId: startId,
|
96
|
startPositionOriginalDocument: startPosition,
|
97
|
endPositionOriginalDocument: endPosition,
|
98
|
selectionLengthOriginalDocument: length,
|
99
|
};
|
100
|
}
|