From e1187f3d8a814688122bf8f7149e0d359c101307 Mon Sep 17 00:00:00 2001 From: Alasdair Smith Date: Tue, 20 Feb 2018 13:16:37 +0000 Subject: [PATCH] Listen for changes to the CodeMirror Doc, instead of all changes to the editor This prevents an issue where switching docs (i.e. files) would cause the newly opened doc from being inserted into the old doc. This approach is similar to Ace's sessions. --- .../coffee/ide/editor/sharejs/vendor/client/cm.coffee | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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 7b32ed541d..d2cf8f2dce 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 @@ -36,6 +36,8 @@ window.sharejs.extendDoc 'attach_cm', (editor, keepEditorContents) -> throw new Error 'Only text documents can be attached to CodeMirror2' sharedoc = @ + editorDoc = editor.getDoc() + check = -> window.setTimeout -> editorText = editor.getValue() @@ -64,11 +66,10 @@ window.sharejs.extendDoc 'attach_cm', (editor, keepEditorContents) -> # Listen for edits in CodeMirror. editorListener = (ed, change) -> return if suppress - applyCMToShareJS editor, change, sharedoc - + applyCMToShareJS editorDoc, change, sharedoc check() - editor.on 'change', editorListener + editorDoc.on 'change', editorListener @on 'insert', (pos, text) -> suppress = true @@ -87,7 +88,7 @@ window.sharejs.extendDoc 'attach_cm', (editor, keepEditorContents) -> @detach_cm = -> # TODO: can we remove the insert and delete event callbacks? - editor.off 'onChange', editorListener + editorDoc.off 'change', editorListener delete @detach_cm return