From d247a8b1065ba67317eff78af3728813df374578 Mon Sep 17 00:00:00 2001 From: KainStar <461345042@qq.com> Date: Mon, 28 Aug 2023 17:58:56 +0800 Subject: [PATCH] fix(core): keep cursor when switch language (#27) --- core/src/components/EditorZone.tsx | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/core/src/components/EditorZone.tsx b/core/src/components/EditorZone.tsx index 726b9f10..ee7c3a2b 100644 --- a/core/src/components/EditorZone.tsx +++ b/core/src/components/EditorZone.tsx @@ -110,6 +110,21 @@ export default function EditorZone(props: { const [displayLeftBar, setDisplayLeftBar] = useAtom(displayLeftBarAtom) + const editorCursorPosition = useRef(null) + + useEffect(() => { + if (editor) { + // restore cursor position + editor.setPosition(editorCursorPosition.current ?? { lineNumber: 1, column: 1 }) + editor.focus() + + return () => { + // save current cursor position + editorCursorPosition.current = editor.getPosition() ?? null + } + } + }, [language, editor]) + useEffect(() => { if (!monaco || !editor) return