@@ -29,30 +29,51 @@ interface PropsInt {
29
29
30
30
31
31
class Rectangle extends Component < PropsInt > {
32
-
32
+ //This assigns the color to the Rect based on componentId's color in the state
33
33
getComponentColor ( componentId : number ) {
34
34
const color = this . props . components . find (
35
35
( comp : ComponentInt ) => comp . id === componentId
36
36
) . color ;
37
37
return color ;
38
38
}
39
39
40
+ //this grabs the component ID of the child component when in the parent component's display mode
41
+ //named pseudochild because it isn't saved as a child in the childrenArray.
40
42
getPseudoChild ( ) {
41
43
return this . props . components . find (
42
44
( comp : ComponentInt ) => comp . id === this . props . childComponentId
43
45
) ;
44
46
}
45
47
48
+ //resize function
46
49
handleResize ( componentId : number , childId : number , target : Konva . Group , blockSnapSize : number ) {
50
+ //find the id of the component where the componentID in the state equals the currently focused component
51
+ //and then find the numberID for that component
52
+ //So, this would be assigning "Container 1", with component ID being whatever the ID for "Container" is
53
+ //and 1 being the child ID
47
54
let focChild : ChildInt | ComponentInt = this . props . components
48
55
. find ( ( comp : ComponentInt ) => comp . id === this . props . componentId )
49
56
. childrenArray . find ( ( child : ChildInt ) => child . childId === childId ) ;
50
57
58
+ //If this is the parent component, it would not have a childID (hence -1), so it just would assign the forChild
59
+ //variable to the parent.
51
60
if ( childId === - 1 ) {
52
61
focChild = this . props . components . find (
53
62
( comp : ComponentInt ) => comp . id === this . props . componentId
54
63
) ;
55
64
}
65
+
66
+ //The math here is easier than it looks
67
+ //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),
69
+ //dividing it by the 'snapsize' or grid unit area (which is 10x10 for this entire application) and re-multiplied by
70
+ //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.
72
+
73
+ //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
75
+ //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.
56
77
const transformation = {
57
78
width :
58
79
Math . round ( ( target . width ( ) * target . scaleX ( ) ) / blockSnapSize ) *
@@ -66,6 +87,8 @@ class Rectangle extends Component<PropsInt> {
66
87
this . props . handleTransform ( componentId , childId , transformation ) ;
67
88
}
68
89
90
+ //mostly the same logic as above, just grabbing the change in position for the focused child and sending it
91
+ //to the action creator.
69
92
handleDrag ( componentId : number , childId : number , target : Konva . Group , blockSnapSize : number ) {
70
93
const transformation = {
71
94
x : Math . round ( target . x ( ) / blockSnapSize ) * blockSnapSize ,
@@ -108,15 +131,15 @@ class Rectangle extends Component<PropsInt> {
108
131
onDragEnd = { event =>
109
132
this . handleDrag ( componentId , childId , event . target , blockSnapSize )
110
133
}
111
- ref = { node => {
134
+ ref = { node => { //this refference actually isn't doing anything since it isn't within the transformer component
112
135
this . group = node ;
113
136
} }
114
137
tabIndex = '0' // required for keypress event to be heard by this.group
115
138
>
116
- < Rect
139
+ < Rect //basically the entire canvas
117
140
// a Konva Rect is generated for each child of the focusComponent (including the pseudochild, representing the focusComponent itself)
118
141
ref = { node => {
119
- this . rect = node ;
142
+ this . rect = node ; //same as above, the reference isn't assigned or pointing to anything
120
143
} }
121
144
tabIndex = '0' // required for keypress event to be heard by this.group
122
145
name = { `${ childId } ` }
@@ -133,45 +156,45 @@ class Rectangle extends Component<PropsInt> {
133
156
stroke = {
134
157
childType === 'COMP'
135
158
? this . getComponentColor ( childComponentId )
136
- : '#000000'
159
+ : '#000000' //sets the parent component color to black
137
160
}
138
161
onTransformEnd = { event =>
139
162
this . handleResize ( componentId , childId , event . target , blockSnapSize )
140
163
}
141
- strokeWidth = { childType === 'COMP' ? 4 : 2 }
164
+ strokeWidth = { childType === 'COMP' ? 4 : 2 }
142
165
strokeScaleEnabled = { false }
143
166
draggable = { false }
144
167
fill = { null }
145
168
shadowBlur = { childId === - 1 ? 6 : null }
146
169
fillPatternImage = { childId === - 1 ? this . props . image : null } //spooky addition, image if uploaded will only be background of App component
147
- fillPatternScaleX = { this . props . image ? width / this . props . image . width : 1 }
148
- fillPatternScaleY = { this . props . image ? height / this . props . image . height : 1 }
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
149
172
_useStrictMode
150
173
/>
151
174
< Label >
152
- < Text
175
+ < Text //this is just the text that goes above each Rect,
153
176
fontStyle = { 'bold' }
154
177
fontVariant = { 'small-caps' }
155
178
// pseudochild's label should look different than normal children:
156
- text = { childId === - 1 ? title . slice ( 0 , title . length - 2 ) : title }
179
+ text = { childId === - 1 ? title . slice ( 0 , title . length - 2 ) : title } //slices the number off of the title of the top component
157
180
fill = {
158
181
childId === - 1
159
182
? this . getComponentColor ( childComponentId )
160
- : '#000000'
183
+ : '#000000' //opposite logic of the stroke
161
184
}
162
185
fontSize = { childId === - 1 ? 15 : 10 }
163
186
x = { 4 }
164
187
y = { childId === - 1 ? - 20 : - 12 }
165
188
/>
166
189
</ Label >
167
190
{ // for all children other than the pseudoChild, find their component's children array and recursively render the children found there
168
- childId !== - 1 &&
169
- childType === 'COMP' &&
170
- components
191
+ 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
193
+ components //map all 'grandchildren' in the child on display to this new component
171
194
. find ( ( comp : ComponentInt ) => comp . title === childComponentName )
172
195
. childrenArray . filter ( ( child : ChildInt ) => child . childId !== - 1 )
173
196
. map ( ( grandchild : ChildInt , i : number ) => (
174
- < GrandchildRectangle
197
+ < GrandchildRectangle
175
198
key = { i }
176
199
components = { components }
177
200
componentId = { componentId }
@@ -181,15 +204,15 @@ class Rectangle extends Component<PropsInt> {
181
204
childComponentId = { grandchild . childComponentId }
182
205
focusChild = { focusChild }
183
206
childId = { childId } // scary addition, grandchildren rects default to childId of "direct" children
184
- width = {
207
+ width = { //this is the logic used to display the grandchildren with proper scaling based on the parent (technically child) h/w
185
208
grandchild . position . width *
186
209
( width / this . getPseudoChild ( ) . position . width )
187
210
}
188
211
height = {
189
212
grandchild . position . height *
190
213
( height / this . getPseudoChild ( ) . position . height )
191
214
}
192
- x = {
215
+ x = { //similar logic to above
193
216
( grandchild . position . x - this . getPseudoChild ( ) . position . x ) *
194
217
( width / this . getPseudoChild ( ) . position . width )
195
218
}
@@ -199,7 +222,7 @@ class Rectangle extends Component<PropsInt> {
199
222
}
200
223
/>
201
224
) ) }
202
- { focusChild && focusChild . childId === childId && draggable && (
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
203
226
< TransformerComponent //This is the component that binds the Rect nodes to the Transformer node so they can be resized.
204
227
focusChild = { focusChild }
205
228
rectClass = { 'childRect' }
0 commit comments