@@ -5,6 +5,7 @@ import TransformerComponent from './TransformerComponent';
5
5
import GrandchildRectangle from './GrandchildRectangle' ;
6
6
import { ComponentsInt , ChildInt } from '../utils/Interfaces' ;
7
7
import { ComponentInt } from '../utils/Interfaces' ;
8
+ import KonvaStage from './KonvaStage' ;
8
9
9
10
interface PropsInt {
10
11
x : number ;
@@ -27,11 +28,10 @@ interface PropsInt {
27
28
image : HTMLImageElement ;
28
29
}
29
30
30
-
31
31
class Rectangle extends Component < PropsInt > {
32
-
33
32
group : Konva . Group ;
34
33
rect : Konva . Rect ;
34
+
35
35
//This assigns the color to the Rect based on componentId's color in the state
36
36
getComponentColor ( componentId : number ) {
37
37
const color = this . props . components . find (
@@ -49,7 +49,12 @@ class Rectangle extends Component<PropsInt> {
49
49
}
50
50
51
51
//resize function
52
- handleResize ( componentId : number , childId : number , target : Konva . Group , blockSnapSize : number ) {
52
+ handleResize (
53
+ componentId : number ,
54
+ childId : number ,
55
+ target : Konva . Group ,
56
+ blockSnapSize : number
57
+ ) {
53
58
//find the id of the component where the componentID in the state equals the currently focused component
54
59
//and then find the numberID for that component
55
60
//So, this would be assigning "Container 1", with component ID being whatever the ID for "Container" is
@@ -68,15 +73,15 @@ class Rectangle extends Component<PropsInt> {
68
73
69
74
//The math here is easier than it looks
70
75
//Basically, the height and width is first rounded up to a whole number (behind the whole snapping phenomenon)
71
- //after scaling it by multiplying it by scaleX/Y (height and width are the original height and width, the scale is the CHANGE in w/h),
76
+ //after scaling it by multiplying it by scaleX/Y (height and width are the original height and width, the scale is the CHANGE in w/h),
72
77
//dividing it by the 'snapsize' or grid unit area (which is 10x10 for this entire application) and re-multiplied by
73
78
//that same snapsize. I'm not entirely sure why this had to be divided before the rounding, and re-multiplied,
74
- //since having positions that aren't a whole number doesn't seem to be that big of a deal.
79
+ //since having positions that aren't a whole number doesn't seem to be that big of a deal.
75
80
76
81
//So there's a bit of redundancy in the x and y info. target.x() or y() only log the CHANGE of position of the Rect component.
77
- //Since when you change the width or height of the component you do not change the actual position, the
82
+ //Since when you change the width or height of the component you do not change the actual position, the
78
83
//value for x an y dispatched to the action creator will always be the same as the current x,y position, unless you can somehow
79
- //resize AND reposition at the same time.
84
+ //resize AND reposition at the same time.
80
85
const transformation = {
81
86
width :
82
87
Math . round ( ( target . width ( ) * target . scaleX ( ) ) / blockSnapSize ) *
@@ -90,9 +95,14 @@ class Rectangle extends Component<PropsInt> {
90
95
this . props . handleTransform ( componentId , childId , transformation ) ;
91
96
}
92
97
93
- //mostly the same logic as above, just grabbing the change in position for the focused child and sending it
98
+ //mostly the same logic as above, just grabbing the change in position for the focused child and sending it
94
99
//to the action creator.
95
- handleDrag ( componentId : number , childId : number , target : Konva . Group , blockSnapSize : number ) {
100
+ handleDrag (
101
+ componentId : number ,
102
+ childId : number ,
103
+ target : Konva . Group ,
104
+ blockSnapSize : number
105
+ ) {
96
106
const transformation = {
97
107
x : Math . round ( target . x ( ) / blockSnapSize ) * blockSnapSize ,
98
108
y : Math . round ( target . y ( ) / blockSnapSize ) * blockSnapSize
@@ -134,7 +144,8 @@ class Rectangle extends Component<PropsInt> {
134
144
onDragEnd = { event =>
135
145
this . handleDrag ( componentId , childId , event . target , blockSnapSize )
136
146
}
137
- ref = { node => { //this refference actually isn't doing anything since it isn't within the transformer component
147
+ ref = { node => {
148
+ //this refference actually isn't doing anything since it isn't within the transformer component
138
149
this . group = node ;
139
150
} }
140
151
tabIndex = '0' // required for keypress event to be heard by this.group
@@ -164,18 +175,22 @@ class Rectangle extends Component<PropsInt> {
164
175
onTransformEnd = { event =>
165
176
this . handleResize ( componentId , childId , event . target , blockSnapSize )
166
177
}
167
- strokeWidth = { childType === 'COMP' ? 4 : 2 }
178
+ strokeWidth = { childType === 'COMP' ? 4 : 2 }
168
179
strokeScaleEnabled = { false }
169
180
draggable = { false }
170
181
fill = { null }
171
182
shadowBlur = { childId === - 1 ? 6 : null }
172
- fillPatternImage = { childId === - 1 ? this . props . image : null } //spooky addition, image if uploaded will only be background of App component
173
- fillPatternScaleX = { this . props . image ? width / this . props . image . width : 1 } //here we are making sure the width of the image will stretch of shrink
174
- fillPatternScaleY = { this . props . image ? height / this . props . image . height : 1 } //based on the width or height of the App component
183
+ fillPatternImage = { childId === - 1 ? this . props . image : null } //spooky addition, image if uploaded will only be background of App component
184
+ fillPatternScaleX = {
185
+ this . props . image ? width / this . props . image . width : 1
186
+ } //here we are making sure the width of the image will stretch of shrink
187
+ fillPatternScaleY = {
188
+ this . props . image ? height / this . props . image . height : 1
189
+ } //based on the width or height of the App component
175
190
_useStrictMode
176
191
/>
177
192
< Label >
178
- < Text //this is just the text that goes above each Rect,
193
+ < Text //this is just the text that goes above each Rect,
179
194
fontStyle = { 'bold' }
180
195
fontVariant = { 'small-caps' }
181
196
// pseudochild's label should look different than normal children:
@@ -192,12 +207,12 @@ class Rectangle extends Component<PropsInt> {
192
207
</ Label >
193
208
{ // for all children other than the pseudoChild, find their component's children array and recursively render the children found there
194
209
childId !== - 1 && //inline conditional to check if a child exists
195
- childType === 'COMP' && //inline conditional to see if the child is a component, not an HTML element
210
+ childType === 'COMP' && //inline conditional to see if the child is a component, not an HTML element
196
211
components //map all 'grandchildren' in the child on display to this new component
197
212
. find ( ( comp : ComponentInt ) => comp . title === childComponentName )
198
213
. childrenArray . filter ( ( child : ChildInt ) => child . childId !== - 1 )
199
214
. map ( ( grandchild : ChildInt , i : number ) => (
200
- < GrandchildRectangle
215
+ < GrandchildRectangle
201
216
key = { i }
202
217
components = { components }
203
218
componentId = { componentId }
@@ -207,15 +222,17 @@ class Rectangle extends Component<PropsInt> {
207
222
childComponentId = { grandchild . childComponentId }
208
223
focusChild = { focusChild }
209
224
childId = { childId } // scary addition, grandchildren rects default to childId of "direct" children
210
- width = { //this is the logic used to display the grandchildren with proper scaling based on the parent (technically child) h/w
225
+ width = {
226
+ //this is the logic used to display the grandchildren with proper scaling based on the parent (technically child) h/w
211
227
grandchild . position . width *
212
228
( width / this . getPseudoChild ( ) . position . width )
213
229
}
214
230
height = {
215
231
grandchild . position . height *
216
232
( height / this . getPseudoChild ( ) . position . height )
217
233
}
218
- x = { //similar logic to above
234
+ x = {
235
+ //similar logic to above
219
236
( grandchild . position . x - this . getPseudoChild ( ) . position . x ) *
220
237
( width / this . getPseudoChild ( ) . position . width )
221
238
}
@@ -225,14 +242,16 @@ class Rectangle extends Component<PropsInt> {
225
242
}
226
243
/>
227
244
) ) }
228
- { focusChild && focusChild . childId === childId && draggable && ( //this conditional logic binds the transformer to the focused child, and Draggable is checked to make sure grandchildren can't be selected
229
- < TransformerComponent //This is the component that binds the Rect nodes to the Transformer node so they can be resized.
230
- focusChild = { focusChild }
231
- rectClass = { 'childRect' }
232
- anchorSize = { 8 }
233
- color = { 'grey' }
234
- />
235
- ) }
245
+ { focusChild &&
246
+ focusChild . childId === childId &&
247
+ draggable && ( //this conditional logic binds the transformer to the focused child, and Draggable is checked to make sure grandchildren can't be selected
248
+ < TransformerComponent //This is the component that binds the Rect nodes to the Transformer node so they can be resized.
249
+ focusChild = { focusChild }
250
+ rectClass = { 'childRect' }
251
+ anchorSize = { 8 }
252
+ color = { 'grey' }
253
+ />
254
+ ) }
236
255
</ Group >
237
256
) ;
238
257
}
0 commit comments