diff --git a/src/codemirror/CodeMirror.vue b/src/codemirror/CodeMirror.vue index 47cca9c5..6dae3df7 100644 --- a/src/codemirror/CodeMirror.vue +++ b/src/codemirror/CodeMirror.vue @@ -24,6 +24,7 @@ const emit = defineEmits<(e: 'change', value: string) => void>() const el = ref() const needAutoResize = inject('autoresize') +const autoSave = inject('autosave') onMounted(() => { const addonOptions = props.readonly @@ -46,10 +47,6 @@ onMounted(() => { ...addonOptions, }) - editor.on('change', () => { - emit('change', editor.getValue()) - }) - watchEffect(() => { const cur = editor.getValue() if (props.value !== cur) { @@ -73,6 +70,19 @@ onMounted(() => { }), ) } + + if (autoSave) { + editor.on('change', () => { + emit('change', editor.getValue()) + }) + } else { + el.value!.addEventListener('keydown', (e: KeyboardEvent) => { + if (e.ctrlKey && e.key === 's') { + e.preventDefault() + emit('change', editor.getValue()) + } + }) + } })