WebAnnotation API tweaks

This commit is contained in:
Rainer Simon 2020-04-10 19:24:12 +02:00
parent 580ece0352
commit 24bb20196b
1 changed files with 24 additions and 17 deletions

View File

@ -1,7 +1,7 @@
export default class WebAnnotation { export default class WebAnnotation {
constructor(annotation) { constructor(annotation) {
this._annotation = annotation; this.underlying = annotation;
} }
/** For convenience - creates an empty web annotation **/ /** For convenience - creates an empty web annotation **/
@ -17,19 +17,19 @@ export default class WebAnnotation {
/** Creates a copy of this annotation **/ /** Creates a copy of this annotation **/
clone = opt_props => { 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 **/ /** An equality check based on the underlying object or (if given) ID **/
isEqual(other) { isEqual(other) {
if (!other) { if (!other) {
return false; return false;
} else if (this._annotation === other._annotation) { } else if (this.underlying === other.underlying) {
return true; return true;
} else if (!this._annotation.id || !other._annotation.id) { } else if (!this.underlying.id || !other.underlying.id) {
return false; 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() { get id() {
return this._annotation.id; return this.underlying.id;
} }
get type() { get type() {
return this._annotation.type; return this.underlying.type;
} }
get motivation() { get motivation() {
return this._annotation.motivation; return this.underlying.motivation;
} }
get body() { get body() {
return this._annotation.body; return this.underlying.body;
} }
get target() { get target() {
return this._annotation.target; return this.underlying.target;
} }
/** Same as .body, but guaranteed to return an array **/ /** Same as .body, but guaranteed to return an array **/
get bodies() { get bodies() {
return (Array.isArray(this._annotation.body)) ? return (Array.isArray(this.underlying.body)) ?
this._annotation.body : [ this._annotation.body ]; this.underlying.body : [ this.underlying.body ];
} }
/** Only bodies are meant to be mutated by the application **/ /** Only bodies are meant to be mutated by the application **/
set bodies(bodies) { set bodies(bodies) {
this._annotation.body = bodies; this.underlying.body = bodies;
} }
/** Same as .target, but guaranteed to return an array **/ /** Same as .target, but guaranteed to return an array **/
get targets() { get targets() {
return (Array.isArray(this._annotation.target)) ? return (Array.isArray(this.underlying.target)) ?
this._annotation.target : [ this._annotation.target ]; this.underlying.target : [ this.underlying.target ];
} }
/*****************************************/ /*****************************************/
@ -80,8 +80,15 @@ export default class WebAnnotation {
/** Selector of the given type **/ /** Selector of the given type **/
selector = type => { selector = type => {
return this._annotation.target.selector && const { target } = this.underlying;
this._annotation.target.selector.find(t => t.type === type);
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 **/ /** Shorthand for the 'exact' field of the TextQuoteSelector **/