Skip to content

Commit f9a94b9

Browse files
committed
Let users import a saved project from the CNT page too
1 parent 1bb3a0d commit f9a94b9

File tree

3 files changed

+46
-34
lines changed

3 files changed

+46
-34
lines changed

web/src/CntChooseArea.svelte

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import boundariesUrl from "../assets/cnt_boundaries.geojson?url";
1313
import { Link } from "./common";
1414
import { createNewProject } from "./title/loader";
15+
import LoadSavedProject from "./title/LoadSavedProject.svelte";
1516
1617
export let loadProject: (project: string) => void;
1718
export let activityIndicatorText: string;
@@ -116,6 +117,8 @@
116117
{/each}
117118
</div>
118119

120+
<LoadSavedProject bind:loading={activityIndicatorText} />
121+
119122
<GeoJSON data={gj} generateId>
120123
<FillLayer
121124
paint={{

web/src/title/LoadSavedProject.svelte

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<script lang="ts">
2+
import { stripPrefix, stripSuffix } from "../common";
3+
import { loadFromLocalStorage } from "./loader";
4+
5+
export let loading: string;
6+
7+
let fileInput: HTMLInputElement;
8+
9+
async function loadFile(e: Event) {
10+
let filename = fileInput.files![0].name;
11+
loading = `Loading from file ${filename}`;
12+
13+
let contents = await fileInput.files![0].text();
14+
let gj = JSON.parse(contents);
15+
// Is this a CNT project or a regular one?
16+
let key = "";
17+
if (gj.study_area_name && gj.study_area_name.startsWith("LAD_")) {
18+
let kind = "ltn_cnt";
19+
// Parse the project name from the filename, best effort. The user may
20+
// have renamed the file.
21+
let projectName = stripSuffix(
22+
stripPrefix(filename, `${kind}_${gj.study_area_name}_`),
23+
".geojson",
24+
);
25+
key = `${kind}/${gj.study_area_name}/${projectName}`;
26+
} else {
27+
let projectName = stripSuffix(stripPrefix(filename, "ltn_"), ".geojson");
28+
key = `ltn_${projectName}`;
29+
}
30+
// TODO Be careful with overwriting files
31+
window.localStorage.setItem(key, contents);
32+
await loadFromLocalStorage(key);
33+
loading = "";
34+
}
35+
</script>
36+
37+
<label>
38+
Load a project from a file
39+
<input bind:this={fileInput} on:change={loadFile} type="file" />
40+
</label>

web/src/title/TitleMode.svelte

+3-34
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import { Loading } from "svelte-utils";
44
import { SplitComponent } from "svelte-utils/top_bar_layout";
55
import CntChooseArea from "../CntChooseArea.svelte";
6-
import { Link, stripPrefix, stripSuffix } from "../common";
6+
import { Link } from "../common";
77
import { routeTool } from "../common/draw_area/stores";
88
import { appFocus, backend, currentProjectKey, map, mode } from "../stores";
99
import { loadFromLocalStorage } from "./loader";
10+
import LoadSavedProject from "./LoadSavedProject.svelte";
1011
1112
export let wasmReady: boolean;
1213
export let firstLoad: boolean;
@@ -68,35 +69,6 @@
6869
return out;
6970
}
7071
71-
let fileInput: HTMLInputElement;
72-
async function loadFile(e: Event) {
73-
let filename = fileInput.files![0].name;
74-
loading = `Loading from file ${filename}`;
75-
76-
let contents = await fileInput.files![0].text();
77-
let gj = JSON.parse(contents);
78-
// Is this a CNT project or a regular one?
79-
let key = "";
80-
if (gj.study_area_name && gj.study_area_name.startsWith("LAD_")) {
81-
let kind = "ltn_cnt";
82-
// Parse the project name from the filename, best effort. The user may
83-
// have renamed the file.
84-
let projectName = stripSuffix(
85-
stripPrefix(filename, `${kind}_${gj.study_area_name}_`),
86-
".geojson",
87-
);
88-
key = `${kind}/${gj.study_area_name}/${projectName}`;
89-
} else {
90-
let projectName = stripSuffix(stripPrefix(filename, "ltn_"), ".geojson");
91-
key = `ltn_${projectName}`;
92-
}
93-
// TODO Be careful with overwriting files
94-
window.localStorage.setItem(key, contents);
95-
projectList = getProjectList();
96-
await loadFromLocalStorage(key);
97-
loading = "";
98-
}
99-
10072
function deleteProject(key: string) {
10173
if (window.confirm(`Really delete project ${key}? You can't undo this.`)) {
10274
window.localStorage.removeItem(key);
@@ -177,10 +149,7 @@
177149
{/each}
178150
</ul>
179151

180-
<label>
181-
Load a project from a file
182-
<input bind:this={fileInput} on:change={loadFile} type="file" />
183-
</label>
152+
<LoadSavedProject bind:loading />
184153
{:else if $appFocus == "cnt"}
185154
<CntChooseArea {loadProject} bind:activityIndicatorText={loading} />
186155
{/if}

0 commit comments

Comments
 (0)