Projekt

Obecné

Profil

Stáhnout (2.96 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 ba94b8c1 Lukáš Vlček
10
    selectedText: string;
11 4bc99591 Lukáš Vlček
}
12 3f66b624 Lukáš Vlček
const idAttributeName = 'aswi-tag-id';
13
14 4bc99591 Lukáš Vlček
export function GetSelectionInfo(annotation: AnnotationInfo): SelectionInfo | null {
15
    const selection = window.getSelection();
16
    if (!selection) {
17
        return null;
18
    }
19 c5d8874c Lukáš Vlček
    if (selection.type.toLowerCase() != 'range') {
20
        return null; // nothing is selected
21
    }
22
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 ba94b8c1 Lukáš Vlček
    const text = selection.toString();
93 4bc99591 Lukáš Vlček
94
    return {
95
        endElementId: endId,
96
        startElementId: startId,
97
        startPositionOriginalDocument: startPosition,
98
        endPositionOriginalDocument: endPosition,
99
        selectionLengthOriginalDocument: length,
100 ba94b8c1 Lukáš Vlček
        selectedText: text,
101 4bc99591 Lukáš Vlček
    };
102
}