2022-01-30 05:35:46 +01:00
|
|
|
import { openSearch, toggleRoomSettings } from '../action/navigation';
|
2021-12-11 06:20:34 +01:00
|
|
|
import navigation from '../state/navigation';
|
2022-03-18 04:39:14 +01:00
|
|
|
import { markAsRead } from '../action/notifications';
|
2021-12-11 06:20:34 +01:00
|
|
|
|
2022-05-12 13:28:19 +02:00
|
|
|
// describes which keys should auto-focus the message field
|
|
|
|
function shouldFocusMessageField(code) {
|
|
|
|
// should focus on alphanumeric values, and backspace
|
|
|
|
if (code.startsWith('Key')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (code.startsWith('Digit')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (code === 'Backspace') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not focus if super key is pressed
|
|
|
|
if (code.startsWith('Meta')) { // chrome
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (code.startsWith('OS')) { // firefox
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not focus on F keys
|
|
|
|
if (/^F\d+$/.test(code)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not focus on numlock/scroll lock
|
|
|
|
if (code === 'NumLock' || code === 'ScrollLock') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-11 06:20:34 +01:00
|
|
|
function listenKeyboard(event) {
|
2022-03-11 09:44:57 +01:00
|
|
|
// Ctrl/Cmd +
|
|
|
|
if (event.ctrlKey || event.metaKey) {
|
2022-05-12 13:28:19 +02:00
|
|
|
// open search modal
|
|
|
|
if (event.code === 'KeyK') {
|
2021-12-11 06:20:34 +01:00
|
|
|
event.preventDefault();
|
2022-05-12 13:28:19 +02:00
|
|
|
if (navigation.isRawModalVisible) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-11 06:20:34 +01:00
|
|
|
openSearch();
|
|
|
|
}
|
2022-05-12 13:28:19 +02:00
|
|
|
|
|
|
|
// focus message field on paste
|
|
|
|
if (event.code === 'KeyV') {
|
|
|
|
const msgTextarea = document.getElementById('message-textarea');
|
|
|
|
msgTextarea?.focus();
|
|
|
|
}
|
2021-12-11 06:20:34 +01:00
|
|
|
}
|
2021-12-22 15:48:32 +01:00
|
|
|
|
2022-05-12 13:28:19 +02:00
|
|
|
if (!event.ctrlKey && !event.altKey && !event.metaKey) {
|
2021-12-13 10:01:43 +01:00
|
|
|
if (navigation.isRawModalVisible) return;
|
2021-12-13 16:35:37 +01:00
|
|
|
if (['text', 'textarea'].includes(document.activeElement.type)) {
|
2021-12-13 10:01:43 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-01-30 05:35:46 +01:00
|
|
|
|
2022-05-12 13:28:19 +02:00
|
|
|
if (event.code === 'Escape') {
|
2022-03-18 04:39:14 +01:00
|
|
|
if (navigation.isRoomSettings) {
|
|
|
|
toggleRoomSettings();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (navigation.selectedRoomId) {
|
|
|
|
markAsRead(navigation.selectedRoomId);
|
|
|
|
return;
|
|
|
|
}
|
2022-01-30 05:35:46 +01:00
|
|
|
}
|
|
|
|
|
2022-05-12 13:28:19 +02:00
|
|
|
// focus the text field on most keypresses
|
|
|
|
if (shouldFocusMessageField(event.code)) {
|
|
|
|
// press any key to focus and type in message field
|
|
|
|
const msgTextarea = document.getElementById('message-textarea');
|
|
|
|
msgTextarea?.focus();
|
2021-12-13 10:01:43 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-11 06:20:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function initHotkeys() {
|
|
|
|
document.body.addEventListener('keydown', listenKeyboard);
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeHotkeys() {
|
|
|
|
document.body.removeEventListener('keydown', listenKeyboard);
|
|
|
|
}
|
|
|
|
|
|
|
|
export { initHotkeys, removeHotkeys };
|