Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(discoverability): set mode to REGION on escape key press #1263

Merged
merged 7 commits into from
Sep 24, 2020
6 changes: 6 additions & 0 deletions src/lib/AnnotationControlsFSM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum AnnotationInput {
CANCEL = 'cancel',
CLICK = 'click',
CREATE = 'create',
RESET = 'reset',
STARTED = 'started',
SUCCESS = 'success',
UPDATE = 'update',
Expand Down Expand Up @@ -52,6 +53,11 @@ export default class AnnotationControlsFSM {
return stateModeMap[this.currentState];
}

if (input === AnnotationInput.RESET) {
this.currentState = AnnotationState.NONE;
return stateModeMap[this.currentState];
}

switch (this.currentState) {
case AnnotationState.NONE:
if (input === AnnotationInput.CREATE || input === AnnotationInput.STARTED) {
Expand Down
10 changes: 9 additions & 1 deletion src/lib/__tests__/AnnotationControlsFSM-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,21 @@ describe('lib/AnnotationControlsFSM', () => {
expect(annotationControlsFSM.getState()).to.equal(AnnotationState.NONE);
});
});

// Should reset state
it('should reset state if input is AnnotationInput.RESET', () => {
ChenCodes marked this conversation as resolved.
Show resolved Hide resolved
const annotationControlsFSM = new AnnotationControlsFSM(AnnotationMode.REGION);
ChenCodes marked this conversation as resolved.
Show resolved Hide resolved

expect(annotationControlsFSM.transition(AnnotationInput.RESET)).to.equal(AnnotationMode.NONE);
expect(annotationControlsFSM.getState()).to.equal(AnnotationState.NONE);
});
});

describe('AnnotationState.HIGHLIGHT/REGION', () => {
// Stay in the same state
[AnnotationState.HIGHLIGHT, AnnotationState.REGION].forEach(state => {
Object.values(AnnotationInput)
.filter(input => input !== AnnotationInput.CLICK)
.filter(input => input !== AnnotationInput.CLICK && input !== AnnotationInput.RESET)
ChenCodes marked this conversation as resolved.
Show resolved Hide resolved
.forEach(input => {
it(`should stay in state ${state} if input is ${input}`, () => {
const annotationControlsFSM = new AnnotationControlsFSM(state);
Expand Down
8 changes: 7 additions & 1 deletion src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ class BaseViewer extends EventEmitter {
this.annotator.emit(ANNOTATOR_EVENT.setVisibility, true);
if (this.options.enableAnnotationsDiscoverability) {
this.annotator.toggleAnnotationMode(AnnotationMode.REGION);
this.processAnnotationModeChange(this.annotationControlsFSM.transition(AnnotationInput.RESET));
}
this.enableAnnotationControls();
}
Expand Down Expand Up @@ -1087,7 +1088,12 @@ class BaseViewer extends EventEmitter {
* @return {void}
*/
handleAnnotationControlsEscape() {
this.annotator.toggleAnnotationMode(AnnotationMode.NONE);
if (this.options.enableAnnotationsDiscoverability) {
this.annotator.toggleAnnotationMode(AnnotationMode.REGION);
this.processAnnotationModeChange(this.annotationControlsFSM.transition(AnnotationInput.RESET));
} else {
this.annotator.toggleAnnotationMode(AnnotationMode.NONE);
}
}

/**
Expand Down
20 changes: 18 additions & 2 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,12 +600,14 @@ describe('lib/viewers/BaseViewer', () => {
destroy: sandbox.mock(),
};
base.options.enableAnnotationsDiscoverability = true;
base.processAnnotationModeChange = sandbox.mock();

base.handleFullscreenExit();

expect(base.annotator.emit).to.be.calledWith(ANNOTATOR_EVENT.setVisibility, true);
expect(base.enableAnnotationControls).to.be.called;
expect(base.annotator.toggleAnnotationMode).to.be.calledWith(AnnotationMode.REGION);
expect(base.processAnnotationModeChange).to.be.calledWith(AnnotationMode.NONE);
expect(base.enableAnnotationControls).to.be.called;
});
});

Expand Down Expand Up @@ -1875,15 +1877,29 @@ describe('lib/viewers/BaseViewer', () => {
});

describe('handleAnnotationControlsEscape()', () => {
it('should call toggleAnnotationMode', () => {
it('should call toggleAnnotationMode with AnnotationMode.NONE if enableAnnotationsDiscoverability is false', () => {
base.annotator = {
toggleAnnotationMode: sandbox.stub(),
};
base.options.enableAnnotationsDiscoverability = false;

base.handleAnnotationControlsEscape();

expect(base.annotator.toggleAnnotationMode).to.be.calledWith(AnnotationMode.NONE);
});

it('should reset annotationControlsFSM state and call toggleAnnotationMode with AnnotationMode.REGION if enableAnnotationsDiscoverability is true', () => {
base.annotator = {
toggleAnnotationMode: sandbox.stub(),
};
base.options.enableAnnotationsDiscoverability = true;
base.processAnnotationModeChange = sandbox.stub();

base.handleAnnotationControlsEscape();

expect(base.annotator.toggleAnnotationMode).to.be.calledWith(AnnotationMode.REGION);
expect(base.processAnnotationModeChange).to.be.calledWith(AnnotationMode.NONE);
});
});

describe('handleAnnotationControlsClick', () => {
Expand Down