From 9f4618c5ef618521ce1ab680d82ed9cc941d10d8 Mon Sep 17 00:00:00 2001 From: Maksymilian Jopek Date: Mon, 12 Dec 2022 23:00:12 +0100 Subject: Added saving and loading Fix same value for each column Add showing errors --- src/lib/Saving.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/lib/Saving.ts (limited to 'src/lib/Saving.ts') diff --git a/src/lib/Saving.ts b/src/lib/Saving.ts new file mode 100644 index 0000000..0d71057 --- /dev/null +++ b/src/lib/Saving.ts @@ -0,0 +1,25 @@ +import type { Grid } from "./Sudoku"; + +export function save(grid: Grid) { + const a = document.createElement("a"); + const fileParts = [JSON.stringify(grid)]; + const blob = new Blob(fileParts, { type: 'application/json' }); // the blob + a.href = URL.createObjectURL(blob); + a.download = "sudoku-save.json"; + a.click(); +} +export function readGridFromFile(f: File): Promise { + return new Promise(res => { + let reader = new FileReader(); + reader.onload = e => { + try { + //@ts-expect-error + const json = JSON.parse(e.target.result); + res(json) + } catch (error) { + res(null) + } + }; + reader.readAsText(f); + }) +} -- cgit v1.3.1