Skip to content

Commit ec7dcbe

Browse files
committed
Finished commenting Reactangle, GrandChild, most of Konva
1 parent 6772582 commit ec7dcbe

File tree

5 files changed

+59
-22
lines changed

5 files changed

+59
-22
lines changed

src/components/GrandchildRectangle.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import React, { Component } from "react";
22
import { Rect, Group } from "react-konva";
33
import { ComponentsInt, ComponentInt, ChildInt } from "../utils/Interfaces.ts";
44

5+
//////////////////////////////////////////////////////////////////////////////
6+
/////Logic in this component is mainly the same as Rectangle.tsx./////////////
7+
/////Not going to bother commenting too much in here for this reason./////////
8+
//////////////////////////////////////////////////////////////////////////////
9+
510
interface PropsInt {
611
x: number;
712
y: number;
@@ -45,6 +50,7 @@ class GrandchildRectangle extends Component<PropsInt, StateInt> {
4550
);
4651
}
4752

53+
//pretty sure this does nothing here because an image source is never passed down this deep...
4854
setImage = (imageSource: string): void => {
4955
if (!imageSource) return;
5056
const image = new window.Image();
@@ -75,7 +81,7 @@ class GrandchildRectangle extends Component<PropsInt, StateInt> {
7581
// the Rect emits changes to child width and height with help from Transformer
7682
return (
7783
<Group
78-
draggable={false}
84+
draggable={false} //this logic is necessary to make sure the user can't click on any grandchildren
7985
x={x}
8086
y={y}
8187
scaleX={scaleX}

src/components/KonvaStage.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class KonvaStage extends Component<PropsInt, StateInt> {
8282
return childrenArrCopy;
8383
}
8484

85+
//currently, only the handlekeydown event listener does anything.
8586
componentDidMount() {
8687
this.checkSize();
8788
// here we should add listener for "container" resize
@@ -92,11 +93,14 @@ class KonvaStage extends Component<PropsInt, StateInt> {
9293
this.createGrid();
9394
}
9495

96+
// I wonder if this lifecycle method is necessary. When I remove it,
97+
//I can't find any noticable changes. Possibly to prevent memory leaks?
9598
componentWillUnmount() {
9699
window.removeEventListener("resize", this.checkSize);
97100
this.container.removeEventListener("keydown", this.handleKeyDown);
98101
}
99102

103+
//something about the logic here isn't working. Will need to check some other time.
100104
checkSize = () => {
101105
const width = this.container.offsetWidth;
102106
const height = this.container.offsetHeight;
@@ -135,7 +139,9 @@ class KonvaStage extends Component<PropsInt, StateInt> {
135139
childId: rectChildId
136140
});
137141
};
138-
142+
//this function creates a grid with those 10x10 squares.
143+
//it first draws a grid or horizaontal lines, and then
144+
//draws the vertical ones.
139145
createGrid = () => {
140146
const output = [];
141147
for (let i = 0; i < this.state.stageWidth / this.state.blockSnapSize; i++) {

src/components/Rectangle.tsx

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,53 @@ interface PropsInt {
2929

3030

3131
class Rectangle extends Component<PropsInt> {
32-
32+
//This assigns the color to the Rect based on componentId's color in the state
3333
getComponentColor(componentId: number) {
3434
const color = this.props.components.find(
3535
(comp: ComponentInt) => comp.id === componentId
3636
).color;
3737
return color;
3838
}
3939

40+
//this grabs the component ID of the child component when in the parent component's display mode
41+
//mysteriously name pseudochild although it is the true child of the component currently rendered on the stage...
42+
//maybe some poeple don't consider children to be real children, kind of how my grandmother treated me better than
43+
//she treated my mother
4044
getPseudoChild() {
4145
return this.props.components.find(
4246
(comp: ComponentInt) => comp.id === this.props.childComponentId
4347
);
4448
}
4549

50+
//resize function
4651
handleResize(componentId: number, childId: number, target: Konva.Group, blockSnapSize: number) {
52+
//find the id of the component where the componentID in the state equals the currently focused component
53+
//and then find the numberID for that component
54+
//So, this would be assigning "Container 1", with component ID being whatever the ID for "Container" is
55+
//and 1 being the child ID
4756
let focChild: ChildInt | ComponentInt = this.props.components
4857
.find((comp: ComponentInt) => comp.id === this.props.componentId)
4958
.childrenArray.find((child: ChildInt) => child.childId === childId);
5059

60+
//If this is the parent component, it would not have a childID (hence -1), so it just would assign the forChild
61+
//variable to the parent.
5162
if (childId === -1) {
5263
focChild = this.props.components.find(
5364
(comp: ComponentInt) => comp.id === this.props.componentId
5465
);
5566
}
67+
68+
//The math here is easier than it looks
69+
//Basically, the height and width is first rounded up to a whole number (behind the whole snapping phenomenon)
70+
//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),
71+
//dividing it by the 'snapsize' or grid unit area (which is 10x10 for this entire application) and re-multiplied by
72+
//that same snapsize. I'm not entirely sure why this had to be divided before the rounding, and re-multiplied,
73+
//since having positions that aren't a whole number doesn't seem to be that big of a deal.
74+
75+
//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.
76+
//Since when you change the width or height of the component you do not change the actual position, the
77+
//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
78+
//resize AND reposition at the same time.
5679
const transformation = {
5780
width:
5881
Math.round((target.width() * target.scaleX()) / blockSnapSize) *
@@ -66,6 +89,8 @@ class Rectangle extends Component<PropsInt> {
6689
this.props.handleTransform(componentId, childId, transformation);
6790
}
6891

92+
//mostly the same logic as above, just grabbing the change in position for the focused child and sending it
93+
//to the action creator.
6994
handleDrag(componentId: number, childId: number, target: Konva.Group, blockSnapSize: number) {
7095
const transformation = {
7196
x: Math.round(target.x() / blockSnapSize) * blockSnapSize,
@@ -108,15 +133,15 @@ class Rectangle extends Component<PropsInt> {
108133
onDragEnd={event =>
109134
this.handleDrag(componentId, childId, event.target, blockSnapSize)
110135
}
111-
ref={node => {
136+
ref={node => { //this refference actually isn't doing anything since it isn't within the transformer component
112137
this.group = node;
113138
}}
114139
tabIndex='0' // required for keypress event to be heard by this.group
115140
>
116-
<Rect
141+
<Rect //basically the entire canvas
117142
// a Konva Rect is generated for each child of the focusComponent (including the pseudochild, representing the focusComponent itself)
118143
ref={node => {
119-
this.rect = node;
144+
this.rect = node; //same as above, the reference isn't assigned or pointing to anything
120145
}}
121146
tabIndex='0' // required for keypress event to be heard by this.group
122147
name={`${childId}`}
@@ -133,45 +158,45 @@ class Rectangle extends Component<PropsInt> {
133158
stroke={
134159
childType === 'COMP'
135160
? this.getComponentColor(childComponentId)
136-
: '#000000'
161+
: '#000000' //sets the parent component color to black, this logic actually doesn't need to exist at the level of this component
137162
}
138163
onTransformEnd={event =>
139164
this.handleResize(componentId, childId, event.target, blockSnapSize)
140165
}
141-
strokeWidth={childType === 'COMP' ? 4 : 2}
166+
strokeWidth={childType === 'COMP' ? 4 : 2}
142167
strokeScaleEnabled={false}
143168
draggable={false}
144169
fill={null}
145170
shadowBlur={childId === -1 ? 6 : null}
146171
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}
172+
fillPatternScaleX={this.props.image ? width / this.props.image.width : 1} //here we are making sure the width of the image will stretch of shrink
173+
fillPatternScaleY={this.props.image? height / this.props.image.height : 1} //based on the width or height of the App component
149174
_useStrictMode
150175
/>
151176
<Label>
152-
<Text
177+
<Text //this is just the text that goes above each Rect,
153178
fontStyle={'bold'}
154179
fontVariant={'small-caps'}
155180
// pseudochild's label should look different than normal children:
156-
text={childId === -1 ? title.slice(0, title.length - 2) : title}
181+
text={childId === -1 ? title.slice(0, title.length - 2) : title} //slices the number off of the title of the top component
157182
fill={
158183
childId === -1
159184
? this.getComponentColor(childComponentId)
160-
: '#000000'
185+
: '#000000' //opposite logic of the stroke
161186
}
162187
fontSize={childId === -1 ? 15 : 10}
163188
x={4}
164189
y={childId === -1 ? -20 : -12}
165190
/>
166191
</Label>
167192
{// 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
193+
childId !== -1 && //inline conditional to check if a child exists
194+
childType === 'COMP' && //inline conditional to see if the child is a component, not an HTML element
195+
components //map all 'grandchildren' in the child on display to this new component
171196
.find((comp: ComponentInt) => comp.title === childComponentName)
172197
.childrenArray.filter((child: ChildInt) => child.childId !== -1)
173198
.map((grandchild: ChildInt, i: number) => (
174-
<GrandchildRectangle
199+
<GrandchildRectangle
175200
key={i}
176201
components={components}
177202
componentId={componentId}
@@ -181,15 +206,15 @@ class Rectangle extends Component<PropsInt> {
181206
childComponentId={grandchild.childComponentId}
182207
focusChild={focusChild}
183208
childId={childId} // scary addition, grandchildren rects default to childId of "direct" children
184-
width={
209+
width={ //this is the logic used to display the grandchildren with proper scaling based on the parent (technically child) h/w
185210
grandchild.position.width *
186211
(width / this.getPseudoChild().position.width)
187212
}
188213
height={
189214
grandchild.position.height *
190215
(height / this.getPseudoChild().position.height)
191216
}
192-
x={
217+
x={ //similar logic to above
193218
(grandchild.position.x - this.getPseudoChild().position.x) *
194219
(width / this.getPseudoChild().position.width)
195220
}
@@ -199,7 +224,7 @@ class Rectangle extends Component<PropsInt> {
199224
}
200225
/>
201226
))}
202-
{focusChild && focusChild.childId === childId && draggable && (
227+
{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
203228
<TransformerComponent //This is the component that binds the Rect nodes to the Transformer node so they can be resized.
204229
focusChild={focusChild}
205230
rectClass={'childRect'}

src/containers/MainContainer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface PropsInt {
2525
// addChild: any; **It's expecting this prop in the interface, but is never used.**
2626
// changeFocusComponent: any; **It's expecting this prop in the interface, but is never used.**
2727
changeFocusChild: any;
28-
changeImagePath: any;
28+
// changeImagePath: any; **It's declared but function below doesn't do anything**
2929
// deleteComponent: any; **It's expecting this prop in the interface, but is never used.**
3030
// createApp: any; **It's expecting this prop in the interface, but is never used.**
3131
// deleteAllData: any; **It's expecting this prop in the interface, but is never used.**

src/utils/componentRender.util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ const componentRender = (
175175
${cloneDeep(childrenArray)
176176
.sort((a: ChildInt, b: ChildInt) => a.childSort - b.childSort)
177177
.map((child: ChildInt) => {
178-
console.log('this is childrenArray', child.HTMLInfo);
178+
//console.log('this is childrenArray', child.HTMLInfo); //Did someone leave this here?//
179179
if (child.componentName == 'Button') {
180180
return `
181181
<${componentNameGenerator(child)} ${propDrillTextGenerator(

0 commit comments

Comments
 (0)