Projekt

Obecné

Profil

Stáhnout (3.03 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { AnnotationInfo } from '../api';
2
import { DOCUMENT_TAG_ID_ATTRIBUTE_NAME } from '../constants';
3

    
4
export interface SelectionInfo {
5
    startElementId: number;
6
    endElementId: number;
7
    startPositionOriginalDocument: number;
8
    endPositionOriginalDocument: number;
9
    selectionLengthOriginalDocument: number;
10

    
11
    selectedText: string;
12
}
13

    
14
export function GetSelectionInfo(annotation: AnnotationInfo): SelectionInfo | null {
15
    const selection = window.getSelection();
16
    if (!selection) {
17
        return null;
18
    }
19
    if (selection.type.toLowerCase() != 'range') {
20
        return null; // nothing is selected
21
    }
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 =
64
        Number(startElement.getAttribute(DOCUMENT_TAG_ID_ATTRIBUTE_NAME)) ?? -1;
65
    let endId: number =
66
        Number(endElement.getAttribute(DOCUMENT_TAG_ID_ATTRIBUTE_NAME)) ?? -1;
67

    
68
    let startPosition =
69
        annotation.tagStartPositions[startId] +
70
        annotation.tagLengths[startId] +
71
        selection.anchorOffset;
72
    let endPosition =
73
        annotation.tagStartPositions[endId] +
74
        annotation.tagLengths[endId] +
75
        selection.focusOffset -
76
        1;
77

    
78
    // need to switch start and end elements (selection was in the opposite way then expected)
79
    if (startPosition > endPosition) {
80
        let temp = startPosition;
81
        startPosition = endPosition;
82
        endPosition = temp;
83

    
84
        temp = startId;
85
        startId = endId;
86
        endId = temp;
87

    
88
        const tempElement = startElement;
89
        startElement = endElement;
90
        endElement = tempElement;
91
    }
92

    
93
    const length = endPosition - startPosition + 1;
94
    const text = selection.toString();
95

    
96
    return {
97
        endElementId: endId,
98
        startElementId: startId,
99
        startPositionOriginalDocument: startPosition,
100
        endPositionOriginalDocument: endPosition,
101
        selectionLengthOriginalDocument: length,
102
        selectedText: text,
103
    };
104
}
(4-4/6)