Skip to content

Commit bf8d792

Browse files
authored
Merge pull request #14 from ReacType-2-0/add-comments
Add comments
2 parents 76bb3e8 + c17a8cc commit bf8d792

File tree

4 files changed

+118
-3
lines changed

4 files changed

+118
-3
lines changed

src/components/GrandchildRectangle.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { Rect, Group } from 'react-konva';
44
import { ComponentState, ChildState } from '../types/types';
55

66
// ** this file might restrict you from making the child of a component one of its references - prevents circular references
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
9+
710

811
type Props = {
912
x: number;

src/components/KonvaStage.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ class KonvaStage extends Component<Props, State> {
4949
const childrenArr = component.children.filter((child: ChildState) => child.childId !== -1);
5050

5151
let childrenArrCopy = cloneDeep(childrenArr);
52-
52+
53+
//pseudoChild is a convenience object; other than its childID, it is a copy of the parent
54+
//Not intended to be rendered
5355
const pseudoChild = {
5456
childId: -1,
5557
childComponentId: component.id,
@@ -83,7 +85,9 @@ class KonvaStage extends Component<Props, State> {
8385
deleteComponent(focusComponent.id);
8486
}
8587
};
86-
88+
//Handles a user click event on the Konva Stage (see line 199)
89+
//Changes the focusChild of the selected component
90+
//The focusChild's props may be changed in the right tab
8791
handleStageMouseDown = (e: any) => {
8892
// clicked on stage - clear selection
8993
if (e.target === e.target.getStage()) {
@@ -95,7 +99,7 @@ class KonvaStage extends Component<Props, State> {
9599
return;
96100
}
97101

98-
// find clicked rect by its name
102+
// find clicked rect by its childId
99103
const rectChildId = e.target.attrs.childId;
100104
this.props.changeFocusChild({ childId: rectChildId });
101105
this.props.changeComponentFocusChild({
@@ -104,6 +108,9 @@ class KonvaStage extends Component<Props, State> {
104108
});
105109
};
106110

111+
//Generates an array of Konva Line components (vertical and horizontal) spaced by blockSnapSize pixels
112+
//Rectangle components are aligned to this grid
113+
//blockSnapSize is used elsewhere to snap same to nearest grid line
107114
createGrid = () => {
108115
if (this.state.grid !== []) {
109116
const grid = this.state.grid;

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: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,107 @@ export const closeExpanded = (components: ComponentState[], id? : number): void
107107
});
108108
}
109109

110+
<<<<<<< HEAD
111+
// This action adds a child to the current focusComponent
112+
// (The component currently selected in LeftContainer)
113+
export const addChild = (
114+
state: ApplicationState,
115+
{ title, childType = '', HTMLInfo = {} }: { title: string; childType: string; HTMLInfo: object },
116+
) => {
117+
const strippedTitle = title;
118+
119+
if (!childType) {
120+
window.alert('addChild Error! no type specified');
121+
}
122+
123+
const htmlElement = childType !== 'COMP' ? childType : null;
124+
if (childType !== 'COMP') {
125+
childType = 'HTML';
126+
}
127+
128+
// view represents the current FOCUSED COMPONENT - this is the component where the child is being added to
129+
// we only add children (perform any action) to the focused component
130+
const view: ComponentState = state.components.find(comp => comp.title === state.focusComponent.title);
131+
132+
// parentComponent is the component this child is generated from (ex. instance of Box has comp of Box)
133+
let parentComponent;
134+
135+
// conditional if adding an HTML component
136+
if (childType === 'COMP') {
137+
parentComponent = state.components.find((comp) => comp.title === title);
138+
}
139+
140+
interface htmlElemPositionInt {
141+
width: number;
142+
height: number;
143+
}
144+
145+
let htmlElemPosition: htmlElemPositionInt = { width: null, height: null };
146+
if (childType === 'HTML') {
147+
htmlElemPosition = getSize(htmlElement);
148+
// if above function doesn't return anything, it means html element is not yet implemented
149+
if (!htmlElemPosition.width) {
150+
console.log(
151+
`Did not add html child: ${htmlElement} the GetSize function indicated that it isnt in our DB`
152+
);
153+
return;
154+
}
155+
}
156+
// The postion of the new child is dependent on its parent, the focusComponent (called view here)
157+
const newPosition =
158+
childType === 'COMP'
159+
? {
160+
//In order to avoid the overlap of newly added children, this adds an x and y offset
161+
x: view.position.x + ((view.nextChildId * 16) % 150), // new children are offset by some amount, map of 150px
162+
y: view.position.y + ((view.nextChildId * 16) % 150),
163+
width: parentComponent.position.width - 1, // the size of new children is based on that of parent component
164+
height: parentComponent.position.height - 1
165+
}
166+
: {
167+
x: view.position.x + view.nextChildId * 16,
168+
y: view.position.y + view.nextChildId * 16,
169+
width: htmlElemPosition.width,
170+
height: htmlElemPosition.height
171+
};
172+
// Creation of the actual new child object with all data necessary to render a Rectangle
173+
const newChild: ChildState = {
174+
childId: view.nextChildId,
175+
childSort: view.nextChildId,
176+
childType,
177+
childComponentId: childType === 'COMP' ? parentComponent.id : null, // only relevant fot children of type COMPONENT
178+
componentName: strippedTitle,
179+
position: newPosition,
180+
color: null, // parentComponent.color, // only relevant for children of type COMPONENT
181+
htmlElement, // only relevant fot children of type HTML
182+
HTMLInfo
183+
};
184+
185+
const compsChildrenArr = [...view.children, newChild];
186+
187+
const component = {
188+
...view,
189+
children: compsChildrenArr,
190+
focusChildId: newChild.childId,
191+
nextChildId: view.nextChildId + 1
192+
};
193+
194+
const components = [
195+
...state.components.filter((comp) => {
196+
if (comp.title !== view.title) return comp;
197+
}),
198+
component
199+
];
200+
201+
return {
202+
...state,
203+
components,
204+
focusChild: newChild,
205+
focusComponent: component // refresh the focus component so we have the new child
206+
};
207+
};
208+
209+
=======
210+
>>>>>>> dev
110211
export const deleteChild = (
111212
state: ApplicationState,
112213
{ parentId = state.focusComponent.id, childId = state.focusChild.childId, calledFromDeleteComponent = false },

0 commit comments

Comments
 (0)