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,58 @@ 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
+ //find updated comp
21
+ const copy = state . components . find ( comp => comp . name === copiedComp . name )
22
+ // make a array of copied children from the copied component
23
+ if ( copy . children . length ) {
24
+ const masterArr = [ ] ;
25
+ const children = copy . children ;
26
+ function deepChildCopy ( children , parentId ) {
27
+ for ( let i = 0 ; i < children . length ; i ++ ) {
28
+ const child = children [ i ] ;
29
+ let id = ( parentId ) ? parentId : null ;
30
+ if ( child . typeId < 1000 ) {
31
+ masterArr . push ( {
32
+ type : "HTML Element" ,
33
+ typeId : child . typeId ,
34
+ childId : id
35
+ } )
36
+ if ( child . children . length ) {
37
+ deepChildCopy ( child . children , child . childId ) ;
38
+ }
39
+ }
40
+ }
41
+ }
42
+ deepChildCopy ( children , null ) ;
43
+ setCopiedChildrenArr ( masterArr ) ;
44
+ }
45
+
46
+ const components = state . components
47
+
48
+ // find the ID of the newly created component
49
+ const newId = components [ components . length - 1 ] [ 'id' ]
50
+ dispatch ( {
51
+ type : 'ADD CHILD' ,
52
+ payload : {
53
+ type : "Component" ,
54
+ typeId : newId ,
55
+ childId : null
56
+ }
57
+ } ) ;
58
+ }
59
+ setNewComp ( false )
60
+ } , [ newComp ] )
61
+
13
62
// Caret start
14
63
Arrow . deleteLines ( ) ;
15
64
// find the current component to render on the canvas
@@ -49,7 +98,7 @@ function Canvas(props): JSX.Element {
49
98
return ;
50
99
}
51
100
// 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 ) {
101
+ if ( item . newInstance && item . instanceType !== "Component" ) {
53
102
dispatch ( {
54
103
type : 'ADD CHILD' ,
55
104
payload : {
@@ -58,12 +107,73 @@ function Canvas(props): JSX.Element {
58
107
childId : null
59
108
}
60
109
} ) ;
110
+ } else if ( item . newInstance && item . instanceType === "Component" ) {
111
+ let hasDiffParent = false ;
112
+ const components = state . components ;
113
+ let newChildName = '' ;
114
+ // loop over componenets array
115
+ for ( let i = 0 ; i < components . length ; i ++ ) {
116
+ const comp = components [ i ] ;
117
+ //loop over each componenets child
118
+ for ( let j = 0 ; j < comp . children . length ; j ++ ) {
119
+ const child = comp . children [ j ] ;
120
+ if ( child . name === 'seperator' ) continue ;
121
+ // check if the item.instanceTypeId matches and child ID
122
+ if ( item . instanceTypeId === child . typeId ) {
123
+ // check if the name of the parent matches the canvas focus name
124
+ // comp is the parent component
125
+ // currentComponent is the canvas.focus component
126
+ if ( comp . name === currentComponent . name ) {
127
+ i = components . length ;
128
+ break ;
129
+ } else {
130
+ // if false
131
+ setCopiedComp ( child ) ;
132
+ hasDiffParent = true ;
133
+ newChildName = child . name ;
134
+ i = components . length ;
135
+ break ;
136
+ }
137
+ }
138
+ }
139
+ }
140
+ if ( ! hasDiffParent ) {
141
+ dispatch ( {
142
+ type : 'ADD CHILD' ,
143
+ payload : {
144
+ type : item . instanceType ,
145
+ typeId : item . instanceTypeId ,
146
+ childId : null
147
+ }
148
+ } ) ;
149
+ } else {
150
+
151
+ // able to duplicate a component in dev only does not work for prod
152
+ // create a new component
153
+
154
+ // let name = prompt("Component already has a parent. \nDo you want to create a new component and import its elements?", "Enter component name here");
155
+ // while (components.some(comp => comp.name === name)) {
156
+ // name = prompt(`${name} component already exists. \nPlease pick a new name.`);
157
+ // }
158
+ // if (name) {
159
+ // dispatch({
160
+ // type: 'ADD COMPONENT',
161
+ // payload: { componentName: name, root: false }
162
+ // });
163
+
164
+ // setNewComp(true);
165
+ // setNewComp(!newComp)
166
+ // }
167
+
168
+ }
169
+
61
170
}
62
171
} ,
63
172
collect : monitor => ( {
64
173
isOver : ! ! monitor . isOver ( )
65
174
} )
66
175
} ) ;
176
+
67
177
// Styling for Canvas
68
178
const defaultCanvasStyle = {
69
179
width : '100%' ,
0 commit comments