Skip to content

Commit

Permalink
feat: CodeMirror support autoSave option (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
btea committed May 24, 2024
1 parent d5b0d40 commit ae80c5b
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/codemirror/CodeMirror.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -46,10 +47,6 @@ onMounted(() => {
...addonOptions,
})
editor.on('change', () => {
emit('change', editor.getValue())
})
watchEffect(() => {
const cur = editor.getValue()
if (props.value !== cur) {
Expand All @@ -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())
}
})
}
})
</script>

Expand Down

0 comments on commit ae80c5b

Please sign in to comment.