From 24bb20196b1fa0fcdf6cd245166dad1214232aa7 Mon Sep 17 00:00:00 2001 From: Rainer Simon Date: Fri, 10 Apr 2020 19:24:12 +0200 Subject: [PATCH] WebAnnotation API tweaks --- src/WebAnnotation.js | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/WebAnnotation.js b/src/WebAnnotation.js index 653d984..5f02fcd 100644 --- a/src/WebAnnotation.js +++ b/src/WebAnnotation.js @@ -1,7 +1,7 @@ export default class WebAnnotation { constructor(annotation) { - this._annotation = annotation; + this.underlying = annotation; } /** For convenience - creates an empty web annotation **/ @@ -17,19 +17,19 @@ export default class WebAnnotation { /** Creates a copy of this annotation **/ clone = opt_props => { - return new WebAnnotation({ ...this._annotation, ...opt_props}); + return new WebAnnotation({ ...this.underlying, ...opt_props}); } /** An equality check based on the underlying object or (if given) ID **/ isEqual(other) { if (!other) { return false; - } else if (this._annotation === other._annotation) { + } else if (this.underlying === other.underlying) { return true; - } else if (!this._annotation.id || !other._annotation.id) { + } else if (!this.underlying.id || !other.underlying.id) { return false; } - return this._annotation.id === other._annotation.id + return this.underlying.id === other.underlying.id } /*************************************/ @@ -38,40 +38,40 @@ export default class WebAnnotation { /*************************************/ get id() { - return this._annotation.id; + return this.underlying.id; } get type() { - return this._annotation.type; + return this.underlying.type; } get motivation() { - return this._annotation.motivation; + return this.underlying.motivation; } get body() { - return this._annotation.body; + return this.underlying.body; } get target() { - return this._annotation.target; + return this.underlying.target; } /** Same as .body, but guaranteed to return an array **/ get bodies() { - return (Array.isArray(this._annotation.body)) ? - this._annotation.body : [ this._annotation.body ]; + return (Array.isArray(this.underlying.body)) ? + this.underlying.body : [ this.underlying.body ]; } /** Only bodies are meant to be mutated by the application **/ set bodies(bodies) { - this._annotation.body = bodies; + this.underlying.body = bodies; } /** Same as .target, but guaranteed to return an array **/ get targets() { - return (Array.isArray(this._annotation.target)) ? - this._annotation.target : [ this._annotation.target ]; + return (Array.isArray(this.underlying.target)) ? + this.underlying.target : [ this.underlying.target ]; } /*****************************************/ @@ -80,8 +80,15 @@ export default class WebAnnotation { /** Selector of the given type **/ selector = type => { - return this._annotation.target.selector && - this._annotation.target.selector.find(t => t.type === type); + const { target } = this.underlying; + + if (target.selector) { + // Normalize to array + const selectors = Array.isArray(target.selector) ? + target.selector : [ target.selector ]; + + return selectors.find(s => s.type === type); + } } /** Shorthand for the 'exact' field of the TextQuoteSelector **/