+ v-model:leftSidebarState="leftSidebarState" :save="writeFileContent" @exportPDF="exportMarkdownPDF">
@@ -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" />
+