mirror of
https://github.com/wanglin2/mind-map.git
synced 2026-02-17 14:04:47 +08:00
Feat:优化render逻辑
This commit is contained in:
@@ -243,10 +243,8 @@ class MindMap {
|
|||||||
|
|
||||||
// 渲染,部分渲染
|
// 渲染,部分渲染
|
||||||
render(callback, source = '') {
|
render(callback, source = '') {
|
||||||
this.batchExecution.push('render', () => {
|
this.initTheme()
|
||||||
this.initTheme()
|
this.renderer.render(callback, source)
|
||||||
this.renderer.render(callback, source)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重新渲染
|
// 重新渲染
|
||||||
@@ -416,7 +414,7 @@ class MindMap {
|
|||||||
this.command.clearHistory()
|
this.command.clearHistory()
|
||||||
this.command.addHistory()
|
this.command.addHistory()
|
||||||
this.renderer.setData(data)
|
this.renderer.setData(data)
|
||||||
this.reRender(() => {}, CONSTANTS.SET_DATA)
|
this.reRender()
|
||||||
this.emit('set_data', data)
|
this.emit('set_data', data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
export const CONSTANTS = {
|
export const CONSTANTS = {
|
||||||
CHANGE_THEME: 'changeTheme',
|
CHANGE_THEME: 'changeTheme',
|
||||||
CHANGE_LAYOUT: 'changeLayout',
|
CHANGE_LAYOUT: 'changeLayout',
|
||||||
SET_DATA: 'setData',
|
|
||||||
MODE: {
|
MODE: {
|
||||||
READONLY: 'readonly',
|
READONLY: 'readonly',
|
||||||
EDIT: 'edit'
|
EDIT: 'edit'
|
||||||
|
|||||||
@@ -81,14 +81,18 @@ class Render {
|
|||||||
this.isRendering = false
|
this.isRendering = false
|
||||||
// 是否存在等待渲染
|
// 是否存在等待渲染
|
||||||
this.hasWaitRendering = false
|
this.hasWaitRendering = false
|
||||||
this.waitRenderingParams = []
|
|
||||||
// 用于缓存节点
|
// 用于缓存节点
|
||||||
this.nodeCache = {}
|
this.nodeCache = {}
|
||||||
this.lastNodeCache = {}
|
this.lastNodeCache = {}
|
||||||
// 触发render的来源
|
// 收集触发render的来源
|
||||||
this.renderSource = ''
|
this.renderSourceList = []
|
||||||
|
// 收集render的回调函数
|
||||||
|
this.renderCallbackList = []
|
||||||
// 当前激活的节点列表
|
// 当前激活的节点列表
|
||||||
this.activeNodeList = []
|
this.activeNodeList = []
|
||||||
|
// 防抖定时器
|
||||||
|
this.emitNodeActiveEventTimer = null
|
||||||
|
this.renderTimer = null
|
||||||
// 根节点
|
// 根节点
|
||||||
this.root = null
|
this.root = null
|
||||||
// 文本编辑框,需要再bindEvent之前实例化,否则单击事件只能触发隐藏文本编辑框,而无法保存文本修改
|
// 文本编辑框,需要再bindEvent之前实例化,否则单击事件只能触发隐藏文本编辑框,而无法保存文本修改
|
||||||
@@ -446,9 +450,10 @@ class Render {
|
|||||||
)
|
)
|
||||||
if (!isChange) return
|
if (!isChange) return
|
||||||
this.lastActiveNodeList = [...activeNodeList]
|
this.lastActiveNodeList = [...activeNodeList]
|
||||||
this.mindMap.batchExecution.push('emitNodeActiveEvent', () => {
|
clearTimeout(this.emitNodeActiveEventTimer)
|
||||||
|
this.emitNodeActiveEventTimer = setTimeout(() => {
|
||||||
this.mindMap.emit('node_active', node, activeNodeList)
|
this.mindMap.emit('node_active', node, activeNodeList)
|
||||||
})
|
}, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 鼠标点击画布时清空当前激活节点列表
|
// 鼠标点击画布时清空当前激活节点列表
|
||||||
@@ -491,22 +496,71 @@ class Render {
|
|||||||
this.lastNodeCache = {}
|
this.lastNodeCache = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 渲染
|
// 保存触发渲染的参数
|
||||||
render(callback = () => {}, source) {
|
addRenderParams(callback, source) {
|
||||||
|
if (callback) {
|
||||||
|
const index = this.renderCallbackList.findIndex(fn => {
|
||||||
|
return fn === callback
|
||||||
|
})
|
||||||
|
if (index === -1) {
|
||||||
|
this.renderCallbackList.push(callback)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (source) {
|
||||||
|
const index = this.renderSourceList.findIndex(s => {
|
||||||
|
return s === source
|
||||||
|
})
|
||||||
|
if (index === -1) {
|
||||||
|
this.renderSourceList.push(source)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断是否包含某种触发渲染源
|
||||||
|
checkHasRenderSource(val) {
|
||||||
|
val = Array.isArray(val) ? val : [val]
|
||||||
|
for (let i = 0; i < this.renderSourceList.length; i++) {
|
||||||
|
if (val.includes(this.renderSourceList[i])) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// 渲染完毕的操作
|
||||||
|
onRenderEnd() {
|
||||||
|
this.renderCallbackList.forEach(fn => {
|
||||||
|
fn()
|
||||||
|
})
|
||||||
|
this.isRendering = false
|
||||||
|
this.reRender = false
|
||||||
|
this.renderCallbackList = []
|
||||||
|
this.renderSourceList = []
|
||||||
|
this.mindMap.emit('node_tree_render_end')
|
||||||
|
}
|
||||||
|
|
||||||
|
// 渲染
|
||||||
|
render(callback, source) {
|
||||||
|
this.addRenderParams(callback, source)
|
||||||
|
clearTimeout(this.renderTimer)
|
||||||
|
this.renderTimer = setTimeout(() => {
|
||||||
|
this._render()
|
||||||
|
}, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 真正的渲染
|
||||||
|
_render() {
|
||||||
// 切换主题时,被收起的节点需要添加样式复位的标注
|
// 切换主题时,被收起的节点需要添加样式复位的标注
|
||||||
if (source === CONSTANTS.CHANGE_THEME) {
|
if (this.checkHasRenderSource(CONSTANTS.CHANGE_THEME)) {
|
||||||
this.resetUnExpandNodeStyle()
|
this.resetUnExpandNodeStyle()
|
||||||
}
|
}
|
||||||
// 如果当前还没有渲染完毕,不再触发渲染
|
// 如果当前还没有渲染完毕,不再触发渲染
|
||||||
if (this.isRendering) {
|
if (this.isRendering) {
|
||||||
// 等待当前渲染完毕后再进行一次渲染
|
// 等待当前渲染完毕后再进行一次渲染
|
||||||
this.hasWaitRendering = true
|
this.hasWaitRendering = true
|
||||||
this.waitRenderingParams = [callback, source]
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.isRendering = true
|
this.isRendering = true
|
||||||
// 触发当前重新渲染的来源
|
|
||||||
this.renderSource = source
|
|
||||||
// 节点缓存
|
// 节点缓存
|
||||||
this.lastNodeCache = this.nodeCache
|
this.lastNodeCache = this.nodeCache
|
||||||
this.nodeCache = {}
|
this.nodeCache = {}
|
||||||
@@ -516,8 +570,7 @@ class Render {
|
|||||||
}
|
}
|
||||||
// 如果没有节点数据
|
// 如果没有节点数据
|
||||||
if (!this.renderTree) {
|
if (!this.renderTree) {
|
||||||
this.isRendering = false
|
this.onRenderEnd()
|
||||||
this.mindMap.emit('node_tree_render_end')
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.mindMap.emit('node_tree_render_start')
|
this.mindMap.emit('node_tree_render_start')
|
||||||
@@ -539,19 +592,12 @@ class Render {
|
|||||||
// 渲染节点
|
// 渲染节点
|
||||||
this.root.render(() => {
|
this.root.render(() => {
|
||||||
this.isRendering = false
|
this.isRendering = false
|
||||||
callback && callback()
|
|
||||||
if (this.hasWaitRendering) {
|
if (this.hasWaitRendering) {
|
||||||
const params = this.waitRenderingParams
|
|
||||||
this.hasWaitRendering = false
|
this.hasWaitRendering = false
|
||||||
this.waitRenderingParams = []
|
this.render()
|
||||||
this.render(...params)
|
return
|
||||||
} else {
|
|
||||||
this.renderSource = ''
|
|
||||||
if (this.reRender) {
|
|
||||||
this.reRender = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
this.mindMap.emit('node_tree_render_end')
|
this.onRenderEnd()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
this.emitNodeActiveEvent()
|
this.emitNodeActiveEvent()
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ class Base {
|
|||||||
|
|
||||||
// 检查当前来源是否需要重新计算节点大小
|
// 检查当前来源是否需要重新计算节点大小
|
||||||
checkIsNeedResizeSources() {
|
checkIsNeedResizeSources() {
|
||||||
return [CONSTANTS.CHANGE_THEME].includes(this.renderer.renderSource)
|
return this.renderer.checkHasRenderSource(CONSTANTS.CHANGE_THEME)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 层级类型改变
|
// 层级类型改变
|
||||||
@@ -61,7 +61,7 @@ class Base {
|
|||||||
|
|
||||||
// 检查是否是结构布局改变重新渲染展开收起按钮占位元素
|
// 检查是否是结构布局改变重新渲染展开收起按钮占位元素
|
||||||
checkIsLayoutChangeRerenderExpandBtnPlaceholderRect(node) {
|
checkIsLayoutChangeRerenderExpandBtnPlaceholderRect(node) {
|
||||||
if (this.renderer.renderSource === CONSTANTS.CHANGE_LAYOUT) {
|
if (this.renderer.checkHasRenderSource(CONSTANTS.CHANGE_LAYOUT)) {
|
||||||
node.needRerenderExpandBtnPlaceholderRect = true
|
node.needRerenderExpandBtnPlaceholderRect = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user