Projekt

Obecné

Profil

Stáhnout (6.25 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 } from 'antd';
6
import {
7
    PlusOutlined,
8
    EyeOutlined,
9
    DownOutlined,
10
    DeleteOutlined,
11
    TagOutlined,
12
} from '@ant-design/icons';
13
import { AnnotationContext } from '../../contexts/AnnotationContext';
14
import { TagInstanceInfo } from '../../api';
15

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

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

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

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

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

    
55
    /**
56
     * Changes visibility of this annotation in the context.
57
     */
58
    const onChangeVisibility = () => {
59
        changeVisibility(props.tag);
60
    };
61

    
62
    /**
63
     * Changes a position of an occurrence of this annotation in the context.
64
     * @param occurrence The occurrence that should be changed.
65
     */
66
    const onChangePosition =
67
        (occurrence: TagInstanceInfo) => (e: ChangeEvent<HTMLInputElement>) => {
68
            changePosition(occurrence, Number(e.currentTarget.value));
69
        };
70

    
71
    /**
72
     * Changes a length of an occurrence of this annotation in the context.
73
     * @param occurrence The occurrence that should be changed.
74
     */
75
    const onChangeLength =
76
        (occurrence: TagInstanceInfo) => (e: ChangeEvent<HTMLInputElement>) => {
77
            changeLength(occurrence, Number(e.currentTarget.value));
78
        };
79

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

    
87
    return (
88
        <Container>
89
            <Row className="border rounded">
90
                <Col sm="auto" className="d-flex align-items-center">
91
                    <TagOutlined />
92
                </Col>
93
                <Col className="d-flex align-items-center">
94
                    {props.tag.tagName}
95
                    {props.tag.subtagName ? ' (' + props.tag.subtagName + ')' : ''}
96
                </Col>
97
                <Col sm="auto">
98
                    <Button
99
                        type="text"
100
                        shape="circle"
101
                        icon={<PlusOutlined />}
102
                        onClick={onAddOccurrence}
103
                    />
104
                    <Button
105
                        type="text"
106
                        shape="circle"
107
                        icon={<EyeOutlined />}
108
                        onClick={onChangeVisibility}
109
                    />
110
                    <Button
111
                        type="text"
112
                        shape="circle"
113
                        icon={<DownOutlined />}
114
                        onClick={changePropertiesVisibility}
115
                    />
116
                </Col>
117
            </Row>
118
            {visibleProperties && (
119
                <Stack gap={1} className="mb-2">
120
                    <div>Kategorie: {props.tag.category}</div>
121
                    <div>Výskyty:</div>
122
                    {props.tag.occurrences.map((occurrence, index) => {
123
                        return (
124
                            <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
                        );
162
                    })}
163
                </Stack>
164
            )}
165
        </Container>
166
    );
167
}
(1-1/7)