aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/Saving.ts
blob: 0d71057dfa000c7d266097733f3824c6a79e1a71 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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);
  })
}