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
|
|
|
function shouldFocusMessageField(code) {
|
|
|
|
// do not focus on F keys
|
2022-05-13 12:08:18 +02:00
|
|
|
if (/^F\d+$/.test(code)) return false;
|
2022-05-12 13:28:19 +02:00
|
|
|
|
|
|
|
// do not focus on numlock/scroll lock
|
2022-05-13 12:08:18 +02:00
|
|
|
if (
|
|
|
|
code.metaKey
|
|
|
|
|| code.startsWith('OS')
|
|
|
|
|| code.startsWith('Meta')
|
|
|
|
|| code.startsWith('Shift')
|
|
|
|
|| code.startsWith('Alt')
|
|
|
|
|| code.startsWith('Control')
|
|
|
|
|| code.startsWith('Arrow')
|
|
|
|
|| code === 'Tab'
|
|
|
|
|| code === 'Space'
|
|
|
|
|| code === 'Enter'
|
|
|
|
|| code === 'NumLock'
|
|
|
|
|| code === 'ScrollLock'
|
|
|
|
) {
|
2022-05-12 13:28:19 +02:00
|
|
|
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
|
2022-08-05 15:42:25 +02:00
|
|
|
if (event.key === 'k') {
|
2021-12-11 06:20:34 +01:00
|
|
|
event.preventDefault();
|
2022-05-14 04:54:21 +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
|
2022-08-05 15:42:25 +02:00
|
|
|
if (event.key === 'v') {
|
2022-05-14 04:54:21 +02:00
|
|
|
if (navigation.isRawModalVisible) return;
|
2022-05-12 13:28:19 +02:00
|
|
|
const msgTextarea = document.getElementById('message-textarea');
|
2022-05-14 16:35:43 +02:00
|
|
|
const { activeElement } = document;
|
|
|
|
if (activeElement !== msgTextarea
|
|
|
|
&& ['input', 'textarea'].includes(activeElement.tagName.toLowerCase())
|
|
|
|
) return;
|
2022-05-12 13:28:19 +02:00
|
|
|
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;
|
2022-01-30 05:35:46 +01:00
|
|
|
|
2022-08-05 15:42:25 +02:00
|
|
|
if (event.key === '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-07-18 18:36:51 +02:00
|
|
|
if (['input', 'textarea'].includes(document.activeElement.tagName.toLowerCase())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 };
|