Add the ability to focus on paste (#545)

* pasting should focus the message field

also refactored a small amount to use KeyEvent.code
instead of KeyEvent.keyCode, which is deprecated.

fixes ajbura/cinny#544

* fix lint

* comments
This commit is contained in:
Dean Bassett 2022-05-12 04:28:19 -07:00 committed by GitHub
parent 44553cc375
commit b3cb48319a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,25 +2,66 @@ import { openSearch, toggleRoomSettings } from '../action/navigation';
import navigation from '../state/navigation'; import navigation from '../state/navigation';
import { markAsRead } from '../action/notifications'; import { markAsRead } from '../action/notifications';
// 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;
}
function listenKeyboard(event) { function listenKeyboard(event) {
// Ctrl/Cmd + // Ctrl/Cmd +
if (event.ctrlKey || event.metaKey) { if (event.ctrlKey || event.metaKey) {
// k - for search Modal // open search modal
if (event.keyCode === 75) { if (event.code === 'KeyK') {
event.preventDefault(); event.preventDefault();
if (navigation.isRawModalVisible) return; if (navigation.isRawModalVisible) {
return;
}
openSearch(); openSearch();
} }
// focus message field on paste
if (event.code === 'KeyV') {
const msgTextarea = document.getElementById('message-textarea');
msgTextarea?.focus();
}
} }
if (!event.ctrlKey && !event.altKey) { if (!event.ctrlKey && !event.altKey && !event.metaKey) {
if (navigation.isRawModalVisible) return; if (navigation.isRawModalVisible) return;
if (['text', 'textarea'].includes(document.activeElement.type)) { if (['text', 'textarea'].includes(document.activeElement.type)) {
return; return;
} }
// esc if (event.code === 'Escape') {
if (event.keyCode === 27) {
if (navigation.isRoomSettings) { if (navigation.isRoomSettings) {
toggleRoomSettings(); toggleRoomSettings();
return; return;
@ -31,17 +72,13 @@ function listenKeyboard(event) {
} }
} }
// Don't allow these keys to type/focus message field // focus the text field on most keypresses
if ((event.keyCode !== 8 && event.keyCode < 48) if (shouldFocusMessageField(event.code)) {
|| (event.keyCode >= 91 && event.keyCode <= 93)
|| (event.keyCode >= 112 && event.keyCode <= 183)) {
return;
}
// press any key to focus and type in message field // press any key to focus and type in message field
const msgTextarea = document.getElementById('message-textarea'); const msgTextarea = document.getElementById('message-textarea');
msgTextarea?.focus(); msgTextarea?.focus();
} }
}
} }
function initHotkeys() { function initHotkeys() {