From 76cbc3f543811b24b22bd90417dd8258ede90cf7 Mon Sep 17 00:00:00 2001 From: nightwing Date: Tue, 13 Dec 2011 19:38:04 +0400 Subject: [PATCH] add support for keyboardHandler.attach/detach --- lib/ace/keyboard/keybinding.js | 41 +++++++++++++++++++++------------- lib/ace/keyboard/vim.js | 11 ++++----- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/lib/ace/keyboard/keybinding.js b/lib/ace/keyboard/keybinding.js index 79fb7aa9..d1c9a0cf 100644 --- a/lib/ace/keyboard/keybinding.js +++ b/lib/ace/keyboard/keybinding.js @@ -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; }; diff --git a/lib/ace/keyboard/vim.js b/lib/ace/keyboard/vim.js index f1392ba9..102414e8 100644 --- a/lib/ace/keyboard/vim.js +++ b/lib/ace/keyboard/vim.js @@ -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"; }; });