diff --git a/src/lib/viewers/BaseViewer.js b/src/lib/viewers/BaseViewer.js index 52122414a8..a03775b104 100644 --- a/src/lib/viewers/BaseViewer.js +++ b/src/lib/viewers/BaseViewer.js @@ -976,7 +976,7 @@ class BaseViewer extends EventEmitter { }); // Add a custom listener to scroll to the specified annotation - this.addListener('scrolltoannotation', data => this.annotator.scrollToAnnotation(data)); + this.addListener('scrolltoannotation', this.handleScrollToAnnotation); // Add a custom listener for events emmited by the annotator this.annotator.addListener('annotatorevent', this.handleAnnotatorEvents); @@ -1019,6 +1019,15 @@ class BaseViewer extends EventEmitter { return permissions.can_annotate || permissions.can_create_annotations; } + /** + * Handles the 'scrolltoannotation' event and calls the annotator scroll method + * @param {Object} data + * @param {string} data.id Annotation id + */ + handleScrollToAnnotation({ id }) { + this.annotator.scrollToAnnotation(id); + } + /** * Returns whether or not annotations are enabled for this viewer. * diff --git a/src/lib/viewers/__tests__/BaseViewer-test.js b/src/lib/viewers/__tests__/BaseViewer-test.js index 06360caab9..c3cb2ad55f 100644 --- a/src/lib/viewers/__tests__/BaseViewer-test.js +++ b/src/lib/viewers/__tests__/BaseViewer-test.js @@ -1180,7 +1180,7 @@ describe('lib/viewers/BaseViewer', () => { expect(base.annotator.init).to.be.calledWith(1.5); 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', 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.emit).to.be.calledWith('annotator', base.annotator); @@ -1258,6 +1258,22 @@ describe('lib/viewers/BaseViewer', () => { }); }); + describe('handleScrollToAnnotation', () => { + it('should call the annotators scrollToAnnotation method', () => { + const scrollToAnnotationStub = sandbox.stub(); + + base.annotator = { + addListener: sandbox.stub(), + init: sandbox.stub(), + scrollToAnnotation: scrollToAnnotationStub, + }; + + base.handleScrollToAnnotation({ id: '123' }); + + expect(scrollToAnnotationStub).to.be.calledWith('123'); + }); + }); + describe('areAnnotationsEnabled()', () => { beforeEach(() => { stubs.getViewerOption = sandbox diff --git a/src/lib/viewers/doc/PresentationViewer.js b/src/lib/viewers/doc/PresentationViewer.js index b6033ef88b..1fa0fe11bb 100644 --- a/src/lib/viewers/doc/PresentationViewer.js +++ b/src/lib/viewers/doc/PresentationViewer.js @@ -313,6 +313,19 @@ class PresentationViewer extends DocBaseViewer { }; }; } + + /** + * @override + * @param {Object} data + * @param {string} data.id - location value of the annotation + * @param {Object} data.location - location value of the annotation + * @param {number} data.location.value - location value of the annotation + */ + handleScrollToAnnotation({ id, location: { value = 1 } }) { + this.setPage(value); + + this.annotator.scrollToAnnotation(id); + } } export default PresentationViewer; diff --git a/src/lib/viewers/doc/__tests__/PresentationViewer-test.js b/src/lib/viewers/doc/__tests__/PresentationViewer-test.js index 02e80bc10d..e16ff5c599 100644 --- a/src/lib/viewers/doc/__tests__/PresentationViewer-test.js +++ b/src/lib/viewers/doc/__tests__/PresentationViewer-test.js @@ -489,4 +489,20 @@ describe('lib/viewers/doc/PresentationViewer', () => { expect(result.last.id).to.equal(1); }); }); + + describe('handleScrollToAnnotation', () => { + it('should call setPage is location value provided', () => { + const setPageStub = sandbox.stub(presentation, 'setPage'); + const scrollToAnnotationStub = sandbox.stub(); + + presentation.annotator = { + scrollToAnnotation: scrollToAnnotationStub, + }; + + presentation.handleScrollToAnnotation({ id: '123', location: { value: 5 } }); + + expect(setPageStub).to.be.calledWith(5); + expect(scrollToAnnotationStub).to.be.calledWith('123'); + }); + }); });