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