The keyboardHandler is now set using a setKeyboardHandler function
This commit is contained in:
parent
3ebd2865ea
commit
2074d723c0
3 changed files with 29 additions and 28 deletions
|
|
@ -55,6 +55,9 @@ exports.launch = function(env) {
|
|||
var TextMode = require("ace/mode/text").Mode;
|
||||
var UndoManager = require("ace/undomanager").UndoManager;
|
||||
|
||||
var vim = require("ace/mode/vim");
|
||||
var emacs = require("ace/mode/emacs");
|
||||
|
||||
var docs = {};
|
||||
|
||||
docs.js = new Document(document.getElementById("jstext").innerHTML);
|
||||
|
|
@ -82,6 +85,9 @@ exports.launch = function(env) {
|
|||
var container = document.getElementById("editor");
|
||||
env.editor = new Editor(new Renderer(container, theme));
|
||||
|
||||
// This is how you change the keyboardHandler.
|
||||
env.editor.setKeyboardHandler(vim);
|
||||
|
||||
function onDocChange() {
|
||||
var doc = getDoc();
|
||||
env.editor.setDocument(doc);
|
||||
|
|
|
|||
|
|
@ -110,6 +110,14 @@ var Editor =function(renderer, doc) {
|
|||
}
|
||||
};
|
||||
|
||||
this.setKeyboardHandler = function(keyboardHandler) {
|
||||
this.keyBinding.setKeyboardHandler(keyboardHandler);
|
||||
};
|
||||
|
||||
this.getKeyboardHandler = function() {
|
||||
return this.keyBinding.getKeyboardHandler();
|
||||
}
|
||||
|
||||
this.setDocument = function(doc) {
|
||||
if (this.doc == doc) return;
|
||||
|
||||
|
|
|
|||
|
|
@ -47,34 +47,11 @@ var default_win = require("ace/conf/keybindings/default_win").bindings;
|
|||
var canon = require("pilot/canon");
|
||||
require("ace/commands/default_commands");
|
||||
|
||||
var inputModes = {
|
||||
vim: require("ace/mode/vim"),
|
||||
emacs: require("ace/mode/emacs")
|
||||
};
|
||||
|
||||
var KeyBinding = function(editor, config) {
|
||||
this.$editor = editor;
|
||||
this.$data = { };
|
||||
this.$keyboardHandler = null;
|
||||
this.setConfig(config);
|
||||
|
||||
// PROBLEM: When this file is loaded via require.js, the types are already
|
||||
// declaired, BUT the startup() function is not called in pilot/types/basic.
|
||||
// Therefore the settings are not declaired. As a workaround, the setting
|
||||
// is declaired when the KeyBinding is created.
|
||||
var InputModeSetting = {
|
||||
name: "inputMode",
|
||||
description: "Which input mode do you want to use?",
|
||||
type: "text",
|
||||
defaultValue: "standard"
|
||||
};
|
||||
|
||||
this.$inputMode = null;
|
||||
settings.addSetting(InputModeSetting);
|
||||
|
||||
settings.getSetting("inputMode").addEventListener("change", function(e) {
|
||||
this.$inputMode = inputModes[e.value];
|
||||
this.$data = { };
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
(function() {
|
||||
|
|
@ -127,15 +104,25 @@ var KeyBinding = function(editor, config) {
|
|||
this.config.reverse = objectReverse.call(this, this.config, "|");
|
||||
};
|
||||
|
||||
this.setKeyboardHandler = function(keyboardHandler) {
|
||||
if (this.$keyboardHandler != keyboardHandler) {
|
||||
this.$data = { };
|
||||
this.$keyboardHandler = keyboardHandler;
|
||||
}
|
||||
};
|
||||
|
||||
this.getKeyboardHandler = function() {
|
||||
return this.$keyboardHandler;
|
||||
};
|
||||
|
||||
this.onCommandKey = function(e, hashId, keyCode) {
|
||||
key = (keyUtil[keyCode] ||
|
||||
String.fromCharCode(keyCode)).toLowerCase();
|
||||
|
||||
var toExecute;
|
||||
if (this.$inputMode) {
|
||||
if (this.$keyboardHandler) {
|
||||
toExecute =
|
||||
this.$inputMode.handleKeyboard(this.$data, hashId, key, e);
|
||||
this.$keyboardHandler.handleKeyboard(this.$data, hashId, key, e);
|
||||
}
|
||||
|
||||
// If there is nothing to execute yet, then use the default keymapping.
|
||||
|
|
@ -156,9 +143,9 @@ var KeyBinding = function(editor, config) {
|
|||
};
|
||||
|
||||
this.onTextInput = function(text) {
|
||||
if (this.$inputMode) {
|
||||
if (this.$keyboardHandler) {
|
||||
var toExecute =
|
||||
this.$inputMode.handleKeyboard(this.$data, 0, text, {});
|
||||
this.$keyboardHandler.handleKeyboard(this.$data, 0, text, {});
|
||||
var success = canon.exec(toExecute.command,
|
||||
{editor: this.$editor}, toExecute.args);
|
||||
if (success) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue