-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathexport.ts
More file actions
125 lines (110 loc) · 3.38 KB
/
export.ts
File metadata and controls
125 lines (110 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { toPng } from 'html-to-image'
import {
downloadFile,
downloadMD,
exportHTML,
exportPDF,
exportPureHTML,
getHtmlContent,
sanitizeTitle,
} from '@/utils'
import { usePostStore } from './post'
import { useRenderStore } from './render'
import { useUIStore } from './ui'
/**
* 导出功能 Store
* 负责处理各种导出功能:HTML、PDF、MD、图片等
*/
export const useExportStore = defineStore(`export`, () => {
const postStore = usePostStore()
const renderStore = useRenderStore()
const uiStore = useUIStore()
// 将编辑器内容转换为 HTML
const editorContent2HTML = () => {
const temp = getHtmlContent()
document.querySelector(`#output`)!.innerHTML = renderStore.output
return temp
}
// 导出编辑器内容为 HTML,并且下载到本地
const exportEditorContent2HTML = async () => {
const currentPost = postStore.currentPost
if (!currentPost)
return
await exportHTML(currentPost.title)
document.querySelector(`#output`)!.innerHTML = renderStore.output
}
// 导出编辑器内容为无样式 HTML
const exportEditorContent2PureHTML = (content: string) => {
const currentPost = postStore.currentPost
if (!currentPost)
return
exportPureHTML(content, currentPost.title)
}
// 下载卡片图片
const downloadAsCardImage = async () => {
const currentPost = postStore.currentPost
if (!currentPost)
return
const el = document.querySelector<HTMLElement>(`#output-wrapper>.preview`)
if (!el)
return
// 添加临时样式:禁用代码块滚动,启用换行,隐藏仅用于预览的 UI 覆盖层
const style = document.createElement('style')
style.textContent = `
.diagram-download-bar { display: none !important; }
.preview pre.code__pre,
.preview .hljs.code__pre,
.preview pre.code__pre > code,
.preview .hljs.code__pre > code,
.preview .code-scroll,
.preview pre section,
.preview code section {
overflow: visible !important;
}
.preview pre.code__pre > code,
.preview .code-scroll,
.preview .code-scroll > div {
white-space: pre-wrap !important;
word-break: break-all !important;
min-width: auto !important;
}
`
document.head.appendChild(style)
try {
await new Promise(resolve => setTimeout(resolve, 100))
const url = await toPng(el, {
backgroundColor: uiStore.isDark ? `` : `#fff`,
skipFonts: true,
pixelRatio: Math.max(window.devicePixelRatio || 1, 2),
style: { margin: `0` },
})
downloadFile(url, `${sanitizeTitle(currentPost.title)}.png`, `image/png`)
}
finally {
style.remove()
}
}
// 导出编辑器内容为 PDF
const exportEditorContent2PDF = async () => {
const currentPost = postStore.currentPost
if (!currentPost)
return
await exportPDF(currentPost.title)
document.querySelector(`#output`)!.innerHTML = renderStore.output
}
// 导出编辑器内容到本地(Markdown)
const exportEditorContent2MD = (content: string) => {
const currentPost = postStore.currentPost
if (!currentPost)
return
downloadMD(content, currentPost.title)
}
return {
editorContent2HTML,
exportEditorContent2HTML,
exportEditorContent2PureHTML,
downloadAsCardImage,
exportEditorContent2PDF,
exportEditorContent2MD,
}
})