Skip to content

Commit

Permalink
feat(annotations): Handle annotations creator status change event (#1254
Browse files Browse the repository at this point in the history
)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
Mingze and mergify[bot] committed Sep 15, 2020
1 parent b3a72d6 commit 46aad4f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
3 changes: 2 additions & 1 deletion 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',
STARTED = 'started',
SUCCESS = 'success',
UPDATE = 'update',
}
Expand Down Expand Up @@ -53,7 +54,7 @@ export default class AnnotationControlsFSM {

switch (this.currentState) {
case AnnotationState.NONE:
if (input === AnnotationInput.CREATE) {
if (input === AnnotationInput.CREATE || input === AnnotationInput.STARTED) {
this.currentState = modeTempStateMap[mode] || AnnotationState.NONE;
}
break;
Expand Down
25 changes: 19 additions & 6 deletions src/lib/__tests__/AnnotationControlsFSM-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ describe('lib/AnnotationControlsFSM', () => {
nextState: AnnotationState.REGION_TEMP,
output: AnnotationMode.REGION,
},
{
input: AnnotationInput.STARTED,
mode: AnnotationMode.HIGHLIGHT,
nextState: AnnotationState.HIGHLIGHT_TEMP,
output: AnnotationMode.HIGHLIGHT,
},
{
input: AnnotationInput.STARTED,
mode: AnnotationMode.REGION,
nextState: AnnotationState.REGION_TEMP,
output: AnnotationMode.REGION,
},
].forEach(({ input, mode, nextState, output }) => {
it(`should go to state ${nextState} and output ${output} if input is ${input} and mode is ${mode}`, () => {
const annotationControlsFSM = new AnnotationControlsFSM();
Expand All @@ -52,16 +64,16 @@ describe('lib/AnnotationControlsFSM', () => {
describe('AnnotationState.HIGHLIGHT/REGION', () => {
// Stay in the same state
[AnnotationState.HIGHLIGHT, AnnotationState.REGION].forEach(state => {
[AnnotationInput.CANCEL, AnnotationInput.CREATE, AnnotationInput.SUCCESS, AnnotationInput.SUCCESS].forEach(
input => {
Object.values(AnnotationInput)
.filter(input => input !== AnnotationInput.CLICK)
.forEach(input => {
it(`should stay in state ${state} if input is ${input}`, () => {
const annotationControlsFSM = new AnnotationControlsFSM(state);

expect(annotationControlsFSM.transition(input)).to.equal(state);
expect(annotationControlsFSM.getState()).to.equal(state);
});
},
);
});
});

// Go to different states
Expand Down Expand Up @@ -108,7 +120,6 @@ describe('lib/AnnotationControlsFSM', () => {
});

describe('AnnotationState.HIGHLIGHT_TEMP/REGION_TEMP', () => {
// Go to none state
[
{
state: AnnotationState.HIGHLIGHT_TEMP,
Expand All @@ -119,6 +130,7 @@ describe('lib/AnnotationControlsFSM', () => {
stateMode: AnnotationMode.REGION,
},
].forEach(({ state, stateMode }) => {
// Go to none state
[
{
input: AnnotationInput.CANCEL,
Expand All @@ -139,7 +151,8 @@ describe('lib/AnnotationControlsFSM', () => {
});
});

[AnnotationInput.CREATE, AnnotationInput.UPDATE].forEach(input => {
// Stay in the same state
[AnnotationInput.CREATE, AnnotationInput.STARTED, AnnotationInput.UPDATE].forEach(input => {
it(`should stay in state ${state} if input is ${input}`, () => {
const annotationControlsFSM = new AnnotationControlsFSM(state);

Expand Down
7 changes: 4 additions & 3 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class BaseViewer extends EventEmitter {
this.handleAnnotationCreateEvent = this.handleAnnotationCreateEvent.bind(this);
this.handleAnnotationControlsClick = this.handleAnnotationControlsClick.bind(this);
this.handleAnnotationControlsEscape = this.handleAnnotationControlsEscape.bind(this);
this.handleAnnotationStagedChangeEvent = this.handleAnnotationStagedChangeEvent.bind(this);
this.handleAnnotationCreatorChangeEvent = this.handleAnnotationCreatorChangeEvent.bind(this);
this.handleFullscreenEnter = this.handleFullscreenEnter.bind(this);
this.handleFullscreenExit = this.handleFullscreenExit.bind(this);
this.createAnnotator = this.createAnnotator.bind(this);
Expand Down Expand Up @@ -1018,7 +1018,8 @@ class BaseViewer extends EventEmitter {

if (this.areNewAnnotationsEnabled() && this.annotationControls) {
this.annotator.addListener('annotations_create', this.handleAnnotationCreateEvent);
this.annotator.addListener('creator_staged_change', this.handleAnnotationStagedChangeEvent);
this.annotator.addListener('creator_staged_change', this.handleAnnotationCreatorChangeEvent);
this.annotator.addListener('creator_status_change', this.handleAnnotationCreatorChangeEvent);
}
}

Expand Down Expand Up @@ -1261,7 +1262,7 @@ class BaseViewer extends EventEmitter {
}
}

handleAnnotationStagedChangeEvent({ status, type }) {
handleAnnotationCreatorChangeEvent({ status, type }) {
if (!this.annotationControls) {
return;
}
Expand Down
10 changes: 7 additions & 3 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,11 @@ describe('lib/viewers/BaseViewer', () => {
);
expect(base.annotator.addListener).to.be.calledWith(
'creator_staged_change',
base.handleAnnotationStagedChangeEvent,
base.handleAnnotationCreatorChangeEvent,
);
expect(base.annotator.addListener).to.be.calledWith(
'creator_status_change',
base.handleAnnotationCreatorChangeEvent,
);
expect(base.emit).to.be.calledWith('annotator', base.annotator);
});
Expand Down Expand Up @@ -1808,13 +1812,13 @@ describe('lib/viewers/BaseViewer', () => {
});
});

describe('handleAnnotationStagedChangeEvent()', () => {
describe('handleAnnotationCreatorChangeEvent()', () => {
it('should set mode', () => {
base.annotationControls = {
destroy: sandbox.stub(),
setMode: sandbox.stub(),
};
base.handleAnnotationStagedChangeEvent({ status: 'create', type: 'highlight' });
base.handleAnnotationCreatorChangeEvent({ status: 'create', type: 'highlight' });

expect(base.annotationControls.setMode).to.be.calledWith('highlight');
});
Expand Down

0 comments on commit 46aad4f

Please sign in to comment.