[+] Import json from editor

This commit is contained in:
2024-12-21 00:50:04 -05:00
parent 9143f6ec3c
commit 81aa244e4f
+17 -6
View File
@@ -59,12 +59,12 @@
ckpts = loadPt()
}
const restorePt = (pt: Checkpoint) => {
pt.hStates.forEach((st, idx) => hStates[idx] = st)
pt.vStates.forEach((st, idx) => vStates[idx] = st)
pt.numbers.forEach((n, idx) => numbers[idx] = n)
pt.nMask.forEach((n, idx) => nMask[idx] = n)
pt.hColors.forEach((n, idx) => hColors[idx] = n)
pt.vColors.forEach((n, idx) => vColors[idx] = n)
pt.hStates?.forEach((st, idx) => hStates[idx] = st)
pt.vStates?.forEach((st, idx) => vStates[idx] = st)
pt.numbers?.forEach((n, idx) => numbers[idx] = n)
pt.nMask?.forEach((n, idx) => nMask[idx] = n)
pt.hColors?.forEach((n, idx) => hColors[idx] = n)
pt.vColors?.forEach((n, idx) => vColors[idx] = n)
colors = pt.colors
updateColors()
}
@@ -292,6 +292,14 @@
// Update every edge
range(eRows).forEach(y => range(eCols).forEach(x => checkPos(x, y)))
}
let importFileEl: HTMLInputElement
function importFile(e: Event & { currentTarget: EventTarget & HTMLInputElement }) {
const reader = new FileReader()
reader.onload = () => restorePt(JsonTy.parse(reader.result as string))
// @ts-ignore
reader.readAsText(e.target!!.files[0])
}
</script>
<main class:color-mode={mode === 'color'} class:colorful-cross={colorfulCross} class:mobile={hasTouch}
@@ -371,6 +379,9 @@
<button on:click={() => confirm("Are you sure you want to reset? (checkpoints will not be removed)") &&
restorePt(JsonTy.parse(resetPoint))
}>Reset</button>
<!-- Import -->
<button on:click={() => importFileEl.click()}>Import</button>
<input type="file" accept=".json" bind:this={importFileEl} on:change={importFile} style="display: none"/>
</div>
{/if}