Projekt

Obecné

Profil

Stáhnout (2.96 KB) Statistiky
| Větev: | Tag: | Revize:
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
    selectedText: string;
11
}
12
const idAttributeName = 'aswi-tag-id';
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 = 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
    const text = selection.toString();
93

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