untangle keybinding.js
This commit is contained in:
parent
d62c0aa08e
commit
97b00ea46b
4 changed files with 67 additions and 82 deletions
|
|
@ -284,6 +284,9 @@ exports.commands = [{
|
||||||
name: "indent",
|
name: "indent",
|
||||||
bindKey: bindKey("Tab", "Tab"),
|
bindKey: bindKey("Tab", "Tab"),
|
||||||
exec: function(editor) { editor.indent(); }
|
exec: function(editor) { editor.indent(); }
|
||||||
|
}, {
|
||||||
|
name: "insertstring",
|
||||||
|
exec: function(editor, str) { editor.insert(str); }
|
||||||
}, {
|
}, {
|
||||||
name: "inserttext",
|
name: "inserttext",
|
||||||
exec: function(editor, args) {
|
exec: function(editor, args) {
|
||||||
|
|
@ -318,21 +321,15 @@ exports.commands = [{
|
||||||
}, {
|
}, {
|
||||||
name: "unfold",
|
name: "unfold",
|
||||||
bindKey: bindKey("Alt-Shift-L", "Alt-Shift-L"),
|
bindKey: bindKey("Alt-Shift-L", "Alt-Shift-L"),
|
||||||
exec: function(editor) {
|
exec: function(editor) { editor.session.toggleFold(true); }
|
||||||
editor.session.toggleFold(true);
|
|
||||||
}
|
|
||||||
}, {
|
}, {
|
||||||
name: "foldall",
|
name: "foldall",
|
||||||
bindKey: bindKey("Alt-0", "Alt-0"),
|
bindKey: bindKey("Alt-0", "Alt-0"),
|
||||||
exec: function(editor) {
|
exec: function(editor) { editor.session.foldAll(); }
|
||||||
editor.session.foldAll();
|
|
||||||
}
|
|
||||||
}, {
|
}, {
|
||||||
name: "unfoldall",
|
name: "unfoldall",
|
||||||
bindKey: bindKey("Alt-Shift-0", "Alt-Shift-0"),
|
bindKey: bindKey("Alt-Shift-0", "Alt-Shift-0"),
|
||||||
exec: function(editor) {
|
exec: function(editor) { editor.session.unfold(); }
|
||||||
editor.session.unfold();
|
|
||||||
}
|
|
||||||
}];
|
}];
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -523,30 +523,11 @@ var Editor = function(renderer, session) {
|
||||||
mode.autoOutdent(lineState, session, cursor.row);
|
mode.autoOutdent(lineState, session, cursor.row);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.onTextInput = function(text, notPasted) {
|
this.onTextInput = function(text, pasted) {
|
||||||
if (!notPasted)
|
if (pasted)
|
||||||
this._emit("paste", text);
|
this._emit("paste", text);
|
||||||
|
|
||||||
// In case the text was not pasted and we got only one character, then
|
this.keyBinding.onTextInput(text, pasted);
|
||||||
// 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) {
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@
|
||||||
* Contributor(s):
|
* Contributor(s):
|
||||||
* Fabian Jakobs <fabian AT ajax DOT org>
|
* Fabian Jakobs <fabian AT ajax DOT org>
|
||||||
* Julian Viereck <julian.viereck@gmail.com>
|
* Julian Viereck <julian.viereck@gmail.com>
|
||||||
|
* Harutyun Amirjanyan <amirjanyan@gmail.com>
|
||||||
*
|
*
|
||||||
* Alternatively, the contents of this file may be used under the terms of
|
* Alternatively, the contents of this file may be used under the terms of
|
||||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||||
|
|
@ -46,71 +47,77 @@ require("../commands/default_commands");
|
||||||
var KeyBinding = function(editor) {
|
var KeyBinding = function(editor) {
|
||||||
this.$editor = editor;
|
this.$editor = editor;
|
||||||
this.$data = { };
|
this.$data = { };
|
||||||
this.$keyboardHandler = null;
|
this.$handlers = [this];
|
||||||
};
|
};
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
this.setKeyboardHandler = function(keyboardHandler) {
|
this.setKeyboardHandler = function(keyboardHandler) {
|
||||||
if (this.$keyboardHandler != keyboardHandler) {
|
if (this.$handlers[this.$handlers.length - 1] == keyboardHandler)
|
||||||
this.$data = { };
|
return;
|
||||||
this.$keyboardHandler = keyboardHandler;
|
this.$data = { };
|
||||||
}
|
this.$handlers = keyboardHandler ? [this, keyboardHandler] : [this];
|
||||||
|
};
|
||||||
|
|
||||||
|
this.addKeyboardHandler = function(keyboardHandler) {
|
||||||
|
this.removeKeyboardHandler(keyboardHandler);
|
||||||
|
this.$handlers.push(keyboardHandler);
|
||||||
|
};
|
||||||
|
|
||||||
|
this.removeKeyboardHandler = function(keyboardHandler) {
|
||||||
|
var i = this.$handlers.indexOf(keyboardHandler);
|
||||||
|
if (i == -1)
|
||||||
|
return false;
|
||||||
|
this.$handlers.splice(i, 1);
|
||||||
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.getKeyboardHandler = function() {
|
this.getKeyboardHandler = function() {
|
||||||
return this.$keyboardHandler;
|
return this.$handlers[this.$handlers - 1];
|
||||||
};
|
};
|
||||||
|
|
||||||
this.$callKeyboardHandler = function (e, hashId, keyOrText, keyCode) {
|
this.$callKeyboardHandlers = function (hashId, keyString, keyCode, e) {
|
||||||
var toExecute;
|
for (var i = this.$handlers.length; i--;) {
|
||||||
var commands = this.$editor.commands;
|
var toExecute = this.$handlers[i].handleKeyboard(
|
||||||
|
this.$data, hashId, keyString, keyCode, e
|
||||||
if (this.$keyboardHandler) {
|
|
||||||
toExecute =
|
|
||||||
this.$keyboardHandler.handleKeyboard(this.$data, hashId, keyOrText, keyCode, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// If there is nothing to execute yet, then use the default keymapping.
|
|
||||||
if (!toExecute || !toExecute.command) {
|
|
||||||
if (hashId != 0 || keyCode != 0) {
|
|
||||||
toExecute = {
|
|
||||||
command: commands.findKeyCommand(hashId, keyOrText)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
toExecute = {
|
|
||||||
command: "inserttext",
|
|
||||||
args: {
|
|
||||||
text: keyOrText
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var success = false;
|
|
||||||
if (toExecute && toExecute.command) {
|
|
||||||
success = commands.exec(
|
|
||||||
toExecute.command,
|
|
||||||
this.$editor, toExecute.args
|
|
||||||
);
|
);
|
||||||
if (success) {
|
if (toExecute)
|
||||||
event.stopEvent(e);
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return success;
|
|
||||||
|
if (!toExecute || !toExecute.command)
|
||||||
|
return false;
|
||||||
|
var success = false, commands = this.$editor.commands;
|
||||||
|
|
||||||
|
// allow keyboardHandler to consume keys
|
||||||
|
if (toExecute.command != "null")
|
||||||
|
success = commands.exec(toExecute.command, this.$editor, toExecute.args);
|
||||||
|
else
|
||||||
|
success = true;
|
||||||
|
|
||||||
|
if (success && e)
|
||||||
|
event.stopEvent(e);
|
||||||
|
|
||||||
|
return success
|
||||||
};
|
};
|
||||||
|
|
||||||
this.onCommandKey = function(e, hashId, keyCode, keyString) {
|
this.handleKeyboard = function(data, hashId, keyString) {
|
||||||
// In case there is no keyString, try to interprete the keyCode.
|
return {
|
||||||
if (!keyString) {
|
command: this.$editor.commands.findKeyCommand(hashId, keyString)
|
||||||
keyString = keyUtil.keyCodeToString(keyCode);
|
|
||||||
}
|
}
|
||||||
return this.$callKeyboardHandler(e, hashId, keyString, keyCode);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.onTextInput = function(text) {
|
this.onCommandKey = function(e, hashId, keyCode) {
|
||||||
return this.$callKeyboardHandler({}, 0, text, 0);
|
var keyString = keyUtil.keyCodeToString(keyCode);
|
||||||
}
|
this.$callKeyboardHandlers(hashId, keyString, keyCode, e);
|
||||||
|
};
|
||||||
|
|
||||||
|
this.onTextInput = function(text, pasted) {
|
||||||
|
var success = false;
|
||||||
|
if (!pasted && text.length == 1)
|
||||||
|
success = this.$callKeyboardHandlers(0, text);
|
||||||
|
if (!success)
|
||||||
|
this.$editor.commands.exec("insertstring", this.$editor, text);
|
||||||
|
};
|
||||||
|
|
||||||
}).call(KeyBinding.prototype);
|
}).call(KeyBinding.prototype);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -73,10 +73,10 @@ var TextInput = function(parentNode, host) {
|
||||||
if (value.charCodeAt(value.length-1) == PLACEHOLDER.charCodeAt(0)) {
|
if (value.charCodeAt(value.length-1) == PLACEHOLDER.charCodeAt(0)) {
|
||||||
value = value.slice(0, -1);
|
value = value.slice(0, -1);
|
||||||
if (value)
|
if (value)
|
||||||
host.onTextInput(value, !pasted);
|
host.onTextInput(value, pasted);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
host.onTextInput(value, !pasted);
|
host.onTextInput(value, pasted);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If editor is no longer focused we quit immediately, since
|
// If editor is no longer focused we quit immediately, since
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue