1
- import React , { useEffect , useMemo , useRef , useState } from 'react'
1
+ import React , { useEffect , useMemo , useRef } from 'react'
2
2
import { useTheme } from 'next-themes'
3
+ import { shikiToMonaco } from '@shikijs/monaco'
3
4
import * as monaco from 'monaco-editor'
4
5
import styles from '@/components/code-editor.module.css'
5
6
import useCodeEditorDTS from '@/hooks/useCodeEditorDTS'
6
7
import useCodeEditorRepl from '@/hooks/useCodeEditorRepl'
8
+ import { useCodeHighlighter } from '@/hooks/useCodeHighlighter'
7
9
import { useMonacoEditor } from '@/hooks/useMonacoEditor'
8
10
import useMonacopilot from '@/hooks/useMonacopilot'
9
11
import { useReplModels } from '@/hooks/useReplModels'
10
12
import { useReplStoredState } from '@/hooks/useReplStoredState'
11
13
import { useUserStoredState } from '@/hooks/useUserStoredState'
12
14
import { PrettierFormattingProvider } from '@/lib/monaco-prettier-formatting-provider'
13
- import { loadMonacoTheme } from '@/lib/monaco-themes'
14
15
import { Themes } from '@/lib/themes'
15
16
import { cn } from '@/lib/utils'
16
17
@@ -20,17 +21,18 @@ if (process.env.NEXT_PUBLIC_NODE_ENV === 'test' && typeof window !== 'undefined'
20
21
}
21
22
22
23
setupMonaco ( )
24
+ setupInitialMonacoThemes ( )
23
25
setupTailwindCSS ( )
24
26
25
27
export default function CodeEditor ( ) {
26
28
const [ replState ] = useReplStoredState ( )
27
29
const [ userState ] = useUserStoredState ( )
28
30
const [ editorRef , setEditor ] = useMonacoEditor ( )
29
31
const { models, readOnlyModels } = useReplModels ( )
32
+ const { highlighter, loadedHighlightTheme } = useCodeHighlighter ( )
30
33
31
34
const containerRef = useRef < HTMLDivElement > ( null )
32
35
33
- const [ isThemeLoaded , setIsThemeLoaded ] = useState ( false )
34
36
const { resolvedTheme : themeId } = useTheme ( )
35
37
const theme = useMemo ( ( ) => Themes . find ( ( theme ) => theme . id === themeId ) ?? Themes [ 0 ] ! , [ themeId ] )
36
38
@@ -53,7 +55,7 @@ export default function CodeEditor() {
53
55
fontSize : userState . editor . fontSize ,
54
56
minimap : { enabled : false } ,
55
57
readOnly : isReadOnly ,
56
- theme : theme . id ,
58
+ theme : loadedHighlightTheme ?? ( theme . isDark ? 'initial-dark' : 'initial-light' ) ,
57
59
quickSuggestions : {
58
60
other : true ,
59
61
comments : true ,
@@ -100,12 +102,13 @@ export default function CodeEditor() {
100
102
} , [ userState . editor . lineNumbers , editorRef ] )
101
103
102
104
useEffect ( ( ) => {
103
- editorInitialOptions . current . theme = theme . id
104
- loadMonacoTheme ( theme ) . then ( ( ) => {
105
- monaco . editor . setTheme ( theme . id )
106
- setIsThemeLoaded ( true )
107
- } )
108
- } , [ theme ] )
105
+ if ( highlighter && loadedHighlightTheme ) {
106
+ editorInitialOptions . current . theme = loadedHighlightTheme
107
+
108
+ shikiToMonaco ( highlighter , monaco )
109
+ monaco . editor . setTheme ( loadedHighlightTheme )
110
+ }
111
+ } , [ highlighter , loadedHighlightTheme ] )
109
112
110
113
useEffect ( ( ) => {
111
114
const editor = monaco . editor . create ( containerRef . current ! , editorInitialOptions . current )
@@ -125,7 +128,7 @@ export default function CodeEditor() {
125
128
return (
126
129
< div
127
130
ref = { containerRef }
128
- className = { cn ( 'min-h-0 flex-1' , styles . codeEditor , { 'opacity-0' : ! isThemeLoaded } ) }
131
+ className = { cn ( 'bg-editor-background min-h-0 flex-1' , styles . codeEditor ) }
129
132
/>
130
133
)
131
134
}
@@ -193,3 +196,27 @@ async function setupTailwindCSS() {
193
196
} ,
194
197
} )
195
198
}
199
+
200
+ function setupInitialMonacoThemes ( ) {
201
+ monaco . editor . defineTheme ( 'initial-dark' , {
202
+ base : 'vs-dark' ,
203
+ inherit : true ,
204
+ rules : [ ] ,
205
+ colors : {
206
+ 'editor.background' : '#00000000' ,
207
+ 'editor.lineHighlightBackground' : '#FFFFFF0F' ,
208
+ focusBorder : '#00000000' ,
209
+ } ,
210
+ } )
211
+
212
+ monaco . editor . defineTheme ( 'initial-light' , {
213
+ base : 'vs' ,
214
+ inherit : true ,
215
+ rules : [ ] ,
216
+ colors : {
217
+ 'editor.background' : '#00000000' ,
218
+ 'editor.lineHighlightBackground' : '#EEEEEE1F' ,
219
+ focusBorder : '#00000000' ,
220
+ } ,
221
+ } )
222
+ }
0 commit comments