Projekt

Obecné

Profil

Stáhnout (2.59 KB) Statistiky
| Větev: | Tag: | Revize:
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 parse from 'html-react-parser';
8
import {
9
    COLOR_HIGHLIGHTED_TAG,
10
    DOCUMENT_INSTANCE_ID_ATTRIBUTE_NAME,
11
    DOCUMENT_OCCURRENCE_ID_ATTRIBUTE_NAME,
12
    DOCUMENT_TAG_ID_ATTRIBUTE_NAME,
13
} from '../../constants';
14

    
15
export function DocumentAnnotationView() {
16
    const {
17
        annotation,
18
        setSelectedInstanceId,
19
        setSelectedOccurrenceId,
20
        selectedInstanceId,
21
        selectedOccurrenceId,
22
    } = useContext(AnnotationContext);
23

    
24
    if (!annotation) {
25
        return <p>Probíhá načítání anotace ...</p>;
26
    }
27

    
28
    function detectSelectedElement(e: any) {
29
        if (!(e.target instanceof HTMLElement)) {
30
            return;
31
        }
32

    
33
        const target = e.target as HTMLElement;
34

    
35
        const id = target.getAttribute(DOCUMENT_TAG_ID_ATTRIBUTE_NAME);
36
        const idInstance = target.getAttribute(DOCUMENT_INSTANCE_ID_ATTRIBUTE_NAME);
37
        const idOccurrence = target.getAttribute(DOCUMENT_OCCURRENCE_ID_ATTRIBUTE_NAME);
38

    
39
        if (!id || !idInstance || !idOccurrence) {
40
            setSelectedInstanceId(null);
41
            setSelectedOccurrenceId(null);
42
            return;
43
        }
44

    
45
        setSelectedInstanceId(idInstance);
46
        setSelectedOccurrenceId(idOccurrence);
47
    }
48

    
49
    function getCss() {
50
        let css = 'span.annotation {border-bottom: 2px solid;}\n';
51
        css += 'span {line-height: 30px}\n';
52
        css +=
53
            'span[end="1"] {border-end-end-radius: 0px; border-right: 1px solid darkgray}\n';
54
        css +=
55
            'span[start="1"] {border-end-start-radius: 0px; border-left: 1px solid darkgray}\n';
56

    
57
        for (const info of annotation?.cssInfo ?? []) {
58
            css += `span[${DOCUMENT_INSTANCE_ID_ATTRIBUTE_NAME}="${info.instanceId}"] { border-color:${info.color}; padding-bottom: ${info.padding}px }\n`;
59
        }
60

    
61
        if (selectedInstanceId) {
62
            css += `span[${DOCUMENT_INSTANCE_ID_ATTRIBUTE_NAME}="${selectedInstanceId}"] { background-color: ${COLOR_HIGHLIGHTED_TAG};  }\n`;
63
        }
64
        if (selectedOccurrenceId) {
65
            css += `span[${DOCUMENT_OCCURRENCE_ID_ATTRIBUTE_NAME}="${selectedOccurrenceId}"] { border-bottom: 4px dotted;  }\n`;
66
        }
67

    
68
        return <style dangerouslySetInnerHTML={{ __html: css }}></style>;
69
    }
70

    
71
    return (
72
        <div>
73
            {getCss()}
74
            <div onClick={detectSelectedElement}>
75
                {parse(annotation.documentToRender ?? '')}
76
            </div>
77
        </div>
78
    );
79
}
(5-5/8)