From 3e4b8cd409c62ead35b051622809ecb8caf530d3 Mon Sep 17 00:00:00 2001 From: Alasdair Smith Date: Tue, 20 Feb 2018 15:03:43 +0000 Subject: [PATCH] Clean up change listeners on sharejs --- .../coffee/ide/editor/sharejs/vendor/client/ace.coffee | 10 +++++++--- .../coffee/ide/editor/sharejs/vendor/client/cm.coffee | 10 +++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/services/web/public/coffee/ide/editor/sharejs/vendor/client/ace.coffee b/services/web/public/coffee/ide/editor/sharejs/vendor/client/ace.coffee index 997116fc02..2b7f7b2ace 100644 --- a/services/web/public/coffee/ide/editor/sharejs/vendor/client/ace.coffee +++ b/services/web/public/coffee/ide/editor/sharejs/vendor/client/ace.coffee @@ -106,7 +106,7 @@ window.sharejs.extendDoc 'attach_ace', (editor, keepEditorContents, maxDocLength # the work of editorDoc.insert and editorDoc.remove manually. These methods are # copied from ace.js doc#insert and #remove, and then inject the remote:true # flag into the delta. - doc.on 'insert', (pos, text) -> + onInsert = (pos, text) -> if (editorDoc.getLength() <= 1) editorDoc.$detectNewLine(text) @@ -129,7 +129,7 @@ window.sharejs.extendDoc 'attach_ace', (editor, keepEditorContents, maxDocLength suppress = false check() - doc.on 'delete', (pos, text) -> + onDelete = (pos, text) -> range = Range.fromPoints offsetToPos(pos), offsetToPos(pos + text.length) start = editorDoc.clippedPos(range.start.row, range.start.column) end = editorDoc.clippedPos(range.end.row, range.end.column) @@ -144,8 +144,12 @@ window.sharejs.extendDoc 'attach_ace', (editor, keepEditorContents, maxDocLength suppress = false check() + doc.on 'insert', onInsert + doc.on 'delete', onDelete + doc.detach_ace = -> - # TODO: can we remove the insert and delete event callbacks? + doc.removeListener 'insert', onInsert + doc.removeListener 'delete', onDelete editorDoc.removeListener 'change', editorListener delete doc.detach_ace diff --git a/services/web/public/coffee/ide/editor/sharejs/vendor/client/cm.coffee b/services/web/public/coffee/ide/editor/sharejs/vendor/client/cm.coffee index b6ea0ede63..f11bf34879 100644 --- a/services/web/public/coffee/ide/editor/sharejs/vendor/client/cm.coffee +++ b/services/web/public/coffee/ide/editor/sharejs/vendor/client/cm.coffee @@ -61,14 +61,14 @@ window.sharejs.extendDoc 'attach_cm', (editor, keepEditorContents) -> editorDoc.on 'change', editorListener - @on 'insert', (pos, text) -> + onInsert = (pos, text) -> suppress = true # All the primitives we need are already in CM's API. editor.replaceRange text, editor.posFromIndex(pos) suppress = false check() - @on 'delete', (pos, text) -> + onDelete = (pos, text) -> suppress = true from = editor.posFromIndex pos to = editor.posFromIndex (pos + text.length) @@ -76,8 +76,12 @@ window.sharejs.extendDoc 'attach_cm', (editor, keepEditorContents) -> suppress = false check() + @on 'insert', onInsert + @on 'delete', onDelete + @detach_cm = -> - # TODO: can we remove the insert and delete event callbacks? + @removeListener 'insert', onInsert + @removeListener 'delete', onDelete editorDoc.off 'change', editorListener delete @detach_cm