feat: 支持 PDF 导出功能

This commit is contained in:
Frankie Huang 2025-05-01 15:30:24 +08:00
parent 94ebd22875
commit f2fa1992e6
2 changed files with 67 additions and 5 deletions

View File

@ -12,7 +12,7 @@
<template #right>
<div ref="splitRight" class="split-right">
<MainEditor ref="mainEditor" v-model:markdownCode="markdownCode"
v-model:leftSidebarState="leftSidebarState" :save="writeFileContent">
v-model:leftSidebarState="leftSidebarState" :save="writeFileContent" @exportPDF="exportMarkdownPDF">
</MainEditor>
</div>
</template>
@ -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

View File

@ -18,6 +18,8 @@
size="22" />
</template>
</NormalToolbar>
<ExportPDF :modelValue="editorState.text" :customize="customizePDF"
@onProgress="handleExportProgress" />
<NormalToolbar title="发布文章" @onClick="publish">
<template #trigger>
<Icon class="md-editor-icon" type="md-cloud-upload" size="22" />
@ -42,8 +44,9 @@
<script setup>
import 'md-editor-v3/lib/style.css';
import '@vavt/v3-extension/lib/asset/PreviewThemeSwitch.css';
import '@vavt/v3-extension/lib/asset/ExportPDF.css';
import { MdEditor, MdCatalog, NormalToolbar } 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 { Message } from 'view-ui-plus'
@ -70,6 +73,7 @@ const props = defineProps({
const emit = defineEmits([
'update:markdownCode',
'update:leftSidebarState',
'exportPDF',
]);
//
@ -126,6 +130,7 @@ const editorState = reactive({
'next',
'save',
2,
3,
'=',
'prettier',
'pageFullscreen',
@ -135,10 +140,11 @@ const editorState = reactive({
'htmlPreview',
'catalog',
'github',
3,
4,
5,
],
toolbarsExclude: ['fullscreen', 'github'], // github
pdfIns: null, // PDF jsPDF
});
// markdownCode text
watch(() => props.markdownCode, (newCode) => {
@ -180,6 +186,26 @@ const getPreviewedHTML = (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 = () => {
// TODO markdown HTML
Message.warning('抱歉,该功能暂未开放');