From 5fac65a4abd10d6dae8a8077f13f1b1d58cb10ce Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 16 Jul 2021 21:00:17 +0300 Subject: [PATCH] Fix various issues with hotkeys (#1279) --- src/components/left/main/ChatFolders.tsx | 2 +- src/components/left/main/ChatList.tsx | 4 ++-- src/components/main/ForwardPicker.tsx | 4 ++-- src/components/middle/composer/MessageInput.tsx | 2 +- src/components/right/RightSearch.tsx | 5 +++-- src/components/ui/SearchInput.tsx | 2 +- src/util/environment.ts | 3 +++ 7 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/components/left/main/ChatFolders.tsx b/src/components/left/main/ChatFolders.tsx index 469d23205..5b11c6d08 100644 --- a/src/components/left/main/ChatFolders.tsx +++ b/src/components/left/main/ChatFolders.tsx @@ -149,7 +149,7 @@ const ChatFolders: FC = ({ useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { - if (e.metaKey && e.code.startsWith('Digit') && folderTabs) { + if (e.ctrlKey && e.shiftKey && e.code.startsWith('Digit') && folderTabs) { const [, digit] = e.code.match(/Digit(\d)/) || []; if (!digit) return; diff --git a/src/components/left/main/ChatList.tsx b/src/components/left/main/ChatList.tsx index 9c4149902..a9c1e7a12 100644 --- a/src/components/left/main/ChatList.tsx +++ b/src/components/left/main/ChatList.tsx @@ -10,7 +10,7 @@ import { import { NotifyException, NotifySettings } from '../../../types'; import { ALL_CHATS_PRELOAD_DISABLED, CHAT_HEIGHT_PX, CHAT_LIST_SLICE } from '../../../config'; -import { IS_ANDROID } from '../../../util/environment'; +import { IS_ANDROID, IS_MAC_OS, IS_PWA } from '../../../util/environment'; import usePrevious from '../../../hooks/usePrevious'; import { mapValues, pick } from '../../../util/iteratees'; import { getChatOrder, prepareChatList, prepareFolderListIds } from '../../../modules/helpers'; @@ -166,7 +166,7 @@ const ChatList: FC = ({ useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (isActive && orderedIds) { - if (e.ctrlKey && e.code.startsWith('Digit')) { + if (IS_PWA && ((IS_MAC_OS && e.metaKey) || (!IS_MAC_OS && e.ctrlKey)) && e.code.startsWith('Digit')) { const [, digit] = e.code.match(/Digit(\d)/) || []; if (!digit) return; diff --git a/src/components/main/ForwardPicker.tsx b/src/components/main/ForwardPicker.tsx index 20b545d84..b401f51d2 100644 --- a/src/components/main/ForwardPicker.tsx +++ b/src/components/main/ForwardPicker.tsx @@ -125,8 +125,8 @@ const ForwardPicker: FC = ({ // eslint-disable-next-line no-null/no-null const containerRef = useRef(null); const handleKeyDown = useKeyboardListNavigation(containerRef, isOpen, (index) => { - if (viewportIds) { - setForwardChatId({ id: viewportIds[index] }); + if (viewportIds && viewportIds.length > 0) { + setForwardChatId({ id: viewportIds[index === -1 ? 0 : index] }); } }, '.ListItem-button', true); diff --git a/src/components/middle/composer/MessageInput.tsx b/src/components/middle/composer/MessageInput.tsx index 16a2e433c..7c7637263 100644 --- a/src/components/middle/composer/MessageInput.tsx +++ b/src/components/middle/composer/MessageInput.tsx @@ -233,7 +233,7 @@ const MessageInput: FC = ({ e.target.removeEventListener('keyup', handleKeyUp); } - if (e.metaKey) { + if (e.metaKey && !html.length) { const targetIndexDelta = e.key === 'ArrowDown' ? 1 : e.key === 'ArrowUp' ? -1 : undefined; if (targetIndexDelta) { e.preventDefault(); diff --git a/src/components/right/RightSearch.tsx b/src/components/right/RightSearch.tsx index 7d176216f..97f559eb0 100644 --- a/src/components/right/RightSearch.tsx +++ b/src/components/right/RightSearch.tsx @@ -135,8 +135,9 @@ const RightSearch: FC = ({ // eslint-disable-next-line no-null/no-null const containerRef = useRef(null); const handleKeyDown = useKeyboardListNavigation(containerRef, true, (index) => { - if (foundResults && foundResults[index]) { - foundResults[index].onClick(); + const foundResult = foundResults && foundResults[index === -1 ? 0 : index]; + if (foundResult) { + foundResult.onClick(); } }, '.ListItem-button', true); diff --git a/src/components/ui/SearchInput.tsx b/src/components/ui/SearchInput.tsx index f0bed89c1..1187285f9 100644 --- a/src/components/ui/SearchInput.tsx +++ b/src/components/ui/SearchInput.tsx @@ -91,7 +91,7 @@ const SearchInput: FC = ({ } const handleKeyDown = useCallback((e: React.KeyboardEvent) => { - if (e.key === 'ArrowDown') { + if (e.key === 'ArrowDown' || e.key === 'Enter') { const element = document.querySelector(`.${parentContainerClassName} .ListItem-button`) as HTMLElement; if (element) { element.focus(); diff --git a/src/util/environment.ts b/src/util/environment.ts index 84b4ed5a4..4c36f15e5 100644 --- a/src/util/environment.ts +++ b/src/util/environment.ts @@ -37,6 +37,9 @@ export const IS_MAC_OS = PLATFORM_ENV === 'macOS'; export const IS_IOS = PLATFORM_ENV === 'iOS'; export const IS_ANDROID = PLATFORM_ENV === 'Android'; export const IS_SAFARI = /^((?!chrome|android).)*safari/i.test(navigator.userAgent); +export const IS_PWA = window.matchMedia('(display-mode: standalone)').matches +|| (window.navigator as any).standalone +|| document.referrer.includes('android-app://'); export const IS_TOUCH_ENV = window.matchMedia('(pointer: coarse)').matches; // Keep in mind the landscape orientation