@@ -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,8 +28,10 @@ interface PropsInt {
27
28
image : HTMLImageElement ;
28
29
}
29
30
30
-
31
31
class Rectangle extends Component < PropsInt > {
32
+ rect : Konva . Rect ;
33
+ group : Konva . Group ;
34
+
32
35
//This assigns the color to the Rect based on componentId's color in the state
33
36
getComponentColor ( componentId : number ) {
34
37
const color = this . props . components . find (
@@ -46,7 +49,12 @@ class Rectangle extends Component<PropsInt> {
46
49
}
47
50
48
51
//resize function
49
- 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
+ ) {
50
58
//find the id of the component where the componentID in the state equals the currently focused component
51
59
//and then find the numberID for that component
52
60
//So, this would be assigning "Container 1", with component ID being whatever the ID for "Container" is
@@ -65,15 +73,15 @@ class Rectangle extends Component<PropsInt> {
65
73
66
74
//The math here is easier than it looks
67
75
//Basically, the height and width is first rounded up to a whole number (behind the whole snapping phenomenon)
68
- //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),
69
77
//dividing it by the 'snapsize' or grid unit area (which is 10x10 for this entire application) and re-multiplied by
70
78
//that same snapsize. I'm not entirely sure why this had to be divided before the rounding, and re-multiplied,
71
- //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.
72
80
73
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.
74
- //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
75
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
76
- //resize AND reposition at the same time.
84
+ //resize AND reposition at the same time.
77
85
const transformation = {
78
86
width :
79
87
Math . round ( ( target . width ( ) * target . scaleX ( ) ) / blockSnapSize ) *
@@ -87,9 +95,14 @@ class Rectangle extends Component<PropsInt> {
87
95
this . props . handleTransform ( componentId , childId , transformation ) ;
88
96
}
89
97
90
- //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
91
99
//to the action creator.
92
- 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
+ ) {
93
106
const transformation = {
94
107
x : Math . round ( target . x ( ) / blockSnapSize ) * blockSnapSize ,
95
108
y : Math . round ( target . y ( ) / blockSnapSize ) * blockSnapSize
@@ -131,7 +144,8 @@ class Rectangle extends Component<PropsInt> {
131
144
onDragEnd = { event =>
132
145
this . handleDrag ( componentId , childId , event . target , blockSnapSize )
133
146
}
134
- 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
135
149
this . group = node ;
136
150
} }
137
151
tabIndex = '0' // required for keypress event to be heard by this.group
@@ -161,18 +175,22 @@ class Rectangle extends Component<PropsInt> {
161
175
onTransformEnd = { event =>
162
176
this . handleResize ( componentId , childId , event . target , blockSnapSize )
163
177
}
164
- strokeWidth = { childType === 'COMP' ? 4 : 2 }
178
+ strokeWidth = { childType === 'COMP' ? 4 : 2 }
165
179
strokeScaleEnabled = { false }
166
180
draggable = { false }
167
181
fill = { null }
168
182
shadowBlur = { childId === - 1 ? 6 : null }
169
- fillPatternImage = { childId === - 1 ? this . props . image : null } //spooky addition, image if uploaded will only be background of App component
170
- fillPatternScaleX = { this . props . image ? width / this . props . image . width : 1 } //here we are making sure the width of the image will stretch of shrink
171
- 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
172
190
_useStrictMode
173
191
/>
174
192
< Label >
175
- < Text //this is just the text that goes above each Rect,
193
+ < Text //this is just the text that goes above each Rect,
176
194
fontStyle = { 'bold' }
177
195
fontVariant = { 'small-caps' }
178
196
// pseudochild's label should look different than normal children:
@@ -189,12 +207,12 @@ class Rectangle extends Component<PropsInt> {
189
207
</ Label >
190
208
{ // for all children other than the pseudoChild, find their component's children array and recursively render the children found there
191
209
childId !== - 1 && //inline conditional to check if a child exists
192
- 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
193
211
components //map all 'grandchildren' in the child on display to this new component
194
212
. find ( ( comp : ComponentInt ) => comp . title === childComponentName )
195
213
. childrenArray . filter ( ( child : ChildInt ) => child . childId !== - 1 )
196
214
. map ( ( grandchild : ChildInt , i : number ) => (
197
- < GrandchildRectangle
215
+ < GrandchildRectangle
198
216
key = { i }
199
217
components = { components }
200
218
componentId = { componentId }
@@ -204,15 +222,17 @@ class Rectangle extends Component<PropsInt> {
204
222
childComponentId = { grandchild . childComponentId }
205
223
focusChild = { focusChild }
206
224
childId = { childId } // scary addition, grandchildren rects default to childId of "direct" children
207
- 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
208
227
grandchild . position . width *
209
228
( width / this . getPseudoChild ( ) . position . width )
210
229
}
211
230
height = {
212
231
grandchild . position . height *
213
232
( height / this . getPseudoChild ( ) . position . height )
214
233
}
215
- x = { //similar logic to above
234
+ x = {
235
+ //similar logic to above
216
236
( grandchild . position . x - this . getPseudoChild ( ) . position . x ) *
217
237
( width / this . getPseudoChild ( ) . position . width )
218
238
}
@@ -222,14 +242,16 @@ class Rectangle extends Component<PropsInt> {
222
242
}
223
243
/>
224
244
) ) }
225
- { 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
226
- < TransformerComponent //This is the component that binds the Rect nodes to the Transformer node so they can be resized.
227
- focusChild = { focusChild }
228
- rectClass = { 'childRect' }
229
- anchorSize = { 8 }
230
- color = { 'grey' }
231
- />
232
- ) }
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
+ ) }
233
255
</ Group >
234
256
) ;
235
257
}
0 commit comments