Projekt

Obecné

Profil

Stáhnout (2.9 KB) Statistiky
| Větev: | Tag: | Revize:
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 c5d8874c Lukáš Vlček
    if (selection.type.toLowerCase() != 'range') {
18
        return null; // nothing is selected
19
    }
20
21
    console.log(selection.type);
22 3f66b624 Lukáš Vlček
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 3f66b624 Lukáš Vlček
    let startId: number = Number(startElement.getAttribute(idAttributeName)) ?? -1;
64
    let endId: number = Number(endElement.getAttribute(idAttributeName)) ?? -1;
65 4bc99591 Lukáš Vlček
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
}