1
- // Copyright (C) 2023-2024 CVAT.ai Corporation
1
+ // Copyright (C) 2023-2025 CVAT.ai Corporation
2
2
//
3
3
// SPDX-License-Identifier: MIT
4
4
@@ -37,7 +37,6 @@ const core = getCore();
37
37
interface State {
38
38
actions : BaseAction [ ] ;
39
39
activeAction : BaseAction | null ;
40
- initialized : boolean ;
41
40
fetching : boolean ;
42
41
progress : number | null ;
43
42
progressMessage : string | null ;
@@ -50,7 +49,6 @@ interface State {
50
49
}
51
50
52
51
enum ReducerActionType {
53
- SET_INITIALIZED = 'SET_INITIALIZED' ,
54
52
SET_ANNOTATIONS_ACTIONS = 'SET_ANNOTATIONS_ACTIONS' ,
55
53
SET_ACTIVE_ANNOTATIONS_ACTION = 'SET_ACTIVE_ANNOTATIONS_ACTION' ,
56
54
UPDATE_PROGRESS = 'UPDATE_PROGRESS' ,
@@ -65,9 +63,6 @@ enum ReducerActionType {
65
63
}
66
64
67
65
export const reducerActions = {
68
- setInitialized : ( initialized : boolean ) => (
69
- createAction ( ReducerActionType . SET_INITIALIZED , { initialized } )
70
- ) ,
71
66
setAnnotationsActions : ( actions : BaseAction [ ] ) => (
72
67
createAction ( ReducerActionType . SET_ANNOTATIONS_ACTIONS , { actions } )
73
68
) ,
@@ -105,7 +100,6 @@ export const reducerActions = {
105
100
106
101
const defaultState = {
107
102
actions : [ ] ,
108
- initialized : false ,
109
103
fetching : false ,
110
104
activeAction : null ,
111
105
progress : null ,
@@ -119,23 +113,13 @@ const defaultState = {
119
113
} ;
120
114
121
115
const reducer = ( state : State = { ...defaultState } , action : ActionUnion < typeof reducerActions > ) : State => {
122
- if ( action . type === ReducerActionType . SET_INITIALIZED ) {
123
- return {
124
- ...state ,
125
- initialized : action . payload . initialized ,
126
- } ;
127
- }
128
-
129
116
if ( action . type === ReducerActionType . SET_ANNOTATIONS_ACTIONS ) {
130
117
const { actions } = action . payload ;
131
- const { targetObjectState } = state ;
132
118
133
- const filteredActions = targetObjectState ? actions
134
- . filter ( ( _action ) => _action . isApplicableForObject ( targetObjectState ) ) : actions ;
135
119
return {
136
120
...state ,
137
121
actions,
138
- activeAction : filteredActions [ 0 ] ?? null ,
122
+ activeAction : state . activeAction ?? actions [ 0 ] ?? null ,
139
123
} ;
140
124
}
141
125
@@ -246,7 +230,6 @@ type ActionParameterProps = NonNullable<BaseAction['parameters']>[keyof BaseActi
246
230
247
231
const componentStorage = createStore ( reducer , {
248
232
actions : [ ] ,
249
- initialized : false ,
250
233
fetching : false ,
251
234
activeAction : null ,
252
235
progress : null ,
@@ -319,15 +302,16 @@ function ActionParameterComponent(props: ActionParameterProps & { onChange: (val
319
302
interface Props {
320
303
onClose : ( ) => void ;
321
304
targetObjectState ?: ObjectState ;
305
+ defaultAnnotationAction ?: string ;
322
306
}
323
307
324
308
function AnnotationsActionsModalContent ( props : Props ) : JSX . Element {
325
- const { onClose, targetObjectState : defaultTargetObjectState } = props ;
309
+ const { onClose, targetObjectState : defaultTargetObjectState , defaultAnnotationAction } = props ;
326
310
const dispatch = useDispatch ( ) ;
327
311
const storage = getCVATStore ( ) ;
328
312
const cancellationRef = useRef < boolean > ( false ) ;
329
313
const {
330
- initialized , actions, activeAction, fetching, targetObjectState, cancelled,
314
+ actions, activeAction, fetching, targetObjectState, cancelled,
331
315
progress, progressMessage, frameFrom, frameTo, actionParameters, modalVisible,
332
316
} = useSelector ( ( state : State ) => ( { ...state } ) , shallowEqual ) ;
333
317
@@ -337,20 +321,25 @@ function AnnotationsActionsModalContent(props: Props): JSX.Element {
337
321
const currentFrameAction = activeAction instanceof BaseCollectionAction || targetObjectState !== null ;
338
322
339
323
useEffect ( ( ) => {
340
- dispatch ( reducerActions . setVisible ( true ) ) ;
341
- dispatch ( reducerActions . updateFrameFrom ( jobInstance . startFrame ) ) ;
342
- dispatch ( reducerActions . updateFrameTo ( jobInstance . stopFrame ) ) ;
343
- dispatch ( reducerActions . updateTargetObjectState ( defaultTargetObjectState ?? null ) ) ;
344
- } , [ ] ) ;
324
+ core . actions . list ( ) . then ( ( list : BaseAction [ ] ) => {
325
+ dispatch ( reducerActions . setAnnotationsActions ( list ) ) ;
326
+
327
+ if ( defaultAnnotationAction ) {
328
+ const defaultAction = list . find ( ( action ) => action . name === defaultAnnotationAction ) ;
329
+ if (
330
+ defaultAction &&
331
+ ( ! defaultTargetObjectState || defaultAction . isApplicableForObject ( defaultTargetObjectState ) )
332
+ ) {
333
+ dispatch ( reducerActions . setActiveAnnotationsAction ( defaultAction ) ) ;
334
+ }
335
+ }
345
336
346
- useEffect ( ( ) => {
347
- if ( ! initialized ) {
348
- core . actions . list ( ) . then ( ( list : BaseAction [ ] ) => {
349
- dispatch ( reducerActions . setAnnotationsActions ( list ) ) ;
350
- dispatch ( reducerActions . setInitialized ( true ) ) ;
351
- } ) ;
352
- }
353
- } , [ initialized ] ) ;
337
+ dispatch ( reducerActions . setVisible ( true ) ) ;
338
+ dispatch ( reducerActions . updateFrameFrom ( jobInstance . startFrame ) ) ;
339
+ dispatch ( reducerActions . updateFrameTo ( jobInstance . stopFrame ) ) ;
340
+ dispatch ( reducerActions . updateTargetObjectState ( defaultTargetObjectState ?? null ) ) ;
341
+ } ) ;
342
+ } , [ ] ) ;
354
343
355
344
return (
356
345
< Modal
@@ -643,14 +632,23 @@ function AnnotationsActionsModalContent(props: Props): JSX.Element {
643
632
644
633
const MemoizedAnnotationsActionsModalContent = React . memo ( AnnotationsActionsModalContent ) ;
645
634
646
- export function openAnnotationsActionModal ( objectState ?: ObjectState ) : void {
635
+ export function openAnnotationsActionModal ( {
636
+ defaultObjectState,
637
+ defaultAnnotationAction,
638
+ } : {
639
+ defaultObjectState ?: ObjectState ,
640
+ defaultAnnotationAction ?: string ,
641
+ } = { } ) : void {
642
+ window . document . dispatchEvent ( new MouseEvent ( 'mousedown' , { bubbles : true } ) ) ;
643
+
647
644
const div = window . document . createElement ( 'div' ) ;
648
645
window . document . body . append ( div ) ;
649
646
const root = createRoot ( div ) ;
650
647
root . render (
651
648
< Provider store = { componentStorage } >
652
649
< MemoizedAnnotationsActionsModalContent
653
- targetObjectState = { objectState }
650
+ targetObjectState = { defaultObjectState }
651
+ defaultAnnotationAction = { defaultAnnotationAction }
654
652
onClose = { ( ) => {
655
653
root . unmount ( ) ;
656
654
div . remove ( ) ;
0 commit comments