mirror of
https://github.com/massbug/judge4c.git
synced 2025-05-18 15:26:33 +00:00
feat(editor): use problem templates in ResetButton and disable when empty
This commit is contained in:
parent
1766c4a632
commit
556fde6546
@ -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" />
|
||||||
|
Loading…
Reference in New Issue
Block a user