Skip to content

Commit 2f8f844

Browse files
committed
fix default theme load process
1 parent 6b1628f commit 2f8f844

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

components/answer-area.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default function AnswerArea({ question, userAnswer, setUserAnswer, onEdit
3232
const textareaRef = useRef<HTMLTextAreaElement>(null)
3333
const [editorLanguage, setEditorLanguage] = useState("java")
3434
const [editorFontSize, setEditorFontSize] = useState(14)
35-
const [editorTheme, setEditorTheme] = useState("github")
35+
const [editorTheme, setEditorTheme] = useState("vs")
3636
const [isCopied, setIsCopied] = useState(false)
3737

3838
// Detect language based on question category or content

components/monaco-editor.tsx

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import { useRef, useState, useEffect } from "react"
44
import Editor, { type Monaco, type OnMount } from "@monaco-editor/react"
55
import { Loader2 } from "lucide-react"
6-
import { useTheme } from "next-themes"
76

87
interface MonacoEditorProps {
98
value: string
@@ -21,24 +20,18 @@ export default function MonacoEditor({
2120
language = "javascript",
2221
height = "300px",
2322
fontSize = 14,
24-
theme: editorTheme = "vs-dark",
23+
theme: editorTheme = "vs",
2524
autoFocus = false,
2625
}: MonacoEditorProps) {
2726
const editorRef = useRef<any>(null)
28-
const { theme: systemTheme } = useTheme()
29-
const [isEditorReady, setIsEditorReady] = useState(false)
3027
const [monacoInstance, setMonacoInstance] = useState<Monaco | null>(null)
28+
const [themesRegistered, setThemesRegistered] = useState(false)
29+
3130

3231
// Configure Monaco editor on mount
3332
const handleEditorDidMount: OnMount = (editor, monaco) => {
3433
editorRef.current = editor
3534
setMonacoInstance(monaco)
36-
setIsEditorReady(true)
37-
38-
// Focus the editor if autoFocus is true
39-
if (autoFocus) {
40-
editor.focus()
41-
}
4235

4336
// Configure editor settings
4437
editor.updateOptions({
@@ -57,8 +50,37 @@ export default function MonacoEditor({
5750

5851
// Register custom themes
5952
registerCustomThemes(monaco)
53+
54+
// Mark themes as registered
55+
setThemesRegistered(true)
56+
57+
// Always start with a built-in theme to ensure stability
58+
monaco.editor.setTheme('vs')
59+
60+
if (autoFocus) {
61+
editor.focus()
62+
}
6063
}
6164

65+
useEffect(() => {
66+
if (!monacoInstance || !themesRegistered) return;
67+
68+
// Apply the theme with a longer delay to ensure registration is complete
69+
const customThemes = ["github", "monokai", "dracula", "nord"]
70+
71+
if (customThemes.includes(editorTheme)) {
72+
const timer = setTimeout(() => {
73+
console.log("Applying custom theme:", editorTheme);
74+
monacoInstance.editor.setTheme(editorTheme);
75+
}, 300);
76+
77+
return () => clearTimeout(timer);
78+
} else {
79+
monacoInstance.editor.setTheme(editorTheme);
80+
}
81+
}, [monacoInstance, themesRegistered, editorTheme]);
82+
83+
6284
// Register custom editor themes
6385
const registerCustomThemes = (monaco: Monaco) => {
6486
// GitHub theme

0 commit comments

Comments
 (0)