Revize 3f66b624
Přidáno uživatelem Lukáš Vlček před více než 2 roky(ů)
webapp/utils/selectionUtils.ts | ||
---|---|---|
7 | 7 |
endPositionOriginalDocument: number; |
8 | 8 |
selectionLengthOriginalDocument: number; |
9 | 9 |
} |
10 |
const idAttributeName = 'aswi-tag-id'; |
|
11 |
|
|
10 | 12 |
export function GetSelectionInfo(annotation: AnnotationInfo): SelectionInfo | null { |
11 | 13 |
const selection = window.getSelection(); |
12 | 14 |
if (!selection) { |
13 | 15 |
return null; |
14 | 16 |
} |
17 |
|
|
15 | 18 |
if (!annotation?.tagStartPositions || !annotation.tagLengths) { |
19 |
// invalid data provided from API |
|
16 | 20 |
console.log('start or lengths not found'); |
17 | 21 |
return null; |
18 | 22 |
} |
19 | 23 |
|
20 |
let startTag = selection.anchorNode; |
|
21 |
let endTag = selection.focusNode; |
|
24 |
let startTag = selection.anchorNode; // anchor node = node where selection started
|
|
25 |
let endTag = selection.focusNode; // focus node = node where selection ended
|
|
22 | 26 |
|
23 | 27 |
if (!startTag || !endTag) { |
24 | 28 |
console.log('Selection not found'); |
... | ... | |
26 | 30 |
} |
27 | 31 |
|
28 | 32 |
if (startTag.nodeName.includes('#')) { |
33 |
// it is not an element (usually #text) |
|
29 | 34 |
startTag = startTag.parentNode; |
30 | 35 |
} |
31 | 36 |
if (endTag.nodeName.includes('#')) { |
37 |
// it is not an element (usually #text) |
|
32 | 38 |
endTag = endTag.parentNode; |
33 | 39 |
} |
40 |
|
|
34 | 41 |
if (!startTag || !endTag) { |
35 | 42 |
console.log('Selection element not found'); |
36 | 43 |
return null; |
... | ... | |
48 | 55 |
let startElement = startTag as Element; |
49 | 56 |
let endElement = endTag as Element; |
50 | 57 |
|
51 |
let startId: number = Number(startElement.getAttribute('aswi-tag-id')) ?? -1;
|
|
52 |
let endId: number = Number(endElement.getAttribute('aswi-tag-id')) ?? -1;
|
|
58 |
let startId: number = Number(startElement.getAttribute(idAttributeName)) ?? -1;
|
|
59 |
let endId: number = Number(endElement.getAttribute(idAttributeName)) ?? -1;
|
|
53 | 60 |
|
54 | 61 |
let startPosition = |
55 | 62 |
annotation.tagStartPositions[startId] + |
Také k dispozici: Unified diff
Comments added to selection utils