Skip to content

Commit dffe188

Browse files
committed
refactor: merge multiple watchers into one watchEffect
1 parent a1a2818 commit dffe188

File tree

1 file changed

+34
-64
lines changed

1 file changed

+34
-64
lines changed

src/monaco/Monaco.vue

Lines changed: 34 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
watch,
1010
computed,
1111
type Ref,
12+
watchEffect,
1213
} from 'vue'
1314
import * as monaco from 'monaco-editor-core'
1415
import { initMonaco } from './env'
@@ -44,7 +45,6 @@ const store = inject<Store>('store')!
4445
4546
initMonaco(store)
4647
47-
const lang = computed(() => (props.mode === 'css' ? 'css' : 'javascript'))
4848
const extension = computed(() => props.filename.split('.').at(-1))
4949
5050
const replTheme = inject<Ref<'dark' | 'light'>>('theme')!
@@ -58,9 +58,6 @@ onMounted(async () => {
5858
}
5959
6060
const editorInstance = monaco.editor.create(containerRef.value, {
61-
...(props.readonly
62-
? { value: props.value, language: lang.value }
63-
: { model: null }),
6461
fontSize: 13,
6562
theme: replTheme.value === 'light' ? theme.light : theme.dark,
6663
readOnly: props.readonly,
@@ -100,62 +97,42 @@ onMounted(async () => {
10097
}
10198
}
10299
103-
watch(
104-
() => props.value,
105-
(value) => {
106-
if (editorInstance.getValue() === value) return
107-
editorInstance.setValue(value || '')
108-
},
109-
{ immediate: true }
110-
)
111-
112-
watch(lang, (lang) =>
113-
monaco.editor.setModelLanguage(editorInstance.getModel()!, lang)
114-
)
100+
watchEffect(() => {
101+
if (editorInstance.getValue() !== props.value)
102+
editorInstance.setValue(props.value || '')
115103
116-
watch(
117-
() => props.readonly,
118-
(readonly) => {
119-
editorInstance.updateOptions({ readOnly: readonly })
120-
}
121-
)
122-
123-
if (!props.readonly) {
124-
watch(
125-
() => props.filename,
126-
(_, oldFilename) => {
127-
if (!editorInstance) return
128-
const file = store.state.files[props.filename]
129-
if (!file) return null
130-
const model = getOrCreateModel(
131-
monaco.Uri.parse(`file:///${props.filename}`),
132-
file.language,
133-
file.code
134-
)
135-
136-
const oldFile = oldFilename ? store.state.files[oldFilename] : null
137-
if (oldFile) {
138-
oldFile.editorViewState = editorInstance.saveViewState()
139-
}
140-
141-
editorInstance.setModel(model)
142-
143-
if (file.editorViewState) {
144-
editorInstance.restoreViewState(file.editorViewState)
145-
editorInstance.focus()
146-
}
147-
},
148-
{ immediate: true }
149-
)
150-
}
104+
editorInstance.updateOptions({
105+
readOnly: props.readonly,
106+
wordWrap: store.state.wordWrap ? 'on' : 'off',
107+
theme: replTheme.value === 'light' ? theme.light : theme.dark,
108+
})
109+
})
151110
152111
watch(
153-
() => store.state.wordWrap,
154-
() => {
155-
editorInstance.updateOptions({
156-
wordWrap: store.state.wordWrap ? 'on' : 'off',
157-
})
158-
}
112+
() => props.filename,
113+
(_, oldFilename) => {
114+
if (!editorInstance) return
115+
const file = store.state.files[props.filename]
116+
if (!file) return null
117+
const model = getOrCreateModel(
118+
monaco.Uri.parse(`file:///${props.filename}`),
119+
file.language,
120+
file.code
121+
)
122+
123+
const oldFile = oldFilename ? store.state.files[oldFilename] : null
124+
if (oldFile) {
125+
oldFile.editorViewState = editorInstance.saveViewState()
126+
}
127+
128+
editorInstance.setModel(model)
129+
130+
if (file.editorViewState) {
131+
editorInstance.restoreViewState(file.editorViewState)
132+
editorInstance.focus()
133+
}
134+
},
135+
{ immediate: true }
159136
)
160137
161138
await loadGrammars(monaco, editorInstance)
@@ -204,13 +181,6 @@ onMounted(async () => {
204181
emit('change', code)
205182
}
206183
})
207-
208-
// update theme
209-
watch(replTheme, (n) => {
210-
editorInstance.updateOptions({
211-
theme: n === 'light' ? theme.light : theme.dark,
212-
})
213-
})
214184
})
215185
216186
onBeforeUnmount(() => {

0 commit comments

Comments
 (0)