Skip to content

Commit ae80c5b

Browse files
authored
feat: CodeMirror support autoSave option (#249)
1 parent d5b0d40 commit ae80c5b

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/codemirror/CodeMirror.vue

+14-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const emit = defineEmits<(e: 'change', value: string) => void>()
2424
2525
const el = ref()
2626
const needAutoResize = inject('autoresize')
27+
const autoSave = inject('autosave')
2728
2829
onMounted(() => {
2930
const addonOptions = props.readonly
@@ -46,10 +47,6 @@ onMounted(() => {
4647
...addonOptions,
4748
})
4849
49-
editor.on('change', () => {
50-
emit('change', editor.getValue())
51-
})
52-
5350
watchEffect(() => {
5451
const cur = editor.getValue()
5552
if (props.value !== cur) {
@@ -73,6 +70,19 @@ onMounted(() => {
7370
}),
7471
)
7572
}
73+
74+
if (autoSave) {
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())
83+
}
84+
})
85+
}
7686
})
7787
</script>
7888

0 commit comments

Comments
 (0)