feat: 添加代码片段编辑功能,支持保存编辑后的代码

This commit is contained in:
ngc2207 2024-11-12 16:36:41 +08:00
parent 415aa13a33
commit 859fd0f725
2 changed files with 21 additions and 0 deletions

13
src/actions/index.ts Normal file
View File

@ -0,0 +1,13 @@
"use server";
import { db } from "@/db";
import { redirect } from "next/navigation";
export async function editSnippet(id: number, code: string) {
await db.snippet.update({
where: { id },
data: { code },
});
redirect(`/snippets/${id}`);
}

View File

@ -1,6 +1,7 @@
"use client";
import { useState } from "react";
import * as actions from "@/actions";
import { Snippet } from "@prisma/client";
import { Editor } from "@monaco-editor/react";
@ -15,6 +16,8 @@ export default function SnippetEditForm({ snippet }: SnippetEditFormProps) {
setCode(value);
};
const editSnippetAction = actions.editSnippet.bind(null, snippet.id, code);
return (
<div>
<Editor
@ -25,6 +28,11 @@ export default function SnippetEditForm({ snippet }: SnippetEditFormProps) {
options={{ minimap: { enabled: false } }}
onChange={handleEditorChange}
/>
<form action={editSnippetAction}>
<button type="submit" className="p-2 border rounded">
Save
</button>
</form>
</div>
);
}