Skip to content

Commit

Permalink
feat: reactivity autoSave option (#266)
Browse files Browse the repository at this point in the history
Co-authored-by: 三咲智子 Kevin Deng <[email protected]>
  • Loading branch information
phk422 and sxzz authored Sep 7, 2024
1 parent 389d255 commit d90082a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
43 changes: 29 additions & 14 deletions src/codemirror/CodeMirror.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
<template>
<div ref="container" class="editor" />
<div
ref="container"
class="editor"
@keydown.ctrl.s.prevent="emitChangeEvent"
@keydown.meta.s.prevent="emitChangeEvent"
/>
</template>

<script setup lang="ts">
import type { ModeSpec, ModeSpecOptions } from 'codemirror'
import { inject, onMounted, useTemplateRef, watchEffect } from 'vue'
import {
inject,
onMounted,
onWatcherCleanup,
useTemplateRef,
watch,
watchEffect,
} from 'vue'
import { debounce } from '../utils'
import CodeMirror from './codemirror'
import { injectKeyProps } from '../../src/types'
Expand All @@ -25,6 +37,11 @@ const emit = defineEmits<(e: 'change', value: string) => void>()
const el = useTemplateRef('container')
const { autoResize, autoSave } = inject(injectKeyProps)!
let editor: CodeMirror.Editor
const emitChangeEvent = () => {
emit('change', editor.getValue())
}
onMounted(() => {
const addonOptions = props.readonly
Expand All @@ -37,7 +54,7 @@ onMounted(() => {
keyMap: 'sublime',
}
const editor = CodeMirror(el.value!, {
editor = CodeMirror(el.value!, {
value: '',
mode: props.mode,
readOnly: props.readonly,
Expand Down Expand Up @@ -71,18 +88,16 @@ onMounted(() => {
)
}
if (autoSave.value) {
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())
watch(
autoSave,
(autoSave) => {
if (autoSave) {
editor.on('change', emitChangeEvent)
onWatcherCleanup(() => editor.off('change', emitChangeEvent))
}
})
}
},
{ immediate: true },
)
})
</script>

Expand Down
37 changes: 23 additions & 14 deletions src/monaco/Monaco.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
nextTick,
onBeforeUnmount,
onMounted,
onWatcherCleanup,
ref,
shallowRef,
useTemplateRef,
Expand Down Expand Up @@ -47,6 +48,11 @@ initMonaco(store.value)
const lang = computed(() => (props.mode === 'css' ? 'css' : 'javascript'))
let editorInstance: monaco.editor.IStandaloneCodeEditor
function emitChangeEvent() {
emit('change', editorInstance.getValue())
}
onMounted(async () => {
const theme = await import('./highlight').then((r) => r.registerHighlighter())
ready.value = true
Expand All @@ -55,8 +61,7 @@ onMounted(async () => {
if (!containerRef.value) {
throw new Error('Cannot find containerRef')
}
const editorInstance = monaco.editor.create(containerRef.value, {
editorInstance = monaco.editor.create(containerRef.value, {
...(props.readonly
? { value: props.value, language: lang.value }
: { model: null }),
Expand Down Expand Up @@ -146,18 +151,17 @@ onMounted(async () => {
// ignore save event
})
if (autoSave) {
editorInstance.onDidChangeModelContent(() => {
emit('change', editorInstance.getValue())
})
} else {
containerRef.value.addEventListener('keydown', (e: KeyboardEvent) => {
if (e.ctrlKey && e.key === 's') {
e.preventDefault()
emit('change', editorInstance.getValue())
watch(
autoSave,
(autoSave) => {
if (autoSave) {
const disposable =
editorInstance.onDidChangeModelContent(emitChangeEvent)
onWatcherCleanup(() => disposable.dispose())
}
})
}
},
{ immediate: true },
)
// update theme
watch(replTheme, (n) => {
Expand All @@ -173,7 +177,12 @@ onBeforeUnmount(() => {
</script>

<template>
<div ref="container" class="editor" />
<div
ref="container"
class="editor"
@keydown.ctrl.s.prevent="emitChangeEvent"
@keydown.meta.s.prevent="emitChangeEvent"
/>
</template>

<style>
Expand Down
1 change: 1 addition & 0 deletions test/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const App = {
// wordWrap: 'on',
},
},
// autoSave: false,
})
},
}
Expand Down

0 comments on commit d90082a

Please sign in to comment.