Skip to content

Commit d90082a

Browse files
phk422sxzz
andauthored
feat: reactivity autoSave option (#266)
Co-authored-by: 三咲智子 Kevin Deng <[email protected]>
1 parent 389d255 commit d90082a

File tree

3 files changed

+53
-28
lines changed

3 files changed

+53
-28
lines changed

src/codemirror/CodeMirror.vue

+29-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
<template>
2-
<div ref="container" class="editor" />
2+
<div
3+
ref="container"
4+
class="editor"
5+
@keydown.ctrl.s.prevent="emitChangeEvent"
6+
@keydown.meta.s.prevent="emitChangeEvent"
7+
/>
38
</template>
49

510
<script setup lang="ts">
611
import type { ModeSpec, ModeSpecOptions } from 'codemirror'
7-
import { inject, onMounted, useTemplateRef, watchEffect } from 'vue'
12+
import {
13+
inject,
14+
onMounted,
15+
onWatcherCleanup,
16+
useTemplateRef,
17+
watch,
18+
watchEffect,
19+
} from 'vue'
820
import { debounce } from '../utils'
921
import CodeMirror from './codemirror'
1022
import { injectKeyProps } from '../../src/types'
@@ -25,6 +37,11 @@ const emit = defineEmits<(e: 'change', value: string) => void>()
2537
2638
const el = useTemplateRef('container')
2739
const { autoResize, autoSave } = inject(injectKeyProps)!
40+
let editor: CodeMirror.Editor
41+
42+
const emitChangeEvent = () => {
43+
emit('change', editor.getValue())
44+
}
2845
2946
onMounted(() => {
3047
const addonOptions = props.readonly
@@ -37,7 +54,7 @@ onMounted(() => {
3754
keyMap: 'sublime',
3855
}
3956
40-
const editor = CodeMirror(el.value!, {
57+
editor = CodeMirror(el.value!, {
4158
value: '',
4259
mode: props.mode,
4360
readOnly: props.readonly,
@@ -71,18 +88,16 @@ onMounted(() => {
7188
)
7289
}
7390
74-
if (autoSave.value) {
75-
editor.on('change', () => {
76-
emit('change', editor.getValue())
77-
})
78-
} else {
79-
el.value!.addEventListener('keydown', (e: KeyboardEvent) => {
80-
if (e.ctrlKey && e.key === 's') {
81-
e.preventDefault()
82-
emit('change', editor.getValue())
91+
watch(
92+
autoSave,
93+
(autoSave) => {
94+
if (autoSave) {
95+
editor.on('change', emitChangeEvent)
96+
onWatcherCleanup(() => editor.off('change', emitChangeEvent))
8397
}
84-
})
85-
}
98+
},
99+
{ immediate: true },
100+
)
86101
})
87102
</script>
88103

src/monaco/Monaco.vue

+23-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
nextTick,
66
onBeforeUnmount,
77
onMounted,
8+
onWatcherCleanup,
89
ref,
910
shallowRef,
1011
useTemplateRef,
@@ -47,6 +48,11 @@ initMonaco(store.value)
4748
4849
const lang = computed(() => (props.mode === 'css' ? 'css' : 'javascript'))
4950
51+
let editorInstance: monaco.editor.IStandaloneCodeEditor
52+
function emitChangeEvent() {
53+
emit('change', editorInstance.getValue())
54+
}
55+
5056
onMounted(async () => {
5157
const theme = await import('./highlight').then((r) => r.registerHighlighter())
5258
ready.value = true
@@ -55,8 +61,7 @@ onMounted(async () => {
5561
if (!containerRef.value) {
5662
throw new Error('Cannot find containerRef')
5763
}
58-
59-
const editorInstance = monaco.editor.create(containerRef.value, {
64+
editorInstance = monaco.editor.create(containerRef.value, {
6065
...(props.readonly
6166
? { value: props.value, language: lang.value }
6267
: { model: null }),
@@ -146,18 +151,17 @@ onMounted(async () => {
146151
// ignore save event
147152
})
148153
149-
if (autoSave) {
150-
editorInstance.onDidChangeModelContent(() => {
151-
emit('change', editorInstance.getValue())
152-
})
153-
} else {
154-
containerRef.value.addEventListener('keydown', (e: KeyboardEvent) => {
155-
if (e.ctrlKey && e.key === 's') {
156-
e.preventDefault()
157-
emit('change', editorInstance.getValue())
154+
watch(
155+
autoSave,
156+
(autoSave) => {
157+
if (autoSave) {
158+
const disposable =
159+
editorInstance.onDidChangeModelContent(emitChangeEvent)
160+
onWatcherCleanup(() => disposable.dispose())
158161
}
159-
})
160-
}
162+
},
163+
{ immediate: true },
164+
)
161165
162166
// update theme
163167
watch(replTheme, (n) => {
@@ -173,7 +177,12 @@ onBeforeUnmount(() => {
173177
</script>
174178

175179
<template>
176-
<div ref="container" class="editor" />
180+
<div
181+
ref="container"
182+
class="editor"
183+
@keydown.ctrl.s.prevent="emitChangeEvent"
184+
@keydown.meta.s.prevent="emitChangeEvent"
185+
/>
177186
</template>
178187

179188
<style>

test/main.ts

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const App = {
7272
// wordWrap: 'on',
7373
},
7474
},
75+
// autoSave: false,
7576
})
7677
},
7778
}

0 commit comments

Comments
 (0)