19
19
<div class =" flex flex-col items-center justify-center" v-html =" html" ></div >
20
20
</div >
21
21
22
- <div ref =" mindmapRef " class =" wh-full" >
22
+ <div ref =" mindMapRef " class =" wh-full" >
23
23
<svg ref =" svgRef" class =" w-full" :style =" { height: `${contentAreaHeight}px` }" />
24
24
<div ref =" toolBarRef" class =" absolute bottom-[10px] right-5" ></div >
25
25
</div >
@@ -32,20 +32,20 @@ import { Markmap } from 'markmap-view'
32
32
import { Transformer } from ' markmap-lib'
33
33
import { Toolbar } from ' markmap-toolbar'
34
34
import markdownit from ' markdown-it'
35
+ import download from ' @/utils/download'
35
36
36
37
const md = markdownit ()
37
38
const message = useMessage () // 消息弹窗
38
39
39
- // TODO @hhero:mindmap 改成 mindMap 更精准哈
40
40
const props = defineProps <{
41
- mindmapResult : string // 生成结果 TODO @hhero 改成 generatedContent 会不会好点
41
+ generatedContent : string // 生成结果
42
42
isEnd: boolean // 是否结束
43
43
isGenerating: boolean // 是否正在生成
44
44
isStart: boolean // 开始状态,开始时需要清除 html
45
45
}>()
46
46
const contentRef = ref <HTMLDivElement >() // 右侧出来header以下的区域
47
47
const mdContainerRef = ref <HTMLDivElement >() // markdown 的容器,用来滚动到底下的
48
- const mindmapRef = ref <HTMLDivElement >() // 思维导图的容器
48
+ const mindMapRef = ref <HTMLDivElement >() // 思维导图的容器
49
49
const svgRef = ref <SVGElement >() // 思维导图的渲染 svg
50
50
const toolBarRef = ref <HTMLDivElement >() // 思维导图右下角的工具栏,缩放等
51
51
const html = ref (' ' ) // 生成过程中的文本
@@ -66,15 +66,16 @@ onMounted(() => {
66
66
}
67
67
})
68
68
69
- watch (props , ({ mindmapResult , isGenerating , isEnd , isStart }) => {
69
+ watch (props , ({ generatedContent , isGenerating , isEnd , isStart }) => {
70
70
// 开始生成的时候清空一下 markdown 的内容
71
71
if (isStart ) {
72
72
html .value = ' '
73
73
}
74
74
// 生成内容的时候使用 markdown 来渲染
75
75
if (isGenerating ) {
76
- html .value = md .render (mindmapResult )
76
+ html .value = md .render (generatedContent )
77
77
}
78
+ // 生成结束时更新思维导图
78
79
if (isEnd ) {
79
80
update ()
80
81
}
@@ -83,7 +84,7 @@ watch(props, ({ mindmapResult, isGenerating, isEnd, isStart }) => {
83
84
/** 更新思维导图的展示 */
84
85
const update = () => {
85
86
try {
86
- const { root } = transformer .transform (processContent (props .mindmapResult ))
87
+ const { root } = transformer .transform (processContent (props .generatedContent ))
87
88
markMap ?.setData (root )
88
89
markMap ?.fit ()
89
90
} catch (e ) {
@@ -106,31 +107,19 @@ const processContent = (text: string) => {
106
107
}
107
108
108
109
/** 下载图片 */
109
- // TODO @hhhero:可以抽到 download 这个里面,src/utils/download.ts 么?复用 image 方法?
110
110
// download SVG to png file
111
111
const downloadImage = () => {
112
- const svgElement = mindmapRef .value
112
+ const svgElement = mindMapRef .value
113
113
// 将 SVG 渲染到图片对象
114
114
const serializer = new XMLSerializer ()
115
- const source =
116
- ' <?xml version="1.0" standalone="no"?>\r\n ' + serializer .serializeToString (svgRef .value ! )
117
- const image = new Image ()
118
- image .src = ' data:image/svg+xml;charset=utf-8,' + encodeURIComponent (source )
119
-
120
- // 将图片对象渲染
121
- const canvas = document .createElement (' canvas' )
122
- canvas .width = svgElement ?.offsetWidth || 0
123
- canvas .height = svgElement ?.offsetHeight || 0
124
- let context = canvas .getContext (' 2d' )
125
- context ?.clearRect (0 , 0 , canvas .width , canvas .height )
126
-
127
- image .onload = function () {
128
- context ?.drawImage (image , 0 , 0 )
129
- const a = document .createElement (' a' )
130
- a .download = ' mindmap.png'
131
- a .href = canvas .toDataURL (` image/png ` )
132
- a .click ()
133
- }
115
+ const source = ` <?xml version="1.0" standalone="no"?>\r\n ${serializer .serializeToString (svgRef .value ! )} `
116
+ const base64Url = ` data:image/svg+xml;charset=utf-8,${encodeURIComponent (source )} `
117
+ download .image ({
118
+ url: base64Url ,
119
+ canvasWidth: svgElement ?.offsetWidth ,
120
+ canvasHeight: svgElement ?.offsetHeight ,
121
+ drawWithImageSize: false
122
+ })
134
123
}
135
124
136
125
defineExpose ({
0 commit comments