From f34de3acd97183baa21bca28e9a7849b4b86c7de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=97=E8=A7=92=E5=B0=8F=E6=9E=97?= <1013335014@qq.com> Date: Thu, 26 Dec 2024 17:54:12 +0800 Subject: [PATCH] =?UTF-8?q?Feat=EF=BC=9A=E7=BC=96=E8=BE=91=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E5=AE=9E=E6=97=B6=E6=9B=B4=E6=96=B0=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E9=98=B2=E6=8A=96=E6=93=8D=E4=BD=9C=EF=BC=8C=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E5=BF=AB=E9=80=9F=E8=BE=93=E5=85=A5=E6=97=B6=E4=B8=8D=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E7=9A=84=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/core/render/Render.js | 3 ++- simple-mind-map/src/utils/index.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) 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