From 6d4524be0c8798204369d66ddceba277f3b43cf1 Mon Sep 17 00:00:00 2001 From: Julien Biezemans Date: Fri, 1 Aug 2014 15:12:18 +0200 Subject: [PATCH] Augment CodeMirror instance with detachShareJsDoc() In a single app context, CodeMirror instances might be attached several times to the same shared doc. This can lead to multiple share channels attached to multiple ghost instances of CodeMirror for the same file, causing an infinite loop while the editors keep appending content to the file. This new method allows for safely disconnecting a shareJs doc from a CodeMirror instance. --- share-codemirror.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/share-codemirror.js b/share-codemirror.js index 9f66c7c..f4a4c34 100644 --- a/share-codemirror.js +++ b/share-codemirror.js @@ -33,11 +33,21 @@ // *** local -> remote changes - cm.on('change', function (cm, change) { - if (suppress) return; + cm.on('change', onLocalChange); + + function onLocalChange(cm, change) { + if (ctx.suppress) return; + ctx.suppress = true; applyToShareJS(cm, change); + ctx.suppress = false; check(); - }); + } + + cm.detachShareJsDoc = function () { + ctx.onRemove = null; + ctx.onInsert = null; + cm.off('change', onLocalChange); + } // Convert a CodeMirror change into an op understood by share.js function applyToShareJS(cm, change) {