@@ -12,46 +12,43 @@ function Annotation({
12
12
annotations,
13
13
} : Annotations ) {
14
14
const [ state , dispatch ] = useContext ( StateContext ) ;
15
- const [ annotation , setAnnotations ] = useState ( '' ) ;
16
- const ref = useRef ( null ) ;
15
+ const [ annotation , setAnnotations ] = useState ( annotations ) ;
17
16
// React hook setting the annotation button modal open/close state
18
17
const [ open , setOpen ] = React . useState ( false ) ;
18
+ const ref = useRef ( null ) ;
19
+ // focusIndex and childrenArray are used to find the array of all elements on the canvas
19
20
const focusIndex = state . canvasFocus . componentId - 1 ;
20
21
const childrenArray = state . components [ focusIndex ] . children ;
21
-
22
+
22
23
// For showing the modal
23
24
const handleOpen = ( id ) => {
24
- const childEle = handleFindAnno ( childrenArray , id ) ;
25
25
setOpen ( true ) ;
26
26
} ;
27
27
28
- // For closing the modal
28
+ // For closing the modal and setting the global annotation value to the hook value
29
29
const handleClose = ( id ) => {
30
- const focusIndex = state . canvasFocus . componentId - 1 ;
31
- const childrenArray = state . components [ focusIndex ] . children ;
32
30
const childEle = handleFindAnno ( childrenArray , id ) ;
33
-
34
- if ( childEle ) {
31
+ if ( childEle ) {
32
+ // set global annotation value for this component to it's hook value
35
33
childEle . annotations = annotation ;
36
34
}
37
-
38
35
setOpen ( false ) ;
39
36
} ;
40
37
41
38
/**
42
39
* Handles when text exists in the textarea of the modal.
43
40
* If text exists/does not exist, corresponding button changes colors.
44
- */
41
+ * Sets hook value to what is contained in the textarea
42
+ */
45
43
const handleAnnoChange = ( event ) => {
46
44
const { value } = event . target ;
47
45
48
- if ( value === '' || value === undefined ) {
46
+ if ( value === '' || value === undefined ) {
49
47
ref . current . style . background = '#3ec1ac' ;
50
- ref . current . id = 'btn' + event . target . id ;
51
- setAnnotations ( value ) ;
52
48
} else {
53
49
ref . current . style . background = '#cc99ff' ;
54
- ref . current . id = 'btn' + event . target . id ;
50
+ }
51
+ if ( value != annotation ) {
55
52
setAnnotations ( value ) ;
56
53
}
57
54
}
@@ -61,13 +58,18 @@ function Annotation({
61
58
* where the canvas components are placed
62
59
*/
63
60
const handleFindAnno = ( array , id ) => {
64
- for ( let i = 0 ; i < array . length ; i ++ ) {
65
- if ( array [ i ] . childId === Number ( id ) ) {
66
- return array [ i ] ;
67
- }
68
-
69
- if ( array [ i ] . children . length > 0 ) {
70
- return handleFindAnno ( array [ i ] , id ) ;
61
+ for ( let i = 0 ; i < array . length ; i ++ ) {
62
+ const currentElement = array [ i ] ;
63
+ if ( currentElement . childId === id ) {
64
+ return currentElement ;
65
+ // finds nested element if nested within canvas
66
+ } else if ( currentElement . children . length > 0 ) {
67
+ // temp is to prevent a return of empty string since canvas element should always exist and allows the
68
+ // recursion to continue
69
+ const temp = handleFindAnno ( currentElement . children , id )
70
+ if ( temp != '' ) {
71
+ return temp ;
72
+ }
71
73
}
72
74
}
73
75
return '' ;
@@ -77,28 +79,24 @@ function Annotation({
77
79
* This useEffect allows the annotations to remain persistent when changing between root level components on the right panel
78
80
*/
79
81
useEffect ( ( ) => {
80
- const targetElement = handleFindAnno ( childrenArray , id ) ;
81
- if ( targetElement !== '' ) {
82
- const event = {
83
- target : { value : targetElement . annotations } ,
84
- id : id ,
85
- }
86
- console . log ( "is this a value?" , event . target . value ) ;
87
- handleAnnoChange ( event ) ;
82
+ const event = {
83
+ target : { value : annotation } ,
84
+ id : id ,
88
85
}
89
- } , [ state . canvasFocus ] )
90
-
86
+ handleAnnoChange ( event ) ;
87
+ } , [ ] )
88
+
91
89
const body = (
92
90
< div className = 'annotate-position' >
93
91
< span className = 'annotate-textarea-header' > Notes for: { name } ( { id } )</ span >
94
- < textarea className = 'annotate-textarea' id = { id . toString ( ) } onChange = { handleAnnoChange } > { handleFindAnno ( childrenArray , id ) . annotations } </ textarea >
92
+ < textarea className = 'annotate-textarea' id = { id . toString ( ) } onChange = { handleAnnoChange } > { annotations } </ textarea >
95
93
</ div >
96
94
)
97
95
98
96
return (
99
- < div style = { { padding : '1px' , float : 'right' } } >
97
+ < div style = { { padding : '1px' , float : 'right' } } >
100
98
< button className = 'annotate-button-empty' id = { "btn" + id } onClick = { ( ) => handleOpen ( id ) } ref = { ref } > Notes</ button >
101
- < Modal
99
+ < Modal
102
100
open = { open }
103
101
onClose = { ( ) => handleClose ( id ) }
104
102
keepMounted = { true }
@@ -110,4 +108,4 @@ function Annotation({
110
108
}
111
109
112
110
export default Annotation ;
113
- // Caret End
111
+ // Caret End
0 commit comments