1
|
/**
|
2
|
* Creates an annotation view of a document.
|
3
|
* @returns The annotation view.
|
4
|
*/
|
5
|
import { useContext } from 'react';
|
6
|
import { AnnotationContext } from '../../contexts/AnnotationContext';
|
7
|
import { Button } from 'antd';
|
8
|
|
9
|
export function DocumentAnnotationView() {
|
10
|
const { annotation } = useContext(AnnotationContext);
|
11
|
|
12
|
if (!annotation) {
|
13
|
return <p>Došlo k chybě. Anotace nebyla nalezena</p>;
|
14
|
}
|
15
|
|
16
|
return (
|
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>
|
78
|
<div
|
79
|
dangerouslySetInnerHTML={{ __html: annotation.documentToRender ?? '' }}
|
80
|
/>
|
81
|
</div>
|
82
|
);
|
83
|
}
|