1
- import React , { Component , createRef , Fragment } from " react" ;
2
- import Button from " @material-ui/core/Button" ;
1
+ import React , { Component , createRef , Fragment } from ' react' ;
2
+ import Button from ' @material-ui/core/Button' ;
3
3
4
4
// import PropTypes from 'prop-types';
5
5
import {
6
- Stage ,
7
- Layer ,
8
- Line ,
9
- Group ,
10
- Label ,
11
- Text ,
12
- Rect ,
13
- Transformer
14
- } from "react-konva" ;
15
- import Rectangle from "./Rectangle.jsx" ;
6
+ Stage , Layer , Line , Group , Label , Text , Rect , Transformer ,
7
+ } from 'react-konva' ;
8
+ import Rectangle from './Rectangle.jsx' ;
16
9
17
10
class KonvaStage extends Component {
18
11
constructor ( props ) {
@@ -22,7 +15,7 @@ class KonvaStage extends Component {
22
15
stageHeight : 1000 ,
23
16
blockSnapSize : 7 ,
24
17
grid : [ ] ,
25
- gridStroke : 1
18
+ gridStroke : 1 ,
26
19
} ;
27
20
}
28
21
@@ -31,36 +24,31 @@ class KonvaStage extends Component {
31
24
// here we should add listener for "container" resize
32
25
// take a look here https://developers.google.com/web/updates/2016/10/resizeobserver
33
26
// for simplicity I will just listen window resize
34
- window . addEventListener ( " resize" , this . checkSize ) ;
27
+ window . addEventListener ( ' resize' , this . checkSize ) ;
35
28
this . createGrid ( ) ;
36
29
}
37
30
38
31
getDirectChildrenCopy ( focusComponent ) {
39
- const component = this . props . components . find (
40
- comp => comp . id === focusComponent . id
41
- ) ;
32
+ const component = this . props . components . find ( comp => comp . id === focusComponent . id ) ;
42
33
43
- const childrenArr = component . childrenArray . filter (
44
- child => child . childId !== "-1"
45
- ) ;
34
+ const childrenArr = component . childrenArray . filter ( child => child . childId !== '-1' ) ;
46
35
47
36
let childrenArrCopy = this . cloneDeep ( childrenArr ) ;
48
37
49
38
const pseudoChild = {
50
- childId : "-1" ,
39
+ childId : '-1' ,
51
40
childComponentId : component . id ,
52
41
componentName : component . title ,
53
42
position : {
54
43
x : component . position . x ,
55
44
y : component . position . y ,
56
45
width : component . position . width ,
57
- height : component . position . height
46
+ height : component . position . height ,
58
47
} ,
59
48
draggable : true ,
60
- color : component . color
49
+ color : component . color ,
61
50
} ;
62
- // console.log('getDirectChildrenCopy, pseudoChild.position: ', pseudoChild.position);
63
- childrenArrCopy = childrenArrCopy . concat ( pseudoChild ) ;
51
+ childrenArrCopy = childrenArrCopy . concat ( pseudoChild ) ; // could just use push here, concat needlessly generate new array
64
52
return childrenArrCopy ;
65
53
}
66
54
@@ -69,19 +57,19 @@ class KonvaStage extends Component {
69
57
70
58
if ( Array . isArray ( value ) ) {
71
59
result = [ ] ;
72
- value . forEach ( elm => {
73
- if ( typeof elm === " object" ) {
60
+ value . forEach ( ( elm ) => {
61
+ if ( typeof elm === ' object' ) {
74
62
result . push ( this . cloneDeep ( elm ) ) ;
75
63
} else {
76
64
result . push ( elm ) ;
77
65
}
78
66
} ) ;
79
67
return result ;
80
68
}
81
- if ( typeof value === " object" && value !== null ) {
69
+ if ( typeof value === ' object' && value !== null ) {
82
70
result = { } ;
83
- Object . keys ( value ) . forEach ( key => {
84
- if ( typeof value [ key ] === " object" ) {
71
+ Object . keys ( value ) . forEach ( ( key ) => {
72
+ if ( typeof value [ key ] === ' object' ) {
85
73
result [ key ] = this . cloneDeep ( value [ key ] ) ;
86
74
} else {
87
75
result [ key ] = value [ key ] ;
@@ -93,40 +81,39 @@ class KonvaStage extends Component {
93
81
}
94
82
95
83
componentWillUnmount ( ) {
96
- window . removeEventListener ( " resize" , this . checkSize ) ;
84
+ window . removeEventListener ( ' resize' , this . checkSize ) ;
97
85
}
98
86
99
87
checkSize = ( ) => {
100
88
const width = this . container . offsetWidth ;
101
89
const height = this . container . offsetHeight ;
102
90
this . setState ( {
103
91
stageWidth : width ,
104
- stageHeight : height
92
+ stageHeight : height ,
105
93
} ) ;
106
94
} ;
107
95
108
- handleStageMouseDown = e => {
96
+ handleStageMouseDown = ( e ) => {
109
97
// // clicked on stage - clear selection
110
98
if ( e . target === e . target . getStage ( ) ) {
111
99
// add functionality for allowing no focusChild
112
- console . log ( " user clicked on canvas:" ) ;
100
+ console . log ( ' user clicked on canvas:' ) ;
113
101
return ;
114
102
}
115
103
// // clicked on transformer - do nothing
116
- const clickedOnTransformer =
117
- e . target . getParent ( ) . className === "Transformer" ;
104
+ const clickedOnTransformer = e . target . getParent ( ) . className === 'Transformer' ;
118
105
if ( clickedOnTransformer ) {
119
- console . log ( " user clicked on transformer" ) ;
106
+ console . log ( ' user clicked on transformer' ) ;
120
107
return ;
121
108
}
122
109
123
110
// find clicked rect by its name
124
111
const rectChildId = e . target . attrs . childId ;
125
- console . log ( " user clicked on child rectangle with childId: " , rectChildId ) ;
112
+ console . log ( ' user clicked on child rectangle with childId: ' , rectChildId ) ;
126
113
this . props . changeFocusChild ( { childId : rectChildId } ) ;
127
114
this . props . changeComponentFocusChild ( {
128
115
componentId : this . props . focusComponent . id ,
129
- childId : rectChildId
116
+ childId : rectChildId ,
130
117
} ) ;
131
118
} ;
132
119
@@ -139,81 +126,73 @@ class KonvaStage extends Component {
139
126
Math . round ( i * this . state . blockSnapSize ) + 0.5 ,
140
127
0 ,
141
128
Math . round ( i * this . state . blockSnapSize ) + 0.5 ,
142
- this . state . stageHeight
129
+ this . state . stageHeight ,
143
130
] }
144
- stroke = { " #ddd" }
131
+ stroke = { ' #ddd' }
145
132
strokeWidth = { this . state . gridStroke }
146
133
key = { `${ i } vertical` }
147
- />
134
+ /> ,
148
135
) ;
149
136
}
150
- for (
151
- let j = 0 ;
152
- j < this . state . stageHeight / this . state . blockSnapSize ;
153
- j ++
154
- ) {
137
+ for ( let j = 0 ; j < this . state . stageHeight / this . state . blockSnapSize ; j ++ ) {
155
138
output . push (
156
139
< Line
157
140
points = { [
158
141
0 ,
159
142
Math . round ( j * this . state . blockSnapSize ) ,
160
143
this . state . stageWidth ,
161
- Math . round ( j * this . state . blockSnapSize )
144
+ Math . round ( j * this . state . blockSnapSize ) ,
162
145
] }
163
- stroke = { " #ddd" }
146
+ stroke = { ' #ddd' }
164
147
strokeWidth = { this . state . gridStroke }
165
148
key = { `${ j } horizontal` }
166
- />
149
+ /> ,
167
150
) ;
168
151
}
169
- console . log ( " calling function to render grid" ) ;
152
+ console . log ( ' calling function to render grid' ) ;
170
153
this . setState ( {
171
- grid : output
154
+ grid : output ,
172
155
} ) ;
173
156
} ;
174
157
175
158
render ( ) {
176
159
const {
177
- components,
178
- handleTransform,
179
- focusComponent,
180
- focusChild,
181
- deleteChild
160
+ components, handleTransform, focusComponent, focusChild, deleteChild,
182
161
} = this . props ;
183
162
184
163
return (
185
164
< div
186
165
style = { {
187
- width : " 100%" ,
188
- height : " 100%"
166
+ width : ' 100%' ,
167
+ height : ' 100%' ,
189
168
} }
190
- ref = { node => {
169
+ ref = { ( node ) => {
191
170
this . container = node ;
192
171
} }
193
172
>
194
173
< Button
195
174
onClick = { deleteChild }
196
175
style = { {
197
- width : " 150px" ,
198
- position : " relative" ,
199
- float : " right" ,
200
- background : " #dbdbdb" ,
201
- zIndex : 2
176
+ width : ' 150px' ,
177
+ position : ' relative' ,
178
+ float : ' right' ,
179
+ background : ' #dbdbdb' ,
180
+ zIndex : 2 ,
202
181
} }
203
182
>
204
183
delete child
205
184
</ Button >
206
185
< Stage
207
- className = { " canvasStage" }
208
- ref = { node => {
186
+ className = { ' canvasStage' }
187
+ ref = { ( node ) => {
209
188
this . stage = node ;
210
189
} }
211
190
onMouseDown = { this . handleStageMouseDown }
212
191
width = { this . state . stageWidth }
213
192
height = { this . state . stageHeight }
214
193
>
215
194
< Layer
216
- ref = { node => {
195
+ ref = { ( node ) => {
217
196
this . layer = node ;
218
197
} }
219
198
>
@@ -242,9 +221,7 @@ class KonvaStage extends Component {
242
221
/>
243
222
) )
244
223
. sort (
245
- ( rectA , rectB ) =>
246
- rectA . props . width * rectA . props . height <
247
- rectB . props . width * rectB . props . height
224
+ ( rectA , rectB ) => rectA . props . width * rectA . props . height < rectB . props . width * rectB . props . height ,
248
225
) // shouldnt this be subtraction instead of < ? see MDN
249
226
// reasoning for the sort:
250
227
// Konva determines zIndex (which rect is clicked on if rects overlap) based on rendering order
0 commit comments