Feat:导出svg时替换svg中存在的 字符,防止导出的svg报错

This commit is contained in:
wanglin2
2023-07-25 09:38:50 +08:00
parent 839c79405f
commit 8c79ffd723
2 changed files with 11 additions and 1 deletions

View File

@@ -1,4 +1,4 @@
import { imgToDataUrl, downloadFile, readBlob } from '../utils'
import { imgToDataUrl, downloadFile, readBlob, removeHTMLEntities } from '../utils'
import { SVG } from '@svgdotjs/svg.js'
import drawBackgroundImageToCanvas from '../utils/simulateCSSBackgroundInCanvas'
import { transformToMarkdown } from '../parse/toMarkdown'
@@ -154,6 +154,7 @@ class Export {
*/
async png(name, transparent = false) {
let { node, str } = await this.getSvgData()
str = removeHTMLEntities(str)
// 如果开启了富文本则使用htmltocanvas转换为图片
if (this.mindMap.richText) {
let res = await this.mindMap.richText.handleExportPng(node.node)
@@ -207,6 +208,7 @@ class Export {
node.first().before(SVG(`<title>${name}</title>`))
await this.drawBackgroundToSvg(node)
let str = node.svg()
str = removeHTMLEntities(str)
// 转换成blob数据
let blob = new Blob([str], {
type: 'image/svg+xml'

View File

@@ -453,3 +453,11 @@ export const loadImage = imgFile => {
}
})
}
// 移除字符串中的html实体
export const removeHTMLEntities = (str) => {
[['&nbsp;', '&#160;']].forEach((item) => {
str = str.replaceAll(item[0], item[1])
})
return str
}