1
- import React , { Component , createRef , Fragment } from " react" ;
1
+ import React , { Component , createRef , Fragment } from ' react' ;
2
2
// import PropTypes from 'prop-types';
3
3
import {
4
- Stage ,
5
- Layer ,
6
- Line ,
7
- Group ,
8
- Label ,
9
- Text ,
10
- Rect ,
11
- Transformer
12
- } from "react-konva" ;
13
- import Rectangle from "./Rectangle.jsx" ;
4
+ Stage , Layer , Line , Group , Label , Text , Rect , Transformer ,
5
+ } from 'react-konva' ;
6
+ import Rectangle from './Rectangle.jsx' ;
14
7
15
8
class KonvaStage extends Component {
16
9
constructor ( props ) {
@@ -20,7 +13,7 @@ class KonvaStage extends Component {
20
13
stageHeight : 1000 ,
21
14
blockSnapSize : 5 ,
22
15
grid : [ ] ,
23
- gridStroke : 1
16
+ gridStroke : 1 ,
24
17
} ;
25
18
}
26
19
@@ -29,120 +22,112 @@ class KonvaStage extends Component {
29
22
// here we should add listener for "container" resize
30
23
// take a look here https://developers.google.com/web/updates/2016/10/resizeobserver
31
24
// for simplicity I will just listen window resize
32
- window . addEventListener ( " resize" , this . checkSize ) ;
25
+ window . addEventListener ( ' resize' , this . checkSize ) ;
33
26
this . createGrid ( ) ;
34
27
}
35
28
36
29
componentWillUnmount ( ) {
37
- window . removeEventListener ( " resize" , this . checkSize ) ;
30
+ window . removeEventListener ( ' resize' , this . checkSize ) ;
38
31
}
39
32
40
33
checkSize = ( ) => {
41
34
const width = this . container . offsetWidth ;
42
35
const height = this . container . offsetHeight ;
43
36
this . setState ( {
44
37
stageWidth : width ,
45
- stageHeight : height
38
+ stageHeight : height ,
46
39
} ) ;
47
40
} ;
48
41
49
- handleStageMouseDown = e => {
42
+ handleStageMouseDown = ( e ) => {
50
43
// // clicked on stage - clear selection
51
44
if ( e . target === e . target . getStage ( ) ) {
52
45
// add functionality for allowing no focusChild
53
- console . log ( " user clicked on canvas:" ) ;
46
+ console . log ( ' user clicked on canvas:' ) ;
54
47
return ;
55
48
}
56
49
// // clicked on transformer - do nothing
57
- const clickedOnTransformer =
58
- e . target . getParent ( ) . className === "Transformer" ;
50
+ const clickedOnTransformer = e . target . getParent ( ) . className === 'Transformer' ;
59
51
if ( clickedOnTransformer ) {
60
- console . log ( " user clicked on transformer" ) ;
61
- console . log ( " HELLOOOO" , e . target . getParent ( ) . className ) ;
52
+ console . log ( ' user clicked on transformer' ) ;
53
+ console . log ( ' HELLOOOO' , e . target . getParent ( ) . className ) ;
62
54
return ;
63
55
}
64
56
65
57
// find clicked rect by its name
66
58
const rectChildId = e . target . attrs . childId ;
67
- console . log ( " user clicked on child rectangle with Id: " , rectChildId ) ;
59
+ console . log ( ' user clicked on child rectangle with childId: ' , rectChildId ) ;
68
60
this . props . changeFocusChild ( { childId : rectChildId } ) ;
69
61
this . props . changeComponentFocusChild ( {
70
62
componentId : this . props . focusComponent . id ,
71
- childId : rectChildId
63
+ childId : rectChildId ,
72
64
} ) ;
73
65
} ;
74
66
75
67
createGrid = ( ) => {
76
- let output = [ ] ;
68
+ const output = [ ] ;
77
69
for ( let i = 0 ; i < this . state . stageWidth / this . state . blockSnapSize ; i ++ ) {
78
70
output . push (
79
71
< Line
80
72
points = { [
81
73
Math . round ( i * this . state . blockSnapSize ) + 0.5 ,
82
74
0 ,
83
75
Math . round ( i * this . state . blockSnapSize ) + 0.5 ,
84
- this . state . stageHeight
76
+ this . state . stageHeight ,
85
77
] }
86
- stroke = { " #ddd" }
78
+ stroke = { ' #ddd' }
87
79
strokeWidth = { this . state . gridStroke }
88
- key = { i + " vertical" }
89
- />
80
+ key = { ` ${ i } vertical` }
81
+ /> ,
90
82
) ;
91
83
}
92
- for (
93
- let j = 0 ;
94
- j < this . state . stageHeight / this . state . blockSnapSize ;
95
- j ++
96
- ) {
84
+ for ( let j = 0 ; j < this . state . stageHeight / this . state . blockSnapSize ; j ++ ) {
97
85
output . push (
98
86
< Line
99
87
points = { [
100
88
0 ,
101
89
Math . round ( j * this . state . blockSnapSize ) ,
102
90
this . state . stageWidth ,
103
- Math . round ( j * this . state . blockSnapSize )
91
+ Math . round ( j * this . state . blockSnapSize ) ,
104
92
] }
105
- stroke = { " #ddd" }
93
+ stroke = { ' #ddd' }
106
94
strokeWidth = { this . state . gridStroke }
107
- key = { j + " horizontal" }
108
- />
95
+ key = { ` ${ j } horizontal` }
96
+ /> ,
109
97
) ;
110
98
}
111
- console . log ( " calling function to render grid" ) ;
99
+ console . log ( ' calling function to render grid' ) ;
112
100
this . setState ( {
113
- grid : output
101
+ grid : output ,
114
102
} ) ;
115
103
} ;
116
104
117
105
render ( ) {
118
106
const {
119
- components,
120
- handleTransform,
121
- focusComponent,
122
- focusChild
107
+ components, handleTransform, focusComponent, focusChild,
123
108
} = this . props ;
124
109
125
110
return (
126
111
< div
127
112
style = { {
128
- width : " 100%" ,
129
- height : " 100%"
113
+ width : ' 100%' ,
114
+ height : ' 100%' ,
130
115
} }
131
- ref = { node => {
116
+ ref = { ( node ) => {
132
117
this . container = node ;
133
118
} }
134
119
>
135
120
< Stage
136
- className = { " canvasStage" }
137
- ref = { node => {
121
+ className = { ' canvasStage' }
122
+ ref = { ( node ) => {
138
123
this . stage = node ;
139
124
} }
140
125
onMouseDown = { this . handleStageMouseDown }
141
126
width = { this . state . stageWidth }
142
127
height = { this . state . stageHeight }
143
128
>
144
129
< Layer
145
- ref = { node => {
130
+ ref = { ( node ) => {
146
131
this . layer = node ;
147
132
} }
148
133
>
@@ -171,10 +156,8 @@ class KonvaStage extends Component {
171
156
/>
172
157
) )
173
158
. sort (
174
- ( rectA , rectB ) =>
175
- rectA . props . width * rectA . props . height <
176
- rectB . props . width * rectB . props . height
177
- )
159
+ ( rectA , rectB ) => rectA . props . width * rectA . props . height < rectB . props . width * rectB . props . height ,
160
+ ) // shouldnt this be subtraction instead of < ? see MDN
178
161
// reasoning for the sort:
179
162
// Konva determines zIndex (which rect is clicked on if rects overlap) based on rendering order
180
163
// as long as the smallest components are rendered last they will always be accessible over the big boys
0 commit comments