Skip to content

Commit

Permalink
Merge pull request #16 from IIIF-Commons/feature/import-raw-json
Browse files Browse the repository at this point in the history
Added raw json helper
  • Loading branch information
stephenwf authored Nov 10, 2022
2 parents 7fe1a47 + c62e5d2 commit 1c73c09
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
18 changes: 1 addition & 17 deletions src/annotation-page-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,11 @@ export class AnnotationPageInstanceBuilder extends BaseEntityBuilder<AnnotationP

createAnnotation(annotation: Annotation) {
// Extract annotation body + target as reference

if (this.defaultAnnotationTarget && !annotation.target) {
annotation.target = this.defaultAnnotationTarget;
}

const body = Array.isArray(annotation.body) ? annotation.body : [annotation.body];

annotation.body = body.map((singleBody) => {
if (singleBody && (singleBody as ChoiceBody).type === 'Choice') {
const choiceBody = singleBody as ChoiceBody;
choiceBody.items = choiceBody.items.map((choiceItem) => {
return this.addEmbeddedInstance(choiceItem, 'ContentResource');
});
return this.addEmbeddedInstance(choiceBody, 'ContentResource');
}

return this.addEmbeddedInstance(singleBody, 'ContentResource');
});

const annotationRef = this.addEmbeddedInstance(annotation, 'Annotation');

const [annotationRef] = this.importRawJson<'Annotation'>(annotation);
this.modified.add('items');
this.entity.items = [...this.entity.items, annotationRef];
}
Expand Down
17 changes: 17 additions & 0 deletions src/base-entity-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import {
CollectionItemSchemas,
AnnotationPageNormalized,
AnnotationPage,
Annotation,
} from '@iiif/presentation-3';
import { ServiceNormalized } from '@iiif/presentation-3/resources/service';
import { IIIFBuilder } from './iiif-builder';
import { normalize } from '@iiif/parser';

export class BaseEntityBuilder<
T extends ManifestNormalized | CollectionNormalized | CanvasNormalized | AnnotationPageNormalized
Expand All @@ -36,6 +38,21 @@ export class BaseEntityBuilder<
this.newInstances = [];
}

importRawJson<Type, Resource = any>(obj: any): [Reference<Type>, Resource] {
const { entities, resource } = normalize(obj);

const types = Object.keys(entities);
for (const type of types) {
const objects = (entities as any)[type];
const ids = Object.keys(objects);
for (const id of ids) {
this.embeddedInstances.push(objects[id]);
}
}

return [{ id: resource.id, type: resource.type }, resource] as any;
}

dispose() {
// Method used only to prevent implementations from holding on to references and
// keeping these classes in memory.
Expand Down

0 comments on commit 1c73c09

Please sign in to comment.