@@ -6,16 +6,21 @@ import { combineStyles } from '../../helperFunctions/combineStyles';
6
6
import renderChildren from '../../helperFunctions/renderChildren' ;
7
7
import Arrow from './Arrow' ;
8
8
import { useDispatch , useSelector } from 'react-redux' ;
9
- import { changeFocus , addChild , snapShotAction } from '../../redux/reducers/slice/appStateSlice' ;
9
+ import {
10
+ changeFocus ,
11
+ addChild ,
12
+ snapShotAction
13
+ } from '../../redux/reducers/slice/appStateSlice' ;
10
14
import { RootState } from '../../redux/store' ;
11
15
12
16
function Canvas ( props ) : JSX . Element {
13
-
14
- const { state, contextParam, isDarkMode } = useSelector ( ( store :RootState ) => ( {
15
- state : store . appState ,
16
- contextParam : store . contextSlice ,
17
- isDarkMode : store . darkMode . isDarkMode
18
- } ) ) ;
17
+ const { state, contextParam, isDarkMode } = useSelector (
18
+ ( store : RootState ) => ( {
19
+ state : store . appState ,
20
+ contextParam : store . contextSlice ,
21
+ isDarkMode : store . darkMode . isDarkMode
22
+ } )
23
+ ) ;
19
24
const dispatch = useDispatch ( ) ;
20
25
21
26
Arrow . deleteLines ( ) ;
@@ -24,23 +29,30 @@ function Canvas(props): JSX.Element {
24
29
( elem : Component ) => elem . id === state . canvasFocus . componentId
25
30
) ;
26
31
27
-
28
32
// changes focus of the canvas to a new component / child
29
- const changeFocusFunction = ( componentId ?: number , childId ?: number | null ) => {
30
- dispatch ( changeFocus ( { componentId, childId} ) ) ;
33
+ const changeFocusFunction = (
34
+ componentId ?: number ,
35
+ childId ?: number | null
36
+ ) => {
37
+ dispatch ( changeFocus ( { componentId, childId } ) ) ;
31
38
} ;
32
39
// onClickHandler is responsible for changing the focused component and child component
33
40
function onClickHandler ( event ) {
34
41
event . stopPropagation ( ) ;
35
- changeFocusFunction ( state . canvasFocus . componentId , null ) ;
42
+ changeFocusFunction ( state . canvasFocus . componentId , null ) ;
36
43
}
37
44
38
45
// stores a snapshot of state into the past array for UNDO. snapShotFunc is also invoked for nestable elements in DirectChildHTMLNestable.tsx
39
46
const snapShotFunc = ( ) => {
40
47
// make a deep clone of state
41
- const deepCopiedState = JSON . parse ( JSON . stringify ( state ) ) ;
42
- const focusIndex = state . canvasFocus . componentId - 1 ;
43
- dispatch ( snapShotAction ( { focusIndex : focusIndex , deepCopiedState : deepCopiedState } ) )
48
+ const deepCopiedState = JSON . parse ( JSON . stringify ( state ) ) ;
49
+ const focusIndex = state . canvasFocus . componentId - 1 ;
50
+ dispatch (
51
+ snapShotAction ( {
52
+ focusIndex : focusIndex ,
53
+ deepCopiedState : deepCopiedState
54
+ } )
55
+ ) ;
44
56
} ;
45
57
46
58
// This hook will allow the user to drag items from the left panel on to the canvas
@@ -55,16 +67,16 @@ function Canvas(props): JSX.Element {
55
67
return ;
56
68
}
57
69
// if item dropped is going to be a new instance (i.e. it came from the left panel), then create a new child component
58
- if ( item . newInstance && item . instanceType !== " Component" ) {
59
- dispatch ( addChild ( {
60
- type : item . instanceType ,
61
- typeId : item . instanceTypeId ,
62
- childId : null ,
63
- contextParam : contextParam
64
-
65
- } ) )
66
-
67
- } else if ( item . newInstance && item . instanceType === " Component" ) {
70
+ if ( item . newInstance && item . instanceType !== ' Component' ) {
71
+ dispatch (
72
+ addChild ( {
73
+ type : item . instanceType ,
74
+ typeId : item . instanceTypeId ,
75
+ childId : null ,
76
+ contextParam : contextParam
77
+ } )
78
+ ) ;
79
+ } else if ( item . newInstance && item . instanceType === ' Component' ) {
68
80
let hasDiffParent = false ;
69
81
const components = state . components ;
70
82
let newChildName = '' ;
@@ -95,15 +107,17 @@ function Canvas(props): JSX.Element {
95
107
}
96
108
}
97
109
// if (!hasDiffParent) {
98
- dispatch ( addChild ( {
110
+ dispatch (
111
+ addChild ( {
99
112
type : item . instanceType ,
100
113
typeId : item . instanceTypeId ,
101
114
childId : null ,
102
115
contextParam : contextParam
103
- } ) )
116
+ } )
117
+ ) ;
104
118
}
105
119
} ,
106
- collect : monitor => ( {
120
+ collect : ( monitor ) => ( {
107
121
isOver : ! ! monitor . isOver ( )
108
122
} )
109
123
} ) ;
@@ -116,25 +130,34 @@ function Canvas(props): JSX.Element {
116
130
border : '1px solid #FBFBF2' ,
117
131
borderStyle : isOver ? 'dotted' : 'solid' ,
118
132
aspectRatio : 'auto 774 / 1200' ,
119
- boxSizing : 'border-box' ,
133
+ boxSizing : 'border-box'
120
134
} ;
121
135
122
136
const darkCanvasStyle = {
123
137
width : '100%' ,
124
138
minHeight : '100%' ,
125
139
backgroundColor : isOver ? '#4d4d4d' : '#21262c' ,
126
140
border : '1px solid #FBFBF2' ,
127
- borderStyle : isOver ? 'dotted' : 'solid' ,
141
+ borderStyle : isOver ? 'dotted' : 'solid'
128
142
} ;
129
143
// Combine the default styles of the canvas with the custom styles set by the user for that component
130
144
// The render children function renders all direct children of a given component
131
145
// Direct children are draggable/clickable
132
146
133
147
const canvasStyle = combineStyles ( defaultCanvasStyle , currentComponent . style ) ;
134
- const darkCombinedCanvasStyle = combineStyles ( darkCanvasStyle , currentComponent . style ) ;
148
+ const darkCombinedCanvasStyle = combineStyles (
149
+ darkCanvasStyle ,
150
+ currentComponent . style
151
+ ) ;
135
152
return (
136
- < div className = { 'componentContainer' } ref = { drop } style = { ! isDarkMode ? canvasStyle : darkCombinedCanvasStyle } onClick = { onClickHandler } >
137
- { renderChildren ( currentComponent . children ) }
153
+ < div
154
+ className = { 'componentContainer' }
155
+ ref = { drop }
156
+ data-testid = "drop"
157
+ style = { ! isDarkMode ? canvasStyle : darkCombinedCanvasStyle }
158
+ onClick = { onClickHandler }
159
+ >
160
+ { renderChildren ( currentComponent . children ) }
138
161
</ div >
139
162
) ;
140
163
}
0 commit comments