From 568f1cf71da708d23b305856b748d5bb64105527 Mon Sep 17 00:00:00 2001 From: sight <1453017105@qq.com> Date: Fri, 7 Jul 2023 14:20:55 +0800 Subject: [PATCH 1/2] refactor: save the code editor view state in the store --- src/monaco/Monaco.vue | 20 +++++++++----------- src/store.ts | 4 ++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/monaco/Monaco.vue b/src/monaco/Monaco.vue index 677e8c2f..c67410a6 100644 --- a/src/monaco/Monaco.vue +++ b/src/monaco/Monaco.vue @@ -105,7 +105,7 @@ onMounted(async () => { } else { watch( () => props.filename, - () => { + (_, oldFilename) => { if (!editorInstance) return const file = store.state.files[props.filename] if (!file) return null @@ -114,10 +114,16 @@ onMounted(async () => { file.language, file.code ) + + if (oldFilename) { + store.state.files[oldFilename].editorViewState = + editorInstance.saveViewState() + } + editorInstance.setModel(model) - if (file.selection) { - editorInstance.setSelection(file.selection) + if (file.editorViewState) { + editorInstance.restoreViewState(file.editorViewState) editorInstance.focus() } }, @@ -135,14 +141,6 @@ onMounted(async () => { emit('change', editorInstance.getValue()) }) - editorInstance.onDidChangeCursorSelection((e) => { - const selection = e.selection - const file = store.state.files[props.filename] - if (file) { - file.selection = selection - } - }) - // update theme watch(replTheme, (n) => { editorInstance.updateOptions({ diff --git a/src/store.ts b/src/store.ts index 88bd21bb..38a72253 100644 --- a/src/store.ts +++ b/src/store.ts @@ -8,7 +8,7 @@ import { SFCTemplateCompileOptions, } from 'vue/compiler-sfc' import { OutputModes } from './output/types' -import type { Selection } from 'monaco-editor-core' +import type { editor } from 'monaco-editor-core' const defaultMainFile = 'src/App.vue' @@ -52,7 +52,7 @@ export class File { css: '', ssr: '', } - selection: Selection | null = null + editorViewState: editor.ICodeEditorViewState | null = null constructor(filename: string, code = '', hidden = false) { this.filename = filename From d531da1fa4a2dd08060bb35d2f362efb4a4808b2 Mon Sep 17 00:00:00 2001 From: sight <1453017105@qq.com> Date: Fri, 7 Jul 2023 15:05:18 +0800 Subject: [PATCH 2/2] chore: update code --- src/monaco/Monaco.vue | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/monaco/Monaco.vue b/src/monaco/Monaco.vue index c67410a6..fb6f14ef 100644 --- a/src/monaco/Monaco.vue +++ b/src/monaco/Monaco.vue @@ -115,9 +115,9 @@ onMounted(async () => { file.code ) - if (oldFilename) { - store.state.files[oldFilename].editorViewState = - editorInstance.saveViewState() + const oldFile = oldFilename ? store.state.files[oldFilename] : null + if (oldFile) { + oldFile.editorViewState = editorInstance.saveViewState() } editorInstance.setModel(model)