11// src/PolylineCreation.js
22
33import { useState } from "react" ;
4- import { decode } from "s2polyline-ts" ;
54import { log } from "./Utils" ;
5+ import { parsePolylineInput } from "./PolylineUtils" ;
66
77function PolylineCreation ( { onSubmit, onClose, buttonPosition } ) {
88 const [ input , setInput ] = useState ( "" ) ;
@@ -13,60 +13,20 @@ function PolylineCreation({ onSubmit, onClose, buttonPosition }) {
1313 const handleSubmit = ( e ) => {
1414 e . preventDefault ( ) ;
1515 try {
16- const trimmedInput = input . trim ( ) ;
17-
18- // Check if input looks like an encoded polyline (single string without spaces)
19- if ( / ^ [ A - Z a - z 0 - 9 + / = \- _ ] + $ / . test ( trimmedInput ) ) {
20- log ( "Attempting to decode S2 polyline:" , trimmedInput ) ;
21- const decodedPoints = decode ( trimmedInput ) ;
22-
23- if ( decodedPoints && decodedPoints . length > 0 ) {
24- // Convert S2 points to our expected format
25- const validWaypoints = decodedPoints . map ( ( point ) => ( {
26- latitude : point . latDegrees ( ) ,
27- longitude : point . lngDegrees ( ) ,
28- } ) ) ;
29-
30- log ( `Decoded ${ validWaypoints . length } points from S2 polyline` ) ;
31- onSubmit ( validWaypoints , { opacity, color, strokeWeight } ) ;
32- setInput ( "" ) ;
33- return ;
34- }
35- }
36-
37- // Existing JSON parsing logic
38- const jsonString = trimmedInput . replace ( / ( \w + ) : / g, '"$1":' ) . replace ( / \s + / g, " " ) ;
39-
40- const inputWithBrackets = jsonString . startsWith ( "[" ) && jsonString . endsWith ( "]" ) ? jsonString : `[${ jsonString } ]` ;
41-
42- const waypoints = JSON . parse ( inputWithBrackets ) ;
43-
44- const validWaypoints = waypoints . filter (
45- ( waypoint ) =>
46- typeof waypoint === "object" &&
47- "latitude" in waypoint &&
48- "longitude" in waypoint &&
49- typeof waypoint . latitude === "number" &&
50- typeof waypoint . longitude === "number"
51- ) ;
52-
53- if ( validWaypoints . length === 0 ) {
54- throw new Error ( "No valid waypoints found" ) ;
55- }
56-
16+ const validWaypoints = parsePolylineInput ( input ) ;
5717 log ( `Parsed ${ validWaypoints . length } valid waypoints` ) ;
5818 onSubmit ( validWaypoints , { opacity, color, strokeWeight } ) ;
19+ setInput ( "" ) ;
5920 } catch ( error ) {
6021 log ( "Invalid input format:" , error ) ;
6122 }
62- setInput ( "" ) ;
6323 } ;
6424
6525 let placeholder = `Paste waypoints here:
6626{ latitude: 52.5163, longitude: 13.2399 },
6727{ latitude: 52.5162, longitude: 13.2400 }
6828
69- Or paste an encoded S2 polyline string` ;
29+ Or paste an encoded S2 or Google Maps polyline string` ;
7030
7131 return (
7232 < div
0 commit comments