Bläddra i källkod

解决内存泄漏问题 (#1089)

Co-authored-by: tangshengzhi <[email protected]>
tangshengzhi 4 år sedan
förälder
incheckning
0b6fc30e0e
4 ändrade filer med 18 tillägg och 4 borttagningar
  1. 4 1
      src/index.ts
  2. 6 2
      src/ts/ui/initUI.ts
  3. 7 1
      src/ts/wysiwyg/index.ts
  4. 1 0
      types/index.d.ts

+ 4 - 1
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 */

+ 6 - 2
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);
     });

+ 7 - 1
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;

+ 1 - 0
types/index.d.ts

@@ -747,6 +747,7 @@ interface IVditor {
         triggerRemoveComment(vditor: IVditor): void,
         showComment(): void,
         hideComment(): void,
+        unbindListener(): void
     };
     ir?: {
         range: Range,