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