Skip to content

Commit 1e22880

Browse files
committed
Commented component reducer in utils half way
1 parent ec7dcbe commit 1e22880

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

src/components/KonvaStage.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,21 @@ class KonvaStage extends Component<PropsInt, StateInt> {
5454
};
5555
}
5656

57+
//makes a copy of the array of children plus the parent component pushed onto it
5758
getDirectChildrenCopy(focusComponent: ComponentInt) {
59+
//assign component to the docused component
5860
const component = this.props.components.find(
5961
(comp: ComponentInt) => comp.id === focusComponent.id
6062
);
61-
63+
//assign childrenArr to an array of all the children of focused component
6264
const childrenArr = component.childrenArray.filter(
6365
(child: ChildInt) => child.childId !== -1
6466
);
6567

68+
//deep clone of childrenArr so addition of parent doesn't mutate the children saved in the state
6669
let childrenArrCopy = cloneDeep(childrenArr);
6770

71+
//adds a pseudochild witrh the parent component's property to the copied children array
6872
const pseudoChild = {
6973
childId: -1,
7074
childComponentId: component.id,
@@ -79,6 +83,7 @@ class KonvaStage extends Component<PropsInt, StateInt> {
7983
color: component.color
8084
};
8185
childrenArrCopy = childrenArrCopy.concat(pseudoChild); // could just use push here, concat needlessly generate new array
86+
//returns that new childrenArr + parent component
8287
return childrenArrCopy;
8388
}
8489

@@ -221,6 +226,8 @@ class KonvaStage extends Component<PropsInt, StateInt> {
221226
}}
222227
>
223228
{this.state.grid}
229+
{/* {The logic hereis that it creates a new rectangle or each component that belongs to this parent component, plus the parent component.
230+
The parent component is rendered last. It renders based on the values in the return value of getDirectChildrenCopy. } */}
224231
{!isEmpty(focusComponent) && this.getDirectChildrenCopy(focusComponent)
225232
.map((child: ChildInt, i: number) => (
226233
<Rectangle

src/components/Rectangle.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ class Rectangle extends Component<PropsInt> {
3838
}
3939

4040
//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
41+
//named pseudochild because it isn't saved as a child in the childrenArray.
4442
getPseudoChild() {
4543
return this.props.components.find(
4644
(comp: ComponentInt) => comp.id === this.props.childComponentId
@@ -158,7 +156,7 @@ class Rectangle extends Component<PropsInt> {
158156
stroke={
159157
childType === 'COMP'
160158
? this.getComponentColor(childComponentId)
161-
: '#000000' //sets the parent component color to black, this logic actually doesn't need to exist at the level of this component
159+
: '#000000' //sets the parent component color to black
162160
}
163161
onTransformEnd={event =>
164162
this.handleResize(componentId, childId, event.target, blockSnapSize)

src/utils/componentReducer.util.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import cloneDeep from './cloneDeep';
55
import {
66
ComponentInt,
77
ApplicationStateInt,
8-
ChildrenInt,
8+
//ChildrenInt, //unused import//
99
ChildInt,
1010
ComponentsInt,
1111
PropInt
1212
} from './Interfaces';
1313

14+
//this is the default values for any component added to the app.
15+
1416
const initialComponentState: ComponentInt = {
1517
id: 0,
1618
stateful: false,
@@ -58,9 +60,12 @@ export const addComponent = (
5860
};
5961
}
6062

63+
//chooses a color for the component from the random color generator
6164
const componentColor = getColor();
65+
//assigns the componentID to whatever issupposed to be next
6266
const componentId = state.nextId;
6367

68+
//creates a newcomponent and prepares it to be added to an array of components in the store
6469
const newComponent: ComponentInt = {
6570
...initialComponentState,
6671
title: strippedTitle,
@@ -71,16 +76,21 @@ export const addComponent = (
7176

7277
const components = [...state.components, newComponent];
7378

79+
//increments both total components and the nextID
7480
const totalComponents = state.totalComponents + 1;
7581
const nextId = state.nextId + 1;
7682

83+
84+
//creates an array of component IDs that can be added as children to this newly created component.
85+
//the newly created component and the App component are never selectable.
7786
const selectableChildren = state.components
7887
.map((comp: ComponentInt) => comp.id)
7988
.filter((id: number) => id !== newComponent.id);
8089

8190
const ancestors: Array<number> = [];
8291

83-
// reset focused child
92+
// reset focused child to null values so when focused component is assigned to the newly created component,
93+
//child from previously focused component doesn;t show up
8494
const newFocusChild = cloneDeep(state.initialApplicationFocusChild);
8595
return {
8696
...state,
@@ -94,6 +104,7 @@ export const addComponent = (
94104
};
95105
};
96106

107+
//reducer function to add component or HTML child to currently focused component
97108
export const addChild = (
98109
state: ApplicationStateInt,
99110
{
@@ -104,10 +115,12 @@ export const addChild = (
104115
) => {
105116
const strippedTitle = title;
106117

118+
//is this warning even possible to trigger witht he current flow?
107119
if (!childType) {
108120
window.alert('addChild Error! no type specified');
109121
}
110122

123+
//weird use of a ternary operator, could've wrapped it in one if statement
111124
const htmlElement = childType !== 'COMP' ? childType : null;
112125
if (childType !== 'COMP') {
113126
childType = 'HTML';
@@ -138,14 +151,17 @@ export const addChild = (
138151
if (childType === 'HTML') {
139152
htmlElemPosition = getSize(htmlElement);
140153
// if above function doesnt reutn anything, it means html element is not in our database
154+
//looks like the group that originally worked on this app planend to have a back end that accessed a DB with element types.
155+
//I don't think this error does anything anymore either.
141156
if (!htmlElemPosition.width) {
142157
console.log(
143158
`Did not add html child: ${htmlElement} the GetSize function indicated that it isnt in our DB`
144159
);
145160
return;
146161
}
147162
}
148-
163+
//intersting how they decided to offset the next child to be placed on the stage by multiplying
164+
//the next child ID by 16. I mean I guess it makes sense.
149165
const newPosition =
150166
childType === 'COMP'
151167
? {
@@ -160,7 +176,7 @@ export const addChild = (
160176
width: htmlElemPosition.width,
161177
height: htmlElemPosition.height
162178
};
163-
179+
164180
const newChild: ChildInt = {
165181
childId: view.nextChildId,
166182
childSort: view.nextChildId,
@@ -174,7 +190,9 @@ export const addChild = (
174190
};
175191

176192
const compsChildrenArr = [...view.childrenArray, newChild];
193+
177194

195+
//values to put into the focus component so it is updated along with the components array
178196
const component = {
179197
...view,
180198
childrenArray: compsChildrenArr,
@@ -261,13 +279,18 @@ export const deleteChild = (
261279
};
262280
};
263281

282+
//Simple function that changes the imageSource in the global state.
283+
//Should be refactored to also store image data not just source,
284+
//since currently HTML image lives in a local state of AppContainer ( a big no-no)
285+
//and if a user clicks on 'clear workspace', the button doesn't reset
264286
export const deleteImage = (state: ApplicationStateInt) => {
265287
return {
266288
...state,
267289
imageSource: ''
268290
};
269291
};
270292

293+
//the function that handles the transformation of all children in the stage
271294
export const handleTransform = (
272295
state: ApplicationStateInt,
273296
{
@@ -291,6 +314,8 @@ export const handleTransform = (
291314
const component = state.components.find(
292315
(comp: ComponentInt) => comp.id === componentId
293316
);
317+
318+
//first check if changed, if falsy then assign the original values
294319
const transformedComponent = {
295320
...component,
296321
position: {
@@ -301,6 +326,7 @@ export const handleTransform = (
301326
}
302327
};
303328

329+
//return state with updated component values
304330
const components = [
305331
...state.components.filter((comp: ComponentInt) => {
306332
if (comp.id !== componentId) return comp;

0 commit comments

Comments
 (0)