完善保存功能

This commit is contained in:
shikong 2024-02-20 01:37:03 +08:00
parent b0fa41e74a
commit 794f03be40
3 changed files with 78 additions and 23 deletions

View File

@ -48,7 +48,7 @@ const {t, locale} = useI18n()
const editorRef = ref() const editorRef = ref()
function initEditor(editorRef:Ref<any>){ function initEditor(editorRef: Ref<any>) {
let editor = new Editor({ let editor = new Editor({
theme: "dark", theme: "dark",
el: editorRef.value, el: editorRef.value,
@ -90,7 +90,8 @@ function initEditor(editorRef:Ref<any>){
return editor; return editor;
} }
let editor:any = {}
let editor: any = {}
onMounted(() => { onMounted(() => {
editor = initEditor(editorRef); editor = initEditor(editorRef);
watch(locale, value => { watch(locale, value => {
@ -103,10 +104,10 @@ onMounted(() => {
}) })
const ctx = reactive({ const ctx = reactive({
data: "" openFilePath: ""
}) })
function openFile(){ function openFile() {
OpenFileDialog({ OpenFileDialog({
title: `${t("general.open")} markdown ${t("general.file")}`, title: `${t("general.open")} markdown ${t("general.file")}`,
filters: [ filters: [
@ -115,55 +116,105 @@ function openFile(){
pattern: "*.md;*.markdown" pattern: "*.md;*.markdown"
} }
] ]
}).then(path=>{ }).then(path => {
console.log(path) console.log(path)
if(!path){ if (!path) {
return return
} }
useApi().readFile({path}).then(resp=>{ useApi().readFile({path}).then(resp => {
ctx.openFilePath = path
editor.setMarkdown(resp.data) editor.setMarkdown(resp.data)
}) })
}) })
} }
function saveFile(){ function getDir(path: string) {
let position = path.lastIndexOf("/")
if (position === -1) {
position = path.lastIndexOf("\\")
}
return path.substring(0, position)
}
function getFileName(path: string) {
let position = path.lastIndexOf("/")
if (position === -1) {
position = path.lastIndexOf("\\")
}
return path.substring(position + 1)
}
function createFile() {
SaveFileDialog({ SaveFileDialog({
title: `${t("general.save")} markdown`, title: `${t("general.new")} markdown ${t("general.file")}`,
filters: [ filters: [
{ {
displayName: "Markdown", displayName: "Markdown",
pattern: "*.md;*.markdown" pattern: "*.md;*.markdown"
} }
] ]
}).then(path=>{ }).then(path => {
console.log(path) ctx.openFilePath = path
editor.setMarkdown("")
})
}
if(!path){ function saveFile(saveAs: boolean = false) {
return
}
function writeFile() {
useApi().saveFile({ useApi().saveFile({
fileName: path, fileName: ctx.openFilePath,
data: new Blob([editor.getMarkdown()], { data: new Blob([editor.getMarkdown()], {
type: 'text/plain' type: 'text/plain'
}) })
}).then(resp=>{ }).then(resp => {
let res = resp.data let res = resp.data
if(res.code === 200){ if (res.code === 200) {
ElMessage.success(res.data) ElMessage.success(res.data)
} else { } else {
ElMessage.error(res.msg) ElMessage.error(res.msg)
} }
}) })
}
if (ctx.openFilePath && !saveAs) {
writeFile()
} else {
SaveFileDialog({
title: `${t("general.save")} markdown ${t("general.file")}`,
defaultDirectory: getDir(ctx.openFilePath),
defaultFileName: getFileName(ctx.openFilePath),
filters: [
{
displayName: "Markdown",
pattern: "*.md;*.markdown"
}
]
}).then(path => {
console.log(path)
ctx.openFilePath = path
if (!path) {
return
}
writeFile()
}) })
}
} }
</script> </script>
<template> <template>
<el-button type="primary" @click="createFile">{{ t("general.new") }}</el-button>
<el-button type="primary" @click="openFile">{{ t("general.open") }}</el-button> <el-button type="primary" @click="openFile">{{ t("general.open") }}</el-button>
<el-button type="success" @click="saveFile">{{ t("general.save")}}</el-button> <el-button type="success" :disabled="!ctx.openFilePath" @click="saveFile()">{{ t("general.save") }}</el-button>
<el-button type="success" @click="saveFile(true)">{{ t("general.saveAs") }}</el-button>
<div ref="editorRef" class="overscroll-none"/> <div ref="editorRef" class="overscroll-none"/>
</template> </template>

View File

@ -32,6 +32,8 @@
"general": { "general": {
"open": "open", "open": "open",
"save": "save", "save": "save",
"file": "file" "saveAs": "save as",
"file": "file",
"new": "new"
} }
} }

View File

@ -32,6 +32,8 @@
"general": { "general": {
"open": "打开", "open": "打开",
"save": "保存", "save": "保存",
"file": "文件" "saveAs": "另存为",
"file": "文件",
"new": "新建"
} }
} }