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

refactor: save the code with ctrl + s #150

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/codemirror/CodeMirror.vue
Expand Up @@ -20,7 +20,7 @@ const props = withDefaults(defineProps<Props>(), {
readonly: false,
})

const emit = defineEmits<(e: 'change', value: string) => void>()
const emit = defineEmits<(e: 'save', value: string) => void>()

const el = ref()
const needAutoResize = inject('autoresize')
Expand All @@ -36,6 +36,10 @@ onMounted(() => {
keyMap: 'sublime',
}

function save() {
emit('save', editor.getValue())
}

const editor = CodeMirror(el.value!, {
value: '',
mode: props.mode,
Expand All @@ -46,8 +50,23 @@ onMounted(() => {
...addonOptions,
})

editor.on('change', () => {
emit('change', editor.getValue())
let changeTimer: NodeJS.Timeout
editor.on('change', (e) => {
clearTimeout(changeTimer) // clear previous timer

changeTimer = setTimeout(() => {
save()
}, 5000)
})

editor.on('blur', () => {
save()
})
el.value.addEventListener('keydown', (e: KeyboardEvent) => {
if ((e.ctrlKey || e.metaKey) && e.code === 'KeyS') {
e.preventDefault()
save()
}
})

watchEffect(() => {
Expand Down
11 changes: 8 additions & 3 deletions src/editor/CodeMirrorEditor.vue
Expand Up @@ -10,8 +10,8 @@ defineOptions({
const props = defineProps<EditorProps>()
const emit = defineEmits<EditorEmits>()

const onChange = (code: string) => {
emit('change', code)
const onSave = (code: string) => {
emit('save', code)
}

const modes: Record<string, Props['mode']> = {
Expand Down Expand Up @@ -39,5 +39,10 @@ const activeMode = computed(() => {
</script>

<template>
<CodeMirror @change="onChange" :value="value" :mode="activeMode" />
<CodeMirror
ref="codeMirrorRef"
:value="value"
:mode="activeMode"
@save="onSave"
/>
</template>
7 changes: 3 additions & 4 deletions src/editor/EditorContainer.vue
@@ -1,7 +1,6 @@
<script setup lang="ts">
import FileSelector from './FileSelector.vue'
import Message from '../Message.vue'
import { debounce } from '../utils'
import { inject, ref, watch } from 'vue'
import { Store } from '../store'
import MessageToggle from './MessageToggle.vue'
Expand All @@ -16,9 +15,9 @@ const props = defineProps<{
const store = inject('store') as Store
const showMessage = ref(getItem())

const onChange = debounce((code: string) => {
function onSave(code: string) {
store.state.activeFile.code = code
}, 250)
}

function setItem() {
localStorage.setItem(SHOW_ERROR_KEY, showMessage.value ? 'true' : 'false')
Expand All @@ -38,7 +37,7 @@ watch(showMessage, () => {
<FileSelector />
<div class="editor-container">
<props.editorComponent
@change="onChange"
@save="onSave"
:value="store.state.activeFile.code"
:filename="store.state.activeFile.filename"
/>
Expand Down
6 changes: 3 additions & 3 deletions src/editor/MonacoEditor.vue
Expand Up @@ -9,14 +9,14 @@ defineOptions({
editorType: 'monaco',
})

const onChange = (code: string) => {
emit('change', code)
const onSave = (code: string) => {
emit('save', code)
}
</script>

<template>
<Monaco
@change="onChange"
@save="onSave"
:filename="filename"
:value="value"
:readonly="readonly"
Expand Down
2 changes: 1 addition & 1 deletion src/editor/types.ts
Expand Up @@ -10,7 +10,7 @@ export interface EditorProps {
}

export interface EditorEmits {
(e: 'change', code: string): void
(e: 'save', code: string): void
}

export type EditorComponentType = FunctionalComponent<
Expand Down
22 changes: 18 additions & 4 deletions src/monaco/Monaco.vue
Expand Up @@ -30,7 +30,7 @@ const props = withDefaults(
)

const emit = defineEmits<{
(e: 'change', value: string): void
(e: 'save', value: string): void
}>()

const containerRef = ref<HTMLDivElement>()
Expand Down Expand Up @@ -138,12 +138,26 @@ onMounted(async () => {

await loadGrammars(monaco, editorInstance)

function save() {
emit('save', editorInstance.getValue())
}

editorInstance.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => {
// ignore save event
save()
})

editorInstance.onDidBlurEditorWidget(() => {
save()
})

editorInstance.onDidChangeModelContent(() => {
emit('change', editorInstance.getValue())
let changeTimer: NodeJS.Timeout

editorInstance.onDidChangeModelContent((e) => {
clearTimeout(changeTimer) // clear previous timer

changeTimer = setTimeout(() => {
save()
}, 5000)
})

// update theme
Expand Down