Skip to content

Commit

Permalink
Added missing API methods + enabled size-ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
Rainer Simon committed Mar 18, 2021
1 parent 4a6e789 commit edaa910
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
31 changes: 28 additions & 3 deletions src/OSDAnnotationLayer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import EventEmitter from 'tiny-emitter';
import OpenSeadragon from 'openseadragon';
import { SVG_NAMESPACE, DrawingTools, drawShape, format, parseRectFragment } from '@recogito/annotorious/src';
import { SVG_NAMESPACE, DrawingTools, drawShape, shapeArea, format, parseRectFragment } from '@recogito/annotorious/src';
import { getSnippet } from './util/ImageSnippet';

export default class OSDAnnotationLayer extends EventEmitter {
Expand Down Expand Up @@ -111,6 +111,9 @@ export default class OSDAnnotationLayer extends EventEmitter {
this.g.appendChild(shape);
}

addDrawingTool = plugin =>
this.tools.registerTool(plugin);

addOrUpdateAnnotation = (annotation, previous) => {
if (this.selectedShape?.annotation === annotation || this.selectShape?.annotation == previous)
this.deselect();
Expand All @@ -120,6 +123,9 @@ export default class OSDAnnotationLayer extends EventEmitter {

this.removeAnnotation(annotation);
this.addAnnotation(annotation);

// Make sure rendering order is large-to-small
this.redraw();
}

currentScale = () => {
Expand All @@ -128,7 +134,7 @@ export default class OSDAnnotationLayer extends EventEmitter {
return zoom * containerWidth / this.viewer.world.getContentFactor();
}

deselect = () => {
deselect = skipRedraw => {
if (this.selectedShape) {
const { annotation } = this.selectedShape;

Expand All @@ -142,6 +148,9 @@ export default class OSDAnnotationLayer extends EventEmitter {

if (!annotation.isSelection)
this.addAnnotation(annotation);

if (!skipRedraw)
this.redraw();
}

this.selectedShape = null;
Expand Down Expand Up @@ -195,6 +204,9 @@ export default class OSDAnnotationLayer extends EventEmitter {
annotations.forEach(this.addAnnotation);
}

listDrawingTools = () =>
this.tools.listTools();

overrideId = (originalId, forcedId) => {
// Update SVG shape data attribute
const shape = this.findShape(originalId);
Expand All @@ -221,6 +233,19 @@ export default class OSDAnnotationLayer extends EventEmitter {
this.viewer.viewport.panTo(center, immediately);
}
}

redraw = () => {
const shapes = Array.from(this.g.querySelectorAll('.a9s-annotation'));

const annotations = shapes.map(s => s.annotation);
annotations.sort((a, b) => shapeArea(b) - shapeArea(a));

// Clear the SVG element
shapes.forEach(s => this.g.removeChild(s));

// Redraw
annotations.forEach(this.addAnnotation);
}

removeAnnotation = annotation => {
if (this.selectedShape?.annotation === annotation)
Expand Down Expand Up @@ -278,7 +303,7 @@ export default class OSDAnnotationLayer extends EventEmitter {

// If another shape is currently selected, deselect first
if (this.selectedShape && this.selectedShape.annotation !== shape.annotation)
this.deselect();
this.deselect(true);

const { annotation } = shape;

Expand Down
6 changes: 6 additions & 0 deletions src/OpenSeadragonAnnotator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ export default class OpenSeadragonAnnotator extends Component {
addAnnotation = annotation =>
this.annotationLayer.addOrUpdateAnnotation(annotation.clone());

addDrawingTool = plugin =>
this.annotationLayer.addDrawingTool(plugin);

cancelSelected = () => {
const { selectedAnnotation } = this.state;
if (selectedAnnotation)
Expand All @@ -177,6 +180,9 @@ export default class OpenSeadragonAnnotator extends Component {
getSelectedImageSnippet = () =>
this.annotationLayer.getSelectedImageSnippet();

listDrawingTools = () =>
this.annotationLayer.listDrawingTools();

panTo = (annotationOrId, immediately) =>
this.annotationLayer.panTo(annotationOrId, immediately);

Expand Down
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class OSDAnnotorious {
handleCancelSelected = annotation =>
this._emitter.emit('cancelSelected', annotation.underlying);

handleSelectionCreated = selection =>
handleSelectionCreated = selection =>
this._emitter.emit('createSelection', selection.underlying);

handleSelectionTargetChanged = target =>
Expand All @@ -95,6 +95,9 @@ class OSDAnnotorious {
addAnnotation = annotation =>
this._app.current.addAnnotation(new WebAnnotation(annotation));

addDrawingTool = plugin =>
this._app.current.addDrawingTool(plugin);

cancelSelected = () =>
this._app.current.cancelSelected();

Expand Down Expand Up @@ -123,6 +126,9 @@ class OSDAnnotorious {
getSelectedImageSnippet = () =>
this._app.current.getSelectedImageSnippet();

listDrawingTools = () =>
this._app.current.listDrawingTools();

loadAnnotations = url => axios.get(url).then(response => {
const annotations = response.data;
this.setAnnotations(annotations);
Expand Down

0 comments on commit edaa910

Please sign in to comment.