Skip to content

Commit db8c1bd

Browse files
authored
feat(monaco): support to keep selection and cursor position (#99)
1 parent b6a0b3b commit db8c1bd

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/monaco/Monaco.vue

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,26 +105,26 @@ onMounted(async () => {
105105
editor.value = editorInstance
106106
107107
// Support for semantic highlighting
108-
const t = (editorInstance as any)._themeService._theme;
108+
const t = (editorInstance as any)._themeService._theme
109109
t.getTokenStyleMetadata = (
110110
type: string,
111111
modifiers: string[],
112112
_language: string
113113
) => {
114-
const _readonly = modifiers.includes('readonly');
114+
const _readonly = modifiers.includes('readonly')
115115
switch (type) {
116116
case 'function':
117117
case 'method':
118-
return { foreground: 12 };
118+
return { foreground: 12 }
119119
case 'class':
120-
return { foreground: 11 };
120+
return { foreground: 11 }
121121
case 'variable':
122122
case 'property':
123-
return { foreground: _readonly ? 21 : 9 };
123+
return { foreground: _readonly ? 21 : 9 }
124124
default:
125-
return { foreground: 0 };
125+
return { foreground: 0 }
126126
}
127-
};
127+
}
128128
129129
if (props.readonly) {
130130
watch(
@@ -147,6 +147,11 @@ onMounted(async () => {
147147
file.code
148148
)
149149
editorInstance.setModel(model)
150+
151+
if (file.selection) {
152+
editorInstance.setSelection(file.selection)
153+
editorInstance.focus()
154+
}
150155
},
151156
{ immediate: true }
152157
)
@@ -161,6 +166,14 @@ onMounted(async () => {
161166
editorInstance.onDidChangeModelContent(() => {
162167
emits('change', editorInstance.getValue())
163168
})
169+
170+
editorInstance.onDidChangeCursorSelection(e => {
171+
const selection = e.selection
172+
const file = store.state.files[props.filename]
173+
if (file) {
174+
file.selection = selection
175+
}
176+
})
164177
})
165178
166179
onBeforeUnmount(() => {

src/store.ts

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

1213
const defaultMainFile = 'App.vue'
1314

@@ -35,6 +36,7 @@ export class File {
3536
css: '',
3637
ssr: ''
3738
}
39+
selection: Selection | null = null
3840

3941
constructor(filename: string, code = '', hidden = false) {
4042
this.filename = filename

0 commit comments

Comments
 (0)