Mode Behaviours

This commit is contained in:
c-spencer 2011-05-21 18:43:40 +01:00
commit 1261c55913
2 changed files with 61 additions and 2 deletions

View file

@ -412,7 +412,7 @@ var Editor =function(renderer, session) {
var mode = session.getMode();
var cursor = this.getCursorPosition();
var transform = mode.transformInput(session.getState(cursor.row), this, session, text);
var transform = mode.transformInsert(session.getState(cursor.row), this, session, text);
if (transform)
text = transform.text;

View file

@ -179,11 +179,70 @@ var Mode = function() {
return defaultHandler ? defaultHandler.apply(this, args) : undefined;
};
this.transformInput = function(state, editor, session, text) {
this.addBehaviour = function (name, insert, remove) {
if (this.$behaviours === undefined) {
this.$behaviours = {};
}
this.$behaviours[name] = {
insert: insert,
remove: remove
};
}
this.removeBehaviour = function (name) {
if (this.$behaviours && this.$behaviours[name]) {
delete this.$behaviours[name];
}
}
this.inheritBehaviours = function (mode) {
if (typeof mode === "function") {
var behaviours = new mode().getBehaviours();
} else {
var behaviours = mode.getBehaviours();
}
for (var key in behaviours) {
if (behaviours.hasOwnProperty(key)) {
this.addBehaviour(key, behaviours[key].insert, behaviours[key].remove);
}
}
}
this.getBehaviours = function (filter) {
if (!filter) {
return this.$behaviours;
} else {
var ret = {}
for (var i = 0; i < filter.length; i++) {
if (this.$behaviours[filter[i]]) {
ret[filter[i]] = this.$behaviours[filter[i]];
}
}
return ret;
}
}
this.transformInsert = function(state, editor, session, text) {
for (var key in this.$behaviours) {
if (this.$behaviours.hasOwnProperty(key)) {
var ret = this.$behaviours[key].insert.apply(this, arguments);
if (ret !== false) {
return ret;
}
}
}
return false;
};
this.transformRemove = function(state, editor, session, range) {
for (var key in this.$behaviours) {
if (this.$behaviours.hasOwnProperty(key)) {
var ret = this.$behaviours[key].remove.apply(this, arguments);
if (ret !== false) {
return ret;
}
}
}
return range;
};