add support for keyboardHandler.attach/detach
This commit is contained in:
parent
1a5ce52188
commit
76cbc3f543
2 changed files with 31 additions and 21 deletions
|
|
@ -42,7 +42,6 @@ define(function(require, exports, module) {
|
||||||
|
|
||||||
var keyUtil = require("../lib/keys");
|
var keyUtil = require("../lib/keys");
|
||||||
var event = require("../lib/event");
|
var event = require("../lib/event");
|
||||||
require("../commands/default_commands");
|
|
||||||
|
|
||||||
var KeyBinding = function(editor) {
|
var KeyBinding = function(editor) {
|
||||||
this.$editor = editor;
|
this.$editor = editor;
|
||||||
|
|
@ -52,34 +51,44 @@ var KeyBinding = function(editor) {
|
||||||
};
|
};
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
this.setDefaultHandler = function(keyboardHandler) {
|
this.setDefaultHandler = function(kb) {
|
||||||
this.removeKeyboardHandler(this.$defaultHandler);
|
this.removeKeyboardHandler(this.$defaultHandler);
|
||||||
this.$defaultHandler = keyboardHandler;
|
this.$defaultHandler = kb;
|
||||||
if (keyboardHandler)
|
this.addKeyboardHandler(kb, 0);
|
||||||
this.$handlers.unshift(keyboardHandler);
|
this.$data = {editor: this.$editor};
|
||||||
this.$data = { };
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.setKeyboardHandler = function(keyboardHandler) {
|
this.setKeyboardHandler = function(kb) {
|
||||||
if (this.$handlers[this.$handlers.length - 1] == keyboardHandler)
|
if (this.$handlers[this.$handlers.length - 1] == kb)
|
||||||
return;
|
return;
|
||||||
this.$data = { };
|
|
||||||
this.$handlers = [];
|
this.$handlers = [];
|
||||||
this.setDefaultHandler(this.$defaultHandler);
|
this.setDefaultHandler(this.$defaultHandler);
|
||||||
if (keyboardHandler)
|
this.addKeyboardHandler(kb, 1);
|
||||||
this.$handlers.push(keyboardHandler);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.addKeyboardHandler = function(keyboardHandler) {
|
this.addKeyboardHandler = function(kb, pos) {
|
||||||
this.removeKeyboardHandler(keyboardHandler);
|
if (!kb)
|
||||||
this.$handlers.push(keyboardHandler);
|
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) {
|
this.removeKeyboardHandler = function(kb) {
|
||||||
var i = this.$handlers.indexOf(keyboardHandler);
|
var i = this.$handlers.indexOf(kb);
|
||||||
if (i == -1)
|
if (i == -1)
|
||||||
return false;
|
return false;
|
||||||
this.$handlers.splice(i, 1);
|
this.$handlers.splice(i, 1);
|
||||||
|
kb.detach && kb.detach(this.$editor);
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,20 +8,21 @@ var util = require("./vim/maps/util");
|
||||||
|
|
||||||
exports.handler = require("./vim/keyboard").handler;
|
exports.handler = require("./vim/keyboard").handler;
|
||||||
|
|
||||||
exports.onCursorMove = function() {
|
exports.onCursorMove = function(e) {
|
||||||
commands.onCursorMove();
|
commands.onCursorMove(e.editor);
|
||||||
onCursorMove.scheduled = false;
|
exports.onCursorMove.scheduled = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.attach = function(editor){
|
exports.handler.attach = function(editor) {
|
||||||
editor.on("click", exports.onCursorMove);
|
editor.on("click", exports.onCursorMove);
|
||||||
if (util.currentMode !== "insert")
|
if (util.currentMode !== "insert")
|
||||||
commands.coreCommands.stop.exec(editor);
|
commands.coreCommands.stop.exec(editor);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.detach = function(editor){
|
exports.handler.detach = function(editor) {
|
||||||
editor.removeListener("click", exports.onCursorMove);
|
editor.removeListener("click", exports.onCursorMove);
|
||||||
commands.coreCommands.start.exec(editor);
|
commands.coreCommands.start.exec(editor);
|
||||||
|
util.currentMode = "normal";
|
||||||
};
|
};
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue