Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add CodeMirrorAdapter#detach to remove CodeMirror listeners

  • Loading branch information...
commit f172b4f447d0c1317cbc69f08ff472365762a0aa 1 parent db10fb2
@timjb timjb authored
View
37 lib/codemirror-adapter.js
@@ -9,13 +9,23 @@ ot.CodeMirrorAdapter = (function () {
this.ignoreNextChange = false;
this.oldValue = this.cm.getValue();
- var self = this;
- cm.on('change', function (_, change) { self.onChange(change); });
- cm.on('cursorActivity', function () { self.trigger('cursorActivity'); });
- cm.on('blur', function () { self.onBlur(); });
- cm.on('focus', function () { self.trigger('cursorActivity'); });
+ bind(this, 'onChange');
+ bind(this, 'onCursorActivity');
+ bind(this, 'onFocus');
+ bind(this, 'onBlur');
+ cm.on('change', this.onChange);
+ cm.on('cursorActivity', this.onCursorActivity);
+ cm.on('focus', this.onFocus);
+ cm.on('blur', this.onBlur);
}
+ CodeMirrorAdapter.prototype.detach = function () {
+ this.cm.off('change', this.onChange);
+ this.cm.off('cursorActivity', this.onCursorActivity);
+ this.cm.off('focus', this.onFocus);
+ this.cm.off('blur', this.onBlur);
+ };
+
// The oldValue is needed to find
CodeMirrorAdapter.operationFromCodeMirrorChange = function (change, oldValue) {
var operation = new TextOperation();
@@ -136,7 +146,7 @@ ot.CodeMirrorAdapter = (function () {
this.callbacks = cb;
};
- CodeMirrorAdapter.prototype.onChange = function (change) {
+ CodeMirrorAdapter.prototype.onChange = function (_, change) {
if (!this.ignoreNextChange) {
var operation = CodeMirrorAdapter.operationFromCodeMirrorChange(change, this.oldValue);
this.trigger('change', this.oldValue, operation);
@@ -145,6 +155,11 @@ ot.CodeMirrorAdapter = (function () {
this.oldValue = this.cm.getValue();
};
+ CodeMirrorAdapter.prototype.onCursorActivity =
+ CodeMirrorAdapter.prototype.onFocus = function () {
+ this.trigger('cursorActivity');
+ };
+
CodeMirrorAdapter.prototype.onBlur = function () {
if (!this.cm.somethingSelected()) { this.trigger('blur'); }
};
@@ -258,6 +273,16 @@ ot.CodeMirrorAdapter = (function () {
}
}
+ // Bind a method to an object, so it doesn't matter whether you call
+ // object.method() directly or pass object.method as a reference to another
+ // function.
+ function bind (obj, method) {
+ var fn = obj[method];
+ obj[method] = function () {
+ fn.apply(obj, arguments);
+ };
+ }
+
return CodeMirrorAdapter;
}());
View
12 test/phantomjs/test-codemirror-adapter.js
@@ -122,6 +122,18 @@
ok(cm.redo === redoFn);
});
+ test("detach", function () {
+ var cm = CodeMirror(document.body, {});
+ var cmAdapter = new CodeMirrorAdapter(cm);
+ var changes = 0;
+ cmAdapter.registerCallbacks({ change: function () { changes += 1; } });
+ cm.setValue("42");
+ ok(changes === 1);
+ cmAdapter.detach();
+ cm.setValue("23");
+ ok(changes === 1);
+ });
+
// TODO:
// * trigger 'cursorActivity' (and ordering with 'change' event)
// * setCursor
Please sign in to comment.
Something went wrong with that request. Please try again.