forked from facebook/sapling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathISLShortcuts.tsx
More file actions
113 lines (105 loc) · 4.43 KB
/
ISLShortcuts.tsx
File metadata and controls
113 lines (105 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {Kbd} from 'isl-components/Kbd';
import {KeyCode, makeCommandDispatcher, Modifier} from 'isl-components/KeyboardShortcuts';
import {isMac} from 'isl-components/OperatingSystem';
import {useMemo} from 'react';
import {TypedEventEmitter} from 'shared/TypedEventEmitter';
import {t} from './i18n';
import {useModal} from './useModal';
import './ISLShortcuts.css';
const CMD = isMac ? Modifier.CMD : Modifier.CTRL;
/* eslint-disable no-bitwise */
export const [ISLCommandContext, useCommand, dispatchCommand, allCommands] = makeCommandDispatcher({
OpenShortcutHelp: [Modifier.SHIFT, KeyCode.QuestionMark],
ToggleSidebar: [CMD, KeyCode.Period],
ToggleLeftSidebar: [CMD, KeyCode.Comma],
OpenUncommittedChangesComparisonView: [CMD, KeyCode.SingleQuote],
OpenHeadChangesComparisonView: [[CMD, Modifier.SHIFT], KeyCode.SingleQuote],
Escape: [Modifier.NONE, KeyCode.Escape],
SelectUpwards: [Modifier.NONE, KeyCode.UpArrow],
SelectDownwards: [Modifier.NONE, KeyCode.DownArrow],
OpenDetails: [Modifier.NONE, KeyCode.RightArrow],
ContinueSelectionUpwards: [Modifier.SHIFT, KeyCode.UpArrow],
ContinueSelectionDownwards: [Modifier.SHIFT, KeyCode.DownArrow],
SelectAllCommits: [Modifier.ALT, KeyCode.A],
HideSelectedCommits: [Modifier.NONE, KeyCode.Backspace],
ZoomIn: [Modifier.ALT, KeyCode.Plus],
ZoomOut: [Modifier.ALT, KeyCode.Minus],
ToggleTheme: [Modifier.ALT, KeyCode.T],
ToggleShelvedChangesDropdown: [Modifier.ALT, KeyCode.S],
ToggleDownloadCommitsDropdown: [Modifier.ALT, KeyCode.D],
ToggleCwdDropdown: [Modifier.ALT, KeyCode.C],
ToggleBulkActionsDropdown: [Modifier.ALT, KeyCode.B],
ToggleFocusMode: [Modifier.ALT, KeyCode.F],
ToggleBookmarksManagerDropdown: [Modifier.ALT, KeyCode.M],
RebaseOntoCurrentStackBase: [Modifier.ALT, KeyCode.R],
});
export type ISLCommandName = Parameters<typeof useCommand>[0];
/** Like useCommand, but returns an eventEmitter you can subscribe to */
export function useCommandEvent(commandName: ISLCommandName): TypedEventEmitter<'change', null> {
const emitter = useMemo(() => new TypedEventEmitter<'change', null>(), []);
useCommand(commandName, () => {
emitter.emit('change', null);
});
return emitter;
}
export const ISLShortcutLabels: Partial<Record<ISLCommandName, string>> = {
Escape: t('Dismiss Tooltips and Popups'),
OpenShortcutHelp: t('Open Shortcut Help'),
ToggleSidebar: t('Toggle Commit Info Sidebar'),
ToggleLeftSidebar: t('Toggle PR Stacks Sidebar'),
OpenUncommittedChangesComparisonView: t('Open Uncommitted Changes Comparison View'),
OpenHeadChangesComparisonView: t('Open Head Changes Comparison View'),
SelectAllCommits: t('Select All Commits'),
ToggleTheme: t('Toggle Light/Dark Theme'),
ZoomIn: t('Zoom In'),
ZoomOut: t('Zoom Out'),
ToggleShelvedChangesDropdown: t('Toggle Shelved Changes Dropdown'),
ToggleDownloadCommitsDropdown: t('Toggle Download Commits Dropdown'),
ToggleCwdDropdown: t('Toggle CWD Dropdown'),
ToggleBulkActionsDropdown: t('Toggle Bulk Actions Dropdown'),
ToggleFocusMode: t('Toggle Focus Mode'),
ToggleBookmarksManagerDropdown: t('Toggle Bookmarks Manager Dropdown'),
RebaseOntoCurrentStackBase: t('Rebase Selected Commits onto Current Stack Base'),
};
export function useShowKeyboardShortcutsHelp(): () => unknown {
const showModal = useModal();
const showShortcutsModal = () => {
showModal({
type: 'custom',
component: () => (
<div className="keyboard-shortcuts-menu">
<table>
{(Object.entries(ISLShortcutLabels) as Array<[ISLCommandName, string]>).map(
([name, label]) => {
const [modifiers, keyCode] = allCommands[name];
{
return (
<tr key={name}>
<td>{label}</td>
<td>
<Kbd
modifiers={Array.isArray(modifiers) ? modifiers : [modifiers]}
keycode={keyCode}
/>
</td>
</tr>
);
}
},
)}
</table>
</div>
),
icon: 'keyboard',
title: t('Keyboard Shortcuts'),
});
};
useCommand('OpenShortcutHelp', showShortcutsModal);
return showShortcutsModal;
}