Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset map viewport when returning to Choose project and opening a pro… #211

Merged
merged 1 commit into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions web/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
<script context="module" lang="ts">
export type AppFocus = "global" | "cnt";
</script>

<script lang="ts">
import onewayArrowUrl from "../assets/arrow.png?url";
import logo from "../assets/logo.svg?url";
Expand All @@ -12,7 +8,7 @@
import initLtn from "backend";
import type { LngLatBoundsLike, Map, StyleSpecification } from "maplibre-gl";
import { init as initRouteSnapper } from "route-snapper-ts";
import { onMount, setContext } from "svelte";
import { onMount } from "svelte";
import {
FillLayer,
GeoJSON,
Expand Down Expand Up @@ -42,6 +38,7 @@
import SetBoundaryMode from "./SetBoundaryMode.svelte";
import Settings from "./Settings.svelte";
import {
appFocus as appFocusStore,
backend,
currentProjectKey,
map as mapStore,
Expand All @@ -55,6 +52,9 @@
import TitleMode from "./title/TitleMode.svelte";
import ViewShortcutsMode from "./ViewShortcutsMode.svelte";

export let appFocus: "global" | "cnt" = "global";
appFocusStore.set(appFocus);

let wasmReady = false;
onMount(async () => {
await initLtn();
Expand Down Expand Up @@ -96,9 +96,6 @@
mapDiv.appendChild($mapContents);
}

export let appFocus: AppFocus = "global";
setContext("appFocus", appFocus);

let initialBounds: LngLatBoundsLike | undefined = undefined;
if (appFocus == "cnt") {
initialBounds = [-8.943, 54.631, -0.901, 59.489];
Expand Down
5 changes: 2 additions & 3 deletions web/src/AutoBoundariesMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
currentProjectKey,
editPerimeterRoads,
mode,
returnToChooseProject,
} from "./stores";
import type { GeneratedBoundaryFeature } from "./wasm";

Expand Down Expand Up @@ -186,9 +187,7 @@
<nav aria-label="breadcrumb">
<ul>
<li>
<Link on:click={() => ($mode = { mode: "title", firstLoad: false })}>
Choose project
</Link>
<Link on:click={returnToChooseProject}>Choose project</Link>
</li>
<li>
<Link on:click={() => ($mode = { mode: "pick-neighbourhood" })}>
Expand Down
6 changes: 2 additions & 4 deletions web/src/DebugDemandMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import BackButton from "./BackButton.svelte";
import { layerId, Link } from "./common";
import { demandColorScale } from "./common/colors";
import { backend, mode } from "./stores";
import { backend, mode, returnToChooseProject } from "./stores";
import type { ZoneDemandProps } from "./wasm";

let gj = emptyGeojson() as FeatureCollection<MultiPolygon, ZoneDemandProps>;
Expand Down Expand Up @@ -81,9 +81,7 @@
<nav aria-label="breadcrumb">
<ul>
<li>
<Link on:click={() => ($mode = { mode: "title", firstLoad: false })}>
Choose project
</Link>
<Link on:click={returnToChooseProject}>Choose project</Link>
</li>
<li>
<Link on:click={() => ($mode = { mode: "pick-neighbourhood" })}>
Expand Down
6 changes: 2 additions & 4 deletions web/src/DebugIntersectionsMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import { SplitComponent } from "svelte-utils/top_bar_layout";
import BackButton from "./BackButton.svelte";
import { layerId, Link, PrevNext } from "./common";
import { backend, mode } from "./stores";
import { backend, mode, returnToChooseProject } from "./stores";

let currentOsm: string | null = null;
let movements = emptyGeojson();
Expand All @@ -29,9 +29,7 @@
<nav aria-label="breadcrumb">
<ul>
<li>
<Link on:click={() => ($mode = { mode: "title", firstLoad: false })}>
Choose project
</Link>
<Link on:click={returnToChooseProject}>Choose project</Link>
</li>
<li>
<Link on:click={() => ($mode = { mode: "pick-neighbourhood" })}>
Expand Down
6 changes: 2 additions & 4 deletions web/src/DebugNeighbourhoodMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
RenderNeighbourhood,
} from "./layers";
import ModalFilterLayer from "./layers/ModalFilterLayer.svelte";
import { backend, mode } from "./stores";
import { backend, mode, returnToChooseProject } from "./stores";

let intersection: DebugIntersection | null = null;
type DebugIntersection = {
Expand All @@ -45,9 +45,7 @@
<nav aria-label="breadcrumb">
<ul>
<li>
<Link on:click={() => ($mode = { mode: "title", firstLoad: false })}>
Choose project
</Link>
<Link on:click={returnToChooseProject}>Choose project</Link>
</li>
<li>
<Link on:click={() => ($mode = { mode: "pick-neighbourhood" })}>
Expand Down
6 changes: 2 additions & 4 deletions web/src/ImpactDetailMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { SplitComponent } from "svelte-utils/top_bar_layout";
import { DotMarker, gjPosition, layerId, Link, PrevNext } from "./common";
import { ModalFilterLayer } from "./layers";
import { backend, mode } from "./stores";
import { backend, mode, returnToChooseProject } from "./stores";

export let road: Feature;

Expand All @@ -28,9 +28,7 @@
<nav aria-label="breadcrumb">
<ul>
<li>
<Link on:click={() => ($mode = { mode: "title", firstLoad: false })}>
Choose project
</Link>
<Link on:click={returnToChooseProject}>Choose project</Link>
</li>
<li>
<Link on:click={() => ($mode = { mode: "pick-neighbourhood" })}>
Expand Down
5 changes: 2 additions & 3 deletions web/src/ImpactOneDestinationMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
backend,
mode,
one_destination,
returnToChooseProject,
route_pt_a,
route_pt_b,
} from "./stores";
Expand Down Expand Up @@ -65,9 +66,7 @@
<nav aria-label="breadcrumb">
<ul>
<li>
<Link on:click={() => ($mode = { mode: "title", firstLoad: false })}>
Choose project
</Link>
<Link on:click={returnToChooseProject}>Choose project</Link>
</li>
<li>
<Link on:click={() => ($mode = { mode: "pick-neighbourhood" })}>
Expand Down
5 changes: 2 additions & 3 deletions web/src/PickNeighbourhoodMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
currentProjectKey,
editPerimeterRoads,
mode,
returnToChooseProject,
} from "./stores";
import type { NeighbourhoodBoundaryFeature } from "./wasm";

Expand Down Expand Up @@ -163,9 +164,7 @@
<nav aria-label="breadcrumb">
<ul>
<li>
<Link on:click={() => ($mode = { mode: "title", firstLoad: false })}>
Choose project
</Link>
<Link on:click={returnToChooseProject}>Choose project</Link>
</li>
<li>
Pick neighbourhood
Expand Down
6 changes: 2 additions & 4 deletions web/src/PredictImpactMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import BackButton from "./BackButton.svelte";
import { layerId, Link } from "./common";
import { ModalFilterLayer } from "./layers";
import { backend, mode } from "./stores";
import { backend, mode, returnToChooseProject } from "./stores";

// Based partly on https://colorbrewer2.org/#type=diverging&scheme=RdYlGn&n=5
// The middle color white doesn't matter; the source data will filter out unchanged roads
Expand All @@ -27,9 +27,7 @@
<nav aria-label="breadcrumb">
<ul>
<li>
<Link on:click={() => ($mode = { mode: "title", firstLoad: false })}>
Choose project
</Link>
<Link on:click={returnToChooseProject}>Choose project</Link>
</li>
<li>
<Link on:click={() => ($mode = { mode: "pick-neighbourhood" })}>
Expand Down
5 changes: 2 additions & 3 deletions web/src/RouteMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
backend,
mainRoadPenalty,
mode,
returnToChooseProject,
route_pt_a,
route_pt_b,
} from "./stores";
Expand All @@ -43,9 +44,7 @@
<nav aria-label="breadcrumb">
<ul>
<li>
<Link on:click={() => ($mode = { mode: "title", firstLoad: false })}>
Choose project
</Link>
<Link on:click={returnToChooseProject}>Choose project</Link>
</li>
<li>
<Link on:click={() => ($mode = { mode: "pick-neighbourhood" })}>
Expand Down
13 changes: 9 additions & 4 deletions web/src/SetBoundaryMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
import { gjPosition, Link } from "./common";
import AreaControls from "./common/draw_area/AreaControls.svelte";
import { calculateArea, waypoints } from "./common/draw_area/stores";
import { autosave, backend, editPerimeterRoads, map, mode } from "./stores";
import {
autosave,
backend,
editPerimeterRoads,
map,
mode,
returnToChooseProject,
} from "./stores";

export let name: string;
export let existing: Feature<Polygon, AreaProps> | null;
Expand Down Expand Up @@ -70,9 +77,7 @@
<nav aria-label="breadcrumb">
<ul>
<li>
<Link on:click={() => ($mode = { mode: "title", firstLoad: false })}>
Choose project
</Link>
<Link on:click={returnToChooseProject}>Choose project</Link>
</li>
<li>
<Link on:click={() => ($mode = { mode: "pick-neighbourhood" })}>
Expand Down
6 changes: 2 additions & 4 deletions web/src/ViewShortcutsMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
OneWayLayer,
RenderNeighbourhood,
} from "./layers";
import { backend, mode } from "./stores";
import { backend, mode, returnToChooseProject } from "./stores";
import type { AllShortcuts } from "./wasm";

type State =
Expand Down Expand Up @@ -55,9 +55,7 @@
<nav aria-label="breadcrumb">
<ul>
<li>
<Link on:click={() => ($mode = { mode: "title", firstLoad: false })}>
Choose project
</Link>
<Link on:click={returnToChooseProject}>Choose project</Link>
</li>
<li>
<Link on:click={() => ($mode = { mode: "pick-neighbourhood" })}>
Expand Down
5 changes: 2 additions & 3 deletions web/src/edit/NeighbourhoodMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
map,
mode,
mutationCounter,
returnToChooseProject,
roadStyle,
} from "../stores";
import type {
Expand Down Expand Up @@ -249,9 +250,7 @@
<nav aria-label="breadcrumb">
<ul>
<li>
<Link on:click={() => ($mode = { mode: "title", firstLoad: false })}>
Choose project
</Link>
<Link on:click={returnToChooseProject}>Choose project</Link>
</li>
<li>
<Link on:click={() => ($mode = { mode: "pick-neighbourhood" })}>
Expand Down
14 changes: 13 additions & 1 deletion web/src/stores.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Feature, Polygon } from "geojson";
import { LngLat, type Map } from "maplibre-gl";
import { LngLat, type LngLatBoundsLike, type Map } from "maplibre-gl";
import { type AreaProps } from "route-snapper-ts";
import { get, writable, type Writable } from "svelte/store";
import type { Backend } from "./wasm";
Expand Down Expand Up @@ -65,6 +65,8 @@ export let currentProjectKey: Writable<string> = writable("");
// False until user activates
export let showAbout: Writable<boolean> = writable(false);

export let appFocus: Writable<"global" | "cnt"> = writable("global");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context is hard to access outside of a component, so I moved it to a store. If you prefer, we could instead try a ChooseProjectLink component, so that context is visible

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A store seems fine!


export let backend: Writable<Backend | null> = writable(null);
export let route_pt_a: Writable<LngLat> = writable(new LngLat(0, 0));
export let route_pt_b: Writable<LngLat> = writable(new LngLat(0, 0));
Expand Down Expand Up @@ -97,3 +99,13 @@ export let useLocalVite: Writable<boolean> = writable(false);
export function assetUrl(path: string): string {
return get(useLocalVite) ? `/${path}` : `https://assets.od2net.org/${path}`;
}

export function returnToChooseProject() {
mode.set({ mode: "title", firstLoad: false });

let bounds = [-180, -90, 180, 90] as LngLatBoundsLike;
if (get(appFocus) == "cnt") {
bounds = [-8.943, 54.631, -0.901, 59.489];
}
get(map)?.fitBounds(bounds, { duration: 500 });
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

500 is maybe a little fast, but anything too long gets annoying. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels maybe a little fast (definitely not too slow!), but not unreasonable. Let's go with it and we can always tweak it later.

}
6 changes: 2 additions & 4 deletions web/src/title/NewProjectMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
backend,
currentProjectKey,
map,
mode,
returnToChooseProject,
} from "../stores";
import { Backend } from "../wasm";
import { afterProjectLoaded, loadFromLocalStorage } from "./loader";
Expand Down Expand Up @@ -74,9 +74,7 @@
<nav aria-label="breadcrumb">
<ul>
<li>
<Link on:click={() => ($mode = { mode: "title", firstLoad: false })}>
Choose project
</Link>
<Link on:click={returnToChooseProject}>Choose project</Link>
</li>
<li>New project</li>
</ul>
Expand Down
9 changes: 3 additions & 6 deletions web/src/title/TitleMode.svelte
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
<script lang="ts">
import { Pencil, Trash2 } from "lucide-svelte";
import { getContext } from "svelte";
import { Loading } from "svelte-utils";
import { SplitComponent } from "svelte-utils/top_bar_layout";
import type { AppFocus } from "../App.svelte";
import CntChooseArea from "../CntChooseArea.svelte";
import { Link } from "../common";
import { routeTool } from "../common/draw_area/stores";
import { backend, currentProjectKey, map, mode } from "../stores";
import { appFocus, backend, currentProjectKey, map, mode } from "../stores";
import { loadFromLocalStorage } from "./loader";

export let wasmReady: boolean;
export let firstLoad: boolean;

let loading = "";
let appFocus: AppFocus = getContext("appFocus");

// When other modes reset here, they can't clear state without a race condition
{
Expand Down Expand Up @@ -123,7 +120,7 @@
</div>
<div slot="sidebar">
{#if $map && wasmReady}
{#if appFocus == "global"}
{#if $appFocus == "global"}
<div>
<Link on:click={() => ($mode = { mode: "new-project" })}>
New project
Expand Down Expand Up @@ -166,7 +163,7 @@
Load a project from a file
<input bind:this={fileInput} on:change={loadFile} type="file" />
</label>
{:else if appFocus == "cnt"}
{:else if $appFocus == "cnt"}
<CntChooseArea {loadProject} bind:activityIndicatorText={loading} />
{/if}
{:else}
Expand Down
2 changes: 1 addition & 1 deletion web/src/title/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export function afterProjectLoaded() {
writable(0),
),
);
get(map)!.fitBounds(get(backend)!.getBounds(), { animate: false });
get(map)!.fitBounds(get(backend)!.getBounds(), { duration: 500 });
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the other way: when you open a project, animate the zoom in a bit -- again, quickly.

This zoom happens after the load screen. I wanted to do this during the load screen, but we'd have to mimic the backend getBounds, which isn't too clear to do just from the GJ savefile. We could start storing a bbox in there, but I'm not sure it's worth the hassle.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing it during loading rather than after would be nice, but I think that could be a low priority followup task.

route_pt_a.set(randomPoint());
route_pt_b.set(randomPoint());
one_destination.set(randomPoint());
Expand Down