2026-04-23 18:00:51 +05:30
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import dynamic from "next/dynamic";
|
2026-04-23 18:29:32 +05:30
|
|
|
import { useEffect, useRef } from "react";
|
2026-04-23 18:00:51 +05:30
|
|
|
import { useTheme } from "next-themes";
|
2026-04-23 18:21:50 +05:30
|
|
|
import { Spinner } from "@/components/ui/spinner";
|
2026-04-23 18:00:51 +05:30
|
|
|
|
|
|
|
|
const MonacoEditor = dynamic(() => import("@monaco-editor/react"), {
|
|
|
|
|
ssr: false,
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-23 18:21:50 +05:30
|
|
|
interface SourceCodeEditorProps {
|
2026-04-23 18:00:51 +05:30
|
|
|
value: string;
|
|
|
|
|
onChange: (next: string) => void;
|
2026-04-23 18:21:50 +05:30
|
|
|
path?: string;
|
|
|
|
|
language?: string;
|
|
|
|
|
readOnly?: boolean;
|
|
|
|
|
fontSize?: number;
|
2026-04-23 18:29:32 +05:30
|
|
|
onSave?: () => Promise<void> | void;
|
2026-04-23 18:00:51 +05:30
|
|
|
}
|
|
|
|
|
|
2026-04-23 18:21:50 +05:30
|
|
|
export function SourceCodeEditor({
|
|
|
|
|
value,
|
|
|
|
|
onChange,
|
|
|
|
|
path,
|
|
|
|
|
language = "plaintext",
|
|
|
|
|
readOnly = false,
|
|
|
|
|
fontSize = 12,
|
2026-04-23 18:29:32 +05:30
|
|
|
onSave,
|
2026-04-23 18:21:50 +05:30
|
|
|
}: SourceCodeEditorProps) {
|
2026-04-23 18:00:51 +05:30
|
|
|
const { resolvedTheme } = useTheme();
|
2026-04-23 18:29:32 +05:30
|
|
|
const onSaveRef = useRef(onSave);
|
2026-04-23 19:25:59 +05:30
|
|
|
const monacoRef = useRef<any>(null);
|
2026-04-23 18:29:32 +05:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
onSaveRef.current = onSave;
|
|
|
|
|
}, [onSave]);
|
|
|
|
|
|
2026-04-23 19:25:59 +05:30
|
|
|
const resolveCssColorToHex = (cssColorValue: string): string | null => {
|
|
|
|
|
if (typeof document === "undefined") return null;
|
|
|
|
|
const probe = document.createElement("div");
|
|
|
|
|
probe.style.color = cssColorValue;
|
|
|
|
|
probe.style.position = "absolute";
|
|
|
|
|
probe.style.pointerEvents = "none";
|
|
|
|
|
probe.style.opacity = "0";
|
|
|
|
|
document.body.appendChild(probe);
|
|
|
|
|
const computedColor = getComputedStyle(probe).color;
|
|
|
|
|
probe.remove();
|
|
|
|
|
const match = computedColor.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/i);
|
|
|
|
|
if (!match) return null;
|
|
|
|
|
const toHex = (value: string) => Number(value).toString(16).padStart(2, "0");
|
|
|
|
|
return `#${toHex(match[1])}${toHex(match[2])}${toHex(match[3])}`;
|
|
|
|
|
};
|
2026-04-23 18:29:32 +05:30
|
|
|
|
2026-04-23 19:25:59 +05:30
|
|
|
const applySidebarTheme = (monaco: any) => {
|
|
|
|
|
const isDark = resolvedTheme === "dark";
|
|
|
|
|
const themeName = isDark ? "surfsense-dark" : "surfsense-light";
|
|
|
|
|
const fallbackBg = isDark ? "#1e1e1e" : "#ffffff";
|
|
|
|
|
const sidebarBgHex = resolveCssColorToHex("var(--sidebar)") ?? fallbackBg;
|
|
|
|
|
monaco.editor.defineTheme(themeName, {
|
|
|
|
|
base: isDark ? "vs-dark" : "vs",
|
|
|
|
|
inherit: true,
|
|
|
|
|
rules: [],
|
|
|
|
|
colors: {
|
|
|
|
|
"editor.background": sidebarBgHex,
|
|
|
|
|
"editorGutter.background": sidebarBgHex,
|
|
|
|
|
"minimap.background": sidebarBgHex,
|
|
|
|
|
"editorLineNumber.background": sidebarBgHex,
|
|
|
|
|
"editor.lineHighlightBackground": "#00000000",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
monaco.editor.setTheme(themeName);
|
|
|
|
|
};
|
2026-04-23 18:29:32 +05:30
|
|
|
|
2026-04-23 19:25:59 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
if (!monacoRef.current) return;
|
|
|
|
|
applySidebarTheme(monacoRef.current);
|
|
|
|
|
}, [resolvedTheme]);
|
2026-04-23 18:29:32 +05:30
|
|
|
|
2026-04-23 19:25:59 +05:30
|
|
|
const isManualSaveEnabled = !!onSave && !readOnly;
|
2026-04-23 18:00:51 +05:30
|
|
|
|
|
|
|
|
return (
|
2026-04-23 19:25:59 +05:30
|
|
|
<div className="h-full w-full overflow-hidden bg-sidebar [&_.monaco-editor]:!bg-sidebar [&_.monaco-editor_.margin]:!bg-sidebar [&_.monaco-editor_.monaco-editor-background]:!bg-sidebar [&_.monaco-editor-background]:!bg-sidebar [&_.monaco-scrollable-element_.scrollbar_.slider]:rounded-full [&_.monaco-scrollable-element_.scrollbar_.slider]:bg-foreground/25 [&_.monaco-scrollable-element_.scrollbar_.slider:hover]:bg-foreground/40">
|
2026-04-23 18:00:51 +05:30
|
|
|
<MonacoEditor
|
2026-04-23 18:21:50 +05:30
|
|
|
path={path}
|
2026-04-23 18:00:51 +05:30
|
|
|
language={language}
|
|
|
|
|
value={value}
|
2026-04-23 19:25:59 +05:30
|
|
|
theme={resolvedTheme === "dark" ? "surfsense-dark" : "surfsense-light"}
|
2026-04-23 18:00:51 +05:30
|
|
|
onChange={(next) => onChange(next ?? "")}
|
2026-04-23 18:21:50 +05:30
|
|
|
loading={
|
|
|
|
|
<div className="flex h-full w-full items-center justify-center">
|
|
|
|
|
<Spinner size="sm" className="text-muted-foreground" />
|
|
|
|
|
</div>
|
|
|
|
|
}
|
2026-04-23 19:25:59 +05:30
|
|
|
beforeMount={(monaco) => {
|
|
|
|
|
monacoRef.current = monaco;
|
|
|
|
|
applySidebarTheme(monaco);
|
|
|
|
|
}}
|
2026-04-23 18:29:32 +05:30
|
|
|
onMount={(editor, monaco) => {
|
2026-04-23 19:25:59 +05:30
|
|
|
monacoRef.current = monaco;
|
|
|
|
|
applySidebarTheme(monaco);
|
2026-04-23 18:29:32 +05:30
|
|
|
if (!isManualSaveEnabled) return;
|
|
|
|
|
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => {
|
|
|
|
|
void onSaveRef.current?.();
|
|
|
|
|
});
|
|
|
|
|
}}
|
2026-04-23 18:00:51 +05:30
|
|
|
options={{
|
|
|
|
|
automaticLayout: true,
|
|
|
|
|
minimap: { enabled: false },
|
|
|
|
|
lineNumbers: "on",
|
|
|
|
|
lineNumbersMinChars: 3,
|
|
|
|
|
lineDecorationsWidth: 12,
|
|
|
|
|
glyphMargin: false,
|
|
|
|
|
folding: true,
|
|
|
|
|
overviewRulerLanes: 0,
|
|
|
|
|
hideCursorInOverviewRuler: true,
|
|
|
|
|
scrollBeyondLastLine: false,
|
2026-04-23 18:29:32 +05:30
|
|
|
renderLineHighlight: "none",
|
|
|
|
|
selectionHighlight: false,
|
|
|
|
|
occurrencesHighlight: "off",
|
2026-04-23 18:39:35 +05:30
|
|
|
quickSuggestions: false,
|
|
|
|
|
suggestOnTriggerCharacters: false,
|
|
|
|
|
acceptSuggestionOnEnter: "off",
|
|
|
|
|
parameterHints: { enabled: false },
|
|
|
|
|
wordBasedSuggestions: "off",
|
2026-04-23 18:00:51 +05:30
|
|
|
wordWrap: "off",
|
|
|
|
|
scrollbar: {
|
2026-04-23 18:39:35 +05:30
|
|
|
vertical: "auto",
|
|
|
|
|
horizontal: "auto",
|
|
|
|
|
verticalScrollbarSize: 8,
|
|
|
|
|
horizontalScrollbarSize: 8,
|
2026-04-23 18:00:51 +05:30
|
|
|
alwaysConsumeMouseWheel: false,
|
|
|
|
|
},
|
|
|
|
|
tabSize: 2,
|
|
|
|
|
insertSpaces: true,
|
2026-04-23 18:21:50 +05:30
|
|
|
fontSize,
|
2026-04-23 18:00:51 +05:30
|
|
|
fontFamily:
|
|
|
|
|
"ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, monospace",
|
|
|
|
|
renderWhitespace: "selection",
|
|
|
|
|
smoothScrolling: true,
|
2026-04-23 18:21:50 +05:30
|
|
|
readOnly,
|
2026-04-23 18:00:51 +05:30
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|