Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Add ignoring of unused drafts #16

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 36 additions & 24 deletions src/sdk/lsf-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,38 +210,50 @@ export class LSFWrapper {
let { annotationStore: cs } = this.lsf;
let annotation;
const activeDrafts = cs.annotations.map(a => a.draftId).filter(Boolean);
const applyDraft = (c, draft) => {
cs.selectAnnotation(c.id);
c.deserializeResults(draft.result);
c.setDraftId(draft.id);
};

if (this.task.drafts) {
const draftsByAnnotationId = {};
const separatedDrafts = [];

for (const draft of this.task.drafts) {
if (activeDrafts.includes(draft.id)) continue;
let c;

if (draft.annotation) {
// Annotation existed - add draft to existed annotation
const draftAnnotationPk = String(draft.annotation);

c = cs.annotations.find(c => c.pk === draftAnnotationPk);
if (c) {
c.addVersions({ draft: draft.result });
c.deleteAllRegions({ deleteReadOnly: true });
} else {
// that shouldn't happen
console.error(`No annotation found for pk=${draftAnnotationPk}`);
continue;
}
draftsByAnnotationId[String(draft.annotation)] = draft;
} else {
separatedDrafts.push(draft);
}
}

for (const [draftAnnotationPk, draft] of Object.entries(draftsByAnnotationId)) {
let c = cs.annotations.find(c => c.pk === draftAnnotationPk);

if (c) {
c.addVersions({ draft: draft.result });
c.deleteAllRegions({ deleteReadOnly: true });
applyDraft(c, draft);
} else {
// Annotation not found - create new annotation from draft
c = cs.addAnnotation({
draft: draft.result,
userGenerate: true,
createdBy: draft.created_username,
createdAgo: draft.created_ago,
});
// that shouldn't happen
console.error(`No annotation found for pk=${draftAnnotationPk}`);
continue;
}
cs.selectAnnotation(c.id);
c.deserializeResults(draft.result);
c.setDraftId(draft.id);
}
for (const draft of separatedDrafts) {
// Annotation not found - create new annotation from draft
const c = cs.addAnnotation({
draft: draft.result,
userGenerate: true,
createdBy: draft.created_username,
createdAgo: draft.created_ago,
});

applyDraft(c, draft);
}

}

const first = this.annotations[0];
Expand Down