feat: 新增编辑器 toolbar 实现文件保存功能

This commit is contained in:
Frankie Huang 2025-04-13 18:15:25 +08:00
parent a9678c0400
commit 5b4f66b7af

View File

@ -17,6 +17,33 @@ async function greet() {
const markdownRef = ref(null);
const markdownCode = ref("# Hello Markdown")
const appendToolbar = [
{
name: "|",
},
{
name: "save-markdown-code",
icon: "fa-save", // font-awesome icon. editormd.css
title: "保存当前内容",
shortcut: [
navigator.userAgent.toUpperCase().indexOf('MAC') >= 0 ? "Cmd-S" : "Ctrl-S"
],
handler: () => {
writeFileContent();
},
nofocus: true,
},
{
name: "publish",
icon: "fa-cloud-upload",
title: "发布文章",
handler: () => {
// TODO markdown HTML
Message.warning('抱歉,该功能暂未开放');
},
nofocus: true,
},
];
// localStorage imageUploadURL
const imageUploadURLOfEditor = ref(localStorage.getItem('imageUploadURLOfEditor') || "")
function changeImageUploadURL(newImageUploadURL) {
@ -27,7 +54,6 @@ async function initMarkdownEditor() {
if (currentFilePath.value.length > 0) {
try {
markdownCode.value = await readTextFile(currentFilePath.value)
loadingCurrentFile.value = true
Message.success('已读取文件内容,并加载到编辑器中:' + currentFilePath.value);
} catch (err) {
Message.error('文件读取失败:' + err);
@ -88,7 +114,6 @@ function showFileTree(treeData) {
// <FolderTree> currentFilePath
// currentFilePath MarkdownEditor
const currentFilePath = ref(localStorage.getItem('currentSelectedFilePath') || "");
const loadingCurrentFile = ref(false);
async function fileSelected(fileNodeData) {
console.log(fileNodeData)
const filePath = fileNodeData.path
@ -99,7 +124,6 @@ async function readFileContent(filePath) {
try {
markdownRef.value.setMarkdownCode(await readTextFile(filePath))
currentFilePath.value = filePath
loadingCurrentFile.value = true
Message.success('已读取文件内容,并加载到编辑器中:' + filePath);
} catch (err) {
Message.error('文件读取失败:' + err);
@ -108,14 +132,12 @@ async function readFileContent(filePath) {
async function writeFileContent() {
//
if (currentFilePath.value.length == 0) {
Message.warning('当前编辑器暂未关联目标文件,请在左侧打开目录并选择一个文件')
return;
}
try {
//
if (!loadingCurrentFile.value) {
await writeTextFile(currentFilePath.value, markdownRef.value.getMarkdownCode())
}
loadingCurrentFile.value = false
Message.success('文件更新成功:' + currentFilePath.value);
} catch (err) {
Message.error('文件更新失败:' + err);
}
@ -149,7 +171,8 @@ onUnmounted(() => {
<MarkdownEditor ref="markdownRef" width="100%" height="100%" :autoInit="false"
:markdownCode="markdownCode" :imageUpload="true" :imageUploadURL="imageUploadURLOfEditor"
:imageUploadURLChange="changeImageUploadURL" :onload="reloadEditorHeight"
:onfullscreenExit="handleWindowResize" @update:markdownCode="writeFileContent" />
:onFullScreenExit="handleWindowResize" @update:markdownCode="newCode => markdownCode = newCode"
:appendToolbar="appendToolbar" />
</div>
</template>
</Split>