Skip to content

Commit

Permalink
Closes #361
Browse files Browse the repository at this point in the history
  • Loading branch information
rsimon committed Jan 24, 2024
1 parent b503b53 commit 56d5ab5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
14 changes: 11 additions & 3 deletions packages/annotorious/src/model/w3c/W3CImageFormatAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const parseW3CImageAnnotation = (
created: created ? new Date(created) : undefined,
creator: parseW3CUser(creator),
updated: modified ? new Date(modified) : undefined,
...rest.target,
...(Array.isArray(rest.target) ? rest.target[0] : rest.target),
annotation: annotationId,
selector
}
Expand Down Expand Up @@ -91,7 +91,7 @@ export const serializeW3CImageAnnotation = (
serializeFragmentSelector(selector.geometry as RectangleGeometry) :
serializeSVGSelector(selector);

return {
const serialized = {
...annotation,
'@context': 'http://www.w3.org/ns/anno.jsonld',
id: annotation.id,
Expand All @@ -105,5 +105,13 @@ export const serializeW3CImageAnnotation = (
source,
selector: w3CSelector
}
}
} as W3CImageAnnotation;

// Remove core properties that should not appear in the W3C annotation
delete serialized.bodies;

if ('annotation' in serialized.target)
delete serialized.target.annotation;

return serialized;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect } from 'vitest';
import { ShapeType } from '../../../src/model';
import { parseW3CImageAnnotation } from '../../../src/model';
import { parseW3CImageAnnotation, serializeW3CImageAnnotation} from '../../../src/model';

import { annotations } from './fixtures';

Expand All @@ -20,3 +20,21 @@ describe('parseW3CImageAnnotation', () => {
});
});

describe('serializeW3CImageAnnotation', () => {
it('should serialize the sample annotations correctly', () => {
// @ts-ignore
const parsed = annotations.map(a => parseW3CImageAnnotation(a));

const core = parsed.map(result => result.parsed!).filter(Boolean);
expect(core.length).toBe(parsed.length);

const serialized = core.map(annotation => serializeW3CImageAnnotation(annotation, 'http://www.example.com/source/1'));

serialized.forEach((a, idx) => {
const e = annotations[idx];
expect(a.id).toBe(e.id);
expect('bodies' in a).toBeFalsy();
})
})
});

0 comments on commit 56d5ab5

Please sign in to comment.