Projekt

Obecné

Profil

Stáhnout (8.55 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Col, Container, Row, Stack } from 'react-bootstrap';
2
import { Tag } from '../types/tag';
3
import { ChangeEvent, useContext, useState } from 'react';
4
import 'antd/dist/antd.css';
5
import { Button, Input, Select } from 'antd';
6
import {
7
    PlusOutlined,
8
    DownOutlined,
9
    DeleteOutlined,
10
    TagOutlined,
11
} from '@ant-design/icons';
12
import { AnnotationContext } from '../../contexts/AnnotationContext';
13
import { ETagSentiment, TagInstanceInfo } from '../../api';
14

    
15
const { Option } = Select;
16

    
17
/**
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
export function AnnotationItem(props: { tag: Tag }) {
23
    const { markSelectedText } = useContext(AnnotationContext);
24

    
25
    /**
26
     * Should properties of this annotation be visible?
27
     */
28
    const [visibleProperties, setVisibleProperties] = useState(false);
29

    
30
    /**
31
     * Context that manages annotations.
32
     */
33
    const {
34
        addOccurrence,
35
        deleteOccurrence,
36
        changePosition,
37
        changeSentiment,
38
        changeLength,
39
    } = useContext(AnnotationContext);
40

    
41
    /**
42
     * Adds new occurrence of this annotation to the context.
43
     */
44
    const onAddOccurrence = () => {
45
        addOccurrence(props.tag);
46
    };
47

    
48
    /**
49
     * Removes an occurrence of this annotation from the context.
50
     * @param occurrence The occurrence that should be removed.
51
     */
52
    const onDeleteOccurrence = (occurrence: TagInstanceInfo) => (e: any) => {
53
        deleteOccurrence(occurrence);
54
    };
55

    
56
    /**
57
     * Changes a position of an occurrence of this annotation in the context.
58
     * @param occurrence The occurrence that should be changed.
59
     */
60
    const onChangePosition =
61
        (occurrence: TagInstanceInfo) => (e: ChangeEvent<HTMLInputElement>) => {
62
            changePosition(occurrence, Number(e.currentTarget.value));
63
        };
64

    
65
    /**
66
     * Changes a length of an occurrence of this annotation in the context.
67
     * @param occurrence The occurrence that should be changed.
68
     */
69
    const onChangeLength =
70
        (occurrence: TagInstanceInfo) => (e: ChangeEvent<HTMLInputElement>) => {
71
            changeLength(occurrence, Number(e.currentTarget.value));
72
        };
73

    
74
    const onChangeSentiment = (occurrence: TagInstanceInfo) => (val: ETagSentiment) => {
75
        changeSentiment(occurrence, val);
76
    };
77

    
78
    /**
79
     * Changes visibility of properties of this annotation.
80
     */
81
    const changePropertiesVisibility = () => {
82
        setVisibleProperties(!visibleProperties);
83
    };
84

    
85
    return (
86
        <Container>
87
            <Row className="border rounded">
88
                <Col sm="auto" className="d-flex align-items-center">
89
                    <TagOutlined />
90
                </Col>
91
                <Col className="d-flex align-items-center">
92
                    {props.tag.tagName}
93
                    {props.tag.subtagName ? ' (' + props.tag.subtagName + ')' : ''}
94
                </Col>
95
                <Col sm="auto">
96
                    <Button
97
                        type="text"
98
                        shape="circle"
99
                        icon={<PlusOutlined />}
100
                        onClick={onAddOccurrence}
101
                    />
102
                    <Button
103
                        type="text"
104
                        shape="circle"
105
                        icon={<DownOutlined />}
106
                        onClick={changePropertiesVisibility}
107
                    />
108
                </Col>
109
            </Row>
110
            {visibleProperties && (
111
                <Stack gap={1} className="mb-2">
112
                    <div>Kategorie: {props.tag.category}</div>
113
                    <div>Výskyty:</div>
114
                    {props.tag.occurrences.map((occurrence, index) => {
115
                        return (
116
                            <Container key={index} className="shadow-sm pb-1 pt-1">
117
                                <Row>
118
                                    <Col>
119
                                        <Row>
120
                                            <Col
121
                                                className="d-flex align-items-center"
122
                                                sm="4"
123
                                            >
124
                                                Pozice:
125
                                            </Col>
126
                                            <Col>
127
                                                <Input
128
                                                    value={occurrence.position}
129
                                                    onChange={onChangePosition(
130
                                                        occurrence
131
                                                    )}
132
                                                />
133
                                            </Col>
134
                                        </Row>
135
                                        <Row>
136
                                            <Col
137
                                                className="d-flex align-items-center"
138
                                                sm="4"
139
                                            >
140
                                                Délka:
141
                                            </Col>
142
                                            <Col>
143
                                                <Input
144
                                                    value={occurrence.length}
145
                                                    onChange={onChangeLength(occurrence)}
146
                                                />
147
                                            </Col>
148
                                        </Row>
149
                                        {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
                                    </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
                        );
195
                    })}
196
                </Stack>
197
            )}
198
        </Container>
199
    );
200
}
(1-1/7)