diff --git a/simple-mind-map/index.js b/simple-mind-map/index.js index 731edd37..babb799c 100644 --- a/simple-mind-map/index.js +++ b/simple-mind-map/index.js @@ -7,9 +7,9 @@ import Style from './src/core/render/node/Style' import KeyCommand from './src/core/command/KeyCommand' import Command from './src/core/command/Command' import BatchExecution from './src/utils/BatchExecution' -import { layoutValueList, CONSTANTS } from './src/constants/constant' +import { layoutValueList, CONSTANTS, commonCaches } from './src/constants/constant' import { SVG } from '@svgdotjs/svg.js' -import { simpleDeepClone } from './src/utils' +import { simpleDeepClone, getType } from './src/utils' import defaultTheme, { checkIsNodeSizeIndependenceConfig } from './src/themes/default' import { defaultOpt } from './src/constants/defaultOptions' @@ -35,6 +35,9 @@ class MindMap { // 初始化主题 this.initTheme() + // 初始化缓存数据 + this.initCache() + // 事件类 this.event = new Event({ mindMap: this @@ -129,6 +132,23 @@ class MindMap { this.event.off(event, fn) } + // 初始化缓存数据 + initCache() { + Object.keys(commonCaches).forEach((key) => { + let type = getType(commonCaches[key]) + let value = '' + switch(type) { + case 'Boolean': + value = false + break + default: + value = null + break + } + commonCaches[key] = value + }) + } + // 设置主题 initTheme() { // 合并主题配置 diff --git a/simple-mind-map/src/constants/constant.js b/simple-mind-map/src/constants/constant.js index cf2ce546..1593bf81 100644 --- a/simple-mind-map/src/constants/constant.js +++ b/simple-mind-map/src/constants/constant.js @@ -324,4 +324,9 @@ export const nodeDataNoStylePropList = [ 'resetRichText', 'uid', 'activeStyle' -] \ No newline at end of file +] + +// 数据缓存 +export const commonCaches = { + measureCustomNodeContentSizeEl: 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 9822484a..c521d2d4 100644 --- a/simple-mind-map/src/core/render/node/nodeCreateContents.js +++ b/simple-mind-map/src/core/render/node/nodeCreateContents.js @@ -1,7 +1,7 @@ import { measureText, resizeImgSize, getTextFromHtml } from '../../../utils' import { Image, SVG, A, G, Rect, Text, ForeignObject } from '@svgdotjs/svg.js' import iconsSvg from '../../../svg/icons' -import { CONSTANTS } from '../../../constants/constant' +import { CONSTANTS, commonCaches } from '../../../constants/constant' // 创建图片节点 function createImgNode() { @@ -293,20 +293,19 @@ function createNoteNode() { } // 测量自定义节点内容元素的宽高 -let warpEl = null function measureCustomNodeContentSize (content) { - if (!warpEl) { - warpEl = document.createElement('div') - warpEl.style.cssText = ` + if (!commonCaches.measureCustomNodeContentSizeEl) { + commonCaches.measureCustomNodeContentSizeEl = document.createElement('div') + commonCaches.measureCustomNodeContentSizeEl.style.cssText = ` position: fixed; left: -99999px; top: -99999px; ` - this.mindMap.el.appendChild(warpEl) + this.mindMap.el.appendChild(commonCaches.measureCustomNodeContentSizeEl) } - warpEl.innerHTML = '' - warpEl.appendChild(content) - let rect = warpEl.getBoundingClientRect() + commonCaches.measureCustomNodeContentSizeEl.innerHTML = '' + commonCaches.measureCustomNodeContentSizeEl.appendChild(content) + let rect = commonCaches.measureCustomNodeContentSizeEl.getBoundingClientRect() return { width: rect.width, height: rect.height diff --git a/simple-mind-map/src/utils/index.js b/simple-mind-map/src/utils/index.js index f28d757e..886e5385 100644 --- a/simple-mind-map/src/utils/index.js +++ b/simple-mind-map/src/utils/index.js @@ -460,4 +460,9 @@ export const removeHTMLEntities = (str) => { str = str.replaceAll(item[0], item[1]) }) return str +} + +// 获取一个数据的类型 +export const getType = (data) => { + return Object.prototype.toString.call(data).slice(7, -1) } \ No newline at end of file