From 7b45cdd145dc4af8403744019e6176bd980bb379 Mon Sep 17 00:00:00 2001 From: tangshengzhi Date: Sun, 10 Oct 2021 18:43:34 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=86=85=E5=AD=98=E6=B3=84?= =?UTF-8?q?=E6=BC=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 5 ++++- src/ts/ui/initUI.ts | 8 ++++++-- src/ts/wysiwyg/index.ts | 8 +++++++- types/index.d.ts | 1 + 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index b34dadc59..10f801c64 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,7 +19,7 @@ import {Tip} from "./ts/tip/index"; import {Toolbar} from "./ts/toolbar/index"; import {disableToolbar, hidePanel} from "./ts/toolbar/setToolbar"; import {enableToolbar} from "./ts/toolbar/setToolbar"; -import {initUI} from "./ts/ui/initUI"; +import {initUI, UIUnbindListener} from "./ts/ui/initUI"; import {setCodeTheme} from "./ts/ui/setCodeTheme"; import {setContentTheme} from "./ts/ui/setContentTheme"; import {setPreviewMode} from "./ts/ui/setPreviewMode"; @@ -322,6 +322,9 @@ class Vditor extends VditorMethod { this.vditor.element.removeAttribute("style"); document.getElementById("vditorIconScript").remove(); this.clearCache(); + + UIUnbindListener(); + this.vditor.wysiwyg.unbindListener(); } /** 获取评论 ID */ diff --git a/src/ts/ui/initUI.ts b/src/ts/ui/initUI.ts index 9f7cc7492..7710e87a0 100644 --- a/src/ts/ui/initUI.ts +++ b/src/ts/ui/initUI.ts @@ -141,10 +141,14 @@ export const setTypewriterPosition = (vditor: IVditor) => { ((height - vditor.toolbar.element.offsetHeight) / 2) + "px"); }; +let resizeCb = () => {} +export function UIUnbindListener () { + window.removeEventListener("resize", resizeCb) +} const afterRender = (vditor: IVditor, contentElement: HTMLElement) => { setTypewriterPosition(vditor); - - window.addEventListener("resize", () => { + UIUnbindListener() + window.addEventListener("resize", resizeCb = () => { setPadding(vditor); setTypewriterPosition(vditor); }); diff --git a/src/ts/wysiwyg/index.ts b/src/ts/wysiwyg/index.ts index 5c538f4eb..b02f3a5ee 100644 --- a/src/ts/wysiwyg/index.ts +++ b/src/ts/wysiwyg/index.ts @@ -237,8 +237,14 @@ class WYSIWYG { event.clipboardData.setData("text/html", ""); } + private scrollListener = () => {}; + public unbindListener() { + window.removeEventListener("scroll", this.scrollListener) + } + private bindEvent(vditor: IVditor) { - window.addEventListener("scroll", () => { + this.unbindListener(); + window.addEventListener("scroll", this.scrollListener = () => { hidePanel(vditor, ["hint"]); if (this.popover.style.display !== "block" || this.selectPopover.style.display !== "block") { return; diff --git a/types/index.d.ts b/types/index.d.ts index 3427b5b99..fb723a123 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -747,6 +747,7 @@ interface IVditor { triggerRemoveComment(vditor: IVditor): void, showComment(): void, hideComment(): void, + unbindListener(): void }; ir?: { range: Range,