11import { useCallback , useEffect , useRef , useState } from "react" ;
22import Editor , { loader } from "@monaco-editor/react" ;
3- import * as monaco from "@codingame/monaco-vscode-editor-api" ;
4- import type * as Monaco from "@codingame/ monaco-vscode- editor-api " ;
3+ import * as monacoRuntime from "@codingame/monaco-vscode-editor-api" ;
4+ import type * as MonacoEditor from "monaco-editor" ;
55import { MonacoLanguageClient } from "monaco-languageclient" ;
66import { MonacoVscodeApiWrapper } from "monaco-languageclient/vscodeApiWrapper" ;
77import { configureDefaultWorkerFactory } from "monaco-languageclient/workerFactory" ;
@@ -30,6 +30,9 @@ import EditorWorker from "@codingame/monaco-vscode-editor-api/esm/vs/editor/edit
3030import { useEditor } from "./editor-context" ;
3131import "monaco-editor/min/vs/editor/editor.main.css" ;
3232
33+ type MonacoApi = typeof import ( "monaco-editor" ) ;
34+ const monaco = monacoRuntime as unknown as MonacoApi ;
35+
3336loader . config ( { monaco } ) ;
3437
3538
@@ -243,9 +246,9 @@ export const CodeEditor = ({ width = 400, onWidthChange }: CodeEditorProps) => {
243246 const [ isVscodeReady , setIsVscodeReady ] = useState ( false ) ;
244247 const [ error , setError ] = useState < string | null > ( null ) ;
245248 const [ isDirty , setIsDirty ] = useState ( false ) ;
246- const editorRef = useRef < Monaco . editor . IStandaloneCodeEditor | null > ( null ) ;
247- const monacoRef = useRef < typeof import ( "@codingame/ monaco-vscode- editor-api " ) | null > ( null ) ;
248- const highlightRef = useRef < Monaco . editor . IEditorDecorationsCollection | null > ( null ) ;
249+ const editorRef = useRef < MonacoEditor . editor . IStandaloneCodeEditor | null > ( null ) ;
250+ const monacoRef = useRef < typeof import ( "monaco-editor" ) | null > ( null ) ;
251+ const highlightRef = useRef < MonacoEditor . editor . IEditorDecorationsCollection | null > ( null ) ;
249252 const highlightTimerRef = useRef < number | null > ( null ) ;
250253 const monacoConfiguredRef = useRef ( false ) ;
251254 const vscodeApiRef = useRef < MonacoVscodeApiWrapper | null > ( null ) ;
@@ -408,7 +411,7 @@ export const CodeEditor = ({ width = 400, onWidthChange }: CodeEditorProps) => {
408411 } , 2000 ) ;
409412 } , [ ] ) ;
410413
411- const configureMonaco = useCallback ( ( monaco : typeof import ( "@codingame/ monaco-vscode- editor-api " ) ) => {
414+ const configureMonaco = useCallback ( ( monaco : typeof import ( "monaco-editor" ) ) => {
412415 if ( monacoConfiguredRef . current ) return ;
413416 monacoConfiguredRef . current = true ;
414417
@@ -587,8 +590,9 @@ export const CodeEditor = ({ width = 400, onWidthChange }: CodeEditorProps) => {
587590 if ( ! selection && options ) {
588591 logNavigationIssue ( "Missing selection for editor navigation" , options ) ;
589592 }
590- if ( textModel && editorRef . current ) {
591- editorRef . current . setModel ( textModel ) ;
593+ const editor = editorRef . current ;
594+ if ( textModel && editor ) {
595+ editor . setModel ( textModel as unknown as MonacoEditor . editor . ITextModel ) ;
592596 const content = textModel . getValue ( ) ;
593597 setCode ( content ) ;
594598 setCurrentFile ( target ) ;
@@ -597,18 +601,24 @@ export const CodeEditor = ({ width = 400, onWidthChange }: CodeEditorProps) => {
597601 if ( selection ) {
598602 revealPosition ( selection . line , selection . column ) ;
599603 }
600- return editorRef . current ;
604+ return editor as unknown as MonacoEditor . editor . ICodeEditor ;
601605 }
602606 const content = textModel ?. getValue ( ) ;
603607 if ( content != null ) {
604608 await openFileWithContentRef . current ?.( target , content , selection ?. line , selection ?. column ) ;
605609 } else {
606610 await openFileRef . current ?.( target , selection ?. line , selection ?. column ) ;
607611 }
608- return editorRef . current ?? undefined ;
612+ const currentEditor = editorRef . current ;
613+ return currentEditor
614+ ? ( currentEditor as unknown as MonacoEditor . editor . ICodeEditor )
615+ : undefined ;
609616 }
610617 logNavigationIssue ( "Missing target URI for editor navigation" , { modelRef, options } ) ;
611- return editorRef . current ?? undefined ;
618+ const currentEditor = editorRef . current ;
619+ return currentEditor
620+ ? ( currentEditor as unknown as MonacoEditor . editor . ICodeEditor )
621+ : undefined ;
612622 } ,
613623 } ,
614624 serviceOverrides : {
@@ -658,7 +668,7 @@ export const CodeEditor = ({ width = 400, onWidthChange }: CodeEditorProps) => {
658668 } ;
659669 } , [ ensureVscodeApi ] ) ;
660670
661- const startLspClient = useCallback ( async ( monacoApi : typeof import ( "@codingame/monaco-vscode-editor-api" ) ) => {
671+ const startLspClient = useCallback ( async ( ) => {
662672 if ( lspClientRef . current || lspStartingRef . current ) return ;
663673 if ( ! window . editorAPI ?. getLspPort || ! window . editorAPI ?. getProjectRoot ) {
664674 return ;
@@ -813,13 +823,13 @@ export const CodeEditor = ({ width = 400, onWidthChange }: CodeEditorProps) => {
813823 } , [ loadFile ] ) ;
814824
815825 const handleEditorDidMount = (
816- editor : Monaco . editor . IStandaloneCodeEditor ,
817- monaco : typeof import ( "@codingame/ monaco-vscode- editor-api " ) ,
826+ editor : MonacoEditor . editor . IStandaloneCodeEditor ,
827+ monaco : typeof import ( "monaco-editor" ) ,
818828 ) => {
819829 editorRef . current = editor ;
820830 monacoRef . current = monaco ;
821831 highlightRef . current = editor . createDecorationsCollection ( ) ;
822- void startLspClient ( monaco ) ;
832+ void startLspClient ( ) ;
823833
824834 // Add Ctrl+S keybinding
825835 editor . addCommand ( monaco . KeyMod . CtrlCmd | monaco . KeyCode . KeyS , ( ) => {
0 commit comments