Projekt

Obecné

Profil

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