pref: debounce
This commit is contained in:
parent
958e734e85
commit
6f3829c36b
|
@ -12,6 +12,8 @@ import { useKUNGalgameEditStore } from '@/store/modules/edit'
|
||||||
import { storeToRefs } from 'pinia'
|
import { storeToRefs } from 'pinia'
|
||||||
// 导入过滤 xss 的工具
|
// 导入过滤 xss 的工具
|
||||||
import DOMPurify from 'dompurify'
|
import DOMPurify from 'dompurify'
|
||||||
|
// 导入防抖函数
|
||||||
|
import { debounce } from '@/utils/debounce'
|
||||||
|
|
||||||
const topicData = storeToRefs(useKUNGalgameEditStore())
|
const topicData = storeToRefs(useKUNGalgameEditStore())
|
||||||
|
|
||||||
|
@ -70,11 +72,14 @@ onBeforeUnmount(() => {
|
||||||
// 编辑器文本改变时自动保存数据
|
// 编辑器文本改变时自动保存数据
|
||||||
const handleChange = (editor: IDomEditor) => {
|
const handleChange = (editor: IDomEditor) => {
|
||||||
editorRef.value = editor
|
editorRef.value = editor
|
||||||
// 防抖
|
// 创建一个防抖处理函数
|
||||||
setTimeout(() => {
|
const debouncedUpdateContent = debounce(() => {
|
||||||
// 过滤 xss
|
// 过滤 xss
|
||||||
topicData.content.value = DOMPurify.sanitize(editor.getHtml())
|
topicData.content.value = DOMPurify.sanitize(editor.getHtml())
|
||||||
}, 1007)
|
}, 1007)
|
||||||
|
|
||||||
|
// 调用防抖处理函数,会在延迟时间内只执行一次更新操作
|
||||||
|
debouncedUpdateContent()
|
||||||
// 计算用户输入了多少个字符
|
// 计算用户输入了多少个字符
|
||||||
textCount.value = editor.getText().trim().length
|
textCount.value = editor.getText().trim().length
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
/*
|
||||||
|
* 防抖函数,接受一个函数和一个延时时间
|
||||||
|
*/
|
||||||
export type DebouncedFunction<T extends (...args: any[]) => any> = (
|
export type DebouncedFunction<T extends (...args: any[]) => any> = (
|
||||||
...args: Parameters<T>
|
...args: Parameters<T>
|
||||||
) => void
|
) => void
|
||||||
|
|
|
@ -9,6 +9,9 @@ import KUNGalgameFooter from '@/components/KUNGalgameFooter.vue'
|
||||||
import { useKUNGalgameEditStore } from '@/store/modules/edit'
|
import { useKUNGalgameEditStore } from '@/store/modules/edit'
|
||||||
import { storeToRefs } from 'pinia'
|
import { storeToRefs } from 'pinia'
|
||||||
|
|
||||||
|
// 导入防抖函数
|
||||||
|
import { debounce } from '@/utils/debounce'
|
||||||
|
|
||||||
const topicData = storeToRefs(useKUNGalgameEditStore())
|
const topicData = storeToRefs(useKUNGalgameEditStore())
|
||||||
|
|
||||||
// 话题标题的文字
|
// 话题标题的文字
|
||||||
|
@ -34,10 +37,14 @@ const handelInput = () => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 防抖
|
// 创建一个防抖处理函数
|
||||||
setTimeout(() => {
|
const debouncedInput = debounce(() => {
|
||||||
|
// 过滤 xss
|
||||||
topicData.title.value = topicTitle.value
|
topicData.title.value = topicTitle.value
|
||||||
}, 1007)
|
}, 300)
|
||||||
|
|
||||||
|
// 调用防抖处理函数,会在延迟时间内只执行一次更新操作
|
||||||
|
debouncedInput()
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue