aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/Saving.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/Saving.ts')
-rw-r--r--src/lib/Saving.ts25
1 files changed, 25 insertions, 0 deletions
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<string[][] | null> {
+ 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);
+ })
+}