feat: 支持 PDF 导出功能

This commit is contained in:
Frankie Huang 2025-05-01 01:06:55 +08:00
parent 823847f6b4
commit 35ab6ec79f
2 changed files with 67 additions and 5 deletions

View File

@ -12,7 +12,7 @@
<template #right> <template #right>
<div ref="splitRight" class="split-right"> <div ref="splitRight" class="split-right">
<MainEditor ref="mainEditor" v-model:markdownCode="markdownCode" <MainEditor ref="mainEditor" v-model:markdownCode="markdownCode"
v-model:leftSidebarState="leftSidebarState" :save="writeFileContent"> v-model:leftSidebarState="leftSidebarState" :save="writeFileContent" @exportPDF="exportMarkdownPDF">
</MainEditor> </MainEditor>
</div> </div>
</template> </template>
@ -24,8 +24,9 @@ import LeftSidebar from './components/LeftSidebar.vue'
import MainEditor from './components/MainEditor.vue' import MainEditor from './components/MainEditor.vue'
import { ref, watch, useTemplateRef, nextTick, onMounted, onUnmounted } from "vue"; import { ref, watch, useTemplateRef, nextTick, onMounted, onUnmounted } from "vue";
import { invoke } from "@tauri-apps/api/core"; import { invoke } from "@tauri-apps/api/core";
import { readTextFile, writeTextFile, exists } from '@tauri-apps/plugin-fs'; import { readTextFile, writeTextFile, writeFile, exists } from '@tauri-apps/plugin-fs';
import { Message } from 'view-ui-plus' import { save } from '@tauri-apps/plugin-dialog';
import { Message, Notice } from 'view-ui-plus'
// //
const greetMsg = ref(""); const greetMsg = ref("");
@ -79,6 +80,12 @@ watch(currentFilePath, async (newFilePath) => {
localStorage.setItem('currentSelectedFilePath', newFilePath); localStorage.setItem('currentSelectedFilePath', newFilePath);
await readFileContent(newFilePath); await readFileContent(newFilePath);
}) })
const getFileNameFromFilePath = (filePath) => {
const fullFileName = currentFilePath.value.split('/').pop();
const splitResult = fullFileName.split(".");
splitResult.pop(); //
return splitResult.join('.');
}
const mainEditor = ref(null); const mainEditor = ref(null);
const markdownCode = ref("# Hello Markdown"); const markdownCode = ref("# Hello Markdown");
@ -122,6 +129,35 @@ async function writeFileContent(markdownCode) {
Message.error('文件更新失败:' + err); 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 = () => { const resizeHandler = () => {
// splitRight splitTrigger // splitRight splitTrigger

View File

@ -18,6 +18,8 @@
size="22" /> size="22" />
</template> </template>
</NormalToolbar> </NormalToolbar>
<ExportPDF :modelValue="editorState.text" :customize="customizePDF"
@onProgress="handleExportProgress" />
<NormalToolbar title="发布文章" @onClick="publish"> <NormalToolbar title="发布文章" @onClick="publish">
<template #trigger> <template #trigger>
<Icon class="md-editor-icon" type="md-cloud-upload" size="22" /> <Icon class="md-editor-icon" type="md-cloud-upload" size="22" />
@ -42,8 +44,9 @@
<script setup> <script setup>
import 'md-editor-v3/lib/style.css'; import 'md-editor-v3/lib/style.css';
import '@vavt/v3-extension/lib/asset/PreviewThemeSwitch.css'; import '@vavt/v3-extension/lib/asset/PreviewThemeSwitch.css';
import '@vavt/v3-extension/lib/asset/ExportPDF.css';
import { MdEditor, MdCatalog, NormalToolbar, DropdownToolbar } from 'md-editor-v3'; import { MdEditor, MdCatalog, NormalToolbar, DropdownToolbar } from 'md-editor-v3';
import { ThemeSwitch, PreviewThemeSwitch } from '@vavt/v3-extension'; import { ThemeSwitch, PreviewThemeSwitch, ExportPDF } from '@vavt/v3-extension';
import { ref, reactive, watch } from "vue"; import { ref, reactive, watch } from "vue";
import { Message } from 'view-ui-plus' import { Message } from 'view-ui-plus'
@ -70,6 +73,7 @@ const props = defineProps({
const emit = defineEmits([ const emit = defineEmits([
'update:markdownCode', 'update:markdownCode',
'update:leftSidebarState', 'update:leftSidebarState',
'exportPDF',
]); ]);
// //
@ -126,6 +130,7 @@ const editorState = reactive({
'next', 'next',
'save', 'save',
2, 2,
3,
'=', '=',
'prettier', 'prettier',
'pageFullscreen', 'pageFullscreen',
@ -135,10 +140,11 @@ const editorState = reactive({
'htmlPreview', 'htmlPreview',
'catalog', 'catalog',
'github', 'github',
3,
4, 4,
5,
], ],
toolbarsExclude: ['fullscreen', 'github'], // github toolbarsExclude: ['fullscreen', 'github'], // github
pdfIns: null, // PDF jsPDF
}); });
// markdownCode text // markdownCode text
watch(() => props.markdownCode, (newCode) => { watch(() => props.markdownCode, (newCode) => {
@ -180,6 +186,26 @@ const getPreviewedHTML = (html) => {
console.log(html); console.log(html);
} }
const customizePDF = (ins) => {
// jsPDF editorState.pdfIns 便
editorState.pdfIns = ins;
}
const handleExportProgress = (progress) => {
// console.log(`Export progress: ${progress.ratio * 100}%`);
// 100%
if (progress.ratio == 1) {
// ExportPDF
if (window.__TAURI_INTERNALS__ === undefined) {
return;
}
// tauri API
if (editorState.pdfIns !== null) {
emit('exportPDF', editorState.pdfIns.output('arraybuffer'));
}
}
};
const publish = () => { const publish = () => {
// TODO markdown HTML // TODO markdown HTML
Message.warning('抱歉,该功能暂未开放'); Message.warning('抱歉,该功能暂未开放');