Projekt

Obecné

Profil

Stáhnout (6.25 KB) Statistiky
| Větev: | Tag: | Revize:
1 4e91ed40 Dominik Poch
import { Col, Container, Row, Stack } from 'react-bootstrap';
2 76242338 Dominik Poch
import { Tag } from '../types/tag';
3 c73aecde Dominik Poch
import { ChangeEvent, useContext, useState } from 'react';
4 967f45fc Dominik Poch
import 'antd/dist/antd.css';
5 4e91ed40 Dominik Poch
import { Button, Input } from 'antd';
6 967f45fc Dominik Poch
import {
7
    PlusOutlined,
8
    EyeOutlined,
9
    DownOutlined,
10
    DeleteOutlined,
11 76242338 Dominik Poch
    TagOutlined,
12 967f45fc Dominik Poch
} from '@ant-design/icons';
13 c73aecde Dominik Poch
import { AnnotationContext } from '../../contexts/AnnotationContext';
14 76242338 Dominik Poch
import { TagInstanceInfo } from '../../api';
15 967f45fc Dominik Poch
16 acb8a961 Dominik Poch
/**
17
 * Creates a single item in an annotation panel.
18
 * @param props Properties should contain a tag that will be shown in this annotation.
19
 * @returns The item that represents an annotation.
20
 */
21 c73aecde Dominik Poch
export function AnnotationItem(props: { tag: Tag }) {
22 4bc99591 Lukáš Vlček
    const { markSelectedText } = useContext(AnnotationContext);
23
24 acb8a961 Dominik Poch
    /**
25
     * Should properties of this annotation be visible?
26
     */
27 967f45fc Dominik Poch
    const [visibleProperties, setVisibleProperties] = useState(false);
28 acb8a961 Dominik Poch
29
    /**
30
     * Context that manages annotations.
31
     */
32 c73aecde Dominik Poch
    const {
33
        addOccurrence,
34
        deleteOccurrence,
35
        changeVisibility,
36
        changePosition,
37
        changeLength,
38
    } = useContext(AnnotationContext);
39 967f45fc Dominik Poch
40 acb8a961 Dominik Poch
    /**
41
     * Adds new occurrence of this annotation to the context.
42
     */
43 c73aecde Dominik Poch
    const onAddOccurrence = () => {
44
        addOccurrence(props.tag);
45 4e91ed40 Dominik Poch
    };
46 967f45fc Dominik Poch
47 acb8a961 Dominik Poch
    /**
48
     * Removes an occurrence of this annotation from the context.
49
     * @param occurrence The occurrence that should be removed.
50
     */
51 76242338 Dominik Poch
    const onDeleteOccurrence = (occurrence: TagInstanceInfo) => (e: any) => {
52 c73aecde Dominik Poch
        deleteOccurrence(occurrence);
53 4e91ed40 Dominik Poch
    };
54 967f45fc Dominik Poch
55 acb8a961 Dominik Poch
    /**
56
     * Changes visibility of this annotation in the context.
57
     */
58 c73aecde Dominik Poch
    const onChangeVisibility = () => {
59
        changeVisibility(props.tag);
60 967f45fc Dominik Poch
    };
61
62 acb8a961 Dominik Poch
    /**
63
     * Changes a position of an occurrence of this annotation in the context.
64
     * @param occurrence The occurrence that should be changed.
65
     */
66 c73aecde Dominik Poch
    const onChangePosition =
67 76242338 Dominik Poch
        (occurrence: TagInstanceInfo) => (e: ChangeEvent<HTMLInputElement>) => {
68 c73aecde Dominik Poch
            changePosition(occurrence, Number(e.currentTarget.value));
69 4e91ed40 Dominik Poch
        };
70
71 acb8a961 Dominik Poch
    /**
72
     * Changes a length of an occurrence of this annotation in the context.
73
     * @param occurrence The occurrence that should be changed.
74
     */
75 c73aecde Dominik Poch
    const onChangeLength =
76 76242338 Dominik Poch
        (occurrence: TagInstanceInfo) => (e: ChangeEvent<HTMLInputElement>) => {
77 c73aecde Dominik Poch
            changeLength(occurrence, Number(e.currentTarget.value));
78 4e91ed40 Dominik Poch
        };
79
80 acb8a961 Dominik Poch
    /**
81
     * Changes visibility of properties of this annotation.
82
     */
83 c73aecde Dominik Poch
    const changePropertiesVisibility = () => {
84
        setVisibleProperties(!visibleProperties);
85
    };
86
87 967f45fc Dominik Poch
    return (
88 c73aecde Dominik Poch
        <Container>
89 76242338 Dominik Poch
            <Row className="border rounded">
90
                <Col sm="auto" className="d-flex align-items-center">
91
                    <TagOutlined />
92
                </Col>
93 4bc99591 Lukáš Vlček
                <Col className="d-flex align-items-center">
94
                    {props.tag.tagName}
95
                    {props.tag.subtagName ? ' (' + props.tag.subtagName + ')' : ''}
96
                </Col>
97 4e91ed40 Dominik Poch
                <Col sm="auto">
98
                    <Button
99
                        type="text"
100
                        shape="circle"
101
                        icon={<PlusOutlined />}
102 c73aecde Dominik Poch
                        onClick={onAddOccurrence}
103 4e91ed40 Dominik Poch
                    />
104
                    <Button
105
                        type="text"
106
                        shape="circle"
107
                        icon={<EyeOutlined />}
108 c73aecde Dominik Poch
                        onClick={onChangeVisibility}
109 4e91ed40 Dominik Poch
                    />
110
                    <Button
111
                        type="text"
112
                        shape="circle"
113
                        icon={<DownOutlined />}
114
                        onClick={changePropertiesVisibility}
115
                    />
116
                </Col>
117 967f45fc Dominik Poch
            </Row>
118
            {visibleProperties && (
119 76242338 Dominik Poch
                <Stack gap={1} className="mb-2">
120 4e91ed40 Dominik Poch
                    <div>Kategorie: {props.tag.category}</div>
121
                    <div>Výskyty:</div>
122
                    {props.tag.occurrences.map((occurrence, index) => {
123 967f45fc Dominik Poch
                        return (
124 76242338 Dominik Poch
                            <Container key={index} className="shadow-sm">
125
                                <Row className="mb-1 mt-1">
126
                                    <Col>
127
                                        <Row>
128
                                            <Col className="d-flex align-items-center">
129
                                                Pozice:
130
                                            </Col>
131
                                            <Col sm="auto">
132
                                                <Input
133
                                                    value={occurrence.position}
134
                                                    onChange={onChangePosition(
135
                                                        occurrence
136
                                                    )}
137
                                                />
138
                                            </Col>
139
                                        </Row>
140
                                        <Row>
141
                                            <Col className="d-flex align-items-center">
142
                                                Délka:
143
                                            </Col>
144
                                            <Col sm="auto">
145
                                                <Input
146
                                                    value={occurrence.length}
147
                                                    onChange={onChangeLength(occurrence)}
148
                                                />
149
                                            </Col>
150
                                        </Row>
151
                                    </Col>
152
                                    <Col sm="auto" className="d-flex align-items-center">
153
                                        <Button
154
                                            icon={<DeleteOutlined />}
155
                                            onClick={onDeleteOccurrence(occurrence)}
156
                                            danger
157
                                        ></Button>
158
                                    </Col>
159
                                </Row>
160
                            </Container>
161 967f45fc Dominik Poch
                        );
162
                    })}
163 4e91ed40 Dominik Poch
                </Stack>
164 967f45fc Dominik Poch
            )}
165 c73aecde Dominik Poch
        </Container>
166 967f45fc Dominik Poch
    );
167
}