Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
qixing-ai committed Nov 22, 2024
1 parent fcfd236 commit 6ad5df7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"echarts": "^5.5.1",
"highlight.js": "^11.9.0",
"iconv-lite": "^0.6.3",
"lodash": "^4.17.21",
"markdown-it": "^14.1.0",
"mitt": "^3.0.1",
"moment": "^2.30.1",
Expand Down
36 changes: 13 additions & 23 deletions src/components/chatcomponents/CodeEditor.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<template>
<a-button type="default" size="small" style="position:sticky; top: 0; right:0; z-index: 1000;" @click="undoEdit">撤销</a-button>
<Codemirror
ref="cmRef"
v-model="code"
:extensions="extensions"
@ready="handleReady"
ref="cmRef"
v-model="code"
:extensions="extensions"
@ready="handleReady"
/>
</template>

<script>
import {defineComponent, ref, watch, onMounted, onBeforeUnmount} from 'vue'
import {defineComponent, ref, watch, onMounted} from 'vue'
import {Codemirror} from 'vue-codemirror'
import {oneDark} from '@codemirror/theme-one-dark'
import {materialLight} from '@ddietr/codemirror-themes/material-light'
Expand All @@ -24,6 +24,7 @@ import {tokyoNight} from '@ddietr/codemirror-themes/tokyo-night'
import {tokyoNightStorm} from '@ddietr/codemirror-themes/tokyo-night-storm'
import {tokyoNightDay} from '@ddietr/codemirror-themes/tokyo-night-day'
import {undo} from '@codemirror/commands'
import {debounce} from 'lodash' // 引入 lodash 的 debounce
const {ipcRenderer} = require('electron');
Expand All @@ -48,7 +49,6 @@ import {liquid} from '@codemirror/lang-liquid'
import {wast} from '@codemirror/lang-wast'
import {java} from '@codemirror/lang-java'
export default defineComponent({
components: {
Codemirror
Expand Down Expand Up @@ -140,31 +140,21 @@ export default defineComponent({
extensions.value = [getLanguageExtension(newLanguage), theme]
})
const saveFile = async () => {
// 使用 debounce 进行自动保存,间隔设置为 500ms
const debouncedSaveFile = debounce(async () => {
await ipcRenderer.invoke('saveFileContent', props.filePath, code.value);
}
}, 500);
watch(() => code.value, () => {
debouncedSaveFile();
})
const undoEdit = () => {
if (cmRef.value && cmRef.value.view) {
undo({state: cmRef.value.view.state, dispatch: cmRef.value.view.dispatch});
}
};
const handleKeyDown = (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === 's') {
e.preventDefault();
saveFile();
}
}
onMounted(() => {
window.addEventListener('keydown', handleKeyDown);
})
onBeforeUnmount(() => {
window.removeEventListener('keydown', handleKeyDown);
})
return {
code,
handleReady,
Expand Down

0 comments on commit 6ad5df7

Please sign in to comment.