From 8ea397da5ce247a9f2bd6ac6ec2246fe13f26b19 Mon Sep 17 00:00:00 2001 From: XinKeng <29268441+gengxiaoxiaoxin@users.noreply.github.com> Date: Mon, 21 Aug 2023 01:14:12 +0800 Subject: [PATCH] perf: increasing the editor performance (#4445) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A function to debounce editor updates was added to the DefaultEditor.vue component. This change was necessary to prevent excessive event executions. Now, 'update:raw', 'update:content', and 'update' events will be emitted 250ms after the last update, reducing the number of redundant operations and increasing the editor's performance. #### What type of PR is this? /kind improvement #### What this PR does / why we need it: 目前默认编辑器在文章内容过多时,会出现输入延迟。 #### Which issue(s) this PR fixes: Fixes #4389 #### Special notes for your reviewer: #### Does this PR introduce a user-facing change? ```release-note 增强默认编辑器在文章内容过大时的性能。 ``` --- console/src/components/editor/DefaultEditor.vue | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/console/src/components/editor/DefaultEditor.vue b/console/src/components/editor/DefaultEditor.vue index 9d424d777..38ea17c28 100644 --- a/console/src/components/editor/DefaultEditor.vue +++ b/console/src/components/editor/DefaultEditor.vue @@ -78,6 +78,7 @@ import { useI18n } from "vue-i18n"; import { i18n } from "@/locales"; import { OverlayScrollbarsComponent } from "overlayscrollbars-vue"; import { usePluginModuleStore, type PluginModule } from "@/stores/plugin"; +import { useDebounceFn } from "@vueuse/core"; const { t } = useI18n(); @@ -141,6 +142,14 @@ onMounted(() => { extensionsFromPlugins.push(...extensions); }); + // debounce OnUpdate + const debounceOnUpdate = useDebounceFn(() => { + const html = editor.value?.getHTML() + ""; + emit("update:raw", html); + emit("update:content", html); + emit("update", html); + }, 250); + editor.value = new Editor({ content: props.raw, extensions: [ @@ -236,9 +245,7 @@ onMounted(() => { ], autofocus: "start", onUpdate: () => { - emit("update:raw", editor.value?.getHTML() + ""); - emit("update:content", editor.value?.getHTML() + ""); - emit("update", editor.value?.getHTML() + ""); + debounceOnUpdate(); nextTick(() => { handleGenerateTableOfContent(); });