1
|
/**
|
2
|
* Creates an annotation view of a document.
|
3
|
* @returns The annotation view.
|
4
|
*/
|
5
|
import { ChangeEvent, useContext } from 'react';
|
6
|
import { AnnotationContext } from '../../contexts/AnnotationContext';
|
7
|
import { Button, Input, Space } from 'antd';
|
8
|
import parse from 'html-react-parser';
|
9
|
import {
|
10
|
COLOR_HIGHLIGHTED_TAG,
|
11
|
DOCUMENT_INSTANCE_ID_ATTRIBUTE_NAME,
|
12
|
DOCUMENT_OCCURRENCE_ID_ATTRIBUTE_NAME,
|
13
|
DOCUMENT_TAG_ID_ATTRIBUTE_NAME,
|
14
|
} from '../../constants';
|
15
|
|
16
|
export function DocumentAnnotationView() {
|
17
|
const {
|
18
|
annotation,
|
19
|
setSelectedInstanceId,
|
20
|
setSelectedOccurrenceId,
|
21
|
selectedInstanceId,
|
22
|
selectedOccurrenceId,
|
23
|
changeDocumentNote,
|
24
|
} = useContext(AnnotationContext);
|
25
|
|
26
|
if (!annotation) {
|
27
|
return <p>Probíhá načítání anotace ...</p>;
|
28
|
}
|
29
|
|
30
|
const onChangeNote = (e: ChangeEvent<HTMLTextAreaElement>) => {
|
31
|
changeDocumentNote(e.currentTarget.value);
|
32
|
};
|
33
|
|
34
|
function detectSelectedElement(e: any) {
|
35
|
if (!(e.target instanceof HTMLElement)) {
|
36
|
return;
|
37
|
}
|
38
|
|
39
|
const target = e.target as HTMLElement;
|
40
|
|
41
|
const id = target.getAttribute(DOCUMENT_TAG_ID_ATTRIBUTE_NAME);
|
42
|
const idInstance = target.getAttribute(DOCUMENT_INSTANCE_ID_ATTRIBUTE_NAME);
|
43
|
const idOccurrence = target.getAttribute(DOCUMENT_OCCURRENCE_ID_ATTRIBUTE_NAME);
|
44
|
|
45
|
if (!id || !idInstance || !idOccurrence) {
|
46
|
setSelectedInstanceId(null);
|
47
|
setSelectedOccurrenceId(null);
|
48
|
return;
|
49
|
}
|
50
|
|
51
|
setSelectedInstanceId(idInstance);
|
52
|
setSelectedOccurrenceId(idOccurrence);
|
53
|
}
|
54
|
|
55
|
function getCss() {
|
56
|
let css = 'span.annotation {border-bottom: 2px solid;}\n';
|
57
|
css += 'span {line-height: 30px}\n';
|
58
|
css +=
|
59
|
'span[end="1"] {border-end-end-radius: 0px; border-right: 1px solid darkgray}\n';
|
60
|
css +=
|
61
|
'span[start="1"] {border-end-start-radius: 0px; border-left: 1px solid darkgray}\n';
|
62
|
|
63
|
for (const info of annotation?.cssInfo ?? []) {
|
64
|
css += `span[${DOCUMENT_INSTANCE_ID_ATTRIBUTE_NAME}="${info.instanceId}"] { border-color:${info.color}; padding-bottom: ${info.padding}px }\n`;
|
65
|
}
|
66
|
|
67
|
if (selectedInstanceId) {
|
68
|
css += `span[${DOCUMENT_INSTANCE_ID_ATTRIBUTE_NAME}="${selectedInstanceId}"] { background-color: ${COLOR_HIGHLIGHTED_TAG}; }\n`;
|
69
|
}
|
70
|
if (selectedOccurrenceId) {
|
71
|
css += `span[${DOCUMENT_OCCURRENCE_ID_ATTRIBUTE_NAME}="${selectedOccurrenceId}"] { border-bottom: 4px dotted; }\n`;
|
72
|
}
|
73
|
|
74
|
return <style dangerouslySetInnerHTML={{ __html: css }}></style>;
|
75
|
}
|
76
|
|
77
|
return (
|
78
|
<div>
|
79
|
<div className="mb-3">
|
80
|
<h5>Poznámka</h5>
|
81
|
<Input.TextArea
|
82
|
defaultValue={annotation.note ?? ''}
|
83
|
onBlur={onChangeNote}
|
84
|
/>
|
85
|
</div>
|
86
|
<hr />
|
87
|
|
88
|
{getCss()}
|
89
|
<div onClick={detectSelectedElement}>
|
90
|
{parse(annotation.documentToRender ?? '')}
|
91
|
</div>
|
92
|
</div>
|
93
|
);
|
94
|
}
|