Commit 202e8670 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Overwrite imported annotations URI with target document one

parent da7b94c9
...@@ -37,12 +37,12 @@ type ImportData = Pick< ...@@ -37,12 +37,12 @@ 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.
*/ */
function getImportData(ann: APIAnnotationData): ImportData { function getImportData(ann: APIAnnotationData, uri?: string): ImportData {
return { return {
target: ann.target, target: ann.target,
tags: ann.tags, tags: ann.tags,
text: ann.text, text: ann.text,
uri: ann.uri, uri: uri ?? ann.uri,
document: ann.document, document: ann.document,
extra: { extra: {
source: 'import', source: 'import',
...@@ -150,6 +150,7 @@ export class ImportAnnotationsService { ...@@ -150,6 +150,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 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));
...@@ -158,8 +159,9 @@ export class ImportAnnotationsService { ...@@ -158,8 +159,9 @@ export class ImportAnnotationsService {
} }
try { try {
// Strip out all the fields that are ignored in an import. // Strip out all the fields that are ignored in an import, and overwrite
const importData = getImportData(ann); // the URI with current document's URI.
const importData = getImportData(ann, currentUri);
// 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,6 +13,7 @@ describe('ImportAnnotationsService', () => { ...@@ -13,6 +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(),
}; };
fakeToastMessenger = { fakeToastMessenger = {
...@@ -100,6 +101,22 @@ describe('ImportAnnotationsService', () => { ...@@ -100,6 +101,22 @@ describe('ImportAnnotationsService', () => {
}); });
}); });
it('overwrites annotation URI with current document one', async () => {
const newUri = 'new_document_uri';
fakeStore.mainFrame.returns({ uri: newUri });
const svc = createService();
const ann = generateAnnotation();
await svc.import([ann]);
assert.calledWith(fakeAnnotationsService.save, {
$tag: 'dummy',
...ann,
uri: newUri,
});
});
it('can save many annotations', async () => { it('can save many annotations', async () => {
const svc = createService(); const svc = createService();
const anns = []; const anns = [];
......
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