From 002ec41ba88261605c19f4ea0b3ef9bd327a93ea Mon Sep 17 00:00:00 2001 From: wanglin2 <1013335014@qq.com> Date: Tue, 15 Aug 2023 09:30:00 +0800 Subject: [PATCH] =?UTF-8?q?Fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E6=96=87=E6=9C=AC=E4=B8=BA=E7=A9=BA=E6=97=B6=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/constants/constant.js | 3 +- .../core/render/node/nodeCreateContents.js | 59 ++++++++++++------- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/simple-mind-map/src/constants/constant.js b/simple-mind-map/src/constants/constant.js index 3b991fe4..269c2c12 100644 --- a/simple-mind-map/src/constants/constant.js +++ b/simple-mind-map/src/constants/constant.js @@ -330,5 +330,6 @@ export const nodeDataNoStylePropList = [ // 数据缓存 export const commonCaches = { - measureCustomNodeContentSizeEl: null + measureCustomNodeContentSizeEl: null, + measureRichtextNodeTextSizeEl: null } \ No newline at end of file diff --git a/simple-mind-map/src/core/render/node/nodeCreateContents.js b/simple-mind-map/src/core/render/node/nodeCreateContents.js index 2d5268bf..ba19d884 100644 --- a/simple-mind-map/src/core/render/node/nodeCreateContents.js +++ b/simple-mind-map/src/core/render/node/nodeCreateContents.js @@ -1,4 +1,10 @@ -import { measureText, resizeImgSize, removeHtmlStyle, addHtmlStyle, checkIsRichText } from '../../../utils' +import { + measureText, + resizeImgSize, + removeHtmlStyle, + addHtmlStyle, + checkIsRichText +} from '../../../utils' import { Image, SVG, A, G, Rect, Text, ForeignObject } from '@svgdotjs/svg.js' import iconsSvg from '../../../svg/icons' import { CONSTANTS, commonCaches } from '../../../constants/constant' @@ -54,7 +60,10 @@ function createIconNode() { } let iconSize = this.mindMap.themeConfig.iconSize return _data.icon.map(item => { - let src = iconsSvg.getNodeIconListIcon(item, this.mindMap.opt.iconList || []) + let src = iconsSvg.getNodeIconListIcon( + item, + this.mindMap.opt.iconList || [] + ) let node = null // svg图标 if (/^${this.nodeData.data.text}` - let div = document.createElement('div') + if (!commonCaches.measureRichtextNodeTextSizeEl) { + commonCaches.measureRichtextNodeTextSizeEl = document.createElement('div') + } + let div = commonCaches.measureRichtextNodeTextSizeEl div.innerHTML = html div.style.cssText = `position: fixed; left: -999999px;` let el = div.children[0] @@ -117,12 +129,18 @@ function createRichTextNode() { el.style.maxWidth = this.mindMap.opt.textAutoWrapWidth + 'px' this.mindMap.el.appendChild(div) let { width, height } = el.getBoundingClientRect() - width = Math.ceil(width) + 1// 修复getBoundingClientRect方法对实际宽度是小数的元素获取到的值是整数,导致宽度不够文本发生换行的问题 + // 如果文本为空,那么需要计算一个默认高度 + if (height <= 0) { + div.innerHTML = '

abc123我和你

' + let elTmp = div.children[0] + elTmp.classList.add('smm-richtext-node-wrap') + height = elTmp.getBoundingClientRect().height + } + width = Math.ceil(width) + 1 // 修复getBoundingClientRect方法对实际宽度是小数的元素获取到的值是整数,导致宽度不够文本发生换行的问题 height = Math.ceil(height) g.attr('data-width', width) g.attr('data-height', height) html = div.innerHTML - this.mindMap.el.removeChild(div) let foreignObject = new ForeignObject() foreignObject.width(width) foreignObject.height(height) @@ -274,9 +292,10 @@ function createNoteNode() { box-shadow: 0 2px 5px rgb(0 0 0 / 10%); display: none; background-color: #fff; - z-index: ${ this.mindMap.opt.nodeNoteTooltipZIndex } + z-index: ${this.mindMap.opt.nodeNoteTooltipZIndex} ` - const targetNode = this.mindMap.opt.customInnerElsAppendTo || document.body + const targetNode = + this.mindMap.opt.customInnerElsAppendTo || document.body targetNode.appendChild(this.noteEl) } this.noteEl.innerText = this.nodeData.data.note @@ -310,7 +329,7 @@ function createNoteNode() { } // 测量自定义节点内容元素的宽高 -function measureCustomNodeContentSize (content) { +function measureCustomNodeContentSize(content) { if (!commonCaches.measureCustomNodeContentSizeEl) { commonCaches.measureCustomNodeContentSizeEl = document.createElement('div') commonCaches.measureCustomNodeContentSizeEl.style.cssText = ` @@ -330,19 +349,19 @@ function measureCustomNodeContentSize (content) { } // 是否使用的是自定义节点内容 -function isUseCustomNodeContent() { +function isUseCustomNodeContent() { return !!this._customNodeContent } export default { - createImgNode, - getImgShowSize, - createIconNode, - createRichTextNode, - createTextNode, - createHyperlinkNode, - createTagNode, - createNoteNode, - measureCustomNodeContentSize, - isUseCustomNodeContent -} \ No newline at end of file + createImgNode, + getImgShowSize, + createIconNode, + createRichTextNode, + createTextNode, + createHyperlinkNode, + createTagNode, + createNoteNode, + measureCustomNodeContentSize, + isUseCustomNodeContent +}