Skip to content

Commit a945563

Browse files
authored
refactor: save editor view state in store (#130)
1 parent 5ab1a2d commit a945563

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

src/monaco/Monaco.vue

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ onMounted(async () => {
105105
} else {
106106
watch(
107107
() => props.filename,
108-
() => {
108+
(_, oldFilename) => {
109109
if (!editorInstance) return
110110
const file = store.state.files[props.filename]
111111
if (!file) return null
@@ -114,10 +114,16 @@ onMounted(async () => {
114114
file.language,
115115
file.code
116116
)
117+
118+
const oldFile = oldFilename ? store.state.files[oldFilename] : null
119+
if (oldFile) {
120+
oldFile.editorViewState = editorInstance.saveViewState()
121+
}
122+
117123
editorInstance.setModel(model)
118124
119-
if (file.selection) {
120-
editorInstance.setSelection(file.selection)
125+
if (file.editorViewState) {
126+
editorInstance.restoreViewState(file.editorViewState)
121127
editorInstance.focus()
122128
}
123129
},
@@ -135,14 +141,6 @@ onMounted(async () => {
135141
emit('change', editorInstance.getValue())
136142
})
137143
138-
editorInstance.onDidChangeCursorSelection((e) => {
139-
const selection = e.selection
140-
const file = store.state.files[props.filename]
141-
if (file) {
142-
file.selection = selection
143-
}
144-
})
145-
146144
// update theme
147145
watch(replTheme, (n) => {
148146
editorInstance.updateOptions({

src/store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
SFCTemplateCompileOptions,
99
} from 'vue/compiler-sfc'
1010
import { OutputModes } from './output/types'
11-
import type { Selection } from 'monaco-editor-core'
11+
import type { editor } from 'monaco-editor-core'
1212

1313
const defaultMainFile = 'src/App.vue'
1414

@@ -52,7 +52,7 @@ export class File {
5252
css: '',
5353
ssr: '',
5454
}
55-
selection: Selection | null = null
55+
editorViewState: editor.ICodeEditorViewState | null = null
5656

5757
constructor(filename: string, code = '', hidden = false) {
5858
this.filename = filename

0 commit comments

Comments
 (0)