-
Notifications
You must be signed in to change notification settings - Fork 116
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
feat(annotations): Handle annotations staged change event #1247
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7f8a098
feat(annotations): Handle annotations staged change event
d6f6f98
feat(annotations): AnnotationControls dumber, FSM smarter
6118793
feat(annotations): Address comments
fa98114
feat(annotations): Add FSM unit tests
d31e134
feat(annotations): More unit tests
b786d05
feat(annotations): Add tests for every possible inputs for every state
4828358
feat(annotations): Address comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having mode default to
none
seems ripe for bugs to be introduced. Would it work if it was just optionally undefined?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The state in FSM cannot be
undefined
, so if mode isundefined
and state transition depends on mode, the state has to benone
which is the same as default mode tonone
.