Skip to content

Commit f48c2c2

Browse files
committed
added comments to increase readibility and cleaned up some code
1 parent c9e4622 commit f48c2c2

4 files changed

Lines changed: 36 additions & 28 deletions

File tree

components/core/CreateShape.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,14 @@ const CreateShape = (props) => {
7070
return;
7171
}
7272

73-
if (name === 'private') {
74-
setShapeInformation({
75-
...shapeInformation,
76-
"private": !shapeInformation.private,
77-
78-
});
79-
return;
80-
}
81-
73+
// If formula value is changed, alter formula value and verticeCoordinates value depending on clipPathType
8274
if (name === "formula") {
75+
8376
const edgeVerticeNumber = shapeInformation.clipPathType === "polygon" && value !== "" ? value.split(",").length: 0;
8477

8578
// If user deletes all, set formula to the Clip-Path type with an empty set of parentheses
8679
if (value === "") {
87-
handleFormulaChange(shapeInformation.clipPathType + "()", edgeVerticeNumber);
80+
handleFormulaChange(shapeInformation.clipPathType + "()", edgeVerticeNumber);
8881
} else if (value.includes("polygon")) {
8982
handleFormulaChange(value, edgeVerticeNumber, "polygon");
9083
} else if (value.includes("circle")) {
@@ -95,9 +88,11 @@ const CreateShape = (props) => {
9588
handleFormulaChange(value, edgeVerticeNumber);
9689
}
9790
return;
91+
9892
}
9993

100-
if (name === "clipPathType") { // If Clip-Path Type is changed, change the value of the clipPathType
94+
// If Clip-Path Type is changed, change the value of the clipPathType
95+
if (name === "clipPathType") {
10196

10297
if (value === "polygon") {
10398
setShapeInformation({
@@ -138,7 +133,8 @@ const CreateShape = (props) => {
138133
return;
139134
}
140135

141-
if (name === "mousemove") { // If DraggableVertice is moved, adjust verticeCoordinates and formula
136+
// If DraggableVertice is moved, adjust verticeCoordinates and formula
137+
if (name === "mousemove") {
142138

143139
const newVerticeCoordinates = addNewVerticeCoordinates(data.x, data.y, number);
144140
const newFormula = generateNewFormula(newVerticeCoordinates);
@@ -151,7 +147,8 @@ const CreateShape = (props) => {
151147
return;
152148
}
153149

154-
if (name === "click" && event.target.id === "shapeShadow" && shapeInformation.clipPathType === "polygon") { // If background of preview is clicked, add a verticeCoordinate value at its location and adjust formula
150+
// If background of preview is clicked and the clipPathType is a polygon, add a verticeCoordinate value at its location and adjust formula
151+
if (name === "click" && event.target.id === "shapeShadow" && shapeInformation.clipPathType === "polygon") {
155152

156153
const newVerticeCoordinates = addNewVerticeCoordinates(event.nativeEvent.offsetX, event.nativeEvent.offsetY, shapeInformation.verticeCoordinates.length);
157154
const newFormula = generateNewFormula(newVerticeCoordinates);
@@ -165,8 +162,9 @@ const CreateShape = (props) => {
165162
});
166163
return;
167164
}
168-
169-
if (event.target.id.includes("deleteButton") && number !== undefined) { // If delete button is pressed, remove the corresponding verticeCoordinate and adjust formula
165+
166+
// If delete button is pressed and passes a number that corresponds to the vertice, remove the corresponding verticeCoordinate and adjust formula
167+
if (event.target.id.includes("deleteButton") && number !== undefined) {
170168

171169
let newVerticeCoordinates = [];
172170

@@ -188,12 +186,16 @@ const CreateShape = (props) => {
188186
return;
189187
}
190188

189+
// Handles all other ShapeForm and ShapePreview Changes
191190
setShapeInformation({
192191
...shapeInformation,
193192
[name]: value,
194193
});
195-
}
194+
}
196195

196+
// Called when there is a change in the textbox for formula in the form
197+
// Adjusts verticeCoordinates, vertices, and edges accordingly
198+
// Ensures that the parentheses remain
197199
function handleFormulaChange(formula, edgeVerticeNumber, clipPathType) {
198200
let newVerticeCoordinates = [];
199201

@@ -221,7 +223,7 @@ const CreateShape = (props) => {
221223
});
222224
}
223225

224-
226+
// Returns an array that has a new verticeCoordinate
225227
function addNewVerticeCoordinates(x ,y, number) {
226228
const xPercentage = Math.round((x / 280.0) * 100.0);
227229
const yPercentage = Math.round((y / 280.0) * 100.0);
@@ -235,6 +237,7 @@ const CreateShape = (props) => {
235237
return newVerticeCoordinates;
236238
}
237239

240+
// Returns a generated formula string from a verticeCoordinate array
238241
function generateNewFormula(newVerticeCoordinates) {
239242
let newFormula = shapeInformation.clipPathType + "(";
240243

@@ -248,6 +251,7 @@ const CreateShape = (props) => {
248251
return newFormula;
249252
}
250253

254+
// Form Validation
251255
const [validated, setValidated] = useState(false);
252256

253257
const handleSubmit = async(event) => {

components/utils/DraggableVertice.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const DraggableVertice = (props) => {
2626
let x = parseFloat(props.x) * 280.0 / 100.0;
2727
let y = parseFloat(props.y) * 280.0 / 100.0;
2828

29+
// Handles when to show the close button
2930
const show = props.focusNumber === props.number;
3031
const target = useRef(null);
3132

components/utils/ShapeForm.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import React from "react";
2+
3+
// Styled Component
24
import styled from "styled-components";
5+
6+
// Bootstrap
37
import Form from "react-bootstrap/Form";
48

59
const ColorPicker = styled.input`

components/utils/ShapePreview.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import React, { useState, useEffect } from "react";
2+
3+
// Styled Component
24
import styled from "styled-components";
5+
6+
// Bootstrap
37
import Form from "react-bootstrap/Form";
48

9+
// Draggablevertice Component
510
import { DraggableVertice } from "..";
611

712
const Playground = styled.div`
@@ -35,22 +40,16 @@ const Component = styled.div`
3540
bottom: 10px;
3641
`;
3742

38-
const ShapeDetails = styled.ul`
39-
background-color: #ebebeb;
40-
border-radius: 4px;
41-
padding: 10px;
42-
width: 100%;
43-
`;
44-
45-
const ShapeDetailsItems = styled.li`
46-
word-wrap: break-word;
47-
`;
48-
4943
const ShapePreview = (props) => {
5044

45+
// Holds an array of DraggableVertices
5146
const [vertices, setVertices] = useState([]);
47+
48+
// Set to a number that determines which DraggableVertice has its tooltip showing
49+
// This way, only one vertice can show its close button at a time
5250
const [focusNumber, setFocusNumber] = useState(-1);
5351

52+
// Creation of DraggableVertices depending on shapeInformation values
5453
useEffect(() => {
5554
const array = [];
5655

0 commit comments

Comments
 (0)