Skip to content

Commit

Permalink
Rename some types and vars
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Jul 13, 2024
1 parent fd1d29b commit 905cdc6
Show file tree
Hide file tree
Showing 21 changed files with 114 additions and 134 deletions.
10 changes: 5 additions & 5 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {
FeatureWithID,
SchemeCollection,
Schemes,
SchemeData,
FeatureProps,
} from "$lib/draw/types";
Expand All @@ -21,7 +21,7 @@ export interface Config<F, S> {

schemeName: (s: SchemeData & S) => string;

backfill: (json: any) => SchemeCollection<F, S>;
backfill: (json: any) => Schemes<F, S>;

initializeEmptyScheme: (scheme: SchemeData) => SchemeData & S;

Expand All @@ -30,20 +30,20 @@ export interface Config<F, S> {
editFeatureForm: null | ComponentType<
SvelteComponent<{
cfg: Config<F, S>;
gjSchemeCollection: Writable<SchemeCollection<F, S>>;
gjSchemes: Writable<Schemes<F, S>>;
id: number;
props: FeatureProps<F>;
}>
>;

editSchemeForm: null | ComponentType<
SvelteComponent<{
gjSchemeCollection: Writable<SchemeCollection<F, S>>;
gjSchemes: Writable<Schemes<F, S>>;
scheme_reference: string;
}>
>;

// Should assign any necessary properties. Runs inside a gjSchemeCollection
// Should assign any necessary properties. Runs inside a gjSchemes
// update; the logic shouldn't look at anything in there.
newPointFeature: (f: FeatureWithID<F, Point>) => void;
newPolygonFeature: (f: FeatureWithID<F, Polygon>) => void;
Expand Down
9 changes: 4 additions & 5 deletions src/lib/draw/EditGeometryMode.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts" generics="F, S">
import type { Feature, LineString, Point, Polygon } from "geojson";
import { mode, pointTool, polygonTool, routeTool } from "$lib/draw/stores";
import type { FeatureWithID, SchemeCollection } from "$lib/draw/types";
import type { FeatureWithID, Schemes } from "$lib/draw/types";
import { ButtonGroup, DefaultButton, SecondaryButton } from "govuk-svelte";
import { type Config } from "$lib/config";
import { onDestroy, onMount } from "svelte";
Expand All @@ -13,7 +13,7 @@
import type { Writable } from "svelte/store";
export let cfg: Config<F, S>;
export let gjSchemeCollection: Writable<SchemeCollection<F, S>>;
export let gjSchemes: Writable<Schemes<F, S>>;
export let id: number;
let name = "";
Expand All @@ -24,7 +24,7 @@
onMount(() => {
let maybeFeature: FeatureWithID<F> | null = null;
gjSchemeCollection.update((gj) => {
gjSchemes.update((gj) => {
maybeFeature = gj.features.find((f) => f.id == id)!;
// Hide it from the regular drawing while we're editing
maybeFeature.properties.hide_while_editing = true;
Expand Down Expand Up @@ -76,7 +76,7 @@
$polygonTool?.stop();
$polygonTool?.clearEventListeners();
gjSchemeCollection.update((gj) => {
gjSchemes.update((gj) => {
let featureToBeUpdated = gj.features.find((f) => f.id == id)!;
// Show the feature again
Expand Down Expand Up @@ -120,7 +120,6 @@
// Copy properties that may come from routeTool. Not all tools or cases
// will produce all of these.
// TODO We're depending on implementation details here and knowing what to copy...
if (source.properties.length_meters) {
destination.properties.length_meters = source.properties.length_meters;
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/draw/HoverLayer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
layerId,
} from "$lib/maplibre";
import { CircleLayer, GeoJSON, LineLayer } from "svelte-maplibre";
import type { SchemeCollection } from "$lib/draw/types";
import type { Schemes } from "$lib/draw/types";
import type { Writable } from "svelte/store";
import type { Config } from "$lib/config";
export let cfg: Config<F, S>;
export let gjSchemeCollection: Writable<SchemeCollection<F, S>>;
export let gjSchemes: Writable<Schemes<F, S>>;
// Use a layer that only ever has zero or one features for hovering.
$: gj =
$sidebarHover == null
? emptyGeojson()
: $gjSchemeCollection.features.find((f) => f.id == $sidebarHover)!;
: $gjSchemes.features.find((f) => f.id == $sidebarHover)!;
</script>

<GeoJSON data={gj}>
Expand Down
12 changes: 6 additions & 6 deletions src/lib/draw/InterventionLayer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
Popup,
type LayerClickInfo,
} from "svelte-maplibre";
import type { FeatureWithID, SchemeCollection } from "$lib/draw/types";
import type { FeatureWithID, Schemes } from "$lib/draw/types";
import type { Writable } from "svelte/store";
export let cfg: Config<F, S>;
export let gjSchemeCollection: Writable<SchemeCollection<F, S>>;
export let gjSchemes: Writable<Schemes<F, S>>;
$: gj = addLineStringEndpoints($gjSchemeCollection);
$: gj = addLineStringEndpoints($gjSchemes);
// TODO Maybe have a separate component for different modes.
const hideWhileEditing: ExpressionSpecification = [
Expand Down Expand Up @@ -79,9 +79,9 @@
}
}
$: colorInterventions = colorByScheme($gjSchemeCollection);
$: colorInterventions = colorByScheme($gjSchemes);
function colorByScheme<F, S>(
gj: SchemeCollection<F, S>,
gj: Schemes<F, S>,
): DataDrivenPropertyValueSpecification<string> {
return constructMatchExpression(
["get", "scheme_reference"],
Expand Down Expand Up @@ -112,7 +112,7 @@
let feature = features[0] as FeatureWithID<F>;
let featureName = cfg.interventionName(feature);
let schemeName = cfg.schemeName(
$gjSchemeCollection.schemes[feature.properties.scheme_reference],
$gjSchemes.schemes[feature.properties.scheme_reference],
);
return `${featureName} (${schemeName})`;
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/draw/Toolbox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import splitRouteIcon from "$lib/assets/split_route.svg";
import streetViewIcon from "$lib/assets/street_view.svg";
import HoverLayer from "./HoverLayer.svelte";
import type { SchemeCollection } from "$lib/draw/types";
import type { Schemes } from "$lib/draw/types";
import type { Writable } from "svelte/store";
import type { Config } from "$lib/config";
export let cfg: Config<F, S>;
export let gjSchemeCollection: Writable<SchemeCollection<F, S>>;
export let gjSchemes: Writable<Schemes<F, S>>;
</script>

<HoverLayer {cfg} {gjSchemeCollection} />
<HoverLayer {cfg} {gjSchemes} />

<div class="top govuk-prose">
<SecondaryButton
Expand Down
6 changes: 3 additions & 3 deletions src/lib/draw/point/PointMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import PointControls from "./PointControls.svelte";
import { type Config } from "$lib/config";
import type { FeatureWithID } from "$lib/draw/types";
import type { SchemeCollection } from "$lib/draw/types";
import type { Schemes } from "$lib/draw/types";
import type { Writable } from "svelte/store";
export let cfg: Config<F, S>;
export let gjSchemeCollection: Writable<SchemeCollection<F, S>>;
export let gjSchemes: Writable<Schemes<F, S>>;
onMount(() => {
$pointTool!.start();
Expand All @@ -30,7 +30,7 @@
function onSuccess(feature: Feature<Point>) {
feature.properties ||= {};
let f = feature as FeatureWithID<F, Point>;
gjSchemeCollection.update((gj) => {
gjSchemes.update((gj) => {
f.id = newFeatureId(gj);
f.properties.scheme_reference = getArbitrarySchemeRef(gj);
cfg.newPointFeature(f);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/draw/polygon/PolygonMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import PolygonControls from "./PolygonControls.svelte";
import { type Config } from "$lib/config";
import type { FeatureWithID } from "$lib/draw/types";
import type { SchemeCollection } from "$lib/draw/types";
import type { Schemes } from "$lib/draw/types";
import type { Writable } from "svelte/store";
export let cfg: Config<F, S>;
export let gjSchemeCollection: Writable<SchemeCollection<F, S>>;
export let gjSchemes: Writable<Schemes<F, S>>;
onMount(() => {
$polygonTool!.startNew();
Expand All @@ -30,7 +30,7 @@
function onSuccess(feature: Feature<Polygon>) {
feature.properties ||= {};
let f = feature as FeatureWithID<F, Polygon>;
gjSchemeCollection.update((gj) => {
gjSchemes.update((gj) => {
f.id = newFeatureId(gj);
f.properties.scheme_reference = getArbitrarySchemeRef(gj);
cfg.newPolygonFeature(f);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/draw/route/RouteMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import { onDestroy, onMount } from "svelte";
import RouteControls from "./RouteControls.svelte";
import { type Config } from "$lib/config";
import type { FeatureWithID, SchemeCollection } from "$lib/draw/types";
import type { FeatureWithID, Schemes } from "$lib/draw/types";
import type { Writable } from "svelte/store";
export let cfg: Config<F, S>;
export let gjSchemeCollection: Writable<SchemeCollection<F, S>>;
export let gjSchemes: Writable<Schemes<F, S>>;
onMount(() => {
$routeTool!.startRoute();
Expand All @@ -28,7 +28,7 @@
function onSuccess(feature: Feature<LineString | Polygon>) {
let f = feature as FeatureWithID<F, LineString>;
gjSchemeCollection.update((gj) => {
gjSchemes.update((gj) => {
f.id = newFeatureId(gj);
f.properties.scheme_reference = getArbitrarySchemeRef(gj);
cfg.newLineStringFeature(f);
Expand Down
10 changes: 5 additions & 5 deletions src/lib/draw/route/SplitRouteMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
import { onDestroy, onMount } from "svelte";
import { CircleLayer, GeoJSON, MapEvents } from "svelte-maplibre";
import splitIcon from "$lib/assets/split_route.svg";
import type { FeatureWithID, SchemeCollection } from "$lib/draw/types";
import type { FeatureWithID, Schemes } from "$lib/draw/types";
import type { Writable } from "svelte/store";
export let cfg: Config<F, S>;
export let gjSchemeCollection: Writable<SchemeCollection<F, S>>;
export let gjSchemes: Writable<Schemes<F, S>>;
const circleRadiusPixels = 10;
const snapDistancePixels = 30;
Expand Down Expand Up @@ -65,7 +65,7 @@
// Are we snapped to anything?
let candidates: [number, Position, number][] = [];
for (let [index, f] of $gjSchemeCollection.features.entries()) {
for (let [index, f] of $gjSchemes.features.entries()) {
if (f.geometry.type == "LineString") {
let snapped = nearestPointOnLine(f.geometry, cursor, {
units: "kilometers",
Expand Down Expand Up @@ -98,7 +98,7 @@
}
let result = splitRoute(
$gjSchemeCollection.features[snappedIndex] as unknown as Feature<
$gjSchemes.features[snappedIndex] as unknown as Feature<
LineString,
RouteProps
>,
Expand All @@ -107,7 +107,7 @@
if (result != null) {
let [piece1, piece2] = result;
gjSchemeCollection.update((gj) => {
gjSchemes.update((gj) => {
// Keep the old ID for one, assign a new ID to the other
piece1.id = gj.features[snappedIndex!].id;
piece2.id = newFeatureId(gj);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/draw/snap_polygon/SnapPolygonMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import SnapPolygonControls from "./SnapPolygonControls.svelte";
import { type Config } from "$lib/config";
import type { FeatureWithID } from "$lib/draw/types";
import type { SchemeCollection } from "$lib/draw/types";
import type { Schemes } from "$lib/draw/types";
import type { Writable } from "svelte/store";
export let cfg: Config<F, S>;
export let gjSchemeCollection: Writable<SchemeCollection<F, S>>;
export let gjSchemes: Writable<Schemes<F, S>>;
onMount(() => {
$routeTool!.startArea();
Expand All @@ -30,7 +30,7 @@
function onSuccess(feature: Feature<LineString | Polygon>) {
// We did startArea, so we know it's a Polygon
let f = feature as FeatureWithID<F, Polygon>;
gjSchemeCollection.update((gj) => {
gjSchemes.update((gj) => {
f.id = newFeatureId(gj);
f.properties.scheme_reference = getArbitrarySchemeRef(gj);
cfg.newPolygonFeature(f);
Expand Down
23 changes: 8 additions & 15 deletions src/lib/draw/stores.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Config } from "$lib/config";
import { writable, type Writable } from "svelte/store";
import type { Mode, SchemeCollection } from "./types";
import type { Mode, Schemes } from "./types";
import { PointTool } from "./point/point_tool";
import { PolygonTool } from "maplibre-draw-polygon";
import { RouteTool } from "route-snapper-ts";
Expand Down Expand Up @@ -37,7 +37,7 @@ export const mode: Writable<Mode> = writable({ mode: "list" });
// NOTE! If you call this twice in a row in a `gjScheme.update` transaction
// without adding one of the new features, then this'll return the same ID
// twice!
export function newFeatureId<F, S>(gj: SchemeCollection<F, S>): number {
export function newFeatureId<F, S>(gj: Schemes<F, S>): number {
let ids = new Set();
for (let f of gj.features) {
ids.add(f.id);
Expand All @@ -51,11 +51,11 @@ export function newFeatureId<F, S>(gj: SchemeCollection<F, S>): number {
}

export function deleteIntervention<F, S>(
gjSchemeCollection: Writable<SchemeCollection<F, S>>,
gjSchemes: Writable<Schemes<F, S>>,
id: number,
) {
console.log(`Deleting intervention ${id}`);
gjSchemeCollection.update((gj) => {
gjSchemes.update((gj) => {
gj.features = gj.features.filter((f) => f.id != id);
return gj;
});
Expand All @@ -65,10 +65,8 @@ export function deleteIntervention<F, S>(

// When creating a new feature, arbitrarily assign it to the first scheme. The
// user can change it later.
export function getArbitrarySchemeRef<F, S>(
schemeCollection: SchemeCollection<F, S>,
): string {
return Object.values(schemeCollection.schemes)[0].scheme_reference;
export function getArbitrarySchemeRef<F, S>(gjSchemes: Schemes<F, S>): string {
return Object.values(gjSchemes.schemes)[0].scheme_reference;
}

// Per https://datatracker.ietf.org/doc/html/rfc7946#section-11.2, 6 decimal
Expand Down Expand Up @@ -105,9 +103,7 @@ function loadUserSettings(): UserSettings {
return settings as UserSettings;
}

export function emptyCollection<F, S>(
cfg: Config<F, S>,
): SchemeCollection<F, S> {
export function emptySchemes<F, S>(cfg: Config<F, S>): Schemes<F, S> {
let gj = {
type: "FeatureCollection" as const,
features: [],
Expand All @@ -117,10 +113,7 @@ export function emptyCollection<F, S>(
return gj;
}

export function addEmptyScheme<F, S>(
cfg: Config<F, S>,
gj: SchemeCollection<F, S>,
) {
export function addEmptyScheme<F, S>(cfg: Config<F, S>, gj: Schemes<F, S>) {
let scheme_reference = uuidv4();
let scheme = cfg.initializeEmptyScheme({
scheme_reference,
Expand Down
Loading

0 comments on commit 905cdc6

Please sign in to comment.