From 89a3903ae1050a6d015387a89479725fcb2af325 Mon Sep 17 00:00:00 2001 From: Conrad Chan Date: Mon, 24 Aug 2020 16:23:13 -0700 Subject: [PATCH 1/4] feat(annotations): Handle mode change events --- src/lib/AnnotationControls.ts | 19 +++++++++++++-- src/lib/__tests__/AnnotationControls-test.js | 25 ++++++++++++++++++++ src/lib/viewers/BaseViewer.js | 8 +++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/lib/AnnotationControls.ts b/src/lib/AnnotationControls.ts index 6ea5c2d83..f9d6cbbdc 100644 --- a/src/lib/AnnotationControls.ts +++ b/src/lib/AnnotationControls.ts @@ -141,11 +141,26 @@ export default class AnnotationControls { }; /** - * Region comment button click handler + * Update the mode. If the mode is different from what is currently saved in state, + * then reset the current controls and apply the active state based on the provided mode. + * @param mode Annotation mode */ - private handleClick = (onClick: ClickHandler, mode: AnnotationMode) => (event: MouseEvent): void => { + public updateMode(mode: AnnotationMode): void { const prevMode = this.currentMode; + // Only update buttons if mode has changed + if (prevMode !== mode) { + this.resetControls(); + this.currentMode = mode as AnnotationMode; + this.updateButton(mode); + } + } + + /** + * Annotation control button click handler + */ + private handleClick = (onClick: ClickHandler, mode: AnnotationMode) => (event: MouseEvent): void => { + const prevMode = this.currentMode; this.resetControls(); if (prevMode !== mode) { diff --git a/src/lib/__tests__/AnnotationControls-test.js b/src/lib/__tests__/AnnotationControls-test.js index 5701c48f1..047a73370 100644 --- a/src/lib/__tests__/AnnotationControls-test.js +++ b/src/lib/__tests__/AnnotationControls-test.js @@ -261,4 +261,29 @@ describe('lib/AnnotationControls', () => { expect(stubs.buttonElement.setAttribute).to.be.calledWith('data-resin-fileId', '0'); }); }); + + describe('updateMode()', () => { + beforeEach(() => { + annotationControls.resetControls = sandbox.stub(); + annotationControls.updateButton = sandbox.stub(); + }); + + it('should do nothing if mode is the same', () => { + annotationControls.currentMode = 'region'; + + annotationControls.updateMode('region'); + + expect(annotationControls.resetControls).not.to.be.called; + expect(annotationControls.updateButton).not.to.be.called; + }); + + it('should update controls if mode is not the same', () => { + annotationControls.currentMode = 'region'; + + annotationControls.updateMode('highlight'); + + expect(annotationControls.resetControls).to.be.called; + expect(annotationControls.updateButton).to.be.calledWith('highlight'); + }); + }); }); diff --git a/src/lib/viewers/BaseViewer.js b/src/lib/viewers/BaseViewer.js index f27fa85d9..73ff9680f 100644 --- a/src/lib/viewers/BaseViewer.js +++ b/src/lib/viewers/BaseViewer.js @@ -151,6 +151,7 @@ class BaseViewer extends EventEmitter { this.mobileZoomEndHandler = this.mobileZoomEndHandler.bind(this); this.handleAnnotatorEvents = this.handleAnnotatorEvents.bind(this); this.handleAnnotationCreateEvent = this.handleAnnotationCreateEvent.bind(this); + this.handleAnnotationModeChangeEvent = this.handleAnnotationModeChangeEvent.bind(this); this.handleAnnotationControlsEscape = this.handleAnnotationControlsEscape.bind(this); this.handleFullscreenEnter = this.handleFullscreenEnter.bind(this); this.handleFullscreenExit = this.handleFullscreenExit.bind(this); @@ -1015,6 +1016,7 @@ class BaseViewer extends EventEmitter { if (this.areNewAnnotationsEnabled() && this.annotationControls) { this.annotator.addListener('annotations_create', this.handleAnnotationCreateEvent); + this.annotator.addListener('annotations_mode_change', this.handleAnnotationModeChangeEvent); } } @@ -1260,6 +1262,12 @@ class BaseViewer extends EventEmitter { } } + handleAnnotationModeChangeEvent({ mode }) { + if (this.annotationControls) { + this.annotationControls.updateMode(mode); + } + } + /** * Creates combined options to give to the annotator * From ec4a355b0563202a80e4ab600d632565b5bf9d7e Mon Sep 17 00:00:00 2001 From: Conrad Chan Date: Tue, 25 Aug 2020 09:45:18 -0700 Subject: [PATCH 2/4] chore: remove jsdoc --- src/lib/AnnotationControls.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/AnnotationControls.ts b/src/lib/AnnotationControls.ts index f9d6cbbdc..e444d2383 100644 --- a/src/lib/AnnotationControls.ts +++ b/src/lib/AnnotationControls.ts @@ -143,7 +143,6 @@ export default class AnnotationControls { /** * Update the mode. If the mode is different from what is currently saved in state, * then reset the current controls and apply the active state based on the provided mode. - * @param mode Annotation mode */ public updateMode(mode: AnnotationMode): void { const prevMode = this.currentMode; From c507a446b52c4a49a552e6b0666b9c006f41bfd7 Mon Sep 17 00:00:00 2001 From: Conrad Chan Date: Tue, 25 Aug 2020 09:57:12 -0700 Subject: [PATCH 3/4] chore: removing unnecessary code --- src/lib/AnnotationControls.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/lib/AnnotationControls.ts b/src/lib/AnnotationControls.ts index e444d2383..27125dc85 100644 --- a/src/lib/AnnotationControls.ts +++ b/src/lib/AnnotationControls.ts @@ -145,12 +145,10 @@ export default class AnnotationControls { * then reset the current controls and apply the active state based on the provided mode. */ public updateMode(mode: AnnotationMode): void { - const prevMode = this.currentMode; - // Only update buttons if mode has changed - if (prevMode !== mode) { + if (this.currentMode !== mode) { this.resetControls(); - this.currentMode = mode as AnnotationMode; + this.currentMode = mode; this.updateButton(mode); } } From 9e1c4087f67cfab3f31b5a5d5165afbee66a7dd6 Mon Sep 17 00:00:00 2001 From: Conrad Chan Date: Tue, 25 Aug 2020 10:35:40 -0700 Subject: [PATCH 4/4] chore: pr comments --- src/lib/AnnotationControls.ts | 14 ++++++++------ src/lib/__tests__/AnnotationControls-test.js | 6 +++--- src/lib/viewers/BaseViewer.js | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/lib/AnnotationControls.ts b/src/lib/AnnotationControls.ts index 27125dc85..d22f6a123 100644 --- a/src/lib/AnnotationControls.ts +++ b/src/lib/AnnotationControls.ts @@ -141,16 +141,18 @@ export default class AnnotationControls { }; /** - * Update the mode. If the mode is different from what is currently saved in state, + * Set the mode. If the mode is different from what is currently saved in state, * then reset the current controls and apply the active state based on the provided mode. */ - public updateMode(mode: AnnotationMode): void { + public setMode(mode: AnnotationMode): void { // Only update buttons if mode has changed - if (this.currentMode !== mode) { - this.resetControls(); - this.currentMode = mode; - this.updateButton(mode); + if (this.currentMode === mode) { + return; } + + this.resetControls(); + this.currentMode = mode; + this.updateButton(mode); } /** diff --git a/src/lib/__tests__/AnnotationControls-test.js b/src/lib/__tests__/AnnotationControls-test.js index 047a73370..dc04e4524 100644 --- a/src/lib/__tests__/AnnotationControls-test.js +++ b/src/lib/__tests__/AnnotationControls-test.js @@ -262,7 +262,7 @@ describe('lib/AnnotationControls', () => { }); }); - describe('updateMode()', () => { + describe('setMode()', () => { beforeEach(() => { annotationControls.resetControls = sandbox.stub(); annotationControls.updateButton = sandbox.stub(); @@ -271,7 +271,7 @@ describe('lib/AnnotationControls', () => { it('should do nothing if mode is the same', () => { annotationControls.currentMode = 'region'; - annotationControls.updateMode('region'); + annotationControls.setMode('region'); expect(annotationControls.resetControls).not.to.be.called; expect(annotationControls.updateButton).not.to.be.called; @@ -280,7 +280,7 @@ describe('lib/AnnotationControls', () => { it('should update controls if mode is not the same', () => { annotationControls.currentMode = 'region'; - annotationControls.updateMode('highlight'); + annotationControls.setMode('highlight'); expect(annotationControls.resetControls).to.be.called; expect(annotationControls.updateButton).to.be.calledWith('highlight'); diff --git a/src/lib/viewers/BaseViewer.js b/src/lib/viewers/BaseViewer.js index 73ff9680f..91329f5b1 100644 --- a/src/lib/viewers/BaseViewer.js +++ b/src/lib/viewers/BaseViewer.js @@ -1264,7 +1264,7 @@ class BaseViewer extends EventEmitter { handleAnnotationModeChangeEvent({ mode }) { if (this.annotationControls) { - this.annotationControls.updateMode(mode); + this.annotationControls.setMode(mode); } }