Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reactivity autoSave option #266

Merged
merged 10 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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