Skip to content
Open
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
85 changes: 83 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"govuk-svelte": "github:acteng/govuk-svelte",
"humanize-string": "^3.0.0",
"js-cookie": "^3.0.5",
"jszip": "^3.10.1",
"maplibre-gl": "^4.7.1",
"scheme-sketcher-lib": "github:acteng/scheme-sketcher-lib",
"svelte": "^4.2.10",
Expand Down
66 changes: 66 additions & 0 deletions src/lib/common/files.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { findSmallestAuthority, type AuthorityBoundaries } from "boundaries";
import JSZip from "jszip";
import type { Schema, Schemes } from "types";

// Returns the local storage key for a file
Expand Down Expand Up @@ -130,6 +131,37 @@ export function exportFile(authority: string, filename: string, gj: Schemes) {
);
}

export function checkThenExportAll() {
let today = getDateString();
let lastBackup =
window.localStorage.getItem("sketches-last-backup-prompt") ?? "";
if (today != lastBackup) {
if (
window.confirm(`Would you like to download a backup copy of your files?`)
) {
exportAll();
}
}
window.localStorage.setItem("sketches-last-backup-prompt", getDateString());
}

// Downloads all sketches as .zip
async function exportAll() {
let name = `scheme_sketch_backup_${getDateString()}`;
let zip = new JSZip();
let folder = zip.folder(name)!;

for (let key of getFileKeys()) {
folder.file(
`${key.replaceAll("/", "-")}.json`,
window.localStorage.getItem(key)!,
);
}

let bytes = await zip.generateAsync({ type: "arraybuffer" });
downloadBinaryFile(bytes, `${name}.zip`);
}

export function detectSchema(gj: any): Schema {
// Blindly assume the input is valid, and let the try/catch handle otherwise
try {
Expand Down Expand Up @@ -207,3 +239,37 @@ function stripPrefix(value: string, prefix: string): string {
function stripSuffix(value: string, suffix: string): string {
return value.endsWith(suffix) ? value.slice(0, -suffix.length) : value;
}

function getFileKeys(): string[] {
let results: string[] = [];
for (let i = 0; i < window.localStorage.length; i++) {
let key = window.localStorage.key(i)!;
if (key.startsWith("sketch")) {
results.push(key);
}
}
return results;
}

function getDateString(): string {
let today = new Date();
let day = today.getDate().toString().padStart(2, "0");
let month = (today.getMonth() + 1).toString().padStart(2, "0");
return `${day}_${month}_${today.getFullYear()}`;
}

function downloadBinaryFile(bytes: ArrayBuffer, filename: string) {
let blob = new Blob([bytes], { type: "application/octet-stream" });
let url = URL.createObjectURL(blob);

let link = document.createElement("a");
link.href = url;
link.download = filename;
link.style.display = "none";

document.body.appendChild(link);
link.click();

document.body.removeChild(link);
URL.revokeObjectURL(url);
}
2 changes: 2 additions & 0 deletions src/pages/ChooseArea.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Popup,
} from "lib/common";
import {
checkThenExportAll,
countFilesPerAuthority,
importFile,
importOldFiles,
Expand Down Expand Up @@ -63,6 +64,7 @@
}

filesPerAuthority = countFilesPerAuthority();
checkThenExportAll();
});

function onClick(e: CustomEvent<LayerClickInfo>) {
Expand Down
3 changes: 3 additions & 0 deletions src/pages/ManageFiles.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Header,
} from "lib/common";
import {
checkThenExportAll,
downloadGeneratedFile,
exportFile,
getEditUrl,
Expand Down Expand Up @@ -52,6 +53,8 @@
) {
window.location.href = `choose_area.html?schema=${$schemaStore}&error=Authority name not found: ${authority}`;
}

checkThenExportAll();
});

function renameFile(filename: string) {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/SketchSchemes.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
MapLibreMap,
} from "lib/common";
import ExternalLink from "lib/common/ExternalLink.svelte";
import { getKey } from "lib/common/files";
import { checkThenExportAll, getKey } from "lib/common/files";
import { cfg } from "lib/sketch/config";
import FileManagement from "lib/sketch/FileManagement.svelte";
import { map as sketchMapStore } from "scheme-sketcher-lib/config";
Expand Down Expand Up @@ -61,6 +61,7 @@
initAll();

boundaryGeojson = await loadAuthorityBoundary();
checkThenExportAll();
});

async function loadAuthorityBoundary(): Promise<AuthorityBoundaries> {
Expand Down
10 changes: 9 additions & 1 deletion tests/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ export async function resetSketch(
page: Page,
schema: string = "v1",
): Promise<string> {
page.on("dialog", (dialog) => {
if (dialog.message() === "What do you want to name your new file?"
|| dialog.message().includes("Really delete")) {
dialog.accept(filename);
} else {
dialog.dismiss();
}
});
await page.goto(`/files.html?authority=LAD_Adur&schema=${schema}`);

let filename = uuidv4();
page.on("dialog", (dialog) => dialog.accept(filename));

await page.getByRole("button", { name: "Create new file" }).click();
await expect(page).toHaveURL(/.*scheme.html\?authority=LAD_Adur/);

Expand Down