-
Notifications
You must be signed in to change notification settings - Fork 1
コード保存/読込機能を追加 #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
コード保存/読込機能を追加 #29
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
prisma/migrations/20251113084438_add_code_state/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| -- CreateTable | ||
| CREATE TABLE "CodeState" ( | ||
| "id" SERIAL NOT NULL, | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "codeData" TEXT NOT NULL, | ||
| "codeName" TEXT NOT NULL, | ||
|
|
||
| CONSTRAINT "CodeState_pkey" PRIMARY KEY ("id") | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| Warnings: | ||
|
|
||
| - You are about to drop the `BoardState` table. If the table is not empty, all the data it contains will be lost. | ||
| - You are about to drop the `CodeState` table. If the table is not empty, all the data it contains will be lost. | ||
|
|
||
| */ | ||
| -- DropTable | ||
| DROP TABLE "BoardState"; | ||
|
|
||
| -- DropTable | ||
| DROP TABLE "CodeState"; | ||
|
|
||
| -- CreateTable | ||
| CREATE TABLE "Board" ( | ||
| "id" SERIAL NOT NULL, | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "data" JSONB NOT NULL, | ||
| "name" TEXT NOT NULL, | ||
| "preview" JSONB NOT NULL, | ||
|
|
||
| CONSTRAINT "Board_pkey" PRIMARY KEY ("id") | ||
| ); | ||
|
|
||
| -- CreateTable | ||
| CREATE TABLE "Code" ( | ||
| "id" SERIAL NOT NULL, | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "data" TEXT NOT NULL, | ||
| "name" TEXT NOT NULL, | ||
|
|
||
| CONSTRAINT "Code_pkey" PRIMARY KEY ("id") | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| export async function saveCode(data: { code: string; name: string }, isJapanese: boolean) { | ||
| try { | ||
| const response = await fetch("/api/code", { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify(data), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error("Failed to communicate with the server."); | ||
| } | ||
|
|
||
| if (isJapanese) { | ||
| alert("コードを保存しました!"); | ||
| } else { | ||
| alert("Code saved!"); | ||
| } | ||
| } catch (err) { | ||
| console.error("Save Error:", err); | ||
| if (isJapanese) { | ||
| alert("保存に失敗しました。"); | ||
| } else { | ||
| alert("Failed to save."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export type CodeListItem = { | ||
| id: number; | ||
| name: string; | ||
| createdAt: string; | ||
| }; | ||
|
|
||
| export async function fetchCodeList(isJapanese: boolean): Promise<CodeListItem[] | undefined> { | ||
| try { | ||
| const response = await fetch("/api/code"); | ||
|
|
||
| if (!response.ok) { | ||
| if (response.status === 404) { | ||
| throw new Error("There is no saved data."); | ||
| } else { | ||
| throw new Error("Failed to communicate with the server."); | ||
| } | ||
| } | ||
|
|
||
| const codeList = await response.json(); | ||
|
|
||
| return codeList as CodeListItem[]; | ||
| } catch (err) { | ||
| console.error("Load error", err); | ||
| if (isJapanese) { | ||
| alert("読み込みに失敗しました。"); | ||
| } else { | ||
| alert("Failed to load."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export async function loadCodeById(id: number, isJapanese: boolean): Promise<string | undefined> { | ||
| try { | ||
| const response = await fetch(`/api/code?id=${id}`); | ||
|
|
||
| if (!response.ok) { | ||
| if (response.status === 404) { | ||
| throw new Error("The specified ID data was not found."); | ||
| } else { | ||
| throw new Error("Failed to communicate with the server."); | ||
| } | ||
| } | ||
|
|
||
| const loadedCode = await response.json(); | ||
|
|
||
| return loadedCode as string; | ||
| } catch (err) { | ||
| console.error("Load error", err); | ||
| if (isJapanese) { | ||
| alert("読み込みに失敗しました。"); | ||
| } else { | ||
| alert("Failed to load."); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| <script lang="ts"> | ||
| import type { BoardManager } from "$lib/models/BoardManager.svelte"; | ||
|
|
||
| let { | ||
| manager, | ||
| isJapanese, | ||
| onSelect, | ||
| }: { | ||
| manager: BoardManager; | ||
| isJapanese: boolean; | ||
| onSelect: (id: number) => void; | ||
| } = $props(); | ||
| </script> | ||
|
|
||
| <dialog class="modal" open={manager.saveState.saving}> | ||
| <form method="dialog" class="modal-box"> | ||
| <h3 class="font-bold text-lg">{isJapanese ? "盤面を保存" : "Save board"}</h3> | ||
| {#if manager.saveState.saving} | ||
| <div class="flex flex-row gap-4 mt-4"> | ||
| <div class="w-90 flex flex-col gap-4"> | ||
| <p class="py-4"> | ||
| {isJapanese | ||
| ? "保存する盤面に名前を付けてください(任意)。" | ||
| : "Please name the board you wish to save (optional)."} | ||
| </p> | ||
| <input | ||
| type="text" | ||
| placeholder={isJapanese ? "盤面名を入力" : "Enter board name"} | ||
| class="input input-bordered w-full max-w-xs" | ||
| bind:value={manager.saveState.name} | ||
| /> | ||
| </div> | ||
| <div class="flex flex-col flex-shrink-0"> | ||
| <div class="text-center text-sm mb-2"> | ||
| {isJapanese ? "プレビュー" : "Preview"} | ||
| </div> | ||
| <div class="board-preview"> | ||
| {#each manager.saveState.preview as row, i (i)} | ||
| <div class="preview-row"> | ||
| {#each row as cell, j (j)} | ||
| <div class="preview-cell {cell ? 'alive' : ''}"></div> | ||
| {/each} | ||
| </div> | ||
| {/each} | ||
| </div> | ||
| </div> | ||
| </div> | ||
| <div class="modal-action"> | ||
| <button type="button" class="btn" onclick={() => manager.closeSaveModal()} | ||
| >{isJapanese ? "キャンセル" : "Cancel"}</button | ||
| > | ||
| <button | ||
| type="submit" | ||
| class="btn btn-primary" | ||
| onclick={() => manager.save(isJapanese)} | ||
| disabled={!manager.saveState.saving} | ||
| > | ||
| {isJapanese ? "保存" : "Save"} | ||
| </button> | ||
| </div> | ||
| {/if} | ||
| </form> | ||
| </dialog> | ||
|
|
||
| <dialog class="modal" open={manager.loadState.state !== "closed"}> | ||
| <div class="modal-box w-11/12 max-w-5xl"> | ||
| <h3 class="font-bold text-lg">{isJapanese ? "盤面をロード" : "Load board"}</h3> | ||
|
|
||
| {#if manager.loadState.state === "loading"} | ||
| <p class="py-4"> | ||
| {isJapanese ? "保存されている盤面を読み込み中..." : "Loading saved boards..."} | ||
| </p> | ||
| <span class="loading loading-spinner loading-lg"></span> | ||
| {:else if manager.loadState.state === "list" && manager.loadState.list.length === 0} | ||
| <p class="py-4"> | ||
| {isJapanese ? "保存されている盤面はありません。" : "No saved boards found."} | ||
| </p> | ||
| {:else if manager.loadState.state === "list"} | ||
| <div class="overflow-x-auto h-96"> | ||
| <table class="table w-full"> | ||
| <thead> | ||
| <tr> | ||
| <th class="pl-5">{isJapanese ? "プレビュー" : "Preview"}</th> | ||
| <th>{isJapanese ? "盤面名" : "Board Name"}</th> | ||
| <th>{isJapanese ? "保存日時" : "Saved At"}</th> | ||
| <th></th> | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| {#each manager.loadState.list as item (item.id)} | ||
| <tr class="hover:bg-base-300"> | ||
| <td> | ||
| <div class="board-preview"> | ||
| {#each item.preview as row, i (i)} | ||
| <div class="preview-row"> | ||
| {#each row as cell, j (j)} | ||
| <div class="preview-cell {cell ? 'alive' : ''}"></div> | ||
| {/each} | ||
| </div> | ||
| {/each} | ||
| </div> | ||
| </td> | ||
| <td>{item.name}</td> | ||
| <td>{new Date(item.createdAt).toLocaleString(isJapanese ? "ja-JP" : "en-US")}</td> | ||
| <td class="text-right"> | ||
| <button class="btn btn-sm btn-primary" onclick={() => onSelect(item.id)}> | ||
| {isJapanese ? "ロード" : "Load"} | ||
| </button> | ||
| </td> | ||
| </tr> | ||
| {/each} | ||
| </tbody> | ||
| </table> | ||
| </div> | ||
| {/if} | ||
|
|
||
| <div class="modal-action"> | ||
| <button class="btn" onclick={() => manager.closeLoadModal()}> | ||
| {isJapanese ? "閉じる" : "Close"} | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </dialog> | ||
|
|
||
| <style> | ||
| .board-preview { | ||
| display: grid; | ||
| grid-template-columns: repeat(20, 3px); | ||
| grid-template-rows: repeat(20, 3px); | ||
| width: 60px; | ||
| height: 60px; | ||
| border: 1px solid #9ca3af; | ||
| background-color: white; | ||
| } | ||
| .preview-row { | ||
| display: contents; | ||
| } | ||
| .preview-cell { | ||
| width: 3px; | ||
| height: 3px; | ||
| } | ||
| .preview-cell.alive { | ||
| background-color: black; | ||
| } | ||
| </style> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.