-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eventing): Emit new annotations_staged_change event
- Loading branch information
Conrad Chan
committed
Sep 1, 2020
1 parent
d0e7cfc
commit c8e00f4
Showing
3 changed files
with
73 additions
and
0 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
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,68 @@ | ||
import eventManager from '../../common/EventManager'; | ||
import { AppState } from '../types'; | ||
import { getCreatorStaged, CreatorItem, isCreatorStagedRegion, isCreatorStagedHighlight } from '../creator'; | ||
import { Event } from '../../@types'; | ||
|
||
type Status = 'create' | 'update' | 'cancel'; | ||
|
||
type Type = 'highlight' | 'region'; | ||
|
||
type StagedChangeEvent = { | ||
status: Status; | ||
type: Type; | ||
}; | ||
|
||
const getStatus = (prevStaged: CreatorItem, nextStaged: CreatorItem): Status | null => { | ||
let status: Status | null = null; | ||
|
||
if (prevStaged === null && nextStaged !== null) { | ||
status = 'create'; | ||
} | ||
|
||
if (prevStaged !== null && nextStaged !== null) { | ||
status = 'update'; | ||
} | ||
|
||
return status; | ||
}; | ||
|
||
const getType = (staged: CreatorItem): Type | null => { | ||
let type: Type | null = null; | ||
|
||
if (isCreatorStagedRegion(staged)) { | ||
type = 'region'; | ||
} | ||
|
||
if (isCreatorStagedHighlight(staged)) { | ||
type = 'highlight'; | ||
} | ||
|
||
return type; | ||
}; | ||
|
||
export const handleSetStagedAction = (prevState: AppState, nextState: AppState): void => { | ||
const prevStaged = getCreatorStaged(prevState); | ||
const nextStaged = getCreatorStaged(nextState); | ||
const status = getStatus(prevStaged, nextStaged); | ||
const type = getType(prevStaged) || getType(nextStaged); | ||
|
||
if (!status || !type) { | ||
return; | ||
} | ||
|
||
const payload: StagedChangeEvent = { type, status }; | ||
eventManager.emit(Event.ANNOTATIONS_STAGED_CHANGE, payload); | ||
}; | ||
|
||
export const handleResetCreatorAction = (prevState: AppState): void => { | ||
const prevStaged = getCreatorStaged(prevState); | ||
const type = getType(prevStaged); | ||
|
||
if (!type) { | ||
return; | ||
} | ||
|
||
const payload: StagedChangeEvent = { type, status: 'cancel' }; | ||
|
||
eventManager.emit(Event.ANNOTATIONS_STAGED_CHANGE, payload); | ||
}; |