Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 4bc99591

Přidáno uživatelem Lukáš Vlček před asi 2 roky(ů)

Adding tag/subtag to selected text

Zobrazit rozdíly:

webapp/contexts/AnnotationContext.tsx
1 1
import React, { createContext, useEffect, useState } from 'react';
2
import { AnnotationInfo, TagInstanceInfo } from '../api';
2
import { AnnotationInfo, ETagType, SubTagInfo, TagInfo, TagInstanceInfo } from '../api';
3 3
import { Tag } from '../components/types/tag';
4 4
import { annotationController } from '../controllers';
5
import { GetSelectionInfo } from '../utils/selectionUtils';
5 6

  
6 7
/**
7 8
 * Interface of an annotation context provider.
......
53 54
    annotation: AnnotationInfo | null;
54 55
    mappedTags: Tag[] | null;
55 56
    refreshAnnotation: () => void;
57

  
58
    markSelectedText: (
59
        tagId: string,
60
        subtagId: string | null,
61
        instanceID: string | null
62
    ) => void;
56 63
}
57 64

  
58 65
/**
......
119 126
    refreshAnnotation: () => {
120 127
        return;
121 128
    },
129

  
130
    markSelectedText: () => {
131
        return;
132
    },
122 133
});
123 134

  
124 135
/**
......
139 150

  
140 151
    const [mappedTags, setMappedTags] = useState<Tag[] | null>(null);
141 152

  
153
    async function markSelectedText(
154
        tagId: string,
155
        subtagId: string | null,
156
        instanceId: string | null
157
    ) {
158
        if (!annotation) {
159
            console.log('annotation not found');
160
            return;
161
        }
162

  
163
        const selectionInfo = GetSelectionInfo(annotation);
164
        if (!selectionInfo) {
165
            console.log(
166
                'not able to continue, selection processing not completed successfully'
167
            );
168
            return;
169
        }
170

  
171
        const id = subtagId ?? tagId;
172
        const type: ETagType = subtagId == null ? ETagType.Tag : ETagType.Subtag;
173

  
174
        const res = await annotationController.annotationAnnotationIdPost(
175
            props.annotationId,
176
            {
177
                id: id,
178
                instanceId,
179
                type,
180
                position: selectionInfo.startPositionOriginalDocument,
181
                length: selectionInfo.selectionLengthOriginalDocument,
182
            }
183
        );
184
        console.log('res');
185
        console.log(res);
186

  
187
        await refreshAnnotation();
188
    }
189

  
142 190
    /**
143 191
     * Default implementation of addOccurrence method.
144 192
     * @param tag The tag with new occurrence.
145 193
     */
146
    const addOccurrence = (tag: Tag) => {
147
        //TODO: Implement method (should use objects from server API)
194
    const addOccurrence = async (tag: Tag) => {
195
        await markSelectedText(tag.tagId, tag.subtagId ?? null, tag.instanceId);
148 196
    };
149 197

  
150 198
    /**
......
182 230
    };
183 231

  
184 232
    const remapAnnotations = (data: AnnotationInfo) => {
185
        let map = new Map<number, Tag>();
233
        let map = new Map<string, Tag>();
186 234
        data.tagInstances?.forEach((tagInstance) => {
187
            if (map.has(tagInstance.instance ?? 0)) {
188
                let tag = map.get(tagInstance.instance ?? 0);
235
            if (map.has(tagInstance.instance ?? '-')) {
236
                let tag = map.get(tagInstance.instance ?? '-');
189 237
                tag!.occurrences = [...tag!.occurrences, tagInstance];
190 238
            } else {
191
                map.set(tagInstance.position ?? 0, {
192
                    name: tagInstance.tagName ?? '',
239
                map.set(tagInstance.instance ?? '-', {
240
                    tagName: tagInstance.tagName ?? '',
241
                    subtagName: tagInstance.subTagName ?? null,
193 242
                    category: tagInstance.tagCategoryName ?? '',
194 243
                    visible: true,
195 244
                    occurrences: [tagInstance],
245
                    tagId: tagInstance.tagId ?? '',
246
                    instanceId: tagInstance.instance ?? '',
247
                    subtagId: tagInstance.subTagId ?? null,
196 248
                });
197 249
            }
198 250
        });
......
229 281
                refreshAnnotation,
230 282
                annotation,
231 283
                mappedTags,
284
                markSelectedText,
232 285
            }}
233 286
        >
234 287
            {props.children}

Také k dispozici: Unified diff