Browse Source

feat: md

master
betaqi 2 weeks ago
parent
commit
33c5ee1d43
  1. 4
      package.json
  2. 164
      src/pages/fileDoc/components/markdownDrawer.vue

4
package.json

@ -14,7 +14,6 @@ @@ -14,7 +14,6 @@
"docker-save": "docker save -o ./docker_output/chuneng-web.tar chuneng-web"
},
"dependencies": {
"@element-plus/icons-vue": "^2.3.1",
"@iconify-json/ep": "^1.1.12",
"@iconify/json": "^2.2.217",
@ -32,6 +31,9 @@ @@ -32,6 +31,9 @@
"cropperjs": "^1.6.2",
"crypto-js": "^4.2.0",
"dayjs": "^1.11.11",
"html2canvas": "^1.4.1",
"jspdf": "^2.5.2",
"jspdf-html2canvas": "^1.5.2",
"jszmq": "^0.1.2",
"lodash": "^4.17.21",
"mitt": "^3.0.1",

164
src/pages/fileDoc/components/markdownDrawer.vue

@ -29,7 +29,8 @@ @@ -29,7 +29,8 @@
<script setup lang="ts">
import { isResError, useMessage } from '@/hooks/useMessage'
import { cloneDeep } from 'lodash'
import html2canvas from 'html2canvas'
import jsPDF from 'jspdf'
import EDFSButton from '@/components/dashboard/Edfs-button/index.vue'
import Vditor from 'vditor'
import { createImage, type ContentType } from '@/api/module/eam/device/document'
@ -84,12 +85,61 @@ function initEditor() { @@ -84,12 +85,61 @@ function initEditor() {
enable: false,
},
after: () => {
// document.getElementById('exportButton').addEventListener('click', () => {
// const content = vditor.getValue() //
// const filename = 'my-exported-document.md' //
// vditor.export(filename, content) //
const exportMdBtn = document.querySelector(
".vditor-hint button[data-type='markdown']"
) as HTMLElement
const exportPdfBtn = document.querySelector(
".vditor-hint button[data-type='pdf']"
) as HTMLElement
const exportHtmlBtn = document.querySelector(
".vditor-hint button[data-type='html']"
) as HTMLElement
// const fileName = props.data.fileName.split(//)
// fileName .md
const fileName = /(.*)\.md/.exec(props.data.fileName)?.[1] ?? '未命名'
console.log(fileName)
if (exportMdBtn) {
exportMdBtn.onclick = event => {
event.preventDefault()
event.stopPropagation()
exportMarkdown(contentEditor.value as Vditor, `${fileName}.md`)
}
}
if (exportPdfBtn) {
exportPdfBtn.onclick = event => {
event.preventDefault()
// event.stopPropagation()
// exportToPDF(contentEditor.value as Vditor, `${fileName}.pdf`)
}
}
if (exportHtmlBtn) {
exportHtmlBtn.onclick = event => {
event.preventDefault()
event.stopPropagation()
exportHTML(contentEditor.value as Vditor, `${fileName}.html`)
}
}
// if (
// contentEditor.value?.vditor.toolbar &&
// contentEditor.value.vditor.toolbar.elements
// ) {
// const el = contentEditor.value.vditor.toolbar.elements.export?.lastElementChild
// el?.dispatchEvent(
// new CustomEvent('click', { detail: { message: 'Hello, World!' } })
// ) //
// //
// el?.addEventListener('click', event => {
// console.log('object :>> ', event)
// if (event.type === 'click' && event.detail) {
// console.log('', event.detail.message) //
// }
// })
// console.log('dwad=>', contentEditor.value?.vditor.toolbar?.elements?.export)
// }
contentEditor.value?.setValue(props?.data?.content ?? '')
},
upload: {
@ -123,6 +173,108 @@ function initEditor() { @@ -123,6 +173,108 @@ function initEditor() {
})
}
function exportMarkdown(vditor: Vditor, filename: string) {
const content = vditor.getValue()
const blob = new Blob([content], { type: 'text/markdown;charset=utf-8' })
triggerDownload(blob, filename)
}
function exportHTML(vditor: Vditor, filename: string) {
const content = vditor.getHTML() // HTML
const blob = new Blob([content], { type: 'text/html;charset=utf-8' })
triggerDownload(blob, filename)
}
async function exportToPDF(vditor: Vditor, filename: string) {
const content = vditor.getHTML()
const tempDiv = document.createElement('div')
tempDiv.style.backgroundColor = 'white'
tempDiv.innerHTML = content
document.body.appendChild(tempDiv)
const canvas = await html2canvas(tempDiv, {
scale: window.devicePixelRatio * 2,
})
const canvasWidth = canvas.width
const canvasHeight = canvas.height
const imgData = canvas.toDataURL('image/jpeg', 1.0)
const pdf = new jsPDF('p', 'mm', 'a4')
const pdfWidth = pdf.internal.pageSize.getWidth()
const pdfHeight = pdf.internal.pageSize.getHeight()
pdf.setFontSize(12)
pdf.setFont('YaHei')
const headerHeight = 10 // Header
const footerHeight = 10 // Footer
const leftMargin = 8 //
const rightMargin = 8 //
const contentWidth = pdfWidth - leftMargin - rightMargin //
const contentHeight = pdfHeight - headerHeight - footerHeight //
// PDF
const imgHeight = (canvasHeight * contentWidth) / canvasWidth
if (imgHeight <= contentHeight) {
pdf.addImage(imgData, 'JPEG', leftMargin, headerHeight, contentWidth, imgHeight)
} else {
//
let position = 0 //
let page = 1 //
while (position < canvasHeight) {
const pageCanvas = document.createElement('canvas')
pageCanvas.width = canvasWidth
pageCanvas.height = (contentHeight * canvasWidth) / contentWidth
const pageCtx = pageCanvas.getContext('2d')
pageCtx?.drawImage(
canvas,
0,
position, // Canvas
canvasWidth,
pageCanvas.height,
0,
0,
canvasWidth,
pageCanvas.height
)
const pageImgData = pageCanvas.toDataURL('image/jpeg', 1.0)
// PDF
pdf.addImage(
pageImgData,
'JPEG',
leftMargin,
headerHeight,
contentWidth,
contentHeight
)
position += pageCanvas.height //
//
if (position < canvasHeight) {
page += 1
pdf.addPage()
}
}
}
pdf.save(filename)
}
function triggerDownload(blob: Blob, filename: string) {
const link = document.createElement('a')
link.href = URL.createObjectURL(blob)
link.download = filename
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
URL.revokeObjectURL(link.href)
}
function onSave(isDraft: 0 | 1) {
emits('on-save', isDraft, contentEditor.value?.getValue())
}

Loading…
Cancel
Save