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 TextMode = require("ace/mode/text").Mode;
|
||||||
var UndoManager = require("ace/undomanager").UndoManager;
|
var UndoManager = require("ace/undomanager").UndoManager;
|
||||||
|
|
||||||
|
var vim = require("ace/mode/vim");
|
||||||
|
var emacs = require("ace/mode/emacs");
|
||||||
|
|
||||||
var docs = {};
|
var docs = {};
|
||||||
|
|
||||||
docs.js = new Document(document.getElementById("jstext").innerHTML);
|
docs.js = new Document(document.getElementById("jstext").innerHTML);
|
||||||
|
|
@ -82,6 +85,9 @@ exports.launch = function(env) {
|
||||||
var container = document.getElementById("editor");
|
var container = document.getElementById("editor");
|
||||||
env.editor = new Editor(new Renderer(container, theme));
|
env.editor = new Editor(new Renderer(container, theme));
|
||||||
|
|
||||||
|
// This is how you change the keyboardHandler.
|
||||||
|
env.editor.setKeyboardHandler(vim);
|
||||||
|
|
||||||
function onDocChange() {
|
function onDocChange() {
|
||||||
var doc = getDoc();
|
var doc = getDoc();
|
||||||
env.editor.setDocument(doc);
|
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) {
|
this.setDocument = function(doc) {
|
||||||
if (this.doc == doc) return;
|
if (this.doc == doc) return;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -47,34 +47,11 @@ var default_win = require("ace/conf/keybindings/default_win").bindings;
|
||||||
var canon = require("pilot/canon");
|
var canon = require("pilot/canon");
|
||||||
require("ace/commands/default_commands");
|
require("ace/commands/default_commands");
|
||||||
|
|
||||||
var inputModes = {
|
|
||||||
vim: require("ace/mode/vim"),
|
|
||||||
emacs: require("ace/mode/emacs")
|
|
||||||
};
|
|
||||||
|
|
||||||
var KeyBinding = function(editor, config) {
|
var KeyBinding = function(editor, config) {
|
||||||
this.$editor = editor;
|
this.$editor = editor;
|
||||||
this.$data = { };
|
this.$data = { };
|
||||||
|
this.$keyboardHandler = null;
|
||||||
this.setConfig(config);
|
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() {
|
(function() {
|
||||||
|
|
@ -127,15 +104,25 @@ var KeyBinding = function(editor, config) {
|
||||||
this.config.reverse = objectReverse.call(this, this.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) {
|
this.onCommandKey = function(e, hashId, keyCode) {
|
||||||
key = (keyUtil[keyCode] ||
|
key = (keyUtil[keyCode] ||
|
||||||
String.fromCharCode(keyCode)).toLowerCase();
|
String.fromCharCode(keyCode)).toLowerCase();
|
||||||
|
|
||||||
var toExecute;
|
var toExecute;
|
||||||
if (this.$inputMode) {
|
if (this.$keyboardHandler) {
|
||||||
toExecute =
|
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.
|
// 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) {
|
this.onTextInput = function(text) {
|
||||||
if (this.$inputMode) {
|
if (this.$keyboardHandler) {
|
||||||
var toExecute =
|
var toExecute =
|
||||||
this.$inputMode.handleKeyboard(this.$data, 0, text, {});
|
this.$keyboardHandler.handleKeyboard(this.$data, 0, text, {});
|
||||||
var success = canon.exec(toExecute.command,
|
var success = canon.exec(toExecute.command,
|
||||||
{editor: this.$editor}, toExecute.args);
|
{editor: this.$editor}, toExecute.args);
|
||||||
if (success) {
|
if (success) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue