Skip to content

Commit b44a267

Browse files
committed
Added comments from previous contributor
1 parent 87ce3ca commit b44a267

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

src/components/GrandchildRectangle.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { Rect, Group } from 'react-konva';
44
import { ComponentsInt, ComponentInt, ChildInt } from '../utils/interfaces';
55

66
// ** this file might restrict you from making the child of a component one of its references - prevents circular references
7-
7+
// Component does enable nesting of arbitrary numbers of child components, but it does NOT prevent circular references
8+
// Only check for that is in LeftColExpansionPanel on or around line 138
89
interface PropsInt {
910
x: number;
1011
y: number;

src/components/KonvaStage.tsx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Rectangle from './Rectangle.tsx';
44
import { cloneDeep } from '../utils/index.util';
55
import { ComponentInt, ComponentsInt, ChildInt } from '../utils/interfaces.ts';
66

7+
//Props are intended to totally describe configuration of a single Rectangle
78
interface PropsInt {
89
components: ComponentsInt;
910
focusComponent: ComponentInt;
@@ -43,14 +44,20 @@ class KonvaStage extends Component<PropsInt, StateInt> {
4344
}
4445

4546
getDirectChildrenCopy(focusComponent: ComponentInt) {
47+
//Finds the component currently selected by the user
4648
const component = this.props.components.find(
4749
(comp: ComponentInt) => comp.id === focusComponent.id,
4850
);
49-
51+
// Removes the pseudoChild from the array
5052
const childrenArr = component.children.filter((child: ChildInt) => child.childId !== -1);
51-
53+
54+
//childrenArr is a different array than component children
55+
//However, it may have nested references (to original component.children array)
56+
//Therefore, a deep copy is necessary to ensure that state is not accidentally mutated
5257
let childrenArrCopy = cloneDeep(childrenArr);
53-
58+
59+
//pseudoChild is a convenience object; other than its childID, it is a copy of the parent
60+
//Not intended to be rendered
5461
const pseudoChild = {
5562
childId: -1,
5663
childComponentId: component.id,
@@ -99,7 +106,9 @@ class KonvaStage extends Component<PropsInt, StateInt> {
99106
this.props.deleteChild({});
100107
}
101108
};
102-
109+
//Handles a user click event on the Konva Stage (see line 199)
110+
//Changes the focusChild of the selected component
111+
//The focusChild's props may be changed in the right tab
103112
handleStageMouseDown = (e: any) => {
104113
// clicked on stage - clear selection
105114
if (e.target === e.target.getStage()) {
@@ -111,7 +120,7 @@ class KonvaStage extends Component<PropsInt, StateInt> {
111120
return;
112121
}
113122

114-
// find clicked rect by its name
123+
// find clicked rect by its childId
115124
const rectChildId = e.target.attrs.childId;
116125
// console.log("user clicked on child rectangle with childId: ", rectChildId);
117126
this.props.changeFocusChild({ childId: rectChildId });
@@ -121,6 +130,9 @@ class KonvaStage extends Component<PropsInt, StateInt> {
121130
});
122131
};
123132

133+
//Generates an array of Konva Line components (vertical and horizontal) spaced by blockSnapSize pixels
134+
//Rectangle components are aligned to this grid
135+
//blockSnapSize is used elsewhere to snap same to nearest grid line
124136
createGrid = () => {
125137
const output = [];
126138
for (let i = 0; i < this.state.stageWidth / this.state.blockSnapSize; i++) {
@@ -195,6 +207,9 @@ class KonvaStage extends Component<PropsInt, StateInt> {
195207
}}
196208
>
197209
{this.state.grid}
210+
{// Given the current focusComponent (selected by clicking in LeftContainer),
211+
// the below code maps over its children and renders a Rectangle component for each
212+
}
198213
{this.getDirectChildrenCopy(focusComponent)
199214
.map((child: ChildInt, i: number) => (
200215
<Rectangle

src/utils/componentRender.util.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ const componentNameGenerator = (child: ChildState) => {
7979
}
8080
}
8181

82+
//Given a component's state (obtained via iteration through state object)
83+
//This generates a React component file using template literals
84+
//(Including import statements, component props, and a render function)
85+
//The other functions in this file are subsidiary to the below
8286
const componentRender = (component: ComponentState, components: ComponentState[]) => {
8387
const { children, title, props } : {
8488
children: ChildState[];

src/utils/reducer.util.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ export const closeExpanded = (components: ComponentState[], id? : number): void
102102
});
103103
}
104104

105+
// This action adds a child to the current focusComponent
106+
// (The component currently selected in LeftContainer)
105107
export const addChild = (
106108
state: ApplicationState,
107109
{ title, childType = '', HTMLInfo = {} }: { title: string; childType: string; HTMLInfo: object },
@@ -117,8 +119,8 @@ export const addChild = (
117119
childType = 'HTML';
118120
}
119121

120-
// view represents the curretn FOCUSED COMPONENT - this is the component where the child is being added to
121-
// we only add childrent (or do any action) to the focused omconent
122+
// view represents the current FOCUSED COMPONENT - this is the component where the child is being added to
123+
// we only add children (perform any action) to the focused component
122124
const view: ComponentState = state.components.find(comp => comp.title === state.focusComponent.title);
123125

124126
// parentComponent is the component this child is generated from (ex. instance of Box has comp of Box)
@@ -137,21 +139,22 @@ export const addChild = (
137139
let htmlElemPosition: htmlElemPositionInt = { width: null, height: null };
138140
if (childType === 'HTML') {
139141
htmlElemPosition = getSize(htmlElement);
140-
// if above function doesnt reutn anything, it means html element is not in our database
142+
// if above function doesn't return anything, it means html element is not yet implemented
141143
if (!htmlElemPosition.width) {
142144
console.log(
143145
`Did not add html child: ${htmlElement} the GetSize function indicated that it isnt in our DB`
144146
);
145147
return;
146148
}
147149
}
148-
150+
// The postion of the new child is dependent on its parent, the focusComponent (called view here)
149151
const newPosition =
150152
childType === 'COMP'
151153
? {
154+
//In order to avoid the overlap of newly added children, this adds an x and y offset
152155
x: view.position.x + ((view.nextChildId * 16) % 150), // new children are offset by some amount, map of 150px
153156
y: view.position.y + ((view.nextChildId * 16) % 150),
154-
width: parentComponent.position.width - 1, // new children have an initial position of their CLASS (maybe don't need 90%)
157+
width: parentComponent.position.width - 1, // the size of new children is based on that of parent component
155158
height: parentComponent.position.height - 1
156159
}
157160
: {
@@ -160,15 +163,15 @@ export const addChild = (
160163
width: htmlElemPosition.width,
161164
height: htmlElemPosition.height
162165
};
163-
166+
// Creation of the actual new child object with all data necessary to render a Rectangle
164167
const newChild: ChildState = {
165168
childId: view.nextChildId,
166169
childSort: view.nextChildId,
167170
childType,
168171
childComponentId: childType === 'COMP' ? parentComponent.id : null, // only relevant fot children of type COMPONENT
169172
componentName: strippedTitle,
170173
position: newPosition,
171-
color: null, // parentComponent.color, // only relevant fot children of type COMPONENT
174+
color: null, // parentComponent.color, // only relevant for children of type COMPONENT
172175
htmlElement, // only relevant fot children of type HTML
173176
HTMLInfo
174177
};

0 commit comments

Comments
 (0)