signal-desktop/ts/components/ShortcutGuide.tsx

516 lines
15 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;
isFormattingFlagEnabled: boolean;
isFormattingSpoilersFlagEnabled: boolean;
2019-11-07 21:36:16 +00:00
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'
2023-05-09 15:52:03 +00:00
| 'F6'
2019-11-07 21:36:16 +00:00
| '↑'
| '↓'
| ','
| '.'
| 'A'
| 'B'
2019-11-07 21:36:16 +00:00
| 'C'
| 'D'
2020-01-23 23:57:37 +00:00
| 'E'
2019-11-07 21:36:16 +00:00
| 'F'
| 'G'
| 'I'
2019-11-19 23:03:00 +00:00
| 'J'
| 'K'
| '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',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--navigate-by-section'),
2023-05-09 15:52:03 +00:00
keys: [
['commandOrCtrl', 'T'],
['commandOrCtrl', 'F6'],
],
},
{
id: 'Keyboard--previous-conversation',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--previous-conversation'),
keys: [
['optionOrAlt', '↑'],
['ctrl', 'shift', 'tab'],
],
},
{
id: 'Keyboard--next-conversation',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--next-conversation'),
keys: [
['optionOrAlt', '↓'],
['ctrl', 'tab'],
],
},
{
id: 'Keyboard--previous-unread-conversation',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--previous-unread-conversation'),
keys: [['optionOrAlt', 'shift', '↑']],
},
{
id: 'Keyboard--next-unread-conversation',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--next-unread-conversation'),
keys: [['optionOrAlt', 'shift', '↓']],
},
{
id: 'Keyboard--conversation-by-index',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--conversation-by-index'),
keys: [['commandOrCtrl', '1 to 9']],
},
2023-05-09 15:52:03 +00:00
{
id: 'Keyboard--most-recent-message',
description: i18n('icu:Keyboard--focus-most-recent-message'),
keys: [['commandOrCtrl', 'J']],
},
{
id: 'Keyboard--preferences',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--preferences'),
keys: [['commandOrCtrl', ',']],
},
{
id: 'Keyboard--open-conversation-menu',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--open-conversation-menu'),
keys: [['commandOrCtrl', 'shift', 'L']],
},
{
id: 'Keyboard--new-conversation',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--new-conversation'),
keys: [['commandOrCtrl', 'N']],
},
{
id: 'Keyboard--search',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--search'),
keys: [['commandOrCtrl', 'F']],
},
{
id: 'Keyboard--search-in-conversation',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--search-in-conversation'),
keys: [['commandOrCtrl', 'shift', 'F']],
},
{
id: 'Keyboard--focus-composer',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--focus-composer'),
keys: [['commandOrCtrl', 'shift', 'T']],
},
{
id: 'Keyboard--open-all-media-view',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--open-all-media-view'),
keys: [['commandOrCtrl', 'shift', 'M']],
},
{
id: 'Keyboard--open-emoji-chooser',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--open-emoji-chooser'),
keys: [['commandOrCtrl', 'shift', 'J']],
},
{
id: 'Keyboard--open-sticker-chooser',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--open-sticker-chooser'),
keys: [['commandOrCtrl', 'shift', 'G']],
},
{
id: 'Keyboard--begin-recording-voice-note',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--begin-recording-voice-note'),
keys: [['commandOrCtrl', 'shift', 'V']],
},
{
id: 'Keyboard--archive-conversation',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--archive-conversation'),
keys: [['commandOrCtrl', 'shift', 'A']],
},
{
id: 'Keyboard--unarchive-conversation',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--unarchive-conversation'),
keys: [['commandOrCtrl', 'shift', 'U']],
},
{
id: 'Keyboard--scroll-to-top',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--scroll-to-top'),
keys: [['commandOrCtrl', '↑']],
},
{
id: 'Keyboard--scroll-to-bottom',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--scroll-to-bottom'),
keys: [['commandOrCtrl', '↓']],
},
{
id: 'Keyboard--close-curent-conversation',
2023-03-30 00:03:25 +00:00
description: i18n('icu: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',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--default-message-action'),
keys: [['enter']],
},
{
id: 'Keyboard--view-details-for-selected-message',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--view-details-for-selected-message'),
keys: [['commandOrCtrl', 'D']],
},
{
id: 'Keyboard--toggle-reply',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--toggle-reply'),
keys: [['commandOrCtrl', 'shift', 'R']],
},
{
id: 'Keyboard--toggle-reaction-picker',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--toggle-reaction-picker'),
keys: [['commandOrCtrl', 'shift', 'E']],
},
{
id: 'Keyboard--save-attachment',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--save-attachment'),
keys: [['commandOrCtrl', 'S']],
},
{
2023-03-30 00:03:25 +00:00
id: 'Keyboard--delete-messages',
description: i18n('icu:Keyboard--delete-messages'),
keys: [['commandOrCtrl', 'shift', 'D']],
},
{
id: 'Keyboard--forward-messages',
description: i18n('icu:Keyboard--forward-messages'),
keys: [['commandOrCtrl', 'shift', 'S']],
},
];
}
2019-11-07 21:36:16 +00:00
function getComposerShortcuts(
i18n: LocalizerType,
isFormattingFlagEnabled: boolean,
isFormattingSpoilersFlagEnabled: boolean
): Array<ShortcutType> {
const shortcuts: Array<ShortcutType> = [
{
id: 'Keyboard--add-newline',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--add-newline'),
keys: [['shift', 'enter']],
},
{
id: 'Keyboard--expand-composer',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--expand-composer'),
keys: [['commandOrCtrl', 'shift', 'K']],
},
{
id: 'Keyboard--send-in-expanded-composer',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--send-in-expanded-composer'),
keys: [['commandOrCtrl', 'enter']],
},
{
id: 'Keyboard--attach-file',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--attach-file'),
keys: [['commandOrCtrl', 'U']],
},
{
id: 'Keyboard--remove-draft-link-preview',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--remove-draft-link-preview'),
keys: [['commandOrCtrl', 'P']],
},
{
id: 'Keyboard--remove-draft-attachments',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--remove-draft-attachments'),
keys: [['commandOrCtrl', 'shift', 'P']],
},
];
if (isFormattingFlagEnabled) {
shortcuts.push({
id: 'Keyboard--composer--bold',
description: i18n('icu:Keyboard--composer--bold'),
keys: [['commandOrCtrl', 'B']],
});
shortcuts.push({
id: 'Keyboard--composer--italic',
description: i18n('icu:Keyboard--composer--italic'),
keys: [['commandOrCtrl', 'I']],
});
shortcuts.push({
id: 'Keyboard--composer--strikethrough',
description: i18n('icu:Keyboard--composer--strikethrough'),
keys: [['commandOrCtrl', 'shift', 'X']],
});
shortcuts.push({
id: 'Keyboard--composer--monospace',
description: i18n('icu:Keyboard--composer--monospace'),
keys: [['commandOrCtrl', 'E']],
});
if (isFormattingSpoilersFlagEnabled) {
shortcuts.push({
id: 'Keyboard--composer--spoiler',
description: i18n('icu:Keyboard--composer--spoiler'),
keys: [['commandOrCtrl', 'shift', 'B']],
});
}
}
return shortcuts;
}
2019-11-07 21:36:16 +00:00
function getCallingShortcuts(i18n: LocalizerType): Array<ShortcutType> {
return [
{
id: 'Keyboard--toggle-audio',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--toggle-audio'),
keys: [['shift', 'M']],
},
{
id: 'Keyboard--toggle-video',
2023-03-30 00:03:25 +00:00
description: i18n('icu: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',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--decline-call'),
keys: [['ctrlOrAlt', 'shift', 'D']],
},
{
id: 'Keyboard--start-audio-call',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--start-audio-call'),
keys: [['ctrlOrAlt', 'shift', 'C']],
},
{
id: 'Keyboard--start-video-call',
2023-03-30 00:03:25 +00:00
description: i18n('icu:Keyboard--start-video-call'),
keys: [['ctrlOrAlt', 'shift', 'Y']],
},
{
id: 'Keyboard--hang-up',
2023-03-30 00:03:25 +00:00
description: i18n('icu: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 {
const {
i18n,
close,
hasInstalledStickers,
isFormattingFlagEnabled,
isFormattingSpoilersFlagEnabled,
platform,
} = props;
2019-11-07 21:36:16 +00:00
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">
2023-03-30 00:03:25 +00:00
{i18n('icu:Keyboard--header')}
2019-11-07 21:36:16 +00:00
</div>
<button
2023-03-30 00:03:25 +00:00
aria-label={i18n('icu:close-popup')}
2019-11-07 21:36:16 +00:00
className="module-shortcut-guide__header-close"
onClick={close}
2023-03-30 00:03:25 +00:00
title={i18n('icu: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">
2023-03-30 00:03:25 +00:00
{i18n('icu:Keyboard--navigation-header')}
2019-11-07 21:36:16 +00:00
</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">
2023-03-30 00:03:25 +00:00
{i18n('icu:Keyboard--messages-header')}
2019-11-07 21:36:16 +00:00
</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">
2023-03-30 00:03:25 +00:00
{i18n('icu:Keyboard--composer-header')}
2019-11-07 21:36:16 +00:00
</div>
<div className="module-shortcut-guide__section-list">
{getComposerShortcuts(
i18n,
isFormattingFlagEnabled,
isFormattingSpoilersFlagEnabled
).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">
2023-03-30 00:03:25 +00:00
{i18n('icu:Keyboard--calling-header')}
2020-06-04 18:16:19 +00:00
</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) {
2023-03-30 00:03:25 +00:00
label = i18n('icu:Keyboard--Key--ctrl');
isSquare = false;
}
2022-05-10 18:14:08 +00:00
if (key === 'ctrlOrAlt' && isMacOS) {
2023-03-30 00:03:25 +00:00
label = i18n('icu:Keyboard--Key--ctrl');
2022-05-10 18:14:08 +00:00
isSquare = false;
}
if (key === 'ctrlOrAlt' && !isMacOS) {
2023-03-30 00:03:25 +00:00
label = i18n('icu:Keyboard--Key--alt');
2022-05-10 18:14:08 +00:00
isSquare = false;
}
if (key === 'optionOrAlt' && isMacOS) {
2023-03-30 00:03:25 +00:00
label = i18n('icu:Keyboard--Key--option');
isSquare = false;
}
if (key === 'optionOrAlt' && !isMacOS) {
2023-03-30 00:03:25 +00:00
label = i18n('icu:Keyboard--Key--alt');
isSquare = false;
}
if (key === 'ctrl') {
2023-03-30 00:03:25 +00:00
label = i18n('icu:Keyboard--Key--ctrl');
isSquare = false;
}
if (key === 'shift') {
2023-03-30 00:03:25 +00:00
label = i18n('icu:Keyboard--Key--shift');
isSquare = false;
}
if (key === 'enter') {
2023-03-30 00:03:25 +00:00
label = i18n('icu:Keyboard--Key--enter');
isSquare = false;
}
if (key === 'tab') {
2023-03-30 00:03:25 +00:00
label = i18n('icu:Keyboard--Key--tab');
isSquare = false;
}
if (key === '1 to 9') {
2023-03-30 00:03:25 +00:00
label = i18n('icu: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>
);
}