Skip to content

Commit

Permalink
fix(core): keep cursor when switch language (Power-Playground#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
kainstar authored Aug 28, 2023
1 parent 9dad3bd commit d247a8b
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions core/src/components/EditorZone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ export default function EditorZone(props: {

const [displayLeftBar, setDisplayLeftBar] = useAtom(displayLeftBarAtom)

const editorCursorPosition = useRef<monacoEditor.Position | null>(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

Expand Down

0 comments on commit d247a8b

Please sign in to comment.