merge julians's key handler changes

This commit is contained in:
Fabian Jakobs 2011-07-15 08:50:13 +00:00
commit aef91c2d31
4 changed files with 52 additions and 18 deletions

View file

@ -84,7 +84,11 @@ exports.launch = function(env) {
emacs: emacs, emacs: emacs,
// This is a way to define simple keyboard remappings // This is a way to define simple keyboard remappings
custom: new HashHandler({ custom: new HashHandler({
"gotoright": "Tab" "gotoright": "Tab",
"indent": "]",
"outdent": "[",
"gotolinestart": "^",
"gotolineend": "$"
}) })
} }

View file

@ -515,8 +515,27 @@ var Editor =function(renderer, session) {
} }
}; };
this.onTextInput = function(text) { this.onTextInput = function(text, notPasted) {
this.keyBinding.onTextInput(text); // In case the text was not pasted and we got only one character, then
// handel it as a command key stroke.
if (notPasted && text.length == 1) {
// Note: The `null` as `keyCode` is important here, as there are
// some checks in the code for `keyCode == 0` meaning the text comes
// from the keyBinding.onTextInput code path.
var handled = this.keyBinding.onCommandKey({}, 0, null, text);
// Check if the text was handled. If not, then handled it as "normal"
// text and insert it to the editor directly. This shouldn't be done
// using the this.keyBinding.onTextInput(text) function, as it would
// make the `text` get sent to the keyboardHandler twice, which might
// turn out to be a bad thing in case there is a custome keyboard
// handler like the StateHandler.
if (!handled) {
this.insert(text);
}
} else {
this.keyBinding.onTextInput(text);
}
}; };
this.onCommandKey = function(e, hashId, keyCode) { this.onCommandKey = function(e, hashId, keyCode) {

View file

@ -88,22 +88,27 @@ var KeyBinding = function(editor) {
} }
} }
var success = false;
if (toExecute) { if (toExecute) {
var success = canon.exec(toExecute.command, success = canon.exec(toExecute.command,
env, "editor", toExecute.args); env, "editor", toExecute.args);
if (success) { if (success) {
return event.stopEvent(e); event.stopEvent(e);
} }
} }
return success;
}; };
this.onCommandKey = function(e, hashId, keyCode) { this.onCommandKey = function(e, hashId, keyCode, keyString) {
var keyString = keyUtil.keyCodeToString(keyCode); // In case there is no keyString, try to interprete the keyCode.
this.$callKeyboardHandler(e, hashId, keyString, keyCode); if (!keyString) {
keyString = keyUtil.keyCodeToString(keyCode);
}
return this.$callKeyboardHandler(e, hashId, keyString, keyCode);
}; };
this.onTextInput = function(text) { this.onTextInput = function(text) {
this.$callKeyboardHandler({}, 0, text, 0); return this.$callKeyboardHandler({}, 0, text, 0);
} }
}).call(KeyBinding.prototype); }).call(KeyBinding.prototype);

View file

@ -54,6 +54,7 @@ var TextInput = function(parentNode, host) {
var inCompostion = false; var inCompostion = false;
var copied = false; var copied = false;
var pasted = false;
var tempStyle = ''; var tempStyle = '';
function sendText(valueToSend) { function sendText(valueToSend) {
@ -64,15 +65,18 @@ var TextInput = function(parentNode, host) {
value = value.slice(0, -1); value = value.slice(0, -1);
if (value) if (value)
host.onTextInput(value); host.onTextInput(value);
} else } else {
host.onTextInput(value); host.onTextInput(value);
}
// If editor is no longer focused we quit immediately, since // If editor is no longer focused we quit immediately, since
// it means that something else like CLI is in charge now. // it means that something else like CLI is in charge now.
if (!isFocused()) return false; if (!isFocused()) return false;
} }
} }
copied = false; copied = false;
pasted = false;
// Safari doesn't fire copy events if no text is selected // Safari doesn't fire copy events if no text is selected
text.value = PLACEHOLDER; text.value = PLACEHOLDER;
@ -157,6 +161,8 @@ var TextInput = function(parentNode, host) {
}; };
event.addListener(text, "textInput", onTextInput); event.addListener(text, "textInput", onTextInput);
event.addListener(text, "paste", function(e) { event.addListener(text, "paste", function(e) {
// Mark that the next input text comes from past.
pasted = true;
// Some browsers support the event.clipboardData API. Use this to get // Some browsers support the event.clipboardData API. Use this to get
// the pasted content which increases speed if pasting a lot of lines. // the pasted content which increases speed if pasting a lot of lines.
if (e.clipboardData && e.clipboardData.getData) { if (e.clipboardData && e.clipboardData.getData) {