2019-11-16 17:24:03 +00:00
|
|
|
import Mousetrap from "mousetrap";
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
import {store} from "./store";
|
2021-10-11 03:48:28 +00:00
|
|
|
import {switchToChannel, router, navigate} from "./router";
|
2019-11-20 20:54:00 +00:00
|
|
|
import isChannelCollapsed from "./helpers/isChannelCollapsed";
|
2020-03-17 10:00:33 +00:00
|
|
|
import isIgnoredKeybind from "./helpers/isIgnoredKeybind";
|
2021-10-07 05:59:19 +00:00
|
|
|
import listenForTwoFingerSwipes from "./helpers/listenForTwoFingerSwipes";
|
2022-06-19 00:25:21 +00:00
|
|
|
import {ClientChan} from "./types";
|
2017-09-06 06:41:11 +00:00
|
|
|
|
2019-11-07 10:31:51 +00:00
|
|
|
// Switch to the next/previous window in the channel list.
|
2020-03-21 20:55:36 +00:00
|
|
|
Mousetrap.bind(["alt+up", "alt+down"], function (e, keys) {
|
2020-03-17 10:00:33 +00:00
|
|
|
if (isIgnoredKeybind(e)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-07 05:59:19 +00:00
|
|
|
navigateWindow(keys.split("+").pop() === "up" ? -1 : 1);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
listenForTwoFingerSwipes(function (cardinalDirection: string) {
|
2021-10-07 05:59:19 +00:00
|
|
|
if (cardinalDirection === "e" || cardinalDirection === "w") {
|
|
|
|
navigateWindow(cardinalDirection === "e" ? -1 : 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function navigateWindow(direction: number) {
|
2019-11-20 20:54:00 +00:00
|
|
|
if (store.state.networks.length === 0) {
|
2021-10-07 05:59:19 +00:00
|
|
|
return;
|
2017-09-06 06:41:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const flatChannels: ClientChan[] = [];
|
2019-11-20 20:54:00 +00:00
|
|
|
let index = -1;
|
|
|
|
|
|
|
|
for (const network of store.state.networks) {
|
|
|
|
for (const channel of network.channels) {
|
|
|
|
if (isChannelCollapsed(network, channel)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
if (index === -1 && store.state.activeChannel?.channel === channel) {
|
2019-11-20 20:54:00 +00:00
|
|
|
index = flatChannels.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
flatChannels.push(channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Circular array, and a modulo bug workaround because in JS it stays negative
|
|
|
|
const length = flatChannels.length;
|
|
|
|
index = (((index + direction) % length) + length) % length;
|
|
|
|
|
|
|
|
jumpToChannel(flatChannels[index]);
|
2021-10-07 05:59:19 +00:00
|
|
|
}
|
2017-09-06 06:41:11 +00:00
|
|
|
|
2019-11-07 10:31:51 +00:00
|
|
|
// Switch to the next/previous lobby in the channel list
|
2020-03-21 20:55:36 +00:00
|
|
|
Mousetrap.bind(["alt+shift+up", "alt+shift+down"], function (e, keys) {
|
2020-03-17 10:00:33 +00:00
|
|
|
if (isIgnoredKeybind(e)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-20 20:54:00 +00:00
|
|
|
const length = store.state.networks.length;
|
2018-03-15 14:10:20 +00:00
|
|
|
|
2019-11-20 20:54:00 +00:00
|
|
|
if (length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-15 14:10:20 +00:00
|
|
|
|
2019-11-20 20:54:00 +00:00
|
|
|
const direction = keys.split("+").pop() === "up" ? -1 : 1;
|
|
|
|
let index = 0;
|
2018-03-15 14:10:20 +00:00
|
|
|
|
2019-11-20 20:54:00 +00:00
|
|
|
// If we're in another window, jump to first lobby
|
|
|
|
if (store.state.activeChannel) {
|
2022-06-19 00:25:21 +00:00
|
|
|
index = store.state.networks.findIndex((n) => n === store.state.activeChannel?.network);
|
2018-03-15 14:10:20 +00:00
|
|
|
|
2019-11-20 20:54:00 +00:00
|
|
|
// If we're in a channel, and it's not the lobby, jump to lobby of this network when going up
|
2022-06-19 00:25:21 +00:00
|
|
|
if (direction !== -1 || store.state.activeChannel?.channel.type === "lobby") {
|
2019-11-20 20:54:00 +00:00
|
|
|
index = (((index + direction) % length) + length) % length;
|
|
|
|
}
|
2018-03-15 14:10:20 +00:00
|
|
|
}
|
|
|
|
|
2019-11-20 20:54:00 +00:00
|
|
|
jumpToChannel(store.state.networks[index].channels[0]);
|
2018-03-20 19:00:03 +00:00
|
|
|
|
|
|
|
return false;
|
2018-03-15 14:10:20 +00:00
|
|
|
});
|
|
|
|
|
2019-08-08 21:14:28 +00:00
|
|
|
// Jump to the first window with a highlight in it, or the first with unread
|
|
|
|
// activity if there are none with highlights.
|
2020-03-21 20:55:36 +00:00
|
|
|
Mousetrap.bind(["alt+a"], function (e) {
|
2020-03-17 10:00:33 +00:00
|
|
|
if (isIgnoredKeybind(e)) {
|
2020-01-23 20:50:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-20 20:54:00 +00:00
|
|
|
let targetChannel;
|
2019-08-08 21:14:28 +00:00
|
|
|
|
2019-11-02 19:40:59 +00:00
|
|
|
outer_loop: for (const network of store.state.networks) {
|
2019-08-08 21:14:28 +00:00
|
|
|
for (const chan of network.channels) {
|
|
|
|
if (chan.highlight) {
|
2019-11-20 20:54:00 +00:00
|
|
|
targetChannel = chan;
|
2019-08-08 21:14:28 +00:00
|
|
|
break outer_loop;
|
|
|
|
}
|
|
|
|
|
2019-11-20 20:54:00 +00:00
|
|
|
if (chan.unread && !targetChannel) {
|
|
|
|
targetChannel = chan;
|
2019-08-08 21:14:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 20:54:00 +00:00
|
|
|
if (targetChannel) {
|
|
|
|
jumpToChannel(targetChannel);
|
2019-08-08 21:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2021-10-11 03:48:28 +00:00
|
|
|
// Show the help menu.
|
2022-11-16 05:50:56 +00:00
|
|
|
Mousetrap.bind(["alt+/"], function (e) {
|
2021-10-11 03:48:28 +00:00
|
|
|
if (isIgnoredKeybind(e)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-16 05:50:56 +00:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
navigate("Help").catch((err) => console.log(err));
|
2021-10-11 03:48:28 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function jumpToChannel(targetChannel: ClientChan) {
|
2019-11-20 20:54:00 +00:00
|
|
|
switchToChannel(targetChannel);
|
|
|
|
|
2019-12-19 13:02:21 +00:00
|
|
|
const element = document.querySelector(
|
|
|
|
`#sidebar .channel-list-item[aria-controls="#chan-${targetChannel.id}"]`
|
2019-11-20 22:16:58 +00:00
|
|
|
);
|
2019-12-19 13:02:21 +00:00
|
|
|
|
|
|
|
if (element) {
|
|
|
|
scrollIntoViewNicely(element);
|
|
|
|
}
|
2019-11-20 20:54:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 08:32:15 +00:00
|
|
|
// Ignored keys which should not automatically focus the input bar
|
|
|
|
const ignoredKeys = {
|
|
|
|
8: true, // Backspace
|
|
|
|
9: true, // Tab
|
|
|
|
12: true, // Clear
|
|
|
|
16: true, // Shift
|
|
|
|
17: true, // Control
|
|
|
|
18: true, // Alt
|
|
|
|
19: true, // Pause
|
|
|
|
20: true, // CapsLock
|
|
|
|
27: true, // Escape
|
|
|
|
35: true, // End
|
|
|
|
36: true, // Home
|
|
|
|
37: true, // ArrowLeft
|
|
|
|
38: true, // ArrowUp
|
|
|
|
39: true, // ArrowRight
|
|
|
|
40: true, // ArrowDown
|
|
|
|
45: true, // Insert
|
|
|
|
46: true, // Delete
|
|
|
|
112: true, // F1
|
|
|
|
113: true, // F2
|
|
|
|
114: true, // F3
|
|
|
|
115: true, // F4
|
|
|
|
116: true, // F5
|
|
|
|
117: true, // F6
|
|
|
|
118: true, // F7
|
|
|
|
119: true, // F8
|
|
|
|
120: true, // F9
|
|
|
|
121: true, // F10
|
|
|
|
122: true, // F11
|
|
|
|
123: true, // F12
|
|
|
|
144: true, // NumLock
|
|
|
|
145: true, // ScrollLock
|
|
|
|
224: true, // Meta
|
|
|
|
};
|
|
|
|
|
2019-11-07 10:31:51 +00:00
|
|
|
document.addEventListener("keydown", (e) => {
|
2021-10-11 03:48:28 +00:00
|
|
|
// Allow navigating back to the previous page when on the help screen.
|
2022-06-19 00:25:21 +00:00
|
|
|
if (e.key === "Escape" && router.currentRoute.value.name === "Help") {
|
2021-10-11 03:48:28 +00:00
|
|
|
router.go(-1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-03 12:51:40 +00:00
|
|
|
// Ignore any key that uses alt modifier
|
|
|
|
// Ignore keys defined above
|
2018-06-10 10:46:00 +00:00
|
|
|
if (e.altKey || ignoredKeys[e.which]) {
|
2018-05-03 12:51:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-13 08:32:15 +00:00
|
|
|
|
2018-05-03 12:51:40 +00:00
|
|
|
// Ignore all ctrl keys except for ctrl+v to allow pasting
|
|
|
|
if ((e.ctrlKey || e.metaKey) && e.which !== 86) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-13 08:32:15 +00:00
|
|
|
|
2019-02-13 08:19:29 +00:00
|
|
|
// Redirect pagedown/pageup keys to messages container so it scrolls
|
|
|
|
if (e.which === 33 || e.which === 34) {
|
2019-12-17 09:21:22 +00:00
|
|
|
const chat = document.querySelector(".window .chat-content .chat");
|
2019-11-07 10:31:51 +00:00
|
|
|
|
|
|
|
if (chat) {
|
2022-06-19 00:25:21 +00:00
|
|
|
(chat as HTMLDivElement).focus();
|
2019-11-07 10:31:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 08:19:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const tagName = (e.target as HTMLElement).tagName;
|
2018-06-10 10:46:00 +00:00
|
|
|
|
|
|
|
// Ignore if we're already typing into <input> or <textarea>
|
|
|
|
if (tagName === "INPUT" || tagName === "TEXTAREA") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-07 10:31:51 +00:00
|
|
|
const input = document.getElementById("input");
|
|
|
|
|
|
|
|
if (!input) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
input.focus();
|
2018-07-09 18:51:27 +00:00
|
|
|
|
2018-05-03 12:51:40 +00:00
|
|
|
// On enter, focus the input but do not propagate the event
|
|
|
|
// This way, a new line is not inserted
|
|
|
|
if (e.which === 13) {
|
2019-11-07 10:31:51 +00:00
|
|
|
e.preventDefault();
|
2018-05-03 12:51:40 +00:00
|
|
|
}
|
|
|
|
});
|
2019-11-03 11:23:03 +00:00
|
|
|
|
|
|
|
function scrollIntoViewNicely(el) {
|
|
|
|
// Ideally this would use behavior: "smooth", but that does not consistently work in e.g. Chrome
|
|
|
|
// https://github.com/iamdustan/smoothscroll/issues/28#issuecomment-364061459
|
|
|
|
el.scrollIntoView({block: "center", inline: "nearest"});
|
|
|
|
}
|