1
|
import React, { createContext, useEffect, useState } from 'react';
|
2
|
import { TagInstanceInfo } from '../api';
|
3
|
import { Occurrence, Tag } from '../components/types/tag';
|
4
|
|
5
|
/**
|
6
|
* Interface of an annotation context provider.
|
7
|
*/
|
8
|
interface IAnnotationContextProvider {
|
9
|
/**
|
10
|
* Tags managed by the context.
|
11
|
*/
|
12
|
tags: TagInstanceInfo[] | null;
|
13
|
|
14
|
/**
|
15
|
* Sets new tags.
|
16
|
* @param newTags An array of new tags.
|
17
|
*/
|
18
|
setTags: (newTags: TagInstanceInfo[] | null) => void;
|
19
|
|
20
|
/**
|
21
|
* Adds occurrence to an annotation.
|
22
|
* @param tag Tag whose occurrence should be added.
|
23
|
*/
|
24
|
addOccurrence: (tag: Tag) => void;
|
25
|
|
26
|
/**
|
27
|
* Changes visibility of an annotation.
|
28
|
* @param tag Tag whose visibility should be changed.
|
29
|
*/
|
30
|
changeVisibility: (tag: Tag) => void;
|
31
|
|
32
|
/**
|
33
|
* Deletes an occurrence of an annotation.
|
34
|
* @param occurrence Occurrence that should be deleted.
|
35
|
*/
|
36
|
deleteOccurrence: (occurrence: Occurrence) => void;
|
37
|
|
38
|
/**
|
39
|
* Changes a position of an occurrence of an annotation.
|
40
|
* @param occurrence Occurrence whose position should be changed.
|
41
|
* @param newValue New value of the position.
|
42
|
*/
|
43
|
changePosition: (occurrence: Occurrence, newValue: number) => void;
|
44
|
|
45
|
/**
|
46
|
* Changes a length of an occurrence of an annotation.
|
47
|
* @param occurrence Occurrence whose length should be changed.
|
48
|
* @param newValue New value of the length.
|
49
|
*/
|
50
|
changeLength: (occurrence: Occurrence, newValue: number) => void;
|
51
|
}
|
52
|
|
53
|
/**
|
54
|
* The annotation context that manages active annotations.
|
55
|
*/
|
56
|
export const AnnotationContext = createContext<IAnnotationContextProvider>({
|
57
|
/**
|
58
|
* Default tags.
|
59
|
*/
|
60
|
tags: null,
|
61
|
|
62
|
/**
|
63
|
* Default implementation of setTags method.
|
64
|
* @param v Array of new tags.
|
65
|
*/
|
66
|
setTags: (v) => {
|
67
|
return;
|
68
|
},
|
69
|
|
70
|
/**
|
71
|
* Default implementation of addOccurrence method.
|
72
|
* @param tag The tag with new occurrence.
|
73
|
*/
|
74
|
addOccurrence: (tag: Tag) => {
|
75
|
return;
|
76
|
},
|
77
|
|
78
|
/**
|
79
|
* Default implementation of changeVisibility method.
|
80
|
* @param tag The tag whose visibility should be changed.
|
81
|
*/
|
82
|
changeVisibility: (tag: Tag) => {
|
83
|
return;
|
84
|
},
|
85
|
|
86
|
/**
|
87
|
* Default implementation of deleteOccurrence method.
|
88
|
* @param occurrence Occurrence that should be deleted.
|
89
|
*/
|
90
|
deleteOccurrence: (occurrence: Occurrence) => {
|
91
|
return;
|
92
|
},
|
93
|
|
94
|
/**
|
95
|
* Default implementation of changePosition method.
|
96
|
* @param occurrence Occurrence whose position should be changed.
|
97
|
* @param newValue A new position.
|
98
|
*/
|
99
|
changePosition: (occurrence: Occurrence, newValue: number) => {
|
100
|
return;
|
101
|
},
|
102
|
|
103
|
/**
|
104
|
* Default implementation of changeLength method.
|
105
|
* @param occurrence Occurrence whose length should be changed.
|
106
|
* @param newValue A new length.
|
107
|
*/
|
108
|
changeLength: (occurrence: Occurrence, newValue: number) => {
|
109
|
return;
|
110
|
},
|
111
|
});
|
112
|
|
113
|
/**
|
114
|
* Provider of the annotation context.
|
115
|
* @param props Children that should have access to the annotation context.
|
116
|
* @returns Prepared html of the provider.
|
117
|
*/
|
118
|
const AnnotationProvider = (props: {
|
119
|
children: React.ReactNode;
|
120
|
annotationId: string;
|
121
|
}) => {
|
122
|
/**
|
123
|
* Tags managed by the context.
|
124
|
*/
|
125
|
const [tags, setTags] = useState<TagInstanceInfo[] | null>(null);
|
126
|
|
127
|
/**
|
128
|
* Default implementation of addOccurrence method.
|
129
|
* @param tag The tag with new occurrence.
|
130
|
*/
|
131
|
const addOccurrence = (tag: Tag) => {
|
132
|
//TODO: Implement method (should use objects from server API)
|
133
|
};
|
134
|
|
135
|
/**
|
136
|
* Changes visibility of an annotation.
|
137
|
* @param tag Tag whose visibility should be changed.
|
138
|
*/
|
139
|
const changeVisibility = (tag: Tag) => {
|
140
|
//TODO: Implement method (should use objects from server API)
|
141
|
};
|
142
|
|
143
|
/**
|
144
|
* Deletes an occurrence of an annotation.
|
145
|
* @param occurrence Occurrence that should be deleted.
|
146
|
*/
|
147
|
const deleteOccurrence = (occurrence: Occurrence) => {
|
148
|
//TODO: Implement method (should use objects from server API)
|
149
|
};
|
150
|
|
151
|
/**
|
152
|
* Changes a position of an occurrence of an annotation.
|
153
|
* @param occurrence Occurrence whose position should be changed.
|
154
|
* @param newValue New value of the position.
|
155
|
*/
|
156
|
const changePosition = (occurrence: Occurrence, newValue: number) => {
|
157
|
//TODO: Implement method (should use objects from server API)
|
158
|
};
|
159
|
|
160
|
/**
|
161
|
* Changes a length of an occurrence of an annotation.
|
162
|
* @param occurrence Occurrence whose length should be changed.
|
163
|
* @param newValue New value of the length.
|
164
|
*/
|
165
|
const changeLength = (occurrence: Occurrence, newValue: number) => {
|
166
|
//TODO: Implement method (should use objects from server API)
|
167
|
};
|
168
|
|
169
|
/**
|
170
|
* Initializes the context.
|
171
|
*/
|
172
|
useEffect(() => {
|
173
|
//TODO: Implement initialization with values from the server
|
174
|
}, [props.annotationId]);
|
175
|
|
176
|
return (
|
177
|
<AnnotationContext.Provider
|
178
|
value={{
|
179
|
tags,
|
180
|
setTags,
|
181
|
addOccurrence,
|
182
|
changeVisibility,
|
183
|
deleteOccurrence,
|
184
|
changeLength,
|
185
|
changePosition,
|
186
|
}}
|
187
|
>
|
188
|
{props.children}
|
189
|
</AnnotationContext.Provider>
|
190
|
);
|
191
|
};
|
192
|
|
193
|
export default AnnotationProvider;
|