Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/components/common/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { useTheme } from "next-themes";
import { useEffect, useRef } from "react";
import { StreamLanguage } from "@codemirror/language";
import { toml } from "@codemirror/legacy-modes/mode/toml";
import { keymap } from "@codemirror/view";
import { indentWithTab } from "@codemirror/commands";

function tomlParseLinter() {
return (view: EditorView): Diagnostic[] => {
Expand Down Expand Up @@ -52,6 +54,7 @@ interface Props {
language?: Language;
onChange: (value: string) => void;
onValidation?: (isValid: boolean) => void;
onSave: () => void;
readOnly?: boolean;
}

Expand All @@ -72,6 +75,7 @@ const CodeEditor = ({
language = "json",
onChange,
onValidation,
onSave,
readOnly = false,
}: Props) => {
const containerRef = useRef<HTMLDivElement>(null);
Expand All @@ -80,6 +84,8 @@ const CodeEditor = ({
onChangeRef.current = onChange;
const onValidationRef = useRef(onValidation);
onValidationRef.current = onValidation;
const onSaveRef = useRef(onSave);
onSaveRef.current = onSave;
const valueRef = useRef(value);
valueRef.current = value;

Expand All @@ -88,6 +94,18 @@ const CodeEditor = ({

useEffect(() => {
if (!containerRef.current) return;
const tabExtension = keymap.of([indentWithTab]);

const saveKeymap = keymap.of([
{
key: "Mod-s",
preventDefault: true,
run: () => {
onSaveRef.current();
return true;
},
},
]);

const langLinter = languageLinters[language];

Expand Down Expand Up @@ -118,6 +136,8 @@ const CodeEditor = ({
"&": { height: "100%", fontSize: "13px" },
".cm-scroller": { overflow: "auto", fontFamily: "monospace" },
}),
tabExtension,
saveKeymap,
],
}),
parent: containerRef.current,
Expand Down
7 changes: 7 additions & 0 deletions src/components/common/ConfigFileEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { browserApiClient } from "@/services/devGuardApi";
import { Pencil } from "lucide-react";
import { useEffect, useState } from "react";
import { toast } from "sonner";
import useSWR from "swr";

const defaultConfigFiles = [
Expand Down Expand Up @@ -71,6 +72,7 @@ const ConfigFileEditor = ({
if (!configFileUrl) {
return;
}

const resp = await browserApiClient(configFileUrl, {
method: "PUT",
headers: { "Content-Type": "text/plain" },
Expand All @@ -81,6 +83,7 @@ const ConfigFileEditor = ({
setCodeError("Failed to save the new Configuration");
return;
}
toast.success("Config saved successfully");
setEditorValue(newConfig);
setCodeError(null);
mutate();
Expand Down Expand Up @@ -118,6 +121,10 @@ const ConfigFileEditor = ({
value={editorValue}
onChange={setEditorValue}
onValidation={handleEditorValidation}
onSave={() => {
if (!!codeError || editorValue === configFile || configFile === undefined) return;
handleConfigFileChange(editorValue);
}}
language={selectedLanguage}
/>
{codeError && <p className="text-sm text-destructive">{codeError}</p>}
Expand Down
Loading