From 814a4db417e96455e91a5afc55a18191b3e3775d Mon Sep 17 00:00:00 2001 From: Mick Ryan Date: Wed, 10 Jun 2020 17:03:47 -0700 Subject: [PATCH 1/5] 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. --- src/lib/viewers/BaseViewer.js | 14 +++++++ src/lib/viewers/__tests__/BaseViewer-test.js | 43 ++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/lib/viewers/BaseViewer.js b/src/lib/viewers/BaseViewer.js index 7d3115321..bedf39de1 100644 --- a/src/lib/viewers/BaseViewer.js +++ b/src/lib/viewers/BaseViewer.js @@ -989,6 +989,7 @@ class BaseViewer extends EventEmitter { if (this.areNewAnnotationsEnabled() && this.annotationControls) { this.annotator.addListener('annotations_create', this.handleAnnotationCreateEvent); + this.handleAnnotationsOnLoad(); } } @@ -1037,6 +1038,19 @@ class BaseViewer extends EventEmitter { this.annotator.scrollToAnnotation(data); } + handleAnnotationsOnLoad = () => { + const { + file: { id }, + } = this.options; + + const loadedAnnotationId = getProp(this.options, `fileOptions.${id}.annotations.activeId`, null); + const annotation = loadedAnnotationId ? this.annotator.getAnnotationById(loadedAnnotationId) : null; + + if (annotation) { + this.handleScrollToAnnotation(annotation); + } + }; + /** * 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 48b3d177f..6689a9218 100644 --- a/src/lib/viewers/__tests__/BaseViewer-test.js +++ b/src/lib/viewers/__tests__/BaseViewer-test.js @@ -1296,6 +1296,49 @@ describe('lib/viewers/BaseViewer', () => { }); }); + describe('handleAnnotationsOnLoad()', () => { + let getAnnotationStub; + let scrollToAnnotationStub; + + beforeEach(() => { + getAnnotationStub = sandbox.stub().returns({ id: 'ABC' }); + scrollToAnnotationStub = sandbox.stub(); + + base.annotator = { + init: sandbox.stub(), + scrollToAnnotation: scrollToAnnotationStub, + getAnnotationById: getAnnotationStub, + }; + }); + + it('should not call handleScrollToAnnotation if there is not an active annotation', () => { + base.options.fileOptions = { + '0': { + annotations: {}, + }, + }; + + base.handleAnnotationsOnLoad(); + + expect(scrollToAnnotationStub).not.to.be.called; + expect(getAnnotationStub).not.to.be.called; + }); + it('should call scroll to annotation if active annotation is set', () => { + base.options.fileOptions = { + '0': { + annotations: { + activeId: 'ABC', + }, + }, + }; + + base.handleAnnotationsOnLoad(); + + expect(scrollToAnnotationStub).to.be.calledWith('ABC'); + expect(getAnnotationStub).to.be.called; + }); + }); + describe('areAnnotationsEnabled()', () => { beforeEach(() => { stubs.getViewerOption = sandbox From c7cb8bbbb21543db8101820b76618fa9a8dd46c1 Mon Sep 17 00:00:00 2001 From: Mick Ryan Date: Thu, 11 Jun 2020 18:32:15 -0700 Subject: [PATCH 2/5] feat(annotations): scroll to annotation on load * PR Feedback to listen for event from annotations. --- src/lib/viewers/BaseViewer.js | 18 +++++++++++++----- src/lib/viewers/__tests__/BaseViewer-test.js | 12 ++++-------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/lib/viewers/BaseViewer.js b/src/lib/viewers/BaseViewer.js index bedf39de1..93755c6f0 100644 --- a/src/lib/viewers/BaseViewer.js +++ b/src/lib/viewers/BaseViewer.js @@ -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_ready', this.handleAnnotationsOnLoad); + this.annotator.init(this.scale); // Once the annotator instance has been created, emit it so that clients can attach their events. @@ -989,7 +992,6 @@ class BaseViewer extends EventEmitter { if (this.areNewAnnotationsEnabled() && this.annotationControls) { this.annotator.addListener('annotations_create', this.handleAnnotationCreateEvent); - this.handleAnnotationsOnLoad(); } } @@ -1038,17 +1040,23 @@ class BaseViewer extends EventEmitter { this.annotator.scrollToAnnotation(data); } - handleAnnotationsOnLoad = () => { + /** + * Handles retrieving the active annotation id from a deep-linked annotation and scrolls to the annotation. + * @param {Object[]} data - annotations array from emitted from box-annotations. + * @return {void} + */ + handleAnnotationsOnLoad = (data = []) => { const { file: { id }, } = this.options; const loadedAnnotationId = getProp(this.options, `fileOptions.${id}.annotations.activeId`, null); - const annotation = loadedAnnotationId ? this.annotator.getAnnotationById(loadedAnnotationId) : null; - if (annotation) { - this.handleScrollToAnnotation(annotation); + if (!loadedAnnotationId) { + return; } + + this.handleScrollToAnnotation(data.find(entry => entry.id === loadedAnnotationId)); }; /** diff --git a/src/lib/viewers/__tests__/BaseViewer-test.js b/src/lib/viewers/__tests__/BaseViewer-test.js index 6689a9218..ad8e7b310 100644 --- a/src/lib/viewers/__tests__/BaseViewer-test.js +++ b/src/lib/viewers/__tests__/BaseViewer-test.js @@ -1189,8 +1189,9 @@ 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_ready', base.handleAnnotationsOnLoad); expect(base.emit).to.be.calledWith('annotator', base.annotator); }); @@ -1297,17 +1298,14 @@ describe('lib/viewers/BaseViewer', () => { }); describe('handleAnnotationsOnLoad()', () => { - let getAnnotationStub; let scrollToAnnotationStub; beforeEach(() => { - getAnnotationStub = sandbox.stub().returns({ id: 'ABC' }); scrollToAnnotationStub = sandbox.stub(); base.annotator = { init: sandbox.stub(), scrollToAnnotation: scrollToAnnotationStub, - getAnnotationById: getAnnotationStub, }; }); @@ -1318,10 +1316,9 @@ describe('lib/viewers/BaseViewer', () => { }, }; - base.handleAnnotationsOnLoad(); + base.handleAnnotationsOnLoad([{ id: '123' }]); expect(scrollToAnnotationStub).not.to.be.called; - expect(getAnnotationStub).not.to.be.called; }); it('should call scroll to annotation if active annotation is set', () => { base.options.fileOptions = { @@ -1332,10 +1329,9 @@ describe('lib/viewers/BaseViewer', () => { }, }; - base.handleAnnotationsOnLoad(); + base.handleAnnotationsOnLoad([{ id: 'ABC' }]); expect(scrollToAnnotationStub).to.be.calledWith('ABC'); - expect(getAnnotationStub).to.be.called; }); }); From ac24125632e3732c26536a1cba3f459abfc5663e Mon Sep 17 00:00:00 2001 From: Mick Ryan Date: Fri, 12 Jun 2020 13:17:25 -0700 Subject: [PATCH 3/5] feat(annotations): scroll to annotation on load * PR Feedback --- src/lib/viewers/BaseViewer.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/lib/viewers/BaseViewer.js b/src/lib/viewers/BaseViewer.js index 93755c6f0..a50424ab6 100644 --- a/src/lib/viewers/BaseViewer.js +++ b/src/lib/viewers/BaseViewer.js @@ -967,7 +967,7 @@ class BaseViewer extends EventEmitter { */ initAnnotations() { // Annotations must be loaded and ready before scrolling can occur on a deep-linked annotation. - this.annotator.addListener('annotations_ready', this.handleAnnotationsOnLoad); + this.annotator.addListener('annotations_initialized', this.handleAnnotationsOnLoad); this.annotator.init(this.scale); @@ -1042,21 +1042,28 @@ class BaseViewer extends EventEmitter { /** * Handles retrieving the active annotation id from a deep-linked annotation and scrolls to the annotation. - * @param {Object[]} data - annotations array from emitted from box-annotations. + * @param {Object} event - annotations array from emitted from box-annotations. + * @param {Array} event.annotations - annotations array from emitted from box-annotations. * @return {void} */ - handleAnnotationsOnLoad = (data = []) => { + handleAnnotationsOnLoad = ({ annotations = [] }) => { const { file: { id }, } = this.options; - const loadedAnnotationId = getProp(this.options, `fileOptions.${id}.annotations.activeId`, null); + const activeAnnotationId = getProp(this.options, `fileOptions.${id}.annotations.activeId`, null); - if (!loadedAnnotationId) { + if (!activeAnnotationId) { return; } - this.handleScrollToAnnotation(data.find(entry => entry.id === loadedAnnotationId)); + const annotation = annotations.find(entry => entry.id === activeAnnotationId); + + if (!annotation) { + return; + } + + this.handleScrollToAnnotation(annotation); }; /** From 6316c708139d88c77234cd3faf8dc8246afd5a24 Mon Sep 17 00:00:00 2001 From: Mick Ryan Date: Fri, 12 Jun 2020 14:02:41 -0700 Subject: [PATCH 4/5] feat(annotations): scroll to annotation on load * Update test --- src/lib/viewers/__tests__/BaseViewer-test.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/lib/viewers/__tests__/BaseViewer-test.js b/src/lib/viewers/__tests__/BaseViewer-test.js index ad8e7b310..740a13bdd 100644 --- a/src/lib/viewers/__tests__/BaseViewer-test.js +++ b/src/lib/viewers/__tests__/BaseViewer-test.js @@ -1191,7 +1191,10 @@ describe('lib/viewers/BaseViewer', () => { expect(base.addListener).to.be.calledWith('scrolltoannotation', base.handleScrollToAnnotation); 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_ready', base.handleAnnotationsOnLoad); + expect(base.annotator.addListener).to.be.calledWith( + 'annotations_initialized', + base.handleAnnotationsOnLoad, + ); expect(base.emit).to.be.calledWith('annotator', base.annotator); }); @@ -1316,7 +1319,7 @@ describe('lib/viewers/BaseViewer', () => { }, }; - base.handleAnnotationsOnLoad([{ id: '123' }]); + base.handleAnnotationsOnLoad({ annotations: [{ id: '123' }] }); expect(scrollToAnnotationStub).not.to.be.called; }); @@ -1329,7 +1332,7 @@ describe('lib/viewers/BaseViewer', () => { }, }; - base.handleAnnotationsOnLoad([{ id: 'ABC' }]); + base.handleAnnotationsOnLoad({ annotations: [{ id: 'ABC' }] }); expect(scrollToAnnotationStub).to.be.calledWith('ABC'); }); From fdefff2404e967503cdaf396e3803d39409b48b8 Mon Sep 17 00:00:00 2001 From: Mick Ryan Date: Tue, 16 Jun 2020 11:58:42 -0700 Subject: [PATCH 5/5] feat(annotations): scroll to annotation on load * PR Feedback --- src/lib/viewers/BaseViewer.js | 9 ++------- src/lib/viewers/__tests__/BaseViewer-test.js | 8 ++++---- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/lib/viewers/BaseViewer.js b/src/lib/viewers/BaseViewer.js index a50424ab6..e5e57ac15 100644 --- a/src/lib/viewers/BaseViewer.js +++ b/src/lib/viewers/BaseViewer.js @@ -967,7 +967,7 @@ class BaseViewer extends EventEmitter { */ initAnnotations() { // Annotations must be loaded and ready before scrolling can occur on a deep-linked annotation. - this.annotator.addListener('annotations_initialized', this.handleAnnotationsOnLoad); + this.annotator.addListener('annotations_initialized', this.handleAnnotationsInitialized); this.annotator.init(this.scale); @@ -1046,17 +1046,12 @@ class BaseViewer extends EventEmitter { * @param {Array} event.annotations - annotations array from emitted from box-annotations. * @return {void} */ - handleAnnotationsOnLoad = ({ annotations = [] }) => { + handleAnnotationsInitialized = ({ annotations = [] }) => { const { file: { id }, } = this.options; const activeAnnotationId = getProp(this.options, `fileOptions.${id}.annotations.activeId`, null); - - if (!activeAnnotationId) { - return; - } - const annotation = annotations.find(entry => entry.id === activeAnnotationId); if (!annotation) { diff --git a/src/lib/viewers/__tests__/BaseViewer-test.js b/src/lib/viewers/__tests__/BaseViewer-test.js index 740a13bdd..9a285f686 100644 --- a/src/lib/viewers/__tests__/BaseViewer-test.js +++ b/src/lib/viewers/__tests__/BaseViewer-test.js @@ -1193,7 +1193,7 @@ describe('lib/viewers/BaseViewer', () => { expect(base.annotator.addListener).to.be.calledWith('annotations_create', base.handleAnnotationCreateEvent); expect(base.annotator.addListener).to.be.calledWith( 'annotations_initialized', - base.handleAnnotationsOnLoad, + base.handleAnnotationsInitialized, ); expect(base.emit).to.be.calledWith('annotator', base.annotator); }); @@ -1300,7 +1300,7 @@ describe('lib/viewers/BaseViewer', () => { }); }); - describe('handleAnnotationsOnLoad()', () => { + describe('handleAnnotationsInitialized()', () => { let scrollToAnnotationStub; beforeEach(() => { @@ -1319,7 +1319,7 @@ describe('lib/viewers/BaseViewer', () => { }, }; - base.handleAnnotationsOnLoad({ annotations: [{ id: '123' }] }); + base.handleAnnotationsInitialized({ annotations: [{ id: '123' }] }); expect(scrollToAnnotationStub).not.to.be.called; }); @@ -1332,7 +1332,7 @@ describe('lib/viewers/BaseViewer', () => { }, }; - base.handleAnnotationsOnLoad({ annotations: [{ id: 'ABC' }] }); + base.handleAnnotationsInitialized({ annotations: [{ id: 'ABC' }] }); expect(scrollToAnnotationStub).to.be.calledWith('ABC'); });