Implement color hotkeys

This commit is contained in:
Pavel Djundik 2016-12-17 22:03:12 +02:00
parent 2f1cc97631
commit f2e43b84be

View File

@ -1284,6 +1284,7 @@ $(function() {
forms.find(".username").val(nick);
});
(function HotkeysScope() {
Mousetrap.bind([
"command+up",
"command+down",
@ -1309,7 +1310,7 @@ $(function() {
});
Mousetrap.bind([
"command+k",
"command+shift+l",
"ctrl+shift+l"
], function(e) {
if (e.target === input[0]) {
@ -1324,6 +1325,41 @@ $(function() {
contextMenuContainer.hide();
});
var colorsHotkeys = {
k: "\x03",
b: "\x02",
u: "\x1F",
i: "\x1D",
o: "\x0F",
};
for (var hotkey in colorsHotkeys) {
Mousetrap.bind([
"command+" + hotkey,
"ctrl+" + hotkey
], function(e) {
e.preventDefault();
const cursorPosStart = input.prop("selectionStart");
const cursorPosEnd = input.prop("selectionEnd");
const value = input.val();
let newValue = value.substring(0, cursorPosStart) + colorsHotkeys[e.key];
if (cursorPosStart === cursorPosEnd) {
// If no text is selected, insert at cursor
newValue += value.substring(cursorPosEnd, value.length);
} else {
// If text is selected, insert formatting character at start and the end
newValue += value.substring(cursorPosStart, cursorPosEnd) + colorsHotkeys[e.key] + value.substring(cursorPosEnd, value.length);
}
input
.val(newValue)
.get(0).setSelectionRange(cursorPosStart + 1, cursorPosEnd + 1);
});
}
}());
setInterval(function() {
chat.find(".chan:not(.active)").each(function() {
var chan = $(this);