1
- import React , { useContext } from 'react' ;
1
+ import React , { useContext , useState , useEffect } from 'react' ;
2
2
import { useDrop , DropTargetMonitor } from 'react-dnd' ;
3
3
import { ItemTypes } from '../../constants/ItemTypes' ;
4
4
import StateContext from '../../context/context' ;
@@ -7,9 +7,92 @@ import { combineStyles } from '../../helperFunctions/combineStyles';
7
7
import renderChildren from '../../helperFunctions/renderChildren' ;
8
8
// Caret start
9
9
import Arrow from './Arrow' ;
10
+ import { getRowsStateFromCache } from '@mui/x-data-grid/hooks/features/rows/gridRowsUtils' ;
10
11
11
12
function Canvas ( props ) : JSX . Element {
12
13
const [ state , dispatch ] = useContext ( StateContext ) ;
14
+ const [ newComp , setNewComp ] = useState ( false ) ;
15
+ const [ copiedChildrenArr , setCopiedChildrenArr ] = useState ( [ ] ) ;
16
+ const [ copiedComp , setCopiedComp ] = useState ( { } ) ;
17
+
18
+ useEffect ( ( ) => {
19
+ if ( newComp ) {
20
+ console . log ( 'inside use effect' , { state} ) ;
21
+ console . log ( 'copiedComp' , copiedComp . name )
22
+ //find updated comp
23
+ const copy = state . components . find ( comp => comp . name === copiedComp . name )
24
+ console . log ( { copy} )
25
+ // make a array of copied children from the copied component
26
+ if ( copy . children . length ) {
27
+ const masterArr = [ ] ;
28
+ const children = copy . children ;
29
+ function deepChildCopy ( children , parentId ) {
30
+ for ( let i = 0 ; i < children . length ; i ++ ) {
31
+ const child = children [ i ] ;
32
+ let id = ( parentId ) ? parentId : null ;
33
+ if ( child . typeId < 1000 ) {
34
+ console . log ( { child} )
35
+ masterArr . push ( {
36
+ type : "HTML Element" ,
37
+ typeId : child . typeId ,
38
+ childId : id
39
+ } )
40
+ if ( child . children . length ) {
41
+ console . log ( 'do a recursive call' ) ;
42
+ console . log ( 'child.childrean array in recursion' , child . children )
43
+ console . log ( 'child.children.id' , child . childId )
44
+ deepChildCopy ( child . children , child . childId ) ;
45
+ }
46
+ }
47
+ }
48
+ }
49
+ deepChildCopy ( children , null ) ;
50
+ console . log ( { masterArr} )
51
+ setCopiedChildrenArr ( masterArr ) ;
52
+ console . log ( 'SADJBFSDJBFLJSHDBFJLDHS' , copiedChildrenArr )
53
+ }
54
+
55
+ const components = state . components
56
+
57
+ // find the ID of the newly created component
58
+ const newId = components [ components . length - 1 ] [ 'id' ]
59
+ dispatch ( {
60
+ type : 'ADD CHILD' ,
61
+ payload : {
62
+ type : "Component" ,
63
+ typeId : newId ,
64
+ childId : null
65
+ }
66
+ } ) ;
67
+ }
68
+ setNewComp ( false )
69
+ } , [ newComp ] )
70
+
71
+ // useEffect(()=>{
72
+ // const lastComp = state.components[state.components.length - 1];
73
+ // if (copiedChildrenArr.length) {
74
+ // console.log('setCopiedChildrenArr use effect running')
75
+ // // copiedChildrenArr.forEach(com => console.log(com))
76
+ // // console.log(copiedChildrenArr[0])
77
+ // // console.log(copiedChildrenArr[1])
78
+ // for (let i = 0; i < copiedChildrenArr.length; i++) {
79
+ // dispatch({
80
+ // type: 'ADD CHILD',
81
+ // payload: {...copiedChildrenArr[i], copyId: lastComp.id}
82
+ // });
83
+ // }
84
+ // // dispatch({
85
+ // // type: 'ADD CHILD',
86
+ // // payload: {...copiedChildrenArr[1], copyId: lastComp.id}
87
+ // // });
88
+ // // setCopiedChildrenArr(copiedChildrenArr.slice(1))
89
+ // setCopiedChildrenArr([])
90
+ // }
91
+
92
+ // },[copiedChildrenArr])
93
+
94
+
95
+
13
96
// Caret start
14
97
Arrow . deleteLines ( ) ;
15
98
// find the current component to render on the canvas
@@ -49,7 +132,7 @@ function Canvas(props): JSX.Element {
49
132
return ;
50
133
}
51
134
// if item dropped is going to be a new instance (i.e. it came from the left panel), then create a new child component
52
- if ( item . newInstance ) {
135
+ if ( item . newInstance && item . instanceType !== "Component" ) {
53
136
dispatch ( {
54
137
type : 'ADD CHILD' ,
55
138
payload : {
@@ -58,12 +141,77 @@ function Canvas(props): JSX.Element {
58
141
childId : null
59
142
}
60
143
} ) ;
144
+ } else if ( item . newInstance && item . instanceType === "Component" ) {
145
+ let hasDiffParent = false ;
146
+ const components = state . components ;
147
+ let newChildName = '' ;
148
+ // loop over componenets array
149
+ for ( let i = 0 ; i < components . length ; i ++ ) {
150
+ const comp = components [ i ] ;
151
+ //loop over each componenets child
152
+ for ( let j = 0 ; j < comp . children . length ; j ++ ) {
153
+ const child = comp . children [ j ] ;
154
+ if ( child . name === 'seperator' ) continue ;
155
+ // check if the item.instanceTypeId matches and child ID
156
+ if ( item . instanceTypeId === child . typeId ) {
157
+ console . log ( 'parent found' , comp )
158
+ // check if the name of the parent matches the canvas focus name
159
+ // comp is the parent component
160
+ // currentComponent is the canvas.focus component
161
+ if ( comp . name === currentComponent . name ) {
162
+ i = components . length ;
163
+ break ;
164
+ } else {
165
+ // if false
166
+ console . log ( 'different parent' ) ;
167
+ setCopiedComp ( child ) ;
168
+ hasDiffParent = true ;
169
+ newChildName = child . name ;
170
+ i = components . length ;
171
+ break ;
172
+ }
173
+ }
174
+ }
175
+ }
176
+ if ( ! hasDiffParent ) {
177
+ dispatch ( {
178
+ type : 'ADD CHILD' ,
179
+ payload : {
180
+ type : item . instanceType ,
181
+ typeId : item . instanceTypeId ,
182
+ childId : null
183
+ }
184
+ } ) ;
185
+ } else {
186
+ // create a new component
187
+ let name = prompt ( "Component already has a parent. \nDo you want to create a new component and import its elements?" , "Enter component name here" ) ;
188
+ // console.log({newChildName}, 1)
189
+ while ( components . some ( comp => comp . name === name ) ) {
190
+ name = prompt ( `${ name } component already exists. \nPlease pick a new name.` ) ;
191
+ }
192
+ if ( name ) {
193
+ dispatch ( {
194
+ type : 'ADD COMPONENT' ,
195
+ payload : { componentName : name , root : false }
196
+ } ) ;
197
+
198
+ setNewComp ( true ) ;
199
+ console . log ( { newComp} )
200
+
201
+ // console.log({components})
202
+ // console.log({newId})
203
+ setNewComp ( ! newComp )
204
+ }
205
+
206
+ }
207
+
61
208
}
62
209
} ,
63
210
collect : monitor => ( {
64
211
isOver : ! ! monitor . isOver ( )
65
212
} )
66
213
} ) ;
214
+
67
215
// Styling for Canvas
68
216
const defaultCanvasStyle = {
69
217
width : '100%' ,
0 commit comments