Skip to content

Commit

Permalink
Fix:修复同时创建多个实例时,文本编辑后节点宽高丢失的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wanglin2 committed Apr 8, 2024
1 parent 487aa38 commit 088fd39
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 34 deletions.
18 changes: 4 additions & 14 deletions simple-mind-map/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import BatchExecution from './src/utils/BatchExecution'
import {
layoutValueList,
CONSTANTS,
commonCaches,
ERROR_TYPES,
cssContent
} from './src/constants/constant'
Expand Down Expand Up @@ -228,19 +227,10 @@ class MindMap {

// 初始化缓存数据
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
})
this.commonCaches = {
measureCustomNodeContentSizeEl: null,
measureRichtextNodeTextSizeEl: null
}
}

// 设置主题
Expand Down
6 changes: 0 additions & 6 deletions simple-mind-map/src/constants/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,6 @@ export const nodeDataNoStylePropList = [
'attachmentName'
]

// 数据缓存
export const commonCaches = {
measureCustomNodeContentSizeEl: null,
measureRichtextNodeTextSizeEl: null
}

// 错误类型
export const ERROR_TYPES = {
READ_CLIPBOARD_ERROR: 'read_clipboard_error',
Expand Down
28 changes: 14 additions & 14 deletions simple-mind-map/src/core/render/node/nodeCreateContents.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
ForeignObject
} from '@svgdotjs/svg.js'
import iconsSvg from '../../../svg/icons'
import { CONSTANTS, commonCaches } from '../../../constants/constant'
import { CONSTANTS } from '../../../constants/constant'

// 创建图片节点
function createImgNode() {
Expand Down Expand Up @@ -149,13 +149,13 @@ function createRichTextNode() {
})
}
let html = `<div>${this.getData('text')}</div>`
if (!commonCaches.measureRichtextNodeTextSizeEl) {
commonCaches.measureRichtextNodeTextSizeEl = document.createElement('div')
commonCaches.measureRichtextNodeTextSizeEl.style.position = 'fixed'
commonCaches.measureRichtextNodeTextSizeEl.style.left = '-999999px'
this.mindMap.el.appendChild(commonCaches.measureRichtextNodeTextSizeEl)
if (!this.mindMap.commonCaches.measureRichtextNodeTextSizeEl) {
this.mindMap.commonCaches.measureRichtextNodeTextSizeEl = document.createElement('div')
this.mindMap.commonCaches.measureRichtextNodeTextSizeEl.style.position = 'fixed'
this.mindMap.commonCaches.measureRichtextNodeTextSizeEl.style.left = '-999999px'
this.mindMap.el.appendChild(this.mindMap.commonCaches.measureRichtextNodeTextSizeEl)
}
let div = commonCaches.measureRichtextNodeTextSizeEl
let div = this.mindMap.commonCaches.measureRichtextNodeTextSizeEl
div.innerHTML = html
let el = div.children[0]
el.classList.add('smm-richtext-node-wrap')
Expand Down Expand Up @@ -413,18 +413,18 @@ function getNoteContentPosition() {

// 测量自定义节点内容元素的宽高
function measureCustomNodeContentSize(content) {
if (!commonCaches.measureCustomNodeContentSizeEl) {
commonCaches.measureCustomNodeContentSizeEl = document.createElement('div')
commonCaches.measureCustomNodeContentSizeEl.style.cssText = `
if (!this.mindMap.commonCaches.measureCustomNodeContentSizeEl) {
this.mindMap.commonCaches.measureCustomNodeContentSizeEl = document.createElement('div')
this.mindMap.commonCaches.measureCustomNodeContentSizeEl.style.cssText = `
position: fixed;
left: -99999px;
top: -99999px;
`
this.mindMap.el.appendChild(commonCaches.measureCustomNodeContentSizeEl)
this.mindMap.el.appendChild(this.mindMap.commonCaches.measureCustomNodeContentSizeEl)
}
commonCaches.measureCustomNodeContentSizeEl.innerHTML = ''
commonCaches.measureCustomNodeContentSizeEl.appendChild(content)
let rect = commonCaches.measureCustomNodeContentSizeEl.getBoundingClientRect()
this.mindMap.commonCaches.measureCustomNodeContentSizeEl.innerHTML = ''
this.mindMap.commonCaches.measureCustomNodeContentSizeEl.appendChild(content)
let rect = this.mindMap.commonCaches.measureCustomNodeContentSizeEl.getBoundingClientRect()
return {
width: rect.width,
height: rect.height
Expand Down

0 comments on commit 088fd39

Please sign in to comment.