Skip to content

Commit b557505

Browse files
authored
Merge pull request #18 from trevorgerhardt/patch-2
Modify isBbox regex to allow whitespace
2 parents ecf8163 + b333b96 commit b557505

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/Main.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ function CreateComponent() {
176176
>();
177177
const [name, setName] = useState<string>("");
178178
const [validationFailure, setValidationFailure] = useState<string>("");
179+
const [parsingFailure, setParsingFailure] = useState<string>("");
179180
// const [showAboutModal, setShowAboutModal] = useState<boolean>(false);
180181

181182
useEffect(() => {
@@ -369,7 +370,7 @@ function CreateComponent() {
369370

370371
const loadTextArea = () => {
371372
if (!drawRef.current || !mapRef.current) return;
372-
const isBbox = /^(-?\d+(\.\d+)?,){3}-?\d+(\.\d+)?$/;
373+
const isBbox = /^\s*(-?\d+(\.\d+)?\s*,\s*){3}-?\d+(\.\d+)?\s*$/;
373374
if (isBbox.test(textAreaValue)) {
374375
const arr = textAreaValue.split(",");
375376
const minX = +arr[0];
@@ -445,10 +446,28 @@ function CreateComponent() {
445446
<div>
446447
<p>Paste bbox or GeoJSON:</p>
447448
<textarea
449+
aria-invalid={parsingFailure !== ""}
448450
value={textAreaValue}
449451
onChange={(e) => setTextAreaValue(e.target.value)}
450452
/>
451-
<button onClick={loadTextArea}>Load</button>
453+
{parsingFailure !== "" && (
454+
<p>Parsing failed. {parsingFailure}</p>
455+
)}
456+
<button onClick={() => {
457+
setParsingFailure("");
458+
try {
459+
loadTextArea();
460+
} catch (e) {
461+
console.error(e);
462+
// JSON.parse throws a SyntaxError if the input is not valid JSON. Might be a bbox.
463+
if (e instanceof SyntaxError) {
464+
setParsingFailure("Expected bbox format:\n minLon, minLat, maxLon, maxLat");
465+
} else {
466+
// If it's not a SyntaxError, it's probably invalid GeoJSON.
467+
setParsingFailure("GeoJSON must contain valid Polygon or MultiPolygon geometries.");
468+
}
469+
}
470+
}}>Load</button>
452471
</div>
453472
<input
454473
value={name}

src/main.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ textarea {
175175
width: 100%;
176176
}
177177

178+
textarea[aria-invalid="true"] {
179+
border-color: red;
180+
}
181+
178182
.lightbox {
179183
position: absolute;
180184
height: calc(100vh - 30px);

0 commit comments

Comments
 (0)