Skip to content

Commit

Permalink
Use more refactored helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed May 5, 2024
1 parent 59fc900 commit 82c9f22
Show file tree
Hide file tree
Showing 17 changed files with 729 additions and 57 deletions.
717 changes: 704 additions & 13 deletions web/package-lock.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"vite-plugin-wasm-pack": "^0.1.12"
},
"dependencies": {
"@turf/bbox": "^6.5.0",
"maplibre-draw-polygon": "github:dabreegster/maplibre-draw-polygon",
"maplibre-gl": "^4.1.0",
"svelte-maplibre": "^0.8.3",
Expand Down
2 changes: 1 addition & 1 deletion web/src/common/LayerControls.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import type { GeoJSON } from "geojson";
import { downloadGeneratedFile } from "./utils";
import { downloadGeneratedFile } from "svelte-utils";
export let gj: GeoJSON;
export let name: string;
Expand Down
3 changes: 1 addition & 2 deletions web/src/common/import/ImportControls.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
network as networkStore,
importCounter,
} from "../store";
import { bbox, downloadGeneratedFile } from "../utils";
import Osm2streetsSettings from "./Osm2streetsSettings.svelte";
import { Loading } from "svelte-utils";
import { bbox, downloadGeneratedFile, Loading } from "svelte-utils";
// This component manages a state machine for importing OSM data (from
// Overpass or built-in files) and letting the user change import settings. It
Expand Down
3 changes: 2 additions & 1 deletion web/src/common/layers/DebugIDs.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">
import LayerControls from "../LayerControls.svelte";
import { network } from "../store";
import { layerId, emptyGeojson } from "../utils";
import { layerId } from "../utils";
import { emptyGeojson } from "svelte-utils";
import { SymbolLayer, GeoJSON } from "svelte-maplibre";
let show = false;
Expand Down
3 changes: 2 additions & 1 deletion web/src/common/layers/DynamicConnectedRoads.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">
import LayerControls from "../LayerControls.svelte";
import { hoveredIntersection, network } from "../store";
import { layerId, emptyGeojson } from "../utils";
import { layerId } from "../utils";
import { emptyGeojson } from "svelte-utils";
import { FillLayer, GeoJSON } from "svelte-maplibre";
let show = false;
Expand Down
3 changes: 2 additions & 1 deletion web/src/common/layers/DynamicMovementArrows.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">
import LayerControls from "../LayerControls.svelte";
import { hoveredLane, network } from "../store";
import { layerId, emptyGeojson } from "../utils";
import { layerId } from "../utils";
import { emptyGeojson } from "svelte-utils";
import { FillLayer, GeoJSON } from "svelte-maplibre";
let show = true;
Expand Down
3 changes: 2 additions & 1 deletion web/src/common/layers/RenderBoundary.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">
import LayerControls from "../LayerControls.svelte";
import { boundaryGj, map } from "../store";
import { layerId, bbox, emptyGeojson } from "../utils";
import { layerId } from "../utils";
import { bbox, emptyGeojson } from "svelte-utils";
import { LineLayer, GeoJSON } from "svelte-maplibre";
let show = true;
Expand Down
3 changes: 2 additions & 1 deletion web/src/common/layers/RenderIntersectionMarkings.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">
import LayerControls from "../LayerControls.svelte";
import { network } from "../store";
import { layerId, emptyGeojson, caseHelper } from "../utils";
import { layerId, caseHelper } from "../utils";
import { emptyGeojson } from "svelte-utils";
import { FillLayer, GeoJSON } from "svelte-maplibre";
let show = true;
Expand Down
3 changes: 2 additions & 1 deletion web/src/common/layers/RenderIntersectionPolygons.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">
import LayerControls from "../LayerControls.svelte";
import { theme, hoveredIntersection, network } from "../store";
import { caseHelper, layerId, emptyGeojson } from "../utils";
import { caseHelper, layerId } from "../utils";
import { emptyGeojson } from "svelte-utils";
import { hoverStateFilter, FillLayer, GeoJSON } from "svelte-maplibre";
export let hoverCursor: string | undefined = undefined;
Expand Down
3 changes: 2 additions & 1 deletion web/src/common/layers/RenderLaneMarkings.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import { layerId, emptyGeojson, caseHelper } from "../utils";
import { layerId, caseHelper } from "../utils";
import { emptyGeojson } from "svelte-utils";
import { FillLayer, GeoJSON } from "svelte-maplibre";
import LayerControls from "../LayerControls.svelte";
import { network } from "../store";
Expand Down
3 changes: 2 additions & 1 deletion web/src/common/layers/RenderLanePolygons.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">
import LayerControls from "../LayerControls.svelte";
import { hoveredLane, network } from "../store";
import { layerId, emptyGeojson, caseHelper } from "../utils";
import { layerId, caseHelper } from "../utils";
import { emptyGeojson } from "svelte-utils";
import { hoverStateFilter, FillLayer, GeoJSON } from "svelte-maplibre";
export let hoverCursor: string | undefined = undefined;
Expand Down
28 changes: 1 addition & 27 deletions web/src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import turfBbox from "@turf/bbox";
import type { Feature, FeatureCollection, GeoJSON, Geometry } from "geojson";
import type { Feature, Geometry } from "geojson";
import { get } from "svelte/store";
import type { ExpressionSpecification } from "maplibre-gl";
import { map as mapStore } from "./store";

// TODO Why can't I find an NPM package to do this?
export function downloadGeneratedFile(filename: string, textInput: string) {
var element = document.createElement("a");
element.setAttribute(
"href",
"data:text/plain;charset=utf-8," + encodeURIComponent(textInput),
);
element.setAttribute("download", filename);
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}

// Helper for https://maplibre.org/maplibre-style-spec/expressions#case based on one property
export function caseHelper(
getKey: string,
Expand Down Expand Up @@ -45,18 +31,6 @@ export function featureStateToggle(
];
}

export function emptyGeojson(): FeatureCollection {
return {
type: "FeatureCollection",
features: [],
};
}

// Suitable for passing to map.fitBounds. Work around https://github.com/Turfjs/turf/issues/1807.
export function bbox(gj: GeoJSON): [number, number, number, number] {
return turfBbox(gj) as [number, number, number, number];
}

// Properties are guaranteed to exist
export type FeatureWithProps<G extends Geometry> = Feature<G> & {
properties: { [name: string]: any };
Expand Down
2 changes: 1 addition & 1 deletion web/src/lane-editor/AllEdits.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { network, importCounter } from "../common";
import { downloadGeneratedFile } from "../common/utils";
import { downloadGeneratedFile } from "svelte-utils";
let editedWays: Set<bigint> = new Set();
Expand Down
3 changes: 2 additions & 1 deletion web/src/lane-editor/App.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">
import { map } from "../common/store";
import { GeoJSON, FillLayer, type LayerClickInfo } from "svelte-maplibre";
import { emptyGeojson, layerId } from "../common/utils";
import { layerId } from "../common/utils";
import { emptyGeojson } from "svelte-utils";
import { network, StreetView } from "../common";
import init from "osm2streets-js";
import { onMount } from "svelte";
Expand Down
4 changes: 2 additions & 2 deletions web/src/street-explorer/RenderBlock.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { caseHelper, layerId, emptyGeojson } from "../common/utils";
import { caseHelper, layerId } from "../common/utils";
import {
hoverStateFilter,
Popup,
Expand All @@ -9,7 +9,7 @@
} from "svelte-maplibre";
import { showingBundles, blockGj } from "./stores";
import { network } from "../common";
import { Legend } from "svelte-utils";
import { emptyGeojson, Legend } from "svelte-utils";
$: active = $blockGj.features.length > 0;
Expand Down
2 changes: 1 addition & 1 deletion web/src/street-explorer/stores.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FeatureCollection } from "geojson";
import { writable, type Writable } from "svelte/store";
import { emptyGeojson } from "../common/utils";
import { emptyGeojson } from "svelte-utils";
import { network } from "../common";

export const blockGj: Writable<FeatureCollection> = writable(emptyGeojson());
Expand Down

0 comments on commit 82c9f22

Please sign in to comment.