diff --git a/package-lock.json b/package-lock.json index 7d4f68fe0..57fdc2997 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,7 @@ "@turf/nearest-point-on-line": "^6.5.0", "@types/geojson": "^7946.0.10", "maplibre-gl": "^2.4.0", - "route-snapper": "^0.1.12", + "route-snapper": "^0.1.13", "svelte": "^3.54.0" }, "devDependencies": { @@ -1851,9 +1851,9 @@ } }, "node_modules/route-snapper": { - "version": "0.1.12", - "resolved": "https://registry.npmjs.org/route-snapper/-/route-snapper-0.1.12.tgz", - "integrity": "sha512-oEmR2JkpxKkJBIVzfO4K1KibiPPxhF1rjyL7Rz5mPoW/tJaWu3AaPiDVzUo+W9ZZz2Hxi1YCAUUakrgxtau4jg==" + "version": "0.1.13", + "resolved": "https://registry.npmjs.org/route-snapper/-/route-snapper-0.1.13.tgz", + "integrity": "sha512-GjILba/OSor8gU2LkdAtioxlNdHx8CVTtFcR1QG9XpRIXjINgzv1slnC3nfpQ2yzb8rGvCEHqqHtUFNqDy2dCg==" }, "node_modules/run-parallel": { "version": "1.2.0", diff --git a/package.json b/package.json index fe12ec6d7..fd4bdd463 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "@turf/nearest-point-on-line": "^6.5.0", "@types/geojson": "^7946.0.10", "maplibre-gl": "^2.4.0", - "route-snapper": "^0.1.12", + "route-snapper": "^0.1.13", "svelte": "^3.54.0" } } diff --git a/src/lib/draw/GeometryMode.svelte b/src/lib/draw/GeometryMode.svelte index b80f609b9..16757db6b 100644 --- a/src/lib/draw/GeometryMode.svelte +++ b/src/lib/draw/GeometryMode.svelte @@ -9,6 +9,7 @@ import type { Feature, FeatureUnion } from "../../types"; import PointControls from "./point/PointControls.svelte"; import PolygonControls from "./polygon/PolygonControls.svelte"; + import SnapPolygonControls from "./snap_polygon/SnapPolygonControls.svelte"; import RouteControls from "./route/RouteControls.svelte"; const thisMode = "edit-geometry"; @@ -20,7 +21,12 @@ // An optional ID of what we're currently editing in this mode let currentlyEditing: number | null = null; - let currentlyEditingControls: "point" | "polygon" | "route" | null = null; + let currentlyEditingControls: + | "point" + | "free-polygon" + | "snap-polygon" + | "route" + | null = null; export function start() {} export function stop() { @@ -55,7 +61,7 @@ }); // Handle successful edits - routeTool.addEventListenerSuccess((editedRoute) => { + routeTool.addEventListenerSuccessRoute((editedRoute) => { if (mode == thisMode) { gjScheme.update((gj) => { let feature = gj.features.find((f) => f.id == currentlyEditing)!; @@ -73,6 +79,23 @@ currentlyEditingControls = null; } }); + routeTool.addEventListenerSuccessArea((editedArea) => { + if (mode == thisMode) { + gjScheme.update((gj) => { + let feature = gj.features.find((f) => f.id == currentlyEditing)!; + // Keep the ID and any properties. Just copy over stuff from routeSnapper. + // TODO We're depending on implementation details here and knowing what to copy... + feature.properties.waypoints = editedArea.properties.waypoints; + delete feature.properties.hide_while_editing; + feature.geometry = editedArea.geometry; + return gj; + }); + + // Stay in this mode + currentlyEditing = null; + currentlyEditingControls = null; + } + }); for (let tool of [pointTool, polygonTool]) { tool.addEventListenerSuccess((feature) => { if (mode == thisMode) { @@ -159,11 +182,16 @@ currentlyEditing = id; if (feature.geometry.type == "LineString") { - routeTool.editExisting(feature as Feature); + routeTool.editExistingRoute(feature as Feature); currentlyEditingControls = "route"; } else if (feature.geometry.type == "Polygon") { - polygonTool.editExisting(feature as Feature); - currentlyEditingControls = "polygon"; + if (feature.properties.waypoints) { + routeTool.editExistingArea(feature as Feature); + currentlyEditingControls = "snap-polygon"; + } else { + polygonTool.editExisting(feature as Feature); + currentlyEditingControls = "free-polygon"; + } } else if (feature.geometry.type == "Point") { // No need to pass in the existing feature.geometry; it's the same as // where the cursor is anyway @@ -176,8 +204,10 @@ {#if mode == thisMode} {#if currentlyEditingControls == "point"} - {:else if currentlyEditingControls == "polygon"} + {:else if currentlyEditingControls == "free-polygon"} + {:else if currentlyEditingControls == "snap-polygon"} + {:else if currentlyEditingControls == "route"} {:else} diff --git a/src/lib/draw/Toolbox.svelte b/src/lib/draw/Toolbox.svelte index dd784efbd..3810466bd 100644 --- a/src/lib/draw/Toolbox.svelte +++ b/src/lib/draw/Toolbox.svelte @@ -11,16 +11,16 @@ import RouteMode from "./route/RouteMode.svelte"; import PointMode from "./point/PointMode.svelte"; import PolygonMode from "./polygon/PolygonMode.svelte"; + import SnapPolygonMode from "./snap_polygon/SnapPolygonMode.svelte"; import SplitRouteMode from "./route/SplitRouteMode.svelte"; export let routeUrl: string; export let planningMode: boolean; // Plumbed up from RouteMode, so we can pass it down to GeometryMode - // TODO Reconsider + // TODO Create this here too? let routeTool: RouteTool; // Create and manage these here, then pass down to modes as needed. - // TODO Teardown too let pointTool = new PointTool($map); let polygonTool = new PolygonTool($map); @@ -30,6 +30,7 @@ let routeMode: RouteMode; let pointMode: PointMode; let polygonMode: PolygonMode; + let snapPolygonMode: SnapPolygonMode; let splitRouteMode: SplitRouteMode; // This must be used; don't manually change mode @@ -39,7 +40,8 @@ "edit-geometry": geometryMode, route: routeMode, point: pointMode, - polygon: polygonMode, + "free-polygon": polygonMode, + "snap-polygon": snapPolygonMode, "split-route": splitRouteMode, }; @@ -57,6 +59,7 @@ onDestroy(() => { pointTool?.tearDown(); polygonTool?.tearDown(); + routeTool?.tearDown(); }); @@ -98,11 +101,26 @@
changeMode("free-polygon")} + disabled={mode == "free-polygon"}>New polygon (freehand)
+
+ + {#if routeTool} + + {/if} +
{#if !planningMode} + +
+ diff --git a/src/lib/draw/snap_polygon/SnapPolygonMode.svelte b/src/lib/draw/snap_polygon/SnapPolygonMode.svelte new file mode 100644 index 000000000..17226104c --- /dev/null +++ b/src/lib/draw/snap_polygon/SnapPolygonMode.svelte @@ -0,0 +1,44 @@ + + +{#if mode == thisMode} + +{/if} diff --git a/src/lib/draw/types.ts b/src/lib/draw/types.ts index 57b6403fd..648816854 100644 --- a/src/lib/draw/types.ts +++ b/src/lib/draw/types.ts @@ -3,5 +3,6 @@ export type Mode = | "edit-geometry" | "route" | "point" - | "polygon" + | "free-polygon" + | "snap-polygon" | "split-route";