Skip to content

Commit

Permalink
selectAnnotation API method
Browse files Browse the repository at this point in the history
  • Loading branch information
rsimon committed Nov 8, 2021
1 parent 54bb91a commit a227537
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
5 changes: 5 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<button id="add-annotation">Add Annotation</button>
<button id="update-annotation">Update Annotation</button>
<button id="remove-annotation">Remove Annotation</button>
<button id="select-annotation">Select Annotation</button>
<button id="toggle-mode">MODE: ANNOTATION</button>
</div>

Expand Down Expand Up @@ -134,6 +135,10 @@ <h1>Homer: The Odyssey</h1>
r.removeAnnotation(myAnnotation);
});

document.getElementById('select-annotation').addEventListener('click', function() {
r.selectAnnotation('#d7197c87-b45d-4217-9c4f-27573030448f');
});

// Switch annotation mode (annotation/relationships)
var annotationMode = 'ANNOTATION'; // or 'RELATIONS'

Expand Down
34 changes: 28 additions & 6 deletions src/TextAnnotator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ export default class TextAnnotator extends Component {
this.highlighter.addOrUpdateAnnotation(annotation.clone());
}

getAnnotations = () => {
const annotations = this.highlighter.getAllAnnotations();
const relations = this.relationsLayer.getAllRelations();
return annotations.concat(relations).map(a => a.clone());
}

removeAnnotation = annotation => {
this.highlighter.removeAnnotation(annotation);

Expand All @@ -259,6 +265,28 @@ export default class TextAnnotator extends Component {
this.clearState();
}

selectAnnotation = arg => {
// De-select in any case
this.setState({
selectedAnnotation: null,
selectedDOMElement: null
}, () => {
if (arg) {
const spans = this.highlighter.findAnnotationSpans(arg);

if (spans.length > 0) {
const selectedDOMElement = spans[0];
const selectedAnnotation = spans[0].annotation;

this.setState({
selectedAnnotation,
selectedDOMElement
});
}
}
});
}

setAnnotations = annotations => {
this.highlighter.clear();
this.relationsLayer.clear();
Expand All @@ -268,12 +296,6 @@ export default class TextAnnotator extends Component {
this.relationsLayer.init(clones));
}

getAnnotations = () => {
const annotations = this.highlighter.getAllAnnotations();
const relations = this.relationsLayer.getAllRelations();
return annotations.concat(relations).map(a => a.clone());
}

setMode = mode => {
if (mode === 'RELATIONS') {
this.clearState();
Expand Down
14 changes: 7 additions & 7 deletions src/highlighter/Highlighter.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ export default class Highlighter {
}
}

_findAnnotationSpans = annotation => {
const allAnnotationSpans = document.querySelectorAll('.r6o-annotation');
return Array.prototype.slice.call(allAnnotationSpans)
.filter(span => span.annotation.isEqual(annotation));
findAnnotationSpans = annotationOrId => {
const id = annotationOrId.id || annotationOrId;
const spans =Array.from(document.querySelectorAll(`.r6o-annotation[data-id="${id}"]`));
return spans;
}

getAllAnnotations = () => {
Expand All @@ -72,8 +72,8 @@ export default class Highlighter {

addOrUpdateAnnotation = (annotation, maybePrevious) => {
// TODO index annotation to make this faster
const annoSpans = this._findAnnotationSpans(annotation);
const prevSpans = maybePrevious ? this._findAnnotationSpans(maybePrevious) : [];
const annoSpans = this.findAnnotationSpans(annotation);
const prevSpans = maybePrevious ? this.findAnnotationSpans(maybePrevious) : [];
const spans = uniqueItems(annoSpans.concat(prevSpans));

if (spans.length > 0) {
Expand All @@ -87,7 +87,7 @@ export default class Highlighter {
}

removeAnnotation = annotation => {
const spans = this._findAnnotationSpans(annotation);
const spans = this.findAnnotationSpans(annotation);
if (spans) {
this._unwrapHighlightings(spans)
this.el.normalize();
Expand Down
9 changes: 9 additions & 0 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export class Recogito {
/* External API */
/******************/

// Common shorthand for handling annotationOrId args
_wrap = annotationOrId =>
annotationOrId?.type === 'Annotation' ? new WebAnnotation(annotationOrId) : annotationOrId;

addAnnotation = annotation =>
this._app.current.addAnnotation(new WebAnnotation(annotation));

Expand Down Expand Up @@ -126,6 +130,11 @@ export class Recogito {
removeAnnotation = annotation =>
this._app.current.removeAnnotation(new WebAnnotation(annotation));

selectAnnotation = annotationOrId => {
const selected = this._app.current.selectAnnotation(this._wrap(annotationOrId));
return selected?.underlying;
}

setAnnotations = arg => {
const annotations = arg || [];
const webannotations = annotations.map(a => new WebAnnotation(a));
Expand Down
2 changes: 1 addition & 1 deletion src/relations/RelationsLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default class RelationsLayer extends EventEmitter {
*/
getConnectionsFor = annotation => {
return this.connections.filter(c =>
c.startAnnotation.isEqual(annotation) || c.endAnnotation.isEqual(annotation));
c.startAnnotation.id === annotation.id || c.endAnnotation.id === annotation.id);
}

destroyConnectionsFor = annotation => {
Expand Down

0 comments on commit a227537

Please sign in to comment.