From 407dff3959982e3b511d6619ff6aefef756b4f48 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Wed, 6 Sep 2017 09:41:11 +0300 Subject: [PATCH] Move keybinds to another file --- client/js/keybinds.js | 112 ++++++++++++++++++++++++++++++++++++++++++ client/js/lounge.js | 107 +--------------------------------------- 2 files changed, 113 insertions(+), 106 deletions(-) create mode 100644 client/js/keybinds.js diff --git a/client/js/keybinds.js b/client/js/keybinds.js new file mode 100644 index 0000000000..961961c42f --- /dev/null +++ b/client/js/keybinds.js @@ -0,0 +1,112 @@ +"use strict"; + +const $ = require("jquery"); +const Mousetrap = require("mousetrap"); +const utils = require("./utils"); +const input = $("#input"); +const sidebar = $("#sidebar"); +const windows = $("#windows"); +const contextMenuContainer = $("#context-menu-container"); + +Mousetrap.bind([ + "pageup", + "pagedown" +], function(e, key) { + let container = windows.find(".window.active"); + + // Chat windows scroll message container + if (container.attr("id") === "chat-container") { + container = container.find(".chan.active .chat"); + } + + container.finish(); + + const offset = container.get(0).clientHeight * 0.9; + let scrollTop = container.scrollTop(); + + if (key === "pageup") { + scrollTop = Math.floor(scrollTop - offset); + } else { + scrollTop = Math.ceil(scrollTop + offset); + } + + container.animate({ + scrollTop: scrollTop + }, 200); + + return false; +}); + +Mousetrap.bind([ + "command+up", + "command+down", + "ctrl+up", + "ctrl+down" +], function(e, keys) { + const channels = sidebar.find(".chan"); + const index = channels.index(channels.filter(".active")); + const direction = keys.split("+").pop(); + let target; + + switch (direction) { + case "up": + target = (channels.length + (index - 1 + channels.length)) % channels.length; + break; + + case "down": + target = (channels.length + (index + 1 + channels.length)) % channels.length; + break; + } + + channels.eq(target).click(); +}); + +Mousetrap.bind([ + "command+shift+l", + "ctrl+shift+l" +], function(e) { + if (e.target === input[0]) { + utils.clear(); + e.preventDefault(); + } +}); + +Mousetrap.bind([ + "escape" +], function() { + contextMenuContainer.hide(); +}); + +const colorsHotkeys = { + k: "\x03", + b: "\x02", + u: "\x1F", + i: "\x1D", + o: "\x0F", +}; + +for (const 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); + }); +} diff --git a/client/js/lounge.js b/client/js/lounge.js index 4ee89321c8..bd0b79f47e 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -4,7 +4,6 @@ require("jquery-ui/ui/widgets/sortable"); const $ = require("jquery"); const moment = require("moment"); -const Mousetrap = require("mousetrap"); const URI = require("urijs"); const fuzzy = require("fuzzy"); @@ -20,6 +19,7 @@ require("./options"); const utils = require("./utils"); require("./autocompletion"); require("./webpush"); +require("./keybinds"); require("./clipboard"); $(function() { @@ -564,111 +564,6 @@ $(function() { $(this).data("lastvalue", nick); }); - (function HotkeysScope() { - Mousetrap.bind([ - "pageup", - "pagedown" - ], function(e, key) { - let container = windows.find(".window.active"); - - // Chat windows scroll message container - if (container.attr("id") === "chat-container") { - container = container.find(".chan.active .chat"); - } - - container.finish(); - - const offset = container.get(0).clientHeight * 0.9; - let scrollTop = container.scrollTop(); - - if (key === "pageup") { - scrollTop = Math.floor(scrollTop - offset); - } else { - scrollTop = Math.ceil(scrollTop + offset); - } - - container.animate({ - scrollTop: scrollTop - }, 200); - - return false; - }); - - Mousetrap.bind([ - "command+up", - "command+down", - "ctrl+up", - "ctrl+down" - ], function(e, keys) { - var channels = sidebar.find(".chan"); - var index = channels.index(channels.filter(".active")); - var direction = keys.split("+").pop(); - switch (direction) { - case "up": - // Loop - var upTarget = (channels.length + (index - 1 + channels.length)) % channels.length; - channels.eq(upTarget).click(); - break; - - case "down": - // Loop - var downTarget = (channels.length + (index + 1 + channels.length)) % channels.length; - channels.eq(downTarget).click(); - break; - } - }); - - Mousetrap.bind([ - "command+shift+l", - "ctrl+shift+l" - ], function(e) { - if (e.target === input[0]) { - utils.clear(); - e.preventDefault(); - } - }); - - Mousetrap.bind([ - "escape" - ], 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); - }); - } - }()); - $(document).on("visibilitychange focus click", () => { if (sidebar.find(".highlight").length === 0) { utils.toggleNotificationMarkers(false);