@@ -12,29 +12,30 @@ function Annotation({
12
12
annotations,
13
13
} : Annotations ) {
14
14
const [ state , dispatch ] = useContext ( StateContext ) ;
15
- const [ annotation , setAnnotations ] = useState ( '' ) ;
15
+ const [ annotation , setAnnotations ] = useState ( annotations ) ;
16
16
const ref = useRef ( null ) ;
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
20
const childrenArray = state . components [ focusIndex ] . children ;
21
21
22
22
// For showing the modal
23
- const handleOpen = ( id ) => {
24
- const childEle = handleFindAnno ( childrenArray , id ) ;
23
+ const handleOpen = ( ) => {
25
24
setOpen ( true ) ;
26
25
} ;
27
-
26
+
28
27
// For closing the modal
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 ) {
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!!!!!!!!')
35
37
childEle . annotations = annotation ;
36
38
}
37
-
38
39
setOpen ( false ) ;
39
40
} ;
40
41
@@ -44,15 +45,11 @@ function Annotation({
44
45
*/
45
46
const handleAnnoChange = ( event ) => {
46
47
const { value } = event . target ;
47
-
48
- if ( value === '' || value === undefined ) {
48
+ setAnnotations ( value ) ;
49
+ if ( value === '' || value === undefined ) {
49
50
ref . current . style . background = '#3ec1ac' ;
50
- ref . current . id = 'btn' + event . target . id ;
51
- setAnnotations ( value ) ;
52
51
} else {
53
52
ref . current . style . background = '#cc99ff' ;
54
- ref . current . id = 'btn' + event . target . id ;
55
- setAnnotations ( value ) ;
56
53
}
57
54
}
58
55
@@ -61,46 +58,49 @@ 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 ] ;
61
+ let refElement = '' ;
62
+ for ( let i = 0 ; i < array . length ; i ++ ) {
63
+ const currentElement = array [ i ] ;
64
+ if ( currentElement . childId === id ) {
65
+ return currentElement ;
66
+ }
67
+ else if ( currentElement . children . length > 0 ) {
68
+ refElement = handleFindAnno ( currentElement . children , id ) ;
69
+ if ( refElement !== '' ) return refElement ;
67
70
}
71
+ }
72
+ return refElement ;
73
+ } ;
68
74
69
- if ( array [ i ] . children . length > 0 ) {
70
- return handleFindAnno ( array [ i ] , id ) ;
75
+ const annoRefresh = ( array ) => {
76
+ for ( const element of array ) {
77
+ if ( element . hasOwnProperty ( annotations ) ) {
78
+ console . log ( 'Somthing here' , ref ) ;
71
79
}
80
+ if ( element . children . length > 0 ) return annoRefresh ( element . children ) ;
72
81
}
73
- return '' ;
74
- }
82
+ } ;
75
83
76
84
/**
77
85
* This useEffect allows the annotations to remain persistent when changing between root level components on the right panel
78
86
*/
79
87
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 ) ;
88
- }
89
- } , [ state . canvasFocus ] )
88
+ annoRefresh ( childrenArray ) ;
89
+ } , [ state . canvasFocus ] ) ;
90
90
91
91
const body = (
92
92
< div className = 'annotate-position' >
93
93
< 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 >
94
+ < textarea className = 'annotate-textarea' id = { id . toString ( ) } onChange = { handleAnnoChange } > { annotations } </ textarea >
95
95
</ div >
96
96
)
97
97
98
98
return (
99
99
< div style = { { padding : '1px' , float : 'right' } } >
100
- < button className = 'annotate-button-empty' id = { "btn" + id } onClick = { ( ) => handleOpen ( id ) } ref = { ref } > Notes</ button >
100
+ < button className = 'annotate-button-empty' id = { "btn" + id } onClick = { ( ) => handleOpen ( ) } ref = { ref } > Notes</ button >
101
101
< Modal
102
102
open = { open }
103
- onClose = { ( ) => handleClose ( id ) }
103
+ onClose = { handleClose }
104
104
keepMounted = { true }
105
105
>
106
106
{ body }
0 commit comments