import { ANNOTATION_ACTIONS } from '@/store/Actions' ; import { IStepInfo } from '@/types/step' ; import { GetFileData , IFileItem , OnSave , OnSubmit } from '@/types/data' ; import { AnnotationActionTypes , ToolInstance } from './types' ; import { LoadImageAndFileData , getStepConfig } from './reducer' ; import { ESubmitType } from '@/constant' ; import { EPageTurningOperation } from '@/data/enums/AnnotationSize' ; import PageOperator from '@/utils/PageOperator' ; import { jsonParser } from '@/utils' ; const dispatchTasks = ( dispatch , tasks ) => tasks . map ( ( task ) => dispatch ( task ) ) ; param { pageTurningOperation } pageTurningOperation returns { ESubmitType } const getSubmitByPageOperation = ( pageTurningOperation ) => { if ( pageTurningOperation === EPageTurningOperation . Forward ) { return ESubmitType . Forward ; } if ( pageTurningOperation === EPageTurningOperation . Backward ) { return ESubmitType . Backward ; } if ( pageTurningOperation === EPageTurningOperation . Jump ) { return ESubmitType . Jump ; } return ESubmitType . Forward ; } ; const getBasicIndex = ( annotationStore , basicIndex ) => { const { imgList , imgIndex } = annotationStore ; const { dataSourceStep } = getStepConfig ( annotationStore . stepList , annotationStore . step ) ; let backwardResult = jsonParser ( imgList [ imgIndex - 1 ] . result ) ; const index = backwardResult [ `step_${ dataSourceStep }` ] ?. result ?. length - 1 ; return index || basicIndex ; } ; export function UpdateToolInstance ( toolInstance ) { return { type : ANNOTATION_ACTIONS . UPDATE_TOOL_INSTANCE , payload : { toolInstance , } , } ; } export function UpdateImgList ( imgList ) { return { type : ANNOTATION_ACTIONS . UPDATE_IMG_LIST , payload : { imgList , } , } ; } export function UpdateAnnotationConfig ( config ) { return { type : ANNOTATION_ACTIONS . UPDATE_ANNOTATION_CONFIG , payload : { config , } , } ; } export function SetTaskConfig ( { stepList , step , } ) { return { type : ANNOTATION_ACTIONS . SET_TASK_CONFIG , payload : { stepList , step , } , } ; } export function UpdateOnSubmit ( onSubmit ) { return { type : ANNOTATION_ACTIONS . UPDATE_ON_SUBMIT , payload : { onSubmit , } , } ; } export function UpdateOnSave ( onSave ) { return { type : ANNOTATION_ACTIONS . UPDATE_ON_SAVE , payload : { onSave , } , } ; } export function UpdateGetFileData ( getFileData ) { return { type : ANNOTATION_ACTIONS . UPDATE_GET_FILE_DATA , payload : { getFileData , } , } ; } export function UpdateRotate ( ) { return { type : ANNOTATION_ACTIONS . UPDATE_ROTATE , } ; } export function CopyBackWordResult ( ) { return { type : ANNOTATION_ACTIONS . COPY_BACKWARD_RESULT , } ; } param param0 export function InitTaskData ( { onSubmit , onSave , getFileData , imgList , step , stepList , initialIndex , } ) { const tasks = [ ] ; if ( onSubmit ) { tasks . push ( UpdateOnSubmit ( onSubmit ) ) ; } if ( onSave ) { tasks . push ( UpdateOnSave ( onSave ) ) ; } if ( getFileData ) { tasks . push ( UpdateGetFileData ( getFileData ) ) ; } tasks . push ( UpdateImgList ( imgList ) ) ; tasks . push ( SetTaskConfig ( { stepList , step } ) ) ; tasks . push ( { type : ANNOTATION_ACTIONS . CALC_STEP_PROGRESS , } ) ; tasks . push ( { type : ANNOTATION_ACTIONS . INIT_TOOL , } ) ; tasks . push ( LoadImageAndFileData ( initialIndex ) ) ; return ( dispatch ) => dispatchTasks ( dispatch , tasks ) ; } /** 获取下一步的step */ const getNextStep = ( step , stepList ) => { const currentStepIndex = stepList ?. findIndex ( ( element ) => element ?. step === step ) ; return stepList [ currentStepIndex + 1 ] ?. step ; } ; /** 切换到下一步 */ export const ToNextStep = ( pageNumber ? ) => ( dispatch , getState ) => { const { annotation } = getState ( ) ; const { step , stepList } = annotation ; const nextStep = getNextStep ( step , stepList ) ; return [ dispatch ( UpdateProcessingStep ( nextStep , pageNumber ) ) ] ; } ; param { number } toStep export const UpdateProcessingStep = ( toStep , index ? ) => ( dispatch , state ) => { const imgIndex = state ( ) ?. annotation ?. imgIndex ?? 0 ; return [ dispatch ( { type : ANNOTATION_ACTIONS . SUBMIT_RESULT } ) , dispatch ( { type : ANNOTATION_ACTIONS . SUBMIT_FILE_DATA , payload : { submitType : ESubmitType . StepChanged } , } ) , dispatch ( { type : ANNOTATION_ACTIONS . SET_STEP , payload : { toStep } } ) , dispatch ( { type : ANNOTATION_ACTIONS . CALC_STEP_PROGRESS } ) , dispatch ( LoadImageAndFileData ( index ?? imgIndex , 0 ) ) , ] ; } ; param submitType export const ToSubmitFileData = ( submitType ) => ( dispatch ) => [ dispatch ( { type : ANNOTATION_ACTIONS . SUBMIT_RESULT } ) , dispatch ( { type : ANNOTATION_ACTIONS . SUBMIT_FILE_DATA , payload : { submitType } } ) , ] ; param dispatch param nextIndex param submitType param nextBasicIndex const SubmitAndChangeFileIndex = ( dispatch , nextIndex , submitType , nextBasicIndex ? ) => [ dispatch ( ToSubmitFileData ( submitType ) ) , dispatch ( LoadImageAndFileData ( nextIndex , nextBasicIndex ) ) , ] ; const ChangeBasicIndex = ( dispatch , nextBasicIndex ) => [ dispatch ( { type : ANNOTATION_ACTIONS . SUBMIT_RESULT } ) , dispatch ( { type : ANNOTATION_ACTIONS . SET_BASIC_INDEX , payload : { basicIndex : nextBasicIndex } } ) , ] ; const ChangeTriggerEventAfterIndexChanged = ( dispatch , triggerEventAfterIndexChanged ) => { dispatch ( { type : ANNOTATION_ACTIONS . SET_TRIGGER_EVENT_AFTER_INDEX_CHANGED , payload : { triggerEventAfterIndexChanged , } , } ) ; } ; /** 向前翻页 */ export const PageBackward = ( triggerEventAfterIndexChanged = false ) => ( dispatch , getState ) => { return DispatcherTurning ( dispatch , getState , EPageTurningOperation . Backward , triggerEventAfterIndexChanged ) ; } ; /** 向后翻页 */ export const PageForward = ( triggerEventAfterIndexChanged = false ) => ( dispatch , getState ) => { return DispatcherTurning ( dispatch , getState , EPageTurningOperation . Forward , triggerEventAfterIndexChanged ) ; } ; param toIndex export const PageJump = ( toIndex , triggerEventAfterIndexChanged = false ) => ( dispatch , getState ) => { if ( toIndex === getState ( ) . imgIndex ) { return ; } return DispatcherTurning ( dispatch , getState , EPageTurningOperation . Jump , triggerEventAfterIndexChanged , toIndex ) ; } ; param dispatch param getState param pageTurningOperation param toIndex export const DispatcherTurning = ( dispatch , getState , pageTurningOperation , triggerEventAfterIndexChanged = false , toIndex ? ) => { const annotationStore = getState ( ) . annotation ; const { fileIndexChanged , fileIndex , basicIndexChanged , basicIndex } = PageOperator . getNextPageInfo ( pageTurningOperation , annotationStore , toIndex ) ; const submitType = getSubmitByPageOperation ( pageTurningOperation ) ; ChangeTriggerEventAfterIndexChanged ( dispatch , triggerEventAfterIndexChanged ) ; if ( fileIndexChanged ) { const index = submitType === ESubmitType . Backward ? getBasicIndex ( annotationStore , basicIndex ) : basicIndex ; return SubmitAndChangeFileIndex ( dispatch , fileIndex , submitType , index ) ; } if ( basicIndexChanged ) { return ChangeBasicIndex ( dispatch , basicIndex ) ; } return dispatch ( ToSubmitFileData ( submitType ) ) ; } ; /**
 * 保存当前页数据
 * */ export const ChangeSave = ( dispatch ) => { dispatch ( ToSubmitFileData ( ESubmitType . Save ) ) ; dispatch ( { type : ANNOTATION_ACTIONS . SAVE_RESULT } ) ; } ; export const SetAnnotationLoading = ( dispatch , loading ) => { dispatch ( { type : ANNOTATION_ACTIONS . SET_LOADING , payload : { loading , } , } ) ; } ;