@@ -9,68 +9,85 @@ import StateContext from '../../context/context';
9
9
function Annotation ( {
10
10
id,
11
11
name,
12
+ annotations,
12
13
} : Annotations ) {
13
- const [ annotations , setAnnotations ] = useState ( '' ) ;
14
14
const [ state , dispatch ] = useContext ( StateContext ) ;
15
+ const [ annotation , setAnnotations ] = useState ( '' ) ;
15
16
const ref = useRef ( null ) ;
16
-
17
17
// React hook setting the annotation button modal open/close state
18
18
const [ open , setOpen ] = React . useState ( false ) ;
19
-
19
+ const focusIndex = state . canvasFocus . componentId - 1 ;
20
+ const childrenArray = state . components [ focusIndex ] . children ;
21
+
20
22
// For showing the modal
21
23
const handleOpen = ( id ) => {
24
+ const childEle = handleFindAnno ( childrenArray , id ) ;
22
25
setOpen ( true ) ;
23
26
} ;
24
27
25
28
// For closing the modal
26
- const handleClose = ( ) => {
29
+ const handleClose = ( id ) => {
30
+ const focusIndex = state . canvasFocus . componentId - 1 ;
31
+ const childrenArray = state . components [ focusIndex ] . children ;
32
+ const childEle = handleFindAnno ( childrenArray , id ) ;
33
+
34
+ if ( childEle ) {
35
+ childEle . annotations = annotation ;
36
+ }
37
+
27
38
setOpen ( false ) ;
28
39
} ;
29
40
30
- // Handles when text exists in the textarea of the modal. If text exists/does not exist, corresponding button changes colors.
41
+ /**
42
+ * Handles when text exists in the textarea of the modal.
43
+ * If text exists/does not exist, corresponding button changes colors.
44
+ */
31
45
const handleAnnoChange = ( event ) => {
32
46
const { value } = event . target ;
33
- const focusIndex = state . canvasFocus . componentId - 1 ;
34
- const childrenArray = state . components [ focusIndex ] . children ;
35
47
36
- if ( value === '' ) {
48
+ if ( value === '' || value === undefined ) {
37
49
ref . current . style . background = '#3ec1ac' ;
38
50
ref . current . id = 'btn' + event . target . id ;
51
+ setAnnotations ( value ) ;
39
52
} else {
40
53
ref . current . style . background = '#cc99ff' ;
41
54
ref . current . id = 'btn' + event . target . id ;
42
55
setAnnotations ( value ) ;
43
-
44
- let childEle = handleSaveAnno ( childrenArray , event . target . id ) ;
45
-
46
- if ( childEle ) {
47
- childEle . annotations = annotations ;
48
- setState ( childEle . annotations ) ;
49
- }
50
56
}
51
57
}
52
58
53
-
54
- const handleSaveAnno = ( array , id ) => {
59
+ /**
60
+ * This handler will find the specific anno for the corresponding component on the canvas in the childrenArray -
61
+ * where the canvas components are placed
62
+ */
63
+ const handleFindAnno = ( array , id ) => {
55
64
for ( let i = 0 ; i < array . length ; i ++ ) {
56
- if ( array [ i ] . childId === id ) {
65
+ if ( array [ i ] . childId === Number ( id ) ) {
57
66
return array [ i ] ;
58
67
}
68
+
59
69
if ( array [ i ] . children . length > 0 ) {
60
- return handleSaveAnno ( array [ i ] , id ) ;
70
+ return handleFindAnno ( array [ i ] , id ) ;
61
71
}
62
72
}
63
73
}
64
74
65
- /*
66
- <span className='annotate-textarea-footer'>
67
- <button className='annotate-textarea-savebutton'>Save Notes</button>
68
- </span>
69
- */
75
+ /**
76
+ * This useEffect allows the annotations to remain persistent when changing between root level components on the right panel
77
+ */
78
+ useEffect ( ( ) => {
79
+ const event = {
80
+ target : { value : handleFindAnno ( childrenArray , id ) . annotations } ,
81
+ id : id ,
82
+ }
83
+
84
+ handleAnnoChange ( event ) ;
85
+ } , [ state . canvasFocus ] )
86
+
70
87
const body = (
71
88
< div className = 'annotate-position' >
72
89
< span className = 'annotate-textarea-header' > Notes for: { name } ( { id } )</ span >
73
- < textarea className = 'annotate-textarea' id = { id . toString ( ) } onChange = { handleAnnoChange } > </ textarea >
90
+ < textarea className = 'annotate-textarea' id = { id . toString ( ) } onChange = { handleAnnoChange } > { handleFindAnno ( childrenArray , id ) . annotations } </ textarea >
74
91
</ div >
75
92
)
76
93
@@ -79,7 +96,7 @@ function Annotation({
79
96
< button className = 'annotate-button-empty' id = { "btn" + id } onClick = { ( ) => handleOpen ( id ) } ref = { ref } > Notes</ button >
80
97
< Modal
81
98
open = { open }
82
- onClose = { handleClose }
99
+ onClose = { ( ) => handleClose ( id ) }
83
100
keepMounted = { true }
84
101
>
85
102
{ body }
0 commit comments