diff --git a/simple-mind-map/src/core/render/Render.js b/simple-mind-map/src/core/render/Render.js index a82027df..e08c32c6 100644 --- a/simple-mind-map/src/core/render/Render.js +++ b/simple-mind-map/src/core/render/Render.js @@ -33,6 +33,7 @@ import { formatGetNodeGeneralization, sortNodeList, throttle, + debounce, checkClipboardReadEnable, isNodeNotNeedRenderData } from '../../utils' @@ -161,7 +162,7 @@ class Render { this.mindMap.on('view_data_change', onViewDataChange) } // 文本编辑时实时更新节点大小 - this.onNodeTextEditChange = this.onNodeTextEditChange.bind(this) + this.onNodeTextEditChange = debounce(this.onNodeTextEditChange, 100, this) if (openRealtimeRenderOnNodeTextEdit) { this.mindMap.on('node_text_edit_change', this.onNodeTextEditChange) } diff --git a/simple-mind-map/src/utils/index.js b/simple-mind-map/src/utils/index.js index 33d817af..b9a3ed9f 100644 --- a/simple-mind-map/src/utils/index.js +++ b/simple-mind-map/src/utils/index.js @@ -290,6 +290,21 @@ export const throttle = (fn, time = 300, ctx) => { } } +// 防抖函数 +export const debounce = (fn, wait = 300, ctx) => { + let timeout = null + + return (...args) => { + if (timeout) clearTimeout(timeout) + const callNow = !timeout + timeout = setTimeout(() => { + timeout = null + fn.apply(ctx, args) + }, wait) + if (callNow) fn.apply(ctx, args) + } +} + // 异步执行任务队列 export const asyncRun = (taskList, callback = () => {}) => { let index = 0