Feat:编辑文本实时更新增加防抖操作,避免快速输入时不必要的计算

This commit is contained in:
街角小林
2024-12-26 17:54:12 +08:00
parent 4a5501f7a3
commit f34de3acd9
2 changed files with 17 additions and 1 deletions

View File

@@ -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)
}

View File

@@ -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