add save feature

This commit is contained in:
Ole Morud
2022-08-25 23:55:51 +02:00
parent c1bdbc7060
commit 587eb27908
2 changed files with 36 additions and 0 deletions

View File

@@ -97,6 +97,9 @@ function Board() {
</IconButton>
<IconButton onClick={() => step()}>
<KeyboardDoubleArrowRightIcon />
</IconButton>
<IconButton href={save(program)} download="save.json">
<SaveIcon />
</IconButton>
</Grid>
<Grid item xs={4}>

33
src/filehandler.tsx Normal file
View File

@@ -0,0 +1,33 @@
import React from "react"
import { newBoard, ProgramState, resetProgram } from "./interpreter"
import instructions, { Instruction } from "./instructions"
interface SaveState {
width: number
height: number
program: string[]
}
export function save(program: ProgramState): string {
const saveObject: SaveState = {
width: program.width,
height: program.height,
program: []
}
program.board.forEach((row) => {
let line = ""
row.forEach((instruction) => {
line += instruction.bytecode
})
saveObject.program.push(line)
})
console.log(saveObject)
const data = new Blob([JSON.stringify(saveObject, null, 2)], { type: "text/plain" })
const file = window.URL.createObjectURL(data)
return file
}