Text Formatter: Keymap independent shortcuts; Global Search, Local Search: Support hotkeys (#1621)
This commit is contained in:
parent
47694e0711
commit
d1bd4733d8
@ -5,8 +5,9 @@ import { getDispatch, withGlobal } from '../../lib/teact/teactn';
|
|||||||
|
|
||||||
import { LeftColumnContent, SettingsScreens } from '../../types';
|
import { LeftColumnContent, SettingsScreens } from '../../types';
|
||||||
|
|
||||||
import { LAYERS_ANIMATION_NAME } from '../../util/environment';
|
import { IS_MAC_OS, LAYERS_ANIMATION_NAME } from '../../util/environment';
|
||||||
import captureEscKeyListener from '../../util/captureEscKeyListener';
|
import captureEscKeyListener from '../../util/captureEscKeyListener';
|
||||||
|
import getKeyFromEvent from '../../util/getKeyFromEvent';
|
||||||
import useFoldersReducer from '../../hooks/reducers/useFoldersReducer';
|
import useFoldersReducer from '../../hooks/reducers/useFoldersReducer';
|
||||||
import { useResize } from '../../hooks/useResize';
|
import { useResize } from '../../hooks/useResize';
|
||||||
|
|
||||||
@ -253,6 +254,25 @@ const LeftColumn: FC<StateProps> = ({
|
|||||||
[activeChatFolder, content, handleReset],
|
[activeChatFolder, content, handleReset],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (content === LeftColumnContent.GlobalSearch) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleKeyDown(e: KeyboardEvent) {
|
||||||
|
if (((IS_MAC_OS && e.metaKey) || (!IS_MAC_OS && e.ctrlKey)) && e.shiftKey && getKeyFromEvent(e) === 'f') {
|
||||||
|
e.preventDefault();
|
||||||
|
setContent(LeftColumnContent.GlobalSearch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('keydown', handleKeyDown, false);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
document.removeEventListener('keydown', handleKeyDown, false);
|
||||||
|
};
|
||||||
|
}, [content]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
clearTwoFaError();
|
clearTwoFaError();
|
||||||
|
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import React, {
|
|||||||
useRef,
|
useRef,
|
||||||
useCallback,
|
useCallback,
|
||||||
useState,
|
useState,
|
||||||
|
useEffect,
|
||||||
} from '../../lib/teact/teact';
|
} from '../../lib/teact/teact';
|
||||||
import { getDispatch, withGlobal } from '../../lib/teact/teactn';
|
import { getDispatch, withGlobal } from '../../lib/teact/teactn';
|
||||||
|
|
||||||
@ -11,7 +12,10 @@ import { MessageListType } from '../../global/types';
|
|||||||
import { MAIN_THREAD_ID } from '../../api/types';
|
import { MAIN_THREAD_ID } from '../../api/types';
|
||||||
import { IAnchorPosition } from '../../types';
|
import { IAnchorPosition } from '../../types';
|
||||||
|
|
||||||
import { ARE_CALLS_SUPPORTED, IS_SINGLE_COLUMN_LAYOUT } from '../../util/environment';
|
import {
|
||||||
|
ARE_CALLS_SUPPORTED, IS_MAC_OS, IS_PWA, IS_SINGLE_COLUMN_LAYOUT,
|
||||||
|
} from '../../util/environment';
|
||||||
|
import getKeyFromEvent from '../../util/getKeyFromEvent';
|
||||||
import {
|
import {
|
||||||
isChatBasicGroup, isChatChannel, isChatSuperGroup, isUserId,
|
isChatBasicGroup, isChatChannel, isChatSuperGroup, isUserId,
|
||||||
} from '../../modules/helpers';
|
} from '../../modules/helpers';
|
||||||
@ -127,6 +131,27 @@ const HeaderActions: FC<OwnProps & StateProps> = ({
|
|||||||
}
|
}
|
||||||
}, [openLocalTextSearch]);
|
}, [openLocalTextSearch]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!canSearch) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleKeyDown(e: KeyboardEvent) {
|
||||||
|
if (
|
||||||
|
IS_PWA && ((IS_MAC_OS && e.metaKey) || (!IS_MAC_OS && e.ctrlKey)) && !e.shiftKey && getKeyFromEvent(e) === 'f'
|
||||||
|
) {
|
||||||
|
e.preventDefault();
|
||||||
|
handleSearchClick();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('keydown', handleKeyDown, false);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
document.removeEventListener('keydown', handleKeyDown, false);
|
||||||
|
};
|
||||||
|
}, [canSearch, handleSearchClick]);
|
||||||
|
|
||||||
const lang = useLang();
|
const lang = useLang();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import { EDITABLE_INPUT_ID } from '../../../config';
|
|||||||
import buildClassName from '../../../util/buildClassName';
|
import buildClassName from '../../../util/buildClassName';
|
||||||
import { ensureProtocol } from '../../../util/ensureProtocol';
|
import { ensureProtocol } from '../../../util/ensureProtocol';
|
||||||
import captureEscKeyListener from '../../../util/captureEscKeyListener';
|
import captureEscKeyListener from '../../../util/captureEscKeyListener';
|
||||||
|
import getKeyFromEvent from '../../../util/getKeyFromEvent';
|
||||||
import useShowTransition from '../../../hooks/useShowTransition';
|
import useShowTransition from '../../../hooks/useShowTransition';
|
||||||
import useVirtualBackdrop from '../../../hooks/useVirtualBackdrop';
|
import useVirtualBackdrop from '../../../hooks/useVirtualBackdrop';
|
||||||
import useFlag from '../../../hooks/useFlag';
|
import useFlag from '../../../hooks/useFlag';
|
||||||
@ -306,16 +307,16 @@ const TextFormatter: FC<OwnProps> = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const handleKeyDown = useCallback((e: KeyboardEvent) => {
|
const handleKeyDown = useCallback((e: KeyboardEvent) => {
|
||||||
const HANDLERS_BY_KEY_CODE: Record<string, AnyToVoidFunction> = {
|
const HANDLERS_BY_KEY: Record<string, AnyToVoidFunction> = {
|
||||||
KeyK: openLinkControl,
|
k: openLinkControl,
|
||||||
KeyB: handleBoldText,
|
b: handleBoldText,
|
||||||
KeyU: handleUnderlineText,
|
u: handleUnderlineText,
|
||||||
KeyI: handleItalicText,
|
i: handleItalicText,
|
||||||
KeyM: handleMonospaceText,
|
m: handleMonospaceText,
|
||||||
KeyS: handleStrikethroughText,
|
s: handleStrikethroughText,
|
||||||
};
|
};
|
||||||
|
|
||||||
const handler = HANDLERS_BY_KEY_CODE[e.code];
|
const handler = HANDLERS_BY_KEY[getKeyFromEvent(e)];
|
||||||
|
|
||||||
if (
|
if (
|
||||||
e.altKey
|
e.altKey
|
||||||
|
|||||||
5
src/util/getKeyFromEvent.ts
Normal file
5
src/util/getKeyFromEvent.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export default function getKeyFromEvent(e: KeyboardEvent) {
|
||||||
|
const key = 'key' in e ? e.key : e.code;
|
||||||
|
|
||||||
|
return key.startsWith('Key') ? key.slice(3).toLowerCase() : key;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user