add support for keyboardHandler.attach/detach

This commit is contained in:
nightwing 2011-12-13 19:38:04 +04:00
commit 76cbc3f543
2 changed files with 31 additions and 21 deletions

View file

@ -42,7 +42,6 @@ define(function(require, exports, module) {
var keyUtil = require("../lib/keys");
var event = require("../lib/event");
require("../commands/default_commands");
var KeyBinding = function(editor) {
this.$editor = editor;
@ -52,34 +51,44 @@ var KeyBinding = function(editor) {
};
(function() {
this.setDefaultHandler = function(keyboardHandler) {
this.setDefaultHandler = function(kb) {
this.removeKeyboardHandler(this.$defaultHandler);
this.$defaultHandler = keyboardHandler;
if (keyboardHandler)
this.$handlers.unshift(keyboardHandler);
this.$data = { };
this.$defaultHandler = kb;
this.addKeyboardHandler(kb, 0);
this.$data = {editor: this.$editor};
};
this.setKeyboardHandler = function(keyboardHandler) {
if (this.$handlers[this.$handlers.length - 1] == keyboardHandler)
this.setKeyboardHandler = function(kb) {
if (this.$handlers[this.$handlers.length - 1] == kb)
return;
this.$data = { };
this.$handlers = [];
this.setDefaultHandler(this.$defaultHandler);
if (keyboardHandler)
this.$handlers.push(keyboardHandler);
this.addKeyboardHandler(kb, 1);
};
this.addKeyboardHandler = function(keyboardHandler) {
this.removeKeyboardHandler(keyboardHandler);
this.$handlers.push(keyboardHandler);
this.addKeyboardHandler = function(kb, pos) {
if (!kb)
return;
var i = this.$handlers.indexOf(kb);
if (i != -1)
this.$handlers.splice(i, 1);
if (pos == undefined)
this.$handlers.push(kb);
else
this.$handlers.splice(pos, 0, kb);
if (i == -1 && kb.attach)
kb.attach(this.$editor);
};
this.removeKeyboardHandler = function(keyboardHandler) {
var i = this.$handlers.indexOf(keyboardHandler);
this.removeKeyboardHandler = function(kb) {
var i = this.$handlers.indexOf(kb);
if (i == -1)
return false;
this.$handlers.splice(i, 1);
kb.detach && kb.detach(this.$editor);
return true;
};

View file

@ -8,20 +8,21 @@ var util = require("./vim/maps/util");
exports.handler = require("./vim/keyboard").handler;
exports.onCursorMove = function() {
commands.onCursorMove();
onCursorMove.scheduled = false;
exports.onCursorMove = function(e) {
commands.onCursorMove(e.editor);
exports.onCursorMove.scheduled = false;
};
exports.attach = function(editor){
exports.handler.attach = function(editor) {
editor.on("click", exports.onCursorMove);
if (util.currentMode !== "insert")
commands.coreCommands.stop.exec(editor);
};
exports.detach = function(editor){
exports.handler.detach = function(editor) {
editor.removeListener("click", exports.onCursorMove);
commands.coreCommands.start.exec(editor);
util.currentMode = "normal";
};
});