Skip to content

Commit

Permalink
feat(annotations): scroll to annotation on load (#1224)
Browse files Browse the repository at this point in the history
* feat(annotations): scroll to annotation on load

* Add handler that will look for a set annotations activeId in the file options and if present, scroll to annotation.

* feat(annotations): scroll to annotation on load

* PR Feedback to listen for event from annotations.

* feat(annotations): scroll to annotation on load

* PR Feedback

* feat(annotations): scroll to annotation on load

* Update test

* feat(annotations): scroll to annotation on load

* PR Feedback

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
mickr and mergify[bot] authored Jun 16, 2020
1 parent 4b6ef6a commit dd4195f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,9 @@ class BaseViewer extends EventEmitter {
* @return {void}
*/
initAnnotations() {
// Annotations must be loaded and ready before scrolling can occur on a deep-linked annotation.
this.annotator.addListener('annotations_initialized', this.handleAnnotationsInitialized);

this.annotator.init(this.scale);

// Once the annotator instance has been created, emit it so that clients can attach their events.
Expand Down Expand Up @@ -1037,6 +1040,27 @@ class BaseViewer extends EventEmitter {
this.annotator.scrollToAnnotation(data);
}

/**
* Handles retrieving the active annotation id from a deep-linked annotation and scrolls to the annotation.
* @param {Object} event - annotations array from emitted from box-annotations.
* @param {Array} event.annotations - annotations array from emitted from box-annotations.
* @return {void}
*/
handleAnnotationsInitialized = ({ annotations = [] }) => {
const {
file: { id },
} = this.options;

const activeAnnotationId = getProp(this.options, `fileOptions.${id}.annotations.activeId`, null);
const annotation = annotations.find(entry => entry.id === activeAnnotationId);

if (!annotation) {
return;
}

this.handleScrollToAnnotation(annotation);
};

/**
* Returns whether or not annotations are enabled for this viewer.
*
Expand Down
44 changes: 43 additions & 1 deletion src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1189,8 +1189,12 @@ describe('lib/viewers/BaseViewer', () => {
expect(base.addListener).to.be.calledWith('toggleannotationmode', sinon.match.func);
expect(base.addListener).to.be.calledWith('scale', sinon.match.func);
expect(base.addListener).to.be.calledWith('scrolltoannotation', base.handleScrollToAnnotation);
expect(base.annotator.addListener).to.be.calledWith('annotations_create', base.handleAnnotationCreateEvent);
expect(base.annotator.addListener).to.be.calledWith('annotatorevent', sinon.match.func);
expect(base.annotator.addListener).to.be.calledWith('annotations_create', base.handleAnnotationCreateEvent);
expect(base.annotator.addListener).to.be.calledWith(
'annotations_initialized',
base.handleAnnotationsInitialized,
);
expect(base.emit).to.be.calledWith('annotator', base.annotator);
});

Expand Down Expand Up @@ -1296,6 +1300,44 @@ describe('lib/viewers/BaseViewer', () => {
});
});

describe('handleAnnotationsInitialized()', () => {
let scrollToAnnotationStub;

beforeEach(() => {
scrollToAnnotationStub = sandbox.stub();

base.annotator = {
init: sandbox.stub(),
scrollToAnnotation: scrollToAnnotationStub,
};
});

it('should not call handleScrollToAnnotation if there is not an active annotation', () => {
base.options.fileOptions = {
'0': {
annotations: {},
},
};

base.handleAnnotationsInitialized({ annotations: [{ id: '123' }] });

expect(scrollToAnnotationStub).not.to.be.called;
});
it('should call scroll to annotation if active annotation is set', () => {
base.options.fileOptions = {
'0': {
annotations: {
activeId: 'ABC',
},
},
};

base.handleAnnotationsInitialized({ annotations: [{ id: 'ABC' }] });

expect(scrollToAnnotationStub).to.be.calledWith('ABC');
});
});

describe('areAnnotationsEnabled()', () => {
beforeEach(() => {
stubs.getViewerOption = sandbox
Expand Down

0 comments on commit dd4195f

Please sign in to comment.