IIIF-Commons / manifesto

IIIF Presentation API client and server utility library.
MIT License
47 stars 31 forks source link

Accessing v3 canvas annotations #125

Open jonw-cogapp opened 1 year ago

jonw-cogapp commented 1 year ago

Question / feature request:

Unless I'm mistaken it seems like manifesto doesn't currently provide methods for directly obtaining embedded annotations in v3 manifests — those that exist in the annotations array at the canvas level.

Here are some example recipes from the cookbook that show the structure I'd like to access:

For my own needs I've been able to use getProperty("annotations") on a canvas to iterate and instantiate annotations with the appropriate manifesto constructors (this only covers something similar to the tagging recipe), like so:

  type RawAnnotationPage = {
    id: string;
    type: string;
    items: Array<RawAnnotation>;
  };

  type RawAnnotation = {
    id: string;
    type: string;
    motivation: string;
    body: RawAnnotationBody;
    target: string;
  };

  type RawAnnotationBody = {
    type: string;
    value: string;
    language: string;
    format: string;
  };

  function getAnnotationPages(canvases: Canvas[], options: Manifest["options"]): Array<AnnotationPage> {
    const annotationPages: Array<AnnotationPage> = [];

    if (canvases.length) {
      canvases.forEach((canvas) => {
        // "getProperty" ejects and results in raw JSON
        // We need to instantiate each level with the appropriate constructor
        const rawAnnotationPages: Array<RawAnnotationPage> =
          canvas.getProperty("annotations") || [];

        annotationPages.push(
          ...rawAnnotationPages.map((rawAnnotationPage) => {
            const rawAnnotations: Array<RawAnnotation> | undefined =
              rawAnnotationPage.items;

            return new AnnotationPage(
              {
                ...rawAnnotationPage,
                items: rawAnnotations.map((rawAnnotation) => {
                  return new Annotation(rawAnnotation, options);
                }),
                type: rawAnnotationPage.type,
              },
              options,
            );
          }),
        );
        return [];
      });
    }
    return annotationPages;
  }

But it would be really cool if there was ways of accessing these annotations with methods directly in manifesto.

cmahnke commented 1 month ago

@jonw-cogapp: Have you tried to create a PR for this? I'm not sure if this project is still active...