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
|
|
18
|
if (!annotation?.tagStartPositions || !annotation.tagLengths) {
|
19
|
// invalid data provided from API
|
20
|
console.log('start or lengths not found');
|
21
|
return null;
|
22
|
}
|
23
|
|
24
|
let startTag = selection.anchorNode; // anchor node = node where selection started
|
25
|
let endTag = selection.focusNode; // focus node = node where selection ended
|
26
|
|
27
|
if (!startTag || !endTag) {
|
28
|
console.log('Selection not found');
|
29
|
return null;
|
30
|
}
|
31
|
|
32
|
if (startTag.nodeName.includes('#')) {
|
33
|
// it is not an element (usually #text)
|
34
|
startTag = startTag.parentNode;
|
35
|
}
|
36
|
if (endTag.nodeName.includes('#')) {
|
37
|
// it is not an element (usually #text)
|
38
|
endTag = endTag.parentNode;
|
39
|
}
|
40
|
|
41
|
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
|
let startId: number = Number(startElement.getAttribute(idAttributeName)) ?? -1;
|
59
|
let endId: number = Number(endElement.getAttribute(idAttributeName)) ?? -1;
|
60
|
|
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
|
}
|