mirror of
https://litchi.icu/ngc2207/judge4c-demo.git
synced 2025-05-18 16:56:34 +00:00
feat: 添加代码片段编辑功能,支持保存编辑后的代码
This commit is contained in:
parent
415aa13a33
commit
859fd0f725
13
src/actions/index.ts
Normal file
13
src/actions/index.ts
Normal 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}`);
|
||||
}
|
@ -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>
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user