Jelajahi Sumber

:rotating_light: https://github.com/Vanessa219/vditor/pull/1089

Vanessa 4 tahun lalu
induk
melakukan
9e6be85979
4 mengubah file dengan 18 tambahan dan 14 penghapusan
  1. 2 0
      CHANGELOG.md
  2. 8 6
      src/ts/ui/initUI.ts
  3. 7 7
      src/ts/wysiwyg/index.ts
  4. 1 1
      types/index.d.ts

+ 2 - 0
CHANGELOG.md

@@ -97,6 +97,8 @@
 
 ### v3.8.8 / 2021-10-xx
 
+* [1089](https://github.com/Vanessa219/vditor/pull/1089) 解决内存泄漏问题 `开发重构`
+
 ### v3.8.7 / 2021-09-21
 
 * [1049](https://github.com/Vanessa219/vditor/issues/1049) #后面没有空格也会渲染为标题 `修复缺陷`

+ 8 - 6
src/ts/ui/initUI.ts

@@ -73,7 +73,7 @@ export const initUI = (vditor: IVditor) => {
             '<iframe style="width: 100%;height: 0;border: 0"></iframe>');
     }
 
-    setEditMode(vditor, vditor.options.mode, afterRender(vditor, contentElement));
+    setEditMode(vditor, vditor.options.mode, afterRender(vditor));
 
     document.execCommand("DefaultParagraphSeparator", false, "p");
 
@@ -141,13 +141,15 @@ export const setTypewriterPosition = (vditor: IVditor) => {
         ((height - vditor.toolbar.element.offsetHeight) / 2) + "px");
 };
 
-let resizeCb = () => {}
-export function UIUnbindListener () {
-    window.removeEventListener("resize", resizeCb)
+let resizeCb: () => void;
+
+export function UIUnbindListener() {
+    window.removeEventListener("resize", resizeCb);
 }
-const afterRender = (vditor: IVditor, contentElement: HTMLElement) => {
+
+const afterRender = (vditor: IVditor) => {
     setTypewriterPosition(vditor);
-    UIUnbindListener()
+    UIUnbindListener();
     window.addEventListener("resize", resizeCb = () => {
         setPadding(vditor);
         setTypewriterPosition(vditor);

+ 7 - 7
src/ts/wysiwyg/index.ts

@@ -39,6 +39,7 @@ class WYSIWYG {
     public preventInput: boolean;
     public composingLock = false;
     public commentIds: string[] = [];
+    private scrollListener: () => void;
 
     constructor(vditor: IVditor) {
         const divElement = document.createElement("div");
@@ -195,6 +196,10 @@ class WYSIWYG {
         this.selectPopover.setAttribute("style", "display:none");
     }
 
+    public unbindListener() {
+        window.removeEventListener("scroll", this.scrollListener);
+    }
+
     private copy(event: ClipboardEvent, vditor: IVditor) {
         const range = getSelection().getRangeAt(0);
         if (range.toString() === "") {
@@ -237,14 +242,9 @@ class WYSIWYG {
         event.clipboardData.setData("text/html", "");
     }
 
-    private scrollListener = () => {};
-    public unbindListener() {
-        window.removeEventListener("scroll", this.scrollListener)
-    }
-
     private bindEvent(vditor: IVditor) {
         this.unbindListener();
-        window.addEventListener("scroll", this.scrollListener  = () => {
+        window.addEventListener("scroll", this.scrollListener = () => {
             hidePanel(vditor, ["hint"]);
             if (this.popover.style.display !== "block" || this.selectPopover.style.display !== "block") {
                 return;
@@ -337,7 +337,7 @@ class WYSIWYG {
                 this.preventInput = false;
                 return;
             }
-            if (this.composingLock ||  event.data === "‘" || event.data === "“" || event.data === "《") {
+            if (this.composingLock || event.data === "‘" || event.data === "“" || event.data === "《") {
                 return;
             }
             const range = getSelection().getRangeAt(0);

+ 1 - 1
types/index.d.ts

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