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
|
|
|
|
|
|
|
function listenKeyboard(event) {
|
2022-03-11 09:44:57 +01:00
|
|
|
// Ctrl/Cmd +
|
|
|
|
if (event.ctrlKey || event.metaKey) {
|
2021-12-11 06:20:34 +01:00
|
|
|
// k - for search Modal
|
|
|
|
if (event.keyCode === 75) {
|
|
|
|
event.preventDefault();
|
2021-12-13 10:01:43 +01:00
|
|
|
if (navigation.isRawModalVisible) return;
|
2021-12-11 06:20:34 +01:00
|
|
|
openSearch();
|
|
|
|
}
|
|
|
|
}
|
2021-12-22 15:48:32 +01:00
|
|
|
|
2021-12-13 10:01:43 +01:00
|
|
|
if (!event.ctrlKey && !event.altKey) {
|
|
|
|
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-03-18 04:39:14 +01:00
|
|
|
// esc
|
|
|
|
if (event.keyCode === 27) {
|
|
|
|
if (navigation.isRoomSettings) {
|
|
|
|
toggleRoomSettings();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (navigation.selectedRoomId) {
|
|
|
|
markAsRead(navigation.selectedRoomId);
|
|
|
|
return;
|
|
|
|
}
|
2022-01-30 05:35:46 +01:00
|
|
|
}
|
|
|
|
|
2022-03-18 04:39:14 +01:00
|
|
|
// Don't allow these keys to type/focus message field
|
2021-12-22 15:48:32 +01:00
|
|
|
if ((event.keyCode !== 8 && event.keyCode < 48)
|
2021-12-13 10:01:43 +01:00
|
|
|
|| (event.keyCode >= 91 && event.keyCode <= 93)
|
|
|
|
|| (event.keyCode >= 112 && event.keyCode <= 183)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-30 05:35:46 +01:00
|
|
|
|
|
|
|
// press any key to focus and type in message field
|
2021-12-13 10:01:43 +01:00
|
|
|
const msgTextarea = document.getElementById('message-textarea');
|
|
|
|
msgTextarea?.focus();
|
|
|
|
}
|
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 };
|