11import React , { useState } from "react" ;
2+
3+ // Bootstrap
24import Container from "react-bootstrap/Container" ;
35import Row from "react-bootstrap/Row" ;
46import Col from "react-bootstrap/Col" ;
57import Modal from "react-bootstrap/Modal" ;
68import Button from "react-bootstrap/Button" ;
79
10+ // ShapeForm and ShapePreview Components
811import { ShapeForm , ShapePreview } from ".." ;
912
1013const CreateShape = ( props ) => {
14+
15+ // shapeInformation holds all information about the shape
1116 const [ shapeInformation , setShapeInformation ] = useState ( {
1217 "name" : "Tilted Square" ,
13- "type" : "tiltedSquare" ,
1418 "formula" : "polygon(10% 10%, 90% 10%, 90% 90%, 10% 80%)" ,
1519 "vertices" : 4 ,
1620 "edges" : 4 ,
@@ -38,23 +42,20 @@ const CreateShape = (props) => {
3842 ]
3943 } ) ;
4044
45+ // Changes shapeInformation when something in ShapeForm or ShapePreview is altered
4146 function handleChange ( event , data , number ) {
4247 const name = event . target . name || event . type ;
4348 const value = event . target . type === "checkbox" ? event . target . checked : event . target . value ;
4449
4550 console . log ( event , data ) ;
4651
47- if ( name === "name" ) {
48- setShapeInformation ( {
49- ...shapeInformation ,
50- "name" : value ,
51- "type" : value . toLowerCase ( ) ,
52- } ) ;
53- } else if ( name === "formula" ) {
52+ // If Clip-Path formula value is changed, it makes sure that the parentheses stay there and also alters the verticeCoordinates value
53+ if ( name === "formula" ) {
5454 const edgeVerticeNumber = shapeInformation . clipPathType === "polygon" ? value . split ( "," ) . length : 0 ;
5555
56+ // If user deletes all, set formula to the Clip-Path type with an empty set of parentheses
5657 if ( value === "" ) {
57- handleFormulaChange ( shapeInformation . clipPathType + "()" , edgeVerticeNumber )
58+ handleFormulaChange ( shapeInformation . clipPathType + "()" , edgeVerticeNumber ) ;
5859 } else if ( value . includes ( "polygon" ) ) {
5960 handleFormulaChange ( value , edgeVerticeNumber , "polygon" ) ;
6061 } else if ( value . includes ( "circle" ) ) {
@@ -64,7 +65,48 @@ const CreateShape = (props) => {
6465 } else {
6566 handleFormulaChange ( value , edgeVerticeNumber ) ;
6667 }
67- } else if ( name === "mousemove" ) {
68+
69+ } else if ( name === "clipPathType" ) { // If Clip-Path Type is changed, change the value of the clipPathType
70+
71+ if ( clipPathType === "polygon" ) {
72+ setShapeInformation ( {
73+ ...shapeInformation ,
74+ "name" : "Tilted Square" ,
75+ "type" : "tiltedSquare" ,
76+ "formula" : "polygon(10% 10%, 90% 10%, 90% 90%, 10% 80%)" ,
77+ } ) ;
78+ }
79+
80+ if ( clipPathType === "circle" ) {
81+ setShapeInformation ( {
82+ ...shapeInformation ,
83+ "name" : "Circle" ,
84+ "type" : "circle" ,
85+ "formula" : "circle(50% at 50% 50%)" ,
86+ } ) ;
87+ }
88+
89+ if ( clipPathType === "ellipse" ) {
90+ setShapeInformation ( {
91+ ...shapeInformation ,
92+ "name" : "Ellipse" ,
93+ "type" : "ellipse" ,
94+ "formula" : "ellipse(25% 40% at 50% 50%)" ,
95+ } ) ;
96+ }
97+
98+ setShapeInformation ( prevState => {
99+ return {
100+ ...prevState ,
101+ "clipPathType" : clipPathType ,
102+ "edges" : clipPathType === "polygon" ? 4 : 0 ,
103+ "vertices" : clipPathType === "polygon" ? 4 : 0 ,
104+ "notes" : "" ,
105+ }
106+ } )
107+
108+ } else if ( name === "mousemove" ) { // If DraggableVertice is moved, adjust verticeCoordinates and formula
109+
68110 const newVerticeCoordinates = addNewVerticeCoordinates ( data . x , data . y , number ) ;
69111 const newFormula = generateNewFormula ( newVerticeCoordinates ) ;
70112
@@ -73,7 +115,9 @@ const CreateShape = (props) => {
73115 "verticeCoordinates" : newVerticeCoordinates ,
74116 "formula" : newFormula ,
75117 } ) ;
76- } else if ( name === "click" && event . target . id === "shapeShadow" ) {
118+
119+ } else if ( name === "click" && event . target . id === "shapeShadow" ) { // If background of preview is clicked, add a verticeCoordinate value at its location and adjust formula
120+
77121 const newVerticeCoordinates = addNewVerticeCoordinates ( event . nativeEvent . offsetX , event . nativeEvent . offsetY , shapeInformation . verticeCoordinates . length ) ;
78122 const newFormula = generateNewFormula ( newVerticeCoordinates ) ;
79123
@@ -84,7 +128,8 @@ const CreateShape = (props) => {
84128 "verticeCoordinates" : newVerticeCoordinates ,
85129 "formula" : newFormula ,
86130 } ) ;
87- } else if ( ( event . target . id . includes ( "deleteButton" ) || event . target . localName === "line" ) && number !== undefined ) {
131+
132+ } else if ( event . target . id . includes ( "deleteButton" ) && number !== undefined ) { // If delete button is pressed, remove the corresponding verticeCoordinate and adjust formula
88133
89134 let newVerticeCoordinates = [ ] ;
90135
@@ -104,41 +149,15 @@ const CreateShape = (props) => {
104149 "formula" : newFormula ,
105150 } ) ;
106151
107- } else if ( name === "clipPathType" ) {
108- handleClipPathChange ( value ) ;
109- } else {
152+ } else { // Handles all other changes
153+
110154 setShapeInformation ( {
111155 ...shapeInformation ,
112156 [ name ] : value ,
113157 } ) ;
114- }
115- }
116-
117- function addNewVerticeCoordinates ( x , y , number ) {
118- const xPercentage = Math . round ( ( x / 280.0 ) * 100.0 ) ;
119- const yPercentage = Math . round ( ( y / 280.0 ) * 100.0 ) ;
120-
121- let newVerticeCoordinates = shapeInformation . verticeCoordinates ;
122- newVerticeCoordinates [ number ] = {
123- "x" : xPercentage + "%" ,
124- "y" : yPercentage + "%"
125- }
126-
127- return newVerticeCoordinates ;
128- }
129-
130- function generateNewFormula ( newVerticeCoordinates ) {
131- let newFormula = shapeInformation . clipPathType + "(" ;
132-
133- for ( let i = 0 ; i < newVerticeCoordinates . length ; i ++ ) {
134- let newX = newVerticeCoordinates [ i ] . x ;
135- let newY = newVerticeCoordinates [ i ] . y ;
136158
137- i === newVerticeCoordinates . length - 1 ? newFormula = newFormula + newX + " " + newY + ")" : newFormula = newFormula + newX + " " + newY + ", " ;
138159 }
139-
140- return newFormula ;
141- }
160+ }
142161
143162 function handleFormulaChange ( formula , edgeVerticeNumber , clipPathType ) {
144163 let newVerticeCoordinates = [ ] ;
@@ -165,47 +184,36 @@ const CreateShape = (props) => {
165184 "verticeCoordinates" : newVerticeCoordinates ,
166185 }
167186 } ) ;
168- }
187+ }
169188
170- function handleClipPathChange ( clipPathType ) {
171- if ( clipPathType === "polygon" ) {
172- setShapeInformation ( {
173- ...shapeInformation ,
174- "name" : "Tilted Square" ,
175- "type" : "tiltedSquare" ,
176- "formula" : "polygon(10% 10%, 90% 10%, 90% 90%, 10% 80%)" ,
177- } ) ;
178- }
189+ function addNewVerticeCoordinates ( x , y , number ) {
190+ const xPercentage = Math . round ( ( x / 280.0 ) * 100.0 ) ;
191+ const yPercentage = Math . round ( ( y / 280.0 ) * 100.0 ) ;
179192
180- if ( clipPathType === "circle" ) {
181- setShapeInformation ( {
182- ...shapeInformation ,
183- "name" : "Circle" ,
184- "type" : "circle" ,
185- "formula" : "circle(50% at 50% 50%)" ,
186- } ) ;
193+ let newVerticeCoordinates = shapeInformation . verticeCoordinates ;
194+ newVerticeCoordinates [ number ] = {
195+ "x" : xPercentage + "%" ,
196+ "y" : yPercentage + "%"
187197 }
188198
189- if ( clipPathType === "ellipse" ) {
190- setShapeInformation ( {
191- ...shapeInformation ,
192- "name" : "Ellipse" ,
193- "type" : "ellipse" ,
194- "formula" : "ellipse(25% 40% at 50% 50%)" ,
195- } ) ;
199+ return newVerticeCoordinates ;
200+ }
201+
202+ function generateNewFormula ( newVerticeCoordinates ) {
203+ let newFormula = shapeInformation . clipPathType + "(" ;
204+
205+ for ( let i = 0 ; i < newVerticeCoordinates . length ; i ++ ) {
206+ let newX = newVerticeCoordinates [ i ] . x ;
207+ let newY = newVerticeCoordinates [ i ] . y ;
208+
209+ i === newVerticeCoordinates . length - 1 ? newFormula = newFormula + newX + " " + newY + ")" : newFormula = newFormula + newX + " " + newY + ", " ;
196210 }
197211
198- setShapeInformation ( prevState => {
199- return {
200- ...prevState ,
201- "clipPathType" : clipPathType ,
202- "edges" : clipPathType === "polygon" ? 4 : 0 ,
203- "vertices" : clipPathType === "polygon" ? 4 : 0 ,
204- "notes" : "" ,
205- }
206- } )
212+ return newFormula ;
207213 }
208214
215+
216+
209217 const [ validated , setValidated ] = useState ( false ) ;
210218
211219 const handleSubmit = ( event ) => {
0 commit comments