From b8df51eb02a3c9bb891d19e24def18a6d5d5c962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=97=E8=A7=92=E5=B0=8F=E6=9E=97?= <1013335014@qq.com> Date: Tue, 12 Nov 2024 18:44:35 +0800 Subject: [PATCH] =?UTF-8?q?Fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=BC=BA=E9=99=B7=E5=92=8C=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/core/render/node/Style.js | 4 +- .../core/render/node/nodeCreateContents.js | 39 ++++++++++--------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/simple-mind-map/src/core/render/node/Style.js b/simple-mind-map/src/core/render/node/Style.js index 4443f034..8fc9d70d 100644 --- a/simple-mind-map/src/core/render/node/Style.js +++ b/simple-mind-map/src/core/render/node/Style.js @@ -192,7 +192,7 @@ class Style { } // 生成内联样式 - createStyleText(customStyle) { + createStyleText(customStyle = {}) { const styles = { color: this.merge('color'), fontFamily: this.merge('fontFamily'), @@ -357,7 +357,7 @@ class Style { // 获取自定义的样式 getCustomStyle() { - let customStyle = {} + const customStyle = {} Object.keys(this.ctx.getData()).forEach(item => { if (checkIsNodeStyleDataKey(item)) { customStyle[item] = this.ctx.getData(item) diff --git a/simple-mind-map/src/core/render/node/nodeCreateContents.js b/simple-mind-map/src/core/render/node/nodeCreateContents.js index 9ecc8a0a..31b1c4f3 100644 --- a/simple-mind-map/src/core/render/node/nodeCreateContents.js +++ b/simple-mind-map/src/core/render/node/nodeCreateContents.js @@ -124,6 +124,19 @@ function createIconNode() { }) } +// 尝试给html指定标签添加内联样式 +function tryAddHtmlStyle(text, style) { + const tagList = ['span', 'strong', 's', 'em', 'u'] + let _text = text + for (let i = 0; i < tagList.length; i++) { + text = addHtmlStyle(text, tagList[i], style) + if (text !== _text) { + break + } + } + return text +} + // 创建富文本节点 function createRichTextNode(specifyText) { const hasCustomWidth = this.hasCustomWidth() @@ -139,36 +152,23 @@ function createRichTextNode(specifyText) { recoverText = true } if ([CONSTANTS.CHANGE_THEME].includes(this.mindMap.renderer.renderSource)) { - // // 如果自定义过样式则不允许覆盖 + // 如果自定义过样式则不允许覆盖 // if (!this.hasCustomStyle() ) { - recoverText = true + recoverText = true // } } if (recoverText && !isUndef(text)) { // 判断节点内容是否是富文本 - let isRichText = checkIsRichText(text) + const isRichText = checkIsRichText(text) // 获取自定义样式 - let customStyle = this.getCustomStyle() + const customStyle = this.style.getCustomStyle() // 样式字符串 - let style = this.style.createStyleText(customStyle) + const style = this.style.createStyleText(customStyle) if (isRichText) { // 如果是富文本那么线移除内联样式 text = removeHtmlStyle(text) // 再添加新的内联样式 - let _text = text - text = addHtmlStyle(text, 'span', style) - // 给span添加样式没有成功,则尝试给strong标签添加样式 - if (text === _text) { - text = addHtmlStyle(text, 'strong', style) - } - // 给strong添加样式没有成功,则尝试给s标签添加样式 - if (text === _text) { - text = addHtmlStyle(text, 's', style) - } - // 给s添加样式没有成功,则尝试给em标签添加样式 - if (text === _text) { - text = addHtmlStyle(text, 'em', style) - } + text = this.tryAddHtmlStyle(text, style) } else { // 非富文本 text = `

${text}

` @@ -547,6 +547,7 @@ export default { createImgNode, getImgShowSize, createIconNode, + tryAddHtmlStyle, createRichTextNode, createTextNode, createHyperlinkNode,