From a6074941fa90850d4a05d09e9ae75b68496e63c0 Mon Sep 17 00:00:00 2001 From: Vincent Marchetti Date: Mon, 12 Aug 2024 15:06:29 -0400 Subject: [PATCH] Added a TextualBody class and the parsing of a bodyValue property defined on an annotation to a TextualBody resource as an annotation body --- src/Annotation.ts | 25 ++++++++++++++++++++++++- src/TextualBody.ts | 30 ++++++++++++++++++++++++++++++ src/internal.ts | 1 + 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/TextualBody.ts diff --git a/src/Annotation.ts b/src/Annotation.ts index bd537d17..7d099b9d 100644 --- a/src/Annotation.ts +++ b/src/Annotation.ts @@ -5,7 +5,8 @@ import { IManifestoOptions, ManifestResource, Resource, - SpecificResource + SpecificResource, + TextualBody } from "./internal"; import { Vector3 } from "threejs-math"; @@ -23,6 +24,28 @@ export class Annotation extends ManifestResource { **/ getBody(): ( AnnotationBody | SpecificResource) [] { let bodies: ( AnnotationBody | SpecificResource)[] = []; + + /* + A bodyValue property in the annotation json will short circuit + the parsing process and be interpreted as a shorthand version of + a TextualBody resource defining as the body + + This procedure is allowed, see Web Annotation Data Model section 3.2.5 + https://www.w3.org/TR/annotation-model/#string-body + */ + + var stringBody : string | undefined = this.getProperty("bodyValue"); + //console.log("retrieved stringBody " + stringBody); + if (stringBody){ + return [new TextualBody( + { "id" : "https://example.com/TextualBody/1", + "value" : stringBody, + "type" : "TextualBody" + }, + this.options + )]; + } + const body: any = this.getProperty("body"); // the following is intended to handle the following cases for diff --git a/src/TextualBody.ts b/src/TextualBody.ts new file mode 100644 index 00000000..bb9e74fd --- /dev/null +++ b/src/TextualBody.ts @@ -0,0 +1,30 @@ +import { + IManifestoOptions, + AnnotationBody} from "./internal"; + +/** +An implementation of the TextualBody class (class in JSON-LD sense) +as it is described in Web Annotation Data Model Section 3.2.4 +https://www.w3.org/TR/annotation-model/#embedded-textual-body +**/ +export class TextualBody extends AnnotationBody { + constructor(jsonld?: any, options?: IManifestoOptions) { + super(jsonld, options); + this.isModel = false; + this.isLight = false; + this.isCamera = false; + } + +/** +identify an instance of this typescript as representing a resource +having these json-ld Class relationships. +**/ +get isTextualBody() : boolean { return true;} +get isText() : boolean {return true;} + +/** +The simple string that is the data content of this resource +will return empty string as a default value +**/ +get Value(): string {return this.getProperty("value") || "" ;} +} \ No newline at end of file diff --git a/src/internal.ts b/src/internal.ts index 2b454d83..80487c48 100644 --- a/src/internal.ts +++ b/src/internal.ts @@ -10,6 +10,7 @@ export * from "./SpecificResource"; export * from "./AnnotationBody"; export * from "./Light"; export * from "./Camera"; +export * from "./TextualBody"; export * from "./AnnotationBodyParser"; export * from "./Annotation";