/** * RawMarkdownEditor — raw markdown editing using Monaco Editor. * Theme follows the app's Nimbalyst theme system. */ import Editor, { loader, type OnChange, type OnMount, } from '@monaco-editor/react'; import { Eye } from 'lucide-react'; import type { editor as monacoEditor } from 'monaco-editor'; import type { JSX } from 'react'; import { useEffect, useRef } from 'react'; import { useTheme } from '@/components/theme-provider'; loader.config({ paths: { vs: '/vs' } }); interface Props { content: string; onChange?: (value: string) => void; readOnly?: boolean; onToggleSource?: () => void; language?: string; } function defineTheme(monaco: typeof import('monaco-editor'), isDark: boolean) { const name = isDark ? 'llmwiki-dark' : 'llmwiki-light'; monaco.editor.defineTheme(name, { base: isDark ? 'vs-dark' : 'vs', inherit: true, rules: [], colors: isDark ? { 'editor.background': '#2d2d2d', 'editor.foreground': '#ffffff', 'editor.lineHighlightBackground': '#3a3a3a', 'editor.selectionBackground': 'rgba(96,165,250,0.2)', 'editorCursor.foreground': '#60a5fa', 'editorLineNumber.foreground': '#808080', 'editorLineNumber.activeForeground': '#b3b3b3', 'editor.selectionHighlightBackground': 'rgba(96,165,250,0.1)', } : { 'editor.background': '#ffffff', 'editor.foreground': '#111827', 'editor.lineHighlightBackground': '#f3f4f6', 'editor.selectionBackground': 'rgba(59,130,246,0.2)', 'editorCursor.foreground': '#3b82f6', 'editorLineNumber.foreground': '#9ca3af', 'editorLineNumber.activeForeground': '#6b7280', 'editor.selectionHighlightBackground': 'rgba(59,130,246,0.1)', }, }); return name; } export default function RawMarkdownEditor({ content, onChange, readOnly = false, onToggleSource, language = 'markdown', }: Props & { showSource?: boolean }): JSX.Element { const { theme } = useTheme(); const editorRef = useRef(null); const monacoRef = useRef(null); const themeApplied = useRef(''); const handleMount: OnMount = (editor, monaco) => { editorRef.current = editor; monacoRef.current = monaco; const isDark = theme === 'dark'; const name = defineTheme(monaco, isDark); monaco.editor.setTheme(name); themeApplied.current = name; }; // Switch Monaco theme when app theme changes useEffect(() => { if (!monacoRef.current) return; const isDark = theme === 'dark'; const expected = isDark ? 'llmwiki-dark' : 'llmwiki-light'; if (themeApplied.current === expected) return; const name = defineTheme(monacoRef.current, isDark); monacoRef.current.editor.setTheme(name); themeApplied.current = name; }, [theme]); const handleChange: OnChange = (value) => { if (onChange && value !== undefined) { onChange(value); } }; return (
); }