| 2 | import { INITIAL_COMPONENTS } from '~core/models/components' |
| 3 | |
| 4 | export async function loadFromJSON() { |
| 5 | const blob = await fileOpen({ |
| 6 | extensions: ['json'], |
| 7 | mimeTypes: ['application/json'], |
| 8 | }) |
| 9 | |
| 10 | const contents: string = await new Promise(resolve => { |
| 11 | const reader = new FileReader() |
| 12 | reader.readAsText(blob, 'utf8') |
| 13 | reader.onloadend = () => { |
| 14 | if (reader.readyState === FileReader.DONE) { |
| 15 | resolve(reader.result as string) |
| 16 | } |
| 17 | } |
| 18 | }) |
| 19 | |
| 20 | try { |
| 21 | return JSON.parse(contents) |
| 22 | } catch (error) {} |
| 23 | |
| 24 | return INITIAL_COMPONENTS |
| 25 | } |
| 26 | |
| 27 | export async function saveAsJSON(components: IComponents) { |
| 28 | const serialized = JSON.stringify(components) |