mirror of
https://litchi.icu/ngc2207/judge4c-demo.git
synced 2025-05-19 02:17:54 +00:00
feat: 添加SnippetEditor用于创建代码片段
This commit is contained in:
parent
5a5a8033da
commit
f20e9db186
@ -1,18 +1,23 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
|
import { useState } from "react";
|
||||||
import * as actions from "@/actions";
|
import * as actions from "@/actions";
|
||||||
|
import SnippetEditor from "@/components/snippet-create-form";
|
||||||
import { useActionState } from "react";
|
import { useActionState } from "react";
|
||||||
|
|
||||||
export default function SnippetCreatePage() {
|
export default function SnippetCreatePage() {
|
||||||
const [formState, action] = useActionState(actions.createSnippet, {
|
const [formState, action] = useActionState(actions.createSnippet, {
|
||||||
message: "",
|
message: "",
|
||||||
});
|
});
|
||||||
|
const [code, setCode] = useState("");
|
||||||
|
const handleCodeChange = (newCode: string = "") => {
|
||||||
|
setCode(newCode);
|
||||||
|
};
|
||||||
return (
|
return (
|
||||||
<form action={action}>
|
<form action={action}>
|
||||||
<h3 className="font-bold m-3">Create a Snippet</h3>
|
<h3 className="font-bold m-3">Create a Snippet</h3>
|
||||||
<div className="flex flex-col gap-4">
|
<div className="flex flex-col gap-4">
|
||||||
<div className="flex gap-4">
|
<div className="flex flex-col gap-4">
|
||||||
<label className="w-12" htmlFor="title">
|
<label className="w-12" htmlFor="title">
|
||||||
Title
|
Title
|
||||||
</label>
|
</label>
|
||||||
@ -22,11 +27,12 @@ export default function SnippetCreatePage() {
|
|||||||
id="title"
|
id="title"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex gap-4">
|
<div className="flex flex-col gap-4">
|
||||||
<label className="w-12" htmlFor="title">
|
<label className="w-12" htmlFor="title">
|
||||||
Code
|
Code
|
||||||
</label>
|
</label>
|
||||||
<input name="code" className="border rounded p-2 w-full" id="code" />
|
<input type="hidden" name="code" value={code} />
|
||||||
|
<SnippetEditor code={code} onCodeChange={handleCodeChange} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{formState.message ? (
|
{formState.message ? (
|
||||||
|
30
src/components/snippet-create-form.tsx
Normal file
30
src/components/snippet-create-form.tsx
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { Editor } from "@monaco-editor/react";
|
||||||
|
|
||||||
|
interface SnippetEditorProps {
|
||||||
|
code: string;
|
||||||
|
onCodeChange: (value: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function SnippetEditor({
|
||||||
|
code,
|
||||||
|
onCodeChange,
|
||||||
|
}: SnippetEditorProps) {
|
||||||
|
const handleEditorChange = (value: string = "") => {
|
||||||
|
onCodeChange(value);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<Editor
|
||||||
|
height="40vh"
|
||||||
|
theme="vs-light"
|
||||||
|
language="c"
|
||||||
|
defaultValue={code}
|
||||||
|
options={{ minimap: { enabled: false } }}
|
||||||
|
onChange={handleEditorChange}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user