Revize c2192cda
Přidáno uživatelem Lukáš Vlček před téměř 3 roky(ů)
webapp/components/annotation/DocumentAnnotationView.tsx | ||
---|---|---|
15 | 15 |
|
16 | 16 |
return ( |
17 | 17 |
<div> |
18 |
<Button |
|
19 |
onClick={() => { |
|
20 |
const selection = window.getSelection(); |
|
21 |
if (!selection) { |
|
22 |
return; |
|
23 |
} |
|
24 |
if (!annotation?.tagStartPositions || !annotation.tagLengths) { |
|
25 |
console.log('start or lengths not found'); |
|
26 |
return; |
|
27 |
} |
|
28 |
|
|
29 |
let startTag = selection.anchorNode; |
|
30 |
let endTag = selection.focusNode; |
|
31 |
|
|
32 |
if (!startTag || !endTag) { |
|
33 |
console.log('Selection not found'); |
|
34 |
return; |
|
35 |
} |
|
36 |
|
|
37 |
if (startTag.nodeName.includes('#')) { |
|
38 |
startTag = startTag.parentNode; |
|
39 |
} |
|
40 |
if (endTag.nodeName.includes('#')) { |
|
41 |
endTag = endTag.parentNode; |
|
42 |
} |
|
43 |
if (!startTag || !endTag) { |
|
44 |
console.log('Selection element not found'); |
|
45 |
return; |
|
46 |
} |
|
47 |
|
|
48 |
if (!(startTag instanceof Element)) { |
|
49 |
console.log('StartTag is not instance of Element'); |
|
50 |
return; |
|
51 |
} |
|
52 |
if (!(endTag instanceof Element)) { |
|
53 |
console.log('EndTag is not instance of Element'); |
|
54 |
return; |
|
55 |
} |
|
56 |
|
|
57 |
const startElement = startTag as Element; |
|
58 |
const endElement = endTag as Element; |
|
59 |
|
|
60 |
const startId: number = |
|
61 |
Number(startElement.getAttribute('aswi-tag-id')) ?? -1; |
|
62 |
const endId: number = |
|
63 |
Number(endElement.getAttribute('aswi-tag-id')) ?? -1; |
|
64 |
|
|
65 |
const startPosition = |
|
66 |
annotation.tagStartPositions[startId] + |
|
67 |
annotation.tagLengths[startId] + |
|
68 |
selection.anchorOffset; |
|
69 |
const endPosition = |
|
70 |
annotation.tagStartPositions[endId] + |
|
71 |
annotation.tagLengths[endId] + |
|
72 |
selection.focusOffset - |
|
73 |
1; |
|
74 |
}} |
|
75 |
> |
|
76 |
Test |
|
77 |
</Button> |
|
18 | 78 |
<div |
19 | 79 |
dangerouslySetInnerHTML={{ __html: annotation.documentToRender ?? '' }} |
20 | 80 |
/> |
Také k dispozici: Unified diff
Selection detection and calculation of start and end position in original document