Projekt

Obecné

Profil

Stáhnout (3.05 KB) Statistiky
| Větev: | Tag: | Revize:
1 ac59aec1 Dominik Poch
/**
2
 * Creates an annotation view of a document.
3
 * @returns The annotation view.
4
 */
5 61acc160 Dominik Poch
import { ChangeEvent, useContext } from 'react';
6 7652cf88 Lukáš Vlček
import { AnnotationContext } from '../../contexts/AnnotationContext';
7 61acc160 Dominik Poch
import { Button, Input, Space } from 'antd';
8 6973d036 Lukáš Vlček
import parse from 'html-react-parser';
9
import {
10 b80a6919 Lukáš Vlček
    COLOR_HIGHLIGHTED_TAG,
11 6973d036 Lukáš Vlček
    DOCUMENT_INSTANCE_ID_ATTRIBUTE_NAME,
12
    DOCUMENT_OCCURRENCE_ID_ATTRIBUTE_NAME,
13
    DOCUMENT_TAG_ID_ATTRIBUTE_NAME,
14
} from '../../constants';
15 7652cf88 Lukáš Vlček
16 6d10fe14 Dominik Poch
export function DocumentAnnotationView() {
17 6973d036 Lukáš Vlček
    const {
18
        annotation,
19
        setSelectedInstanceId,
20
        setSelectedOccurrenceId,
21
        selectedInstanceId,
22
        selectedOccurrenceId,
23 3533354f Vojtěch Bartička
        changeDocumentNote,
24 6973d036 Lukáš Vlček
    } = useContext(AnnotationContext);
25 7652cf88 Lukáš Vlček
26
    if (!annotation) {
27 c9762683 Vojtěch Bartička
        return <p>Probíhá načítání anotace ...</p>;
28 7652cf88 Lukáš Vlček
    }
29 6a250c18 Lukáš Vlček
30 61acc160 Dominik Poch
    const onChangeNote = (e: ChangeEvent<HTMLTextAreaElement>) => {
31
        changeDocumentNote(e.currentTarget.value);
32
    };
33 7652cf88 Lukáš Vlček
34 6973d036 Lukáš Vlček
    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 e02e004e Lukáš Vlček
51 6973d036 Lukáš Vlček
        setSelectedInstanceId(idInstance);
52
        setSelectedOccurrenceId(idOccurrence);
53
    }
54
55
    function getCss() {
56 e02e004e Lukáš Vlček
        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 6973d036 Lukáš Vlček
        if (selectedInstanceId) {
68 b80a6919 Lukáš Vlček
            css += `span[${DOCUMENT_INSTANCE_ID_ATTRIBUTE_NAME}="${selectedInstanceId}"] { background-color: ${COLOR_HIGHLIGHTED_TAG};  }\n`;
69 6973d036 Lukáš Vlček
        }
70
        if (selectedOccurrenceId) {
71
            css += `span[${DOCUMENT_OCCURRENCE_ID_ATTRIBUTE_NAME}="${selectedOccurrenceId}"] { border-bottom: 4px dotted;  }\n`;
72
        }
73
74 e02e004e Lukáš Vlček
        return <style dangerouslySetInnerHTML={{ __html: css }}></style>;
75
    }
76
77 ac59aec1 Dominik Poch
    return (
78
        <div>
79 6a250c18 Lukáš Vlček
            <div className="mb-3">
80 61acc160 Dominik Poch
                <h5>Poznámka</h5>
81
                <Input.TextArea
82
                    defaultValue={annotation.note ?? ''}
83
                    onBlur={onChangeNote}
84
                />
85
            </div>
86 6a250c18 Lukáš Vlček
            <hr />
87
88 e02e004e Lukáš Vlček
            {getCss()}
89 6973d036 Lukáš Vlček
            <div onClick={detectSelectedElement}>
90
                {parse(annotation.documentToRender ?? '')}
91
            </div>
92 ac59aec1 Dominik Poch
        </div>
93
    );
94
}