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 {
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 **/