diff --git a/src/components/workspace/Editor/Editor.tsx b/src/components/workspace/Editor/Editor.tsx index 0b2a03c..cc0eb3c 100644 --- a/src/components/workspace/Editor/Editor.tsx +++ b/src/components/workspace/Editor/Editor.tsx @@ -43,7 +43,7 @@ const Editor: FC = ({ file, projectId, className = '' }) => { const editorRef = useRef(null); const monacoRef = useRef(null); - const vimStatusBarRef = useRef(null); + const vimStatusBarRef = useRef(null); const vimModeRef = useRef<{ dispose: () => void; } | null>(null); @@ -162,20 +162,28 @@ const Editor: FC = ({ file, projectId, className = '' }) => { updateOpenFile(file.id, { isDirty: true }, projectId); }; + const initializeEditorMode = async () => { + if (!editorRef.current || !vimStatusBarRef.current) return; + + if (editorMode === 'vim') { + const { initVimMode } = await import('monaco-vim'); + vimModeRef.current = initVimMode( + editorRef.current, + vimStatusBarRef.current as unknown as HTMLElement, + ); + } else { + vimModeRef.current?.dispose(); + } + }; + useEffect(() => { - (async () => { - if (!editorRef.current) return; - if (editorMode === 'vim') { - const { initVimMode } = await import('monaco-vim'); - vimModeRef.current = initVimMode( - editorRef.current, - vimStatusBarRef.current as unknown as HTMLElement, - ); - } else { - vimModeRef.current?.dispose(); - } - })().catch(() => {}); - }, [editorRef, editorMode]); + if (editorRef.current) { + initializeEditorMode().catch(() => {}); + } + return () => { + vimModeRef.current?.dispose(); + }; + }, [editorMode]); useEffect(() => { if (!isEditorInitialized) { @@ -244,6 +252,7 @@ const Editor: FC = ({ file, projectId, className = '' }) => { setIsEditorInitialized(true); await editorOnMount(editor, monaco); + await initializeEditorMode(); editor.onDidChangeCursorPosition(() => { const position = editor.getPosition(); if (position) {