@@ -247,21 +247,57 @@ const CreateShape = (props) => {
247247
248248 if ( clipPathType === "polygon" ) {
249249 let formulaNumbers = formula . slice ( formula . indexOf ( "(" ) + 1 , formula . indexOf ( ")" ) ) ;
250- formulaNumbers = formulaNumbers . split ( ", " ) ;
250+ formulaNumbers = formulaNumbers . split ( "," ) ;
251251 newVerticeCoordinates = formulaNumbers . map ( x => {
252- let percentageArray = x . split ( " " ) ;
253- return {
254- "x" : percentageArray [ 0 ] ,
255- "y" : percentageArray [ 1 ] ,
256- }
252+ let values = x . trim ( ) ;
253+ let xValue = "" ;
254+ let yValue = "" ;
255+
256+ // If the formula includes both percentage and px
257+ // Figure out which one comes first and use that index of find it
258+ if ( values . includes ( "%" ) && values . includes ( "px" ) ) {
259+
260+ let indexOfPX = values . indexOf ( "px" ) ;
261+ let indexOfPercentage = values . indexOf ( "%" ) ;
262+
263+ if ( indexOfPX < indexOfPercentage ) {
264+ xValue = values . substring ( 0 , values . indexOf ( "px" ) + 2 ) . trim ( ) ;
265+ yValue = values . substring ( values . indexOf ( "px" ) + 2 ) . trim ( ) ;
266+ }
267+
268+ if ( indexOfPercentage < indexOfPX ) {
269+ xValue = values . substring ( 0 , values . indexOf ( "%" ) + 1 ) . trim ( ) ;
270+ yValue = values . substring ( values . indexOf ( "%" ) + 1 ) . trim ( ) ;
271+ }
272+
273+ } else if ( values . includes ( "%" ) ) {
274+ xValue = values . substring ( 0 , values . indexOf ( "%" ) + 1 ) . trim ( ) ;
275+ yValue = values . substring ( values . indexOf ( "%" ) + 1 ) . trim ( ) ;
276+ } else if ( values . includes ( "px" ) ) {
277+ xValue = values . substring ( 0 , values . indexOf ( "px" ) + 2 ) . trim ( ) ;
278+ yValue = values . substring ( values . indexOf ( "px" ) + 2 ) . trim ( ) ;
279+ }
280+
281+ if ( ! ( xValue . includes ( "px" ) || xValue . includes ( "%" ) ) || xValue . includes ( " " ) ) {
282+ xValue = "0%" ;
283+ }
284+
285+ if ( ! ( yValue . includes ( "px" ) || yValue . includes ( "%" ) ) || yValue === "" ) {
286+ yValue = "0%" ;
287+ }
288+
289+ return {
290+ "x" : xValue ,
291+ "y" : yValue ,
292+ }
257293 } ) ;
258294 }
259295
260296 setShapeInformation ( prevState => {
261297 return {
262298 ...prevState ,
263299 "formula" : formula . includes ( "(" ) && formula . includes ( ")" ) ? formula : prevState . formula ,
264- "clipPathType" : clipPathType === null ? prevState . clipPathType : clipPathType ,
300+ "clipPathType" : clipPathType === undefined ? prevState . clipPathType : clipPathType ,
265301 "vertices" : edgeVerticeNumber ,
266302 "edges" : edgeVerticeNumber ,
267303 "verticeCoordinates" : newVerticeCoordinates ,
@@ -271,13 +307,35 @@ const CreateShape = (props) => {
271307
272308 // Returns an array that has a new verticeCoordinate
273309 const addNewVerticeCoordinates = ( x , y , number ) => {
274- const xPercentage = Math . round ( ( x / 280.0 ) * 100.0 ) ;
275- const yPercentage = Math . round ( ( y / 280.0 ) * 100.0 ) ;
310+
311+ let xValue ;
312+ let yValue ;
313+
314+ // If there is a new coordinate
315+ if ( shapeInformation . verticeCoordinates . length === number ) {
316+ xValue = Math . round ( ( x / 280.0 ) * 100.0 ) + "%" ;
317+ yValue = Math . round ( ( y / 280.0 ) * 100.0 ) + "%" ;
318+ } else {
319+
320+ // Determines whether previous x coordinate was in percentage or px and adjusts value to maintain same unit of measurement
321+ if ( shapeInformation . verticeCoordinates [ number ] . x . includes ( "%" ) ) {
322+ xValue = Math . round ( ( x / 280.0 ) * 100.0 ) + "%" ;
323+ } else if ( shapeInformation . verticeCoordinates [ number ] . x . includes ( "px" ) ) {
324+ xValue = Math . round ( x ) + "px" ;
325+ }
326+
327+ // Determines whether previous y coordinate was in percentage or px and adjusts value to maintain same unit of measurement
328+ if ( shapeInformation . verticeCoordinates [ number ] . y . includes ( "%" ) ) {
329+ yValue = Math . round ( ( y / 280.0 ) * 100.0 ) + "%" ;
330+ } else if ( shapeInformation . verticeCoordinates [ number ] . y . includes ( "px" ) ) {
331+ yValue = Math . round ( y ) + "px" ;
332+ }
333+ }
276334
277335 let newVerticeCoordinates = shapeInformation . verticeCoordinates ;
278336 newVerticeCoordinates [ number ] = {
279- "x" : xPercentage + "%" ,
280- "y" : yPercentage + "%"
337+ "x" : xValue ,
338+ "y" : yValue
281339 }
282340
283341 return newVerticeCoordinates ;
0 commit comments