diff --git a/prisma/migrations/20251109055509_add_board_name/migration.sql b/prisma/migrations/20251109055509_add_board_name/migration.sql new file mode 100644 index 0000000..5ec5c34 --- /dev/null +++ b/prisma/migrations/20251109055509_add_board_name/migration.sql @@ -0,0 +1,9 @@ +-- CreateTable +CREATE TABLE "BoardState" ( + "id" SERIAL NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "boardData" JSONB NOT NULL, + "boardName" TEXT NOT NULL, + + CONSTRAINT "BoardState_pkey" PRIMARY KEY ("id") +); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 961ea04..ff842dc 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -19,4 +19,5 @@ model BoardState { id Int @id @default(autoincrement()) createdAt DateTime @default(now()) boardData Json + boardName String } diff --git a/src/iframe/life-game.html b/src/iframe/life-game.html index 9608c87..f9c5624 100644 --- a/src/iframe/life-game.html +++ b/src/iframe/life-game.html @@ -13,10 +13,6 @@ " >
- - - - diff --git a/src/iframe/life-game.js b/src/iframe/life-game.js index 1250aec..2875fc6 100644 --- a/src/iframe/life-game.js +++ b/src/iframe/life-game.js @@ -131,10 +131,6 @@ on.pause = () => { clearInterval(timerId); }; -on.load_board = (boardTemplate) => { - board = boardTemplate; -}; - on.resize = (newBoardSize) => { boardSize = newBoardSize; }; @@ -191,18 +187,17 @@ on.stateupdate = () => { on.sizechange(boardSize); -saveButton.onclick = async () => { +on.save_board = async () => { window.parent.postMessage({ type: "save_board", data: board }, "*"); }; -loadButton.onclick = async () => { +on.load_board = async () => { window.parent.postMessage({ type: "request:load_board" }, "*"); }; -on.load_board = (loadedBoard) => { - console.log("on.load_board"); - board = loadedBoard; +on.apply_board = (newBoard) => { + board = newBoard; renderBoard(); generationChange(0); - stop(); + on.pause(); }; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 574deed..5f24510 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -33,6 +33,10 @@ let generationFigure = $state(0); let sizeInputValue = $state(20); + let saveModalOpen = $state(false); + let boardNameInput = $state(""); + let boardToSave: boolean[][] | undefined = $state(undefined); + onMount(() => { const handleMessage = (event: MessageEvent) => { if (event.data.type === "patternError") { @@ -60,22 +64,20 @@ onMount(() => { const handler = async (event: MessageEvent) => { - console.log("handler call"); const data = event.data as | { type: "unknown event" } | { type: "save_board"; data: boolean[][] } | { type: "request:load_board" }; if (data.type === "save_board") { - console.log("board saved!"); - await saveBoard(data.data); + boardToSave = data.data; + saveModalOpen = true; return; } if (data.type === "request:load_board") { - console.log("loaded board"); const board = await loadBoard(); if (board) { - sendEvent("load_board", board); + sendEvent("apply_board", board); } return; } @@ -84,6 +86,18 @@ window.addEventListener("message", handler); return () => window.removeEventListener("message", handler); }); + + async function handleSave() { + if (!boardToSave) return; + + const name = boardNameInput.trim() === "" ? "Unnamed Board" : boardNameInput.trim(); + + await saveBoard({ board: boardToSave, name: name }); + + saveModalOpen = false; + boardNameInput = ""; + boardToSave = undefined; + } + + +