From f2fa1992e68408316732cd5d203ecb0cd1afd54f Mon Sep 17 00:00:00 2001 From: Frankie Huang Date: Thu, 1 May 2025 15:30:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20PDF=20=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 42 ++++++++++++++++++++++++++++++++--- src/components/MainEditor.vue | 30 +++++++++++++++++++++++-- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/src/App.vue b/src/App.vue index 0d28f19..daef869 100644 --- a/src/App.vue +++ b/src/App.vue @@ -12,7 +12,7 @@ @@ -24,8 +24,9 @@ import LeftSidebar from './components/LeftSidebar.vue' import MainEditor from './components/MainEditor.vue' import { ref, watch, useTemplateRef, nextTick, onMounted, onUnmounted } from "vue"; import { invoke } from "@tauri-apps/api/core"; -import { readTextFile, writeTextFile, exists } from '@tauri-apps/plugin-fs'; -import { Message } from 'view-ui-plus' +import { readTextFile, writeTextFile, writeFile, exists } from '@tauri-apps/plugin-fs'; +import { save } from '@tauri-apps/plugin-dialog'; +import { Message, Notice } from 'view-ui-plus' // 原始示例数据,仅供参考 const greetMsg = ref(""); @@ -79,6 +80,12 @@ watch(currentFilePath, async (newFilePath) => { localStorage.setItem('currentSelectedFilePath', newFilePath); await readFileContent(newFilePath); }) +const getFileNameFromFilePath = (filePath) => { + const fullFileName = filePath.split('/').pop(); + const splitResult = fullFileName.split("."); + splitResult.pop(); // 丢掉后缀 + return splitResult.join('.'); +} const mainEditor = ref(null); const markdownCode = ref("# Hello Markdown"); @@ -122,6 +129,35 @@ async function writeFileContent(markdownCode) { Message.error('文件更新失败:' + err); } } +async function exportMarkdownPDF(pdfBuffer) { + let fileName = 'markdown.pdf'; + if (currentFilePath.value.length > 0) { + fileName = getFileNameFromFilePath(currentFilePath.value) + '.pdf'; + } + + try { + const filePath = await save({ + title: '保存 PDF', // 对话框标题 + defaultPath: fileName, // 保存的文件名称 + }); + + if (filePath) { + await writeFile(filePath, new Uint8Array(pdfBuffer)); + Notice.success({ + title: '导出成功', + desc: 'PDF 已保存至 ' + filePath, + }); + } else { + console.log('用户主动取消'); + } + } catch (err) { + console.log(err); + Notice.error({ + title: '导出失败', + desc: '请将文件保存在用户有权限的目录之下', + }); + } +} const resizeHandler = () => { // splitRight 的宽度为视口宽度右边百分比,再减去 splitTrigger 的宽度 diff --git a/src/components/MainEditor.vue b/src/components/MainEditor.vue index 165fb26..6d31396 100644 --- a/src/components/MainEditor.vue +++ b/src/components/MainEditor.vue @@ -18,6 +18,8 @@ size="22" /> +