[代码优化]AI: 思维导图

pull/489/head
hhhero 2024-07-30 23:47:40 +08:00
parent ed36d2bfb4
commit 8d4c9e9c16
4 changed files with 46 additions and 42 deletions

View File

@ -34,16 +34,31 @@ const download = {
download0(data, fileName, 'text/markdown')
},
// 下载图片(允许跨域)
image: (url: string) => {
image: ({
url,
canvasWidth,
canvasHeight,
drawWithImageSize = true
}: {
url: string
canvasWidth?: number // 指定画布宽度
canvasHeight?: number // 指定画布高度
drawWithImageSize?: boolean // 将图片绘制在画布上时带上图片的宽高值, 默认是要带上的
}) => {
const image = new Image()
image.setAttribute('crossOrigin', 'anonymous')
// image.setAttribute('crossOrigin', 'anonymous')
image.src = url
image.onload = () => {
const canvas = document.createElement('canvas')
canvas.width = image.width
canvas.height = image.height
const ctx = canvas.getContext('2d') as CanvasDrawImage
ctx.drawImage(image, 0, 0, image.width, image.height)
canvas.width = canvasWidth || image.width
canvas.height = canvasHeight || image.height
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D
ctx?.clearRect(0, 0, canvas.width, canvas.height)
if (drawWithImageSize) {
ctx.drawImage(image, 0, 0, image.width, image.height)
} else {
ctx.drawImage(image, 0, 0)
}
const url = canvas.toDataURL('image/png')
const a = document.createElement('a')
a.href = url

View File

@ -150,7 +150,7 @@ const handleImageButtonClick = async (type: string, imageDetail: ImageVO) => {
}
//
if (type === 'download') {
await download.image(imageDetail.picUrl)
await download.image({ url: imageDetail.picUrl })
return
}
//

View File

@ -19,7 +19,7 @@
<div class="flex flex-col items-center justify-center" v-html="html"></div>
</div>
<div ref="mindmapRef" class="wh-full">
<div ref="mindMapRef" class="wh-full">
<svg ref="svgRef" class="w-full" :style="{ height: `${contentAreaHeight}px` }" />
<div ref="toolBarRef" class="absolute bottom-[10px] right-5"></div>
</div>
@ -32,20 +32,20 @@ import { Markmap } from 'markmap-view'
import { Transformer } from 'markmap-lib'
import { Toolbar } from 'markmap-toolbar'
import markdownit from 'markdown-it'
import download from '@/utils/download'
const md = markdownit()
const message = useMessage() //
// TODO @hheromindmap mindMap
const props = defineProps<{
mindmapResult: string // TODO @hhero generatedContent
generatedContent: string //
isEnd: boolean //
isGenerating: boolean //
isStart: boolean // html
}>()
const contentRef = ref<HTMLDivElement>() // header
const mdContainerRef = ref<HTMLDivElement>() // markdown
const mindmapRef = ref<HTMLDivElement>() //
const mindMapRef = ref<HTMLDivElement>() //
const svgRef = ref<SVGElement>() // svg
const toolBarRef = ref<HTMLDivElement>() //
const html = ref('') //
@ -66,15 +66,16 @@ onMounted(() => {
}
})
watch(props, ({ mindmapResult, isGenerating, isEnd, isStart }) => {
watch(props, ({ generatedContent, isGenerating, isEnd, isStart }) => {
// markdown
if (isStart) {
html.value = ''
}
// 使 markdown
if (isGenerating) {
html.value = md.render(mindmapResult)
html.value = md.render(generatedContent)
}
//
if (isEnd) {
update()
}
@ -83,7 +84,7 @@ watch(props, ({ mindmapResult, isGenerating, isEnd, isStart }) => {
/** 更新思维导图的展示 */
const update = () => {
try {
const { root } = transformer.transform(processContent(props.mindmapResult))
const { root } = transformer.transform(processContent(props.generatedContent))
markMap?.setData(root)
markMap?.fit()
} catch (e) {
@ -106,31 +107,19 @@ const processContent = (text: string) => {
}
/** 下载图片 */
// TODO @hhhero download src/utils/download.ts image
// download SVG to png file
const downloadImage = () => {
const svgElement = mindmapRef.value
const svgElement = mindMapRef.value
// SVG
const serializer = new XMLSerializer()
const source =
'<?xml version="1.0" standalone="no"?>\r\n' + serializer.serializeToString(svgRef.value!)
const image = new Image()
image.src = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(source)
//
const canvas = document.createElement('canvas')
canvas.width = svgElement?.offsetWidth || 0
canvas.height = svgElement?.offsetHeight || 0
let context = canvas.getContext('2d')
context?.clearRect(0, 0, canvas.width, canvas.height)
image.onload = function () {
context?.drawImage(image, 0, 0)
const a = document.createElement('a')
a.download = 'mindmap.png'
a.href = canvas.toDataURL(`image/png`)
a.click()
}
const source = `<?xml version="1.0" standalone="no"?>\r\n${serializer.serializeToString(svgRef.value!)}`
const base64Url = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(source)}`
download.image({
url: base64Url,
canvasWidth: svgElement?.offsetWidth,
canvasHeight: svgElement?.offsetHeight,
drawWithImageSize: false
})
}
defineExpose({

View File

@ -10,7 +10,7 @@
<!--右边生成思维导图区域-->
<Right
ref="rightRef"
:mindmapResult="mindmapResult"
:generatedContent="generatedContent"
:isEnd="isEnd"
:isGenerating="isGenerating"
:isStart="isStart"
@ -33,7 +33,7 @@ const isStart = ref(false) // 开始生成,用来清空思维导图
const isEnd = ref(true) //
const message = useMessage() //
const mindmapResult = ref('') //
const generatedContent = ref('') //
const leftRef = ref<InstanceType<typeof Left>>() //
const rightRef = ref<InstanceType<typeof Right>>() //
@ -41,7 +41,7 @@ const rightRef = ref<InstanceType<typeof Right>>() // 右边组件
/** 使用已有内容直接生成 **/
const directGenerate = (existPrompt: string) => {
isEnd.value = false // falsetruewatch
mindmapResult.value = existPrompt
generatedContent.value = existPrompt
isEnd.value = true
}
@ -58,7 +58,7 @@ const submit = (data: AiMindMapGenerateReqVO) => {
isStart.value = true
isEnd.value = false
ctrl.value = new AbortController() //
mindmapResult.value = '' //
generatedContent.value = '' //
AiMindMapApi.generateMindMap({
data,
onMessage: async (res) => {
@ -68,13 +68,13 @@ const submit = (data: AiMindMapGenerateReqVO) => {
stopStream()
return
}
mindmapResult.value = mindmapResult.value + data
generatedContent.value = generatedContent.value + data
await nextTick()
rightRef.value?.scrollBottom()
},
onClose() {
isEnd.value = true
leftRef.value?.setGeneratedContent(mindmapResult.value)
leftRef.value?.setGeneratedContent(generatedContent.value)
stopStream()
},
onError(err) {
@ -87,6 +87,6 @@ const submit = (data: AiMindMapGenerateReqVO) => {
/** 初始化 */
onMounted(() => {
mindmapResult.value = MindMapContentExample
generatedContent.value = MindMapContentExample
})
</script>