From 587eb2790873a8563f833fbb3b59bb10b65cf604 Mon Sep 17 00:00:00 2001 From: Ole Morud Date: Thu, 25 Aug 2022 23:55:51 +0200 Subject: [PATCH] add save feature --- src/components/Board.tsx | 3 +++ src/filehandler.tsx | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/filehandler.tsx diff --git a/src/components/Board.tsx b/src/components/Board.tsx index 0c69f54..b41b2a8 100644 --- a/src/components/Board.tsx +++ b/src/components/Board.tsx @@ -98,6 +98,9 @@ function Board() { step()}> + + + output: {program.output} diff --git a/src/filehandler.tsx b/src/filehandler.tsx new file mode 100644 index 0000000..2060580 --- /dev/null +++ b/src/filehandler.tsx @@ -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 +}