signal-desktop/ts/components/ShortcutGuide.tsx

448 lines
13 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2019 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2019-11-07 21:36:16 +00:00
import * as React from 'react';
import classNames from 'classnames';
2021-09-17 22:24:21 +00:00
import { useRestoreFocus } from '../hooks/useRestoreFocus';
import type { LocalizerType } from '../types/Util';
2019-11-07 21:36:16 +00:00
export type Props = {
hasInstalledStickers: boolean;
platform: string;
readonly close: () => unknown;
readonly i18n: LocalizerType;
};
type KeyType =
| 'commandOrCtrl'
2022-05-10 18:14:08 +00:00
| 'ctrlOrAlt'
2019-11-07 21:36:16 +00:00
| 'optionOrAlt'
| 'shift'
| 'enter'
| 'tab'
| 'ctrl'
2019-11-07 21:36:16 +00:00
| '↑'
| '↓'
| ','
| '.'
| 'A'
| 'C'
| 'D'
2020-01-23 23:57:37 +00:00
| 'E'
2019-11-07 21:36:16 +00:00
| 'F'
2019-11-19 23:03:00 +00:00
| 'J'
| 'L'
2019-11-07 21:36:16 +00:00
| 'M'
| 'N'
2019-11-07 21:36:16 +00:00
| 'P'
| 'R'
| 'S'
| 'T'
| 'U'
| 'V'
| 'X'
2022-05-10 18:14:08 +00:00
| 'Y'
| '1 to 9';
2019-11-07 21:36:16 +00:00
type ShortcutType = {
id: string;
2019-11-07 21:36:16 +00:00
description: string;
keys: Array<Array<KeyType>>;
2019-11-07 21:36:16 +00:00
};
function getNavigationShortcuts(i18n: LocalizerType): Array<ShortcutType> {
return [
{
id: 'Keyboard--navigate-by-section',
description: i18n('Keyboard--navigate-by-section'),
keys: [['commandOrCtrl', 'T']],
},
{
id: 'Keyboard--previous-conversation',
description: i18n('Keyboard--previous-conversation'),
keys: [
['optionOrAlt', '↑'],
['ctrl', 'shift', 'tab'],
],
},
{
id: 'Keyboard--next-conversation',
description: i18n('Keyboard--next-conversation'),
keys: [
['optionOrAlt', '↓'],
['ctrl', 'tab'],
],
},
{
id: 'Keyboard--previous-unread-conversation',
description: i18n('Keyboard--previous-unread-conversation'),
keys: [['optionOrAlt', 'shift', '↑']],
},
{
id: 'Keyboard--next-unread-conversation',
description: i18n('Keyboard--next-unread-conversation'),
keys: [['optionOrAlt', 'shift', '↓']],
},
{
id: 'Keyboard--conversation-by-index',
description: i18n('Keyboard--conversation-by-index'),
keys: [['commandOrCtrl', '1 to 9']],
},
{
id: 'Keyboard--preferences',
description: i18n('Keyboard--preferences'),
keys: [['commandOrCtrl', ',']],
},
{
id: 'Keyboard--open-conversation-menu',
description: i18n('Keyboard--open-conversation-menu'),
keys: [['commandOrCtrl', 'shift', 'L']],
},
{
id: 'Keyboard--new-conversation',
description: i18n('Keyboard--new-conversation'),
keys: [['commandOrCtrl', 'N']],
},
{
id: 'Keyboard--search',
description: i18n('Keyboard--search'),
keys: [['commandOrCtrl', 'F']],
},
{
id: 'Keyboard--search-in-conversation',
description: i18n('Keyboard--search-in-conversation'),
keys: [['commandOrCtrl', 'shift', 'F']],
},
{
id: 'Keyboard--focus-composer',
description: i18n('Keyboard--focus-composer'),
keys: [['commandOrCtrl', 'shift', 'T']],
},
{
id: 'Keyboard--open-all-media-view',
description: i18n('Keyboard--open-all-media-view'),
keys: [['commandOrCtrl', 'shift', 'M']],
},
{
id: 'Keyboard--open-emoji-chooser',
description: i18n('Keyboard--open-emoji-chooser'),
keys: [['commandOrCtrl', 'shift', 'J']],
},
{
id: 'Keyboard--open-sticker-chooser',
description: i18n('Keyboard--open-sticker-chooser'),
keys: [['commandOrCtrl', 'shift', 'S']],
},
{
id: 'Keyboard--begin-recording-voice-note',
description: i18n('Keyboard--begin-recording-voice-note'),
keys: [['commandOrCtrl', 'shift', 'V']],
},
{
id: 'Keyboard--archive-conversation',
description: i18n('Keyboard--archive-conversation'),
keys: [['commandOrCtrl', 'shift', 'A']],
},
{
id: 'Keyboard--unarchive-conversation',
description: i18n('Keyboard--unarchive-conversation'),
keys: [['commandOrCtrl', 'shift', 'U']],
},
{
id: 'Keyboard--scroll-to-top',
description: i18n('Keyboard--scroll-to-top'),
keys: [['commandOrCtrl', '↑']],
},
{
id: 'Keyboard--scroll-to-bottom',
description: i18n('Keyboard--scroll-to-bottom'),
keys: [['commandOrCtrl', '↓']],
},
{
id: 'Keyboard--close-curent-conversation',
description: i18n('Keyboard--close-curent-conversation'),
keys: [['commandOrCtrl', 'shift', 'C']],
},
];
}
2019-11-07 21:36:16 +00:00
function getMessageShortcuts(i18n: LocalizerType): Array<ShortcutType> {
return [
{
id: 'Keyboard--default-message-action',
description: i18n('Keyboard--default-message-action'),
keys: [['enter']],
},
{
id: 'Keyboard--view-details-for-selected-message',
description: i18n('Keyboard--view-details-for-selected-message'),
keys: [['commandOrCtrl', 'D']],
},
{
id: 'Keyboard--toggle-reply',
description: i18n('Keyboard--toggle-reply'),
keys: [['commandOrCtrl', 'shift', 'R']],
},
{
id: 'Keyboard--toggle-reaction-picker',
description: i18n('Keyboard--toggle-reaction-picker'),
keys: [['commandOrCtrl', 'shift', 'E']],
},
{
id: 'Keyboard--save-attachment',
description: i18n('Keyboard--save-attachment'),
keys: [['commandOrCtrl', 'S']],
},
{
id: 'Keyboard--delete-message',
description: i18n('Keyboard--delete-message'),
keys: [['commandOrCtrl', 'shift', 'D']],
},
];
}
2019-11-07 21:36:16 +00:00
function getComposerShortcuts(i18n: LocalizerType): Array<ShortcutType> {
return [
{
id: 'Keyboard--add-newline',
description: i18n('Keyboard--add-newline'),
keys: [['shift', 'enter']],
},
{
id: 'Keyboard--expand-composer',
description: i18n('Keyboard--expand-composer'),
keys: [['commandOrCtrl', 'shift', 'X']],
},
{
id: 'Keyboard--send-in-expanded-composer',
description: i18n('Keyboard--send-in-expanded-composer'),
keys: [['commandOrCtrl', 'enter']],
},
{
id: 'Keyboard--attach-file',
description: i18n('Keyboard--attach-file'),
keys: [['commandOrCtrl', 'U']],
},
{
id: 'Keyboard--remove-draft-link-preview',
description: i18n('Keyboard--remove-draft-link-preview'),
keys: [['commandOrCtrl', 'P']],
},
{
id: 'Keyboard--remove-draft-attachments',
description: i18n('Keyboard--remove-draft-attachments'),
keys: [['commandOrCtrl', 'shift', 'P']],
},
];
}
2019-11-07 21:36:16 +00:00
function getCallingShortcuts(i18n: LocalizerType): Array<ShortcutType> {
return [
{
id: 'Keyboard--toggle-audio',
description: i18n('Keyboard--toggle-audio'),
keys: [['shift', 'M']],
},
{
id: 'Keyboard--toggle-video',
description: i18n('Keyboard--toggle-video'),
keys: [['shift', 'V']],
},
{
id: 'icu:Keyboard--accept-video-call',
description: i18n('icu:Keyboard--accept-video-call'),
keys: [['ctrlOrAlt', 'shift', 'V']],
},
{
id: 'icu:Keyboard--accept-call-without-video',
description: i18n('icu:Keyboard--accept-call-without-video'),
keys: [['ctrlOrAlt', 'shift', 'A']],
},
{
id: 'Keyboard--decline-call',
description: i18n('Keyboard--decline-call'),
keys: [['ctrlOrAlt', 'shift', 'D']],
},
{
id: 'Keyboard--start-audio-call',
description: i18n('Keyboard--start-audio-call'),
keys: [['ctrlOrAlt', 'shift', 'C']],
},
{
id: 'Keyboard--start-video-call',
description: i18n('Keyboard--start-video-call'),
keys: [['ctrlOrAlt', 'shift', 'Y']],
},
{
id: 'Keyboard--hang-up',
description: i18n('Keyboard--hang-up'),
keys: [['ctrlOrAlt', 'shift', 'E']],
},
];
}
2020-06-04 18:16:19 +00:00
2022-11-18 00:45:19 +00:00
export function ShortcutGuide(props: Props): JSX.Element {
2019-11-07 21:36:16 +00:00
const { i18n, close, hasInstalledStickers, platform } = props;
const isMacOS = platform === 'darwin';
// Restore focus on teardown
const [focusRef] = useRestoreFocus();
2019-11-07 21:36:16 +00:00
return (
<div className="module-shortcut-guide">
<div className="module-shortcut-guide__header">
<div className="module-shortcut-guide__header-text">
{i18n('Keyboard--header')}
</div>
<button
2020-09-12 00:46:52 +00:00
aria-label={i18n('close-popup')}
2019-11-07 21:36:16 +00:00
className="module-shortcut-guide__header-close"
onClick={close}
title={i18n('close-popup')}
2020-09-12 00:46:52 +00:00
type="button"
2019-11-07 21:36:16 +00:00
/>
</div>
<div
className="module-shortcut-guide__scroll-container"
ref={focusRef}
tabIndex={-1}
>
<div className="module-shortcut-guide__section-container">
<div className="module-shortcut-guide__section">
<div className="module-shortcut-guide__section-header">
{i18n('Keyboard--navigation-header')}
</div>
<div className="module-shortcut-guide__section-list">
{getNavigationShortcuts(i18n).map((shortcut, index) => {
2019-11-07 21:36:16 +00:00
if (
!hasInstalledStickers &&
shortcut.description === 'Keyboard--open-sticker-chooser'
) {
return null;
}
return renderShortcut(shortcut, index, isMacOS, i18n);
})}
</div>
</div>
<div className="module-shortcut-guide__section">
<div className="module-shortcut-guide__section-header">
{i18n('Keyboard--messages-header')}
</div>
<div className="module-shortcut-guide__section-list">
{getMessageShortcuts(i18n).map((shortcut, index) =>
2019-11-07 21:36:16 +00:00
renderShortcut(shortcut, index, isMacOS, i18n)
)}
</div>
</div>
<div className="module-shortcut-guide__section">
<div className="module-shortcut-guide__section-header">
{i18n('Keyboard--composer-header')}
</div>
<div className="module-shortcut-guide__section-list">
{getComposerShortcuts(i18n).map((shortcut, index) =>
2019-11-07 21:36:16 +00:00
renderShortcut(shortcut, index, isMacOS, i18n)
)}
</div>
</div>
2020-06-04 18:16:19 +00:00
<div className="module-shortcut-guide__section">
<div className="module-shortcut-guide__section-header">
{i18n('Keyboard--calling-header')}
</div>
<div className="module-shortcut-guide__section-list">
{getCallingShortcuts(i18n).map((shortcut, index) =>
2020-06-04 18:16:19 +00:00
renderShortcut(shortcut, index, isMacOS, i18n)
)}
</div>
</div>
2019-11-07 21:36:16 +00:00
</div>
</div>
</div>
);
2022-11-18 00:45:19 +00:00
}
2019-11-07 21:36:16 +00:00
function renderShortcut(
shortcut: ShortcutType,
index: number,
isMacOS: boolean,
i18n: LocalizerType
) {
return (
2020-09-12 00:46:52 +00:00
<div key={index} className="module-shortcut-guide__shortcut">
2019-11-07 21:36:16 +00:00
<div className="module-shortcut-guide__shortcut__description">
{shortcut.description}
2019-11-07 21:36:16 +00:00
</div>
<div className="module-shortcut-guide__shortcut__key-container">
2020-09-12 00:46:52 +00:00
{shortcut.keys.map(keys => (
<div
2020-09-12 00:46:52 +00:00
key={`${shortcut.description}--${keys.map(k => k).join('-')}`}
className="module-shortcut-guide__shortcut__key-inner-container"
>
2020-09-12 00:46:52 +00:00
{keys.map(key => {
let label: string = key;
let isSquare = true;
2019-11-07 21:36:16 +00:00
if (key === 'commandOrCtrl' && isMacOS) {
label = '⌘';
}
if (key === 'commandOrCtrl' && !isMacOS) {
label = i18n('Keyboard--Key--ctrl');
isSquare = false;
}
2022-05-10 18:14:08 +00:00
if (key === 'ctrlOrAlt' && isMacOS) {
label = i18n('Keyboard--Key--ctrl');
isSquare = false;
}
if (key === 'ctrlOrAlt' && !isMacOS) {
label = i18n('Keyboard--Key--alt');
isSquare = false;
}
if (key === 'optionOrAlt' && isMacOS) {
label = i18n('Keyboard--Key--option');
isSquare = false;
}
if (key === 'optionOrAlt' && !isMacOS) {
label = i18n('Keyboard--Key--alt');
isSquare = false;
}
if (key === 'ctrl') {
label = i18n('Keyboard--Key--ctrl');
isSquare = false;
}
if (key === 'shift') {
label = i18n('Keyboard--Key--shift');
isSquare = false;
}
if (key === 'enter') {
label = i18n('Keyboard--Key--enter');
isSquare = false;
}
if (key === 'tab') {
label = i18n('Keyboard--Key--tab');
isSquare = false;
}
if (key === '1 to 9') {
label = i18n('Keyboard--Key--one-to-nine-range');
isSquare = false;
}
2019-11-07 21:36:16 +00:00
return (
<span
2020-09-12 00:46:52 +00:00
key={`shortcut__key--${key}`}
className={classNames(
'module-shortcut-guide__shortcut__key',
isSquare
? 'module-shortcut-guide__shortcut__key--square'
: null
)}
>
{label}
</span>
);
})}
</div>
))}
2019-11-07 21:36:16 +00:00
</div>
</div>
);
}