pref: debounce

This commit is contained in:
KUN1007 2023-08-24 23:59:37 +08:00
parent 958e734e85
commit 6f3829c36b
3 changed files with 20 additions and 5 deletions

View file

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

View file

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

View file

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