-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(annotations): Handle annotations staged change event (#1247)
* feat(annotations): Handle annotations staged change event * feat(annotations): AnnotationControls dumber, FSM smarter * feat(annotations): Address comments * feat(annotations): Add FSM unit tests * feat(annotations): More unit tests * feat(annotations): Add tests for every possible inputs for every state * feat(annotations): Address comments
- Loading branch information
Mingze
authored
Sep 9, 2020
1 parent
4047402
commit 37e7003
Showing
10 changed files
with
337 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { AnnotationMode } from './AnnotationControls'; | ||
|
||
export enum AnnotationState { | ||
HIGHLIGHT = 'highlight', | ||
HIGHLIGHT_TEMP = 'highlight_temp', | ||
NONE = 'none', | ||
REGION = 'region', | ||
REGION_TEMP = 'region_temp', | ||
} | ||
|
||
export enum AnnotationInput { | ||
CANCEL = 'cancel', | ||
CLICK = 'click', | ||
CREATE = 'create', | ||
SUCCESS = 'success', | ||
UPDATE = 'update', | ||
} | ||
|
||
export const modeStateMap = { | ||
[AnnotationMode.HIGHLIGHT]: AnnotationState.HIGHLIGHT, | ||
[AnnotationMode.NONE]: AnnotationState.NONE, | ||
[AnnotationMode.REGION]: AnnotationState.REGION, | ||
}; | ||
|
||
export const modeTempStateMap = { | ||
[AnnotationMode.HIGHLIGHT]: AnnotationState.HIGHLIGHT_TEMP, | ||
[AnnotationMode.NONE]: AnnotationState.NONE, | ||
[AnnotationMode.REGION]: AnnotationState.REGION_TEMP, | ||
}; | ||
|
||
export const stateModeMap = { | ||
[AnnotationState.HIGHLIGHT]: AnnotationMode.HIGHLIGHT, | ||
[AnnotationState.HIGHLIGHT_TEMP]: AnnotationMode.HIGHLIGHT, | ||
[AnnotationState.NONE]: AnnotationMode.NONE, | ||
[AnnotationState.REGION]: AnnotationMode.REGION, | ||
[AnnotationState.REGION_TEMP]: AnnotationMode.REGION, | ||
}; | ||
|
||
export default class AnnotationControlsFSM { | ||
private currentState: AnnotationState; | ||
|
||
constructor(initialState = AnnotationState.NONE) { | ||
this.currentState = initialState; | ||
} | ||
|
||
public getState = (): AnnotationState => this.currentState; | ||
|
||
public transition = (input: AnnotationInput, mode: AnnotationMode = AnnotationMode.NONE): AnnotationMode => { | ||
if (input === AnnotationInput.CLICK) { | ||
this.currentState = mode === stateModeMap[this.currentState] ? AnnotationState.NONE : modeStateMap[mode]; | ||
return stateModeMap[this.currentState]; | ||
} | ||
|
||
switch (this.currentState) { | ||
case AnnotationState.NONE: | ||
if (input === AnnotationInput.CREATE) { | ||
this.currentState = modeTempStateMap[mode] || AnnotationState.NONE; | ||
} | ||
break; | ||
case AnnotationState.HIGHLIGHT_TEMP: | ||
case AnnotationState.REGION_TEMP: | ||
if (input === AnnotationInput.CANCEL || input === AnnotationInput.SUCCESS) { | ||
this.currentState = AnnotationState.NONE; | ||
} | ||
break; | ||
default: | ||
} | ||
|
||
return stateModeMap[this.currentState]; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.