From 322b40da07d62a0756eec5eb0081383bf46d428b Mon Sep 17 00:00:00 2001 From: usecodelee Date: Wed, 21 Apr 2021 20:38:34 +0800 Subject: [PATCH] fix memory leak in electron --- src/editor.vue | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/editor.vue b/src/editor.vue index 1fc1461..3853d6f 100644 --- a/src/editor.vue +++ b/src/editor.vue @@ -92,6 +92,8 @@ this.initialize() }, beforeDestroy() { + this.quill.off('selection-change', this.handleSelectionChange) + this.quill.off('text-change', this.handleTextChange) this.quill = null delete this.quill }, @@ -119,28 +121,32 @@ } // Mark model as touched if editor lost focus - this.quill.on('selection-change', range => { - if (!range) { - this.$emit('blur', this.quill) - } else { - this.$emit('focus', this.quill) - } - }) + this.quill.on('selection-change', this.handleSelectionChange) // Update model if text changes - this.quill.on('text-change', (delta, oldDelta, source) => { - let html = this.$refs.editor.children[0].innerHTML - const quill = this.quill - const text = this.quill.getText() - if (html === '


') html = '' - this._content = html - this.$emit('input', this._content) - this.$emit('change', { html, text, quill }) - }) + this.quill.on('text-change', this.handleTextChange) // Emit ready event this.$emit('ready', this.quill) } + }, + // handle selection-change + handleSelectionChange(range) { + if (!range) { + this.$emit('blur', this.quill) + } else { + this.$emit('focus', this.quill) + } + }, + // handle text-change + handleTextChange(delta, oldDelta, source) { + let html = this.$refs.editor.children[0].innerHTML + const quill = this.quill + const text = this.quill.getText() + if (html === '


') html = '' + this._content = html + this.$emit('input', this._content) + this.$emit('change', { html, text, quill }) } }, watch: {