Commit e225bc09 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Make sure imported annotations use the new document metadata

parent 1d26c5c6
import type { Annotation, APIAnnotationData } from '../../types/api'; import type { Annotation, APIAnnotationData } from '../../types/api';
import { quote } from '../helpers/annotation-metadata'; import { quote } from '../helpers/annotation-metadata';
import type { SidebarStore } from '../store'; import type { SidebarStore } from '../store';
import type { Frame } from '../store/modules/frames';
import type { AnnotationsService } from './annotations'; import type { AnnotationsService } from './annotations';
import type { ToastMessengerService } from './toast-messenger'; import type { ToastMessengerService } from './toast-messenger';
...@@ -35,15 +36,19 @@ type ImportData = Pick< ...@@ -35,15 +36,19 @@ type ImportData = Pick<
/** /**
* Return a copy of `ann` that contains only fields which can be preserved by * Return a copy of `ann` that contains only fields which can be preserved by
* an import performed on the client. * an import performed on the client, overwriting some of them with the ones
* from current frame, if provided
*/ */
function getImportData(ann: APIAnnotationData, uri?: string): ImportData { function getImportData(
ann: APIAnnotationData,
currentFrame: Frame | null,
): ImportData {
return { return {
target: ann.target, target: ann.target,
tags: ann.tags, tags: ann.tags,
text: ann.text, text: ann.text,
uri: uri ?? ann.uri, uri: currentFrame?.uri ?? ann.uri,
document: ann.document, document: currentFrame?.metadata ?? ann.document,
extra: { extra: {
source: 'import', source: 'import',
original_id: ann.id, original_id: ann.id,
...@@ -150,7 +155,7 @@ export class ImportAnnotationsService { ...@@ -150,7 +155,7 @@ export class ImportAnnotationsService {
this._store.beginImport(anns.length); this._store.beginImport(anns.length);
const existingAnns = this._store.allAnnotations(); const existingAnns = this._store.allAnnotations();
const currentUri = this._store.mainFrame()?.uri; const currentFrame = this._store.mainFrame();
const importAnn = async (ann: APIAnnotationData): Promise<ImportResult> => { const importAnn = async (ann: APIAnnotationData): Promise<ImportResult> => {
const existingAnn = existingAnns.find(ex => duplicateMatch(ann, ex)); const existingAnn = existingAnns.find(ex => duplicateMatch(ann, ex));
...@@ -161,7 +166,7 @@ export class ImportAnnotationsService { ...@@ -161,7 +166,7 @@ export class ImportAnnotationsService {
try { try {
// Strip out all the fields that are ignored in an import, and overwrite // Strip out all the fields that are ignored in an import, and overwrite
// the URI with current document's URI. // the URI with current document's URI.
const importData = getImportData(ann, currentUri); const importData = getImportData(ann, currentFrame);
// Fill out the annotation with default values for the current user and // Fill out the annotation with default values for the current user and
// group. // group.
......
...@@ -13,7 +13,7 @@ describe('ImportAnnotationsService', () => { ...@@ -13,7 +13,7 @@ describe('ImportAnnotationsService', () => {
allAnnotations: sinon.stub().returns([]), allAnnotations: sinon.stub().returns([]),
beginImport: sinon.stub(), beginImport: sinon.stub(),
completeImport: sinon.stub(), completeImport: sinon.stub(),
mainFrame: sinon.stub(), mainFrame: sinon.stub().returns(null),
}; };
fakeToastMessenger = { fakeToastMessenger = {
...@@ -59,13 +59,13 @@ describe('ImportAnnotationsService', () => { ...@@ -59,13 +59,13 @@ describe('ImportAnnotationsService', () => {
} }
/** Return the expected imported annotation for a given annotation. */ /** Return the expected imported annotation for a given annotation. */
function importedAnnotation(ann) { function importedAnnotation(ann, uri = undefined, document = undefined) {
return { return {
document: ann.document, document: document ?? ann.document,
tags: ann.tags, tags: ann.tags,
target: ann.target, target: ann.target,
text: ann.text, text: ann.text,
uri: ann.uri, uri: uri ?? ann.uri,
extra: { extra: {
source: 'import', source: 'import',
original_id: ann.id, original_id: ann.id,
...@@ -101,9 +101,13 @@ describe('ImportAnnotationsService', () => { ...@@ -101,9 +101,13 @@ describe('ImportAnnotationsService', () => {
}); });
}); });
it('overwrites annotation URI with current document one', async () => { it('sets annotation URI and document metadata to match current document', async () => {
const newUri = 'new_document_uri'; const newUri = 'new_document_uri';
fakeStore.mainFrame.returns({ uri: newUri }); const newTitle = 'new_document_title';
fakeStore.mainFrame.returns({
uri: newUri,
metadata: { title: newTitle },
});
const svc = createService(); const svc = createService();
const ann = generateAnnotation(); const ann = generateAnnotation();
...@@ -112,8 +116,7 @@ describe('ImportAnnotationsService', () => { ...@@ -112,8 +116,7 @@ describe('ImportAnnotationsService', () => {
assert.calledWith(fakeAnnotationsService.save, { assert.calledWith(fakeAnnotationsService.save, {
$tag: 'dummy', $tag: 'dummy',
...importedAnnotation(ann), ...importedAnnotation(ann, newUri, { title: newTitle }),
uri: newUri,
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment