feat(editor): use problem templates in ResetButton and disable when empty

This commit is contained in:
cfngc4594 2025-03-09 11:09:14 +08:00
parent 1766c4a632
commit 556fde6546

View File

@ -8,11 +8,20 @@ import {
} from "@/components/ui/tooltip"; } from "@/components/ui/tooltip";
import { RotateCcw } from "lucide-react"; import { RotateCcw } from "lucide-react";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { EditorLanguage } from "@prisma/client";
import { useCodeEditorStore } from "@/store/useCodeEditorStore"; import { useCodeEditorStore } from "@/store/useCodeEditorStore";
import { TEMP_DEFAULT_EDITOR_VALUE } from "@/config/problem/value";
export default function ResetButton() { interface ResetButtonProps {
templates: { language: EditorLanguage; template: string }[];
}
export default function ResetButton({
templates,
}: ResetButtonProps) {
const { editor, language } = useCodeEditorStore(); const { editor, language } = useCodeEditorStore();
const currentTemplate = templates.find((t) => t.language === language)?.template ?? "";
return ( return (
<TooltipProvider delayDuration={0}> <TooltipProvider delayDuration={0}>
<Tooltip> <Tooltip>
@ -23,20 +32,20 @@ export default function ResetButton() {
aria-label="Reset Code" aria-label="Reset Code"
onClick={() => { onClick={() => {
if (editor) { if (editor) {
const value = TEMP_DEFAULT_EDITOR_VALUE[language];
const model = editor.getModel(); const model = editor.getModel();
if (model) { if (model) {
const fullRange = model.getFullModelRange(); const fullRange = model.getFullModelRange();
editor.executeEdits("reset-code", [ editor.executeEdits("reset-code", [
{ {
range: fullRange, range: fullRange,
text: value, text: currentTemplate,
forceMoveMarkers: true, forceMoveMarkers: true,
}, },
]); ]);
} }
} }
}} }}
disabled={templates.length === 0}
className="h-6 w-6 px-1.5 py-0.5 border-none hover:bg-muted" className="h-6 w-6 px-1.5 py-0.5 border-none hover:bg-muted"
> >
<RotateCcw size={16} strokeWidth={2} aria-hidden="true" /> <RotateCcw size={16} strokeWidth={2} aria-hidden="true" />