11/*
22Copyright 2021 Clemens Zeidler
3+ Copyright 2022 Šimon Brandner <[email protected] > 34
45Licensed under the Apache License, Version 2.0 (the "License");
56you may not use this file except in compliance with the License.
@@ -14,127 +15,10 @@ See the License for the specific language governing permissions and
1415limitations under the License.
1516*/
1617
18+ import { KeyBindingAction } from "./accessibility/KeyboardShortcuts" ;
1719import { defaultBindingsProvider } from './KeyBindingsDefaults' ;
1820import { isMac } from './Keyboard' ;
1921
20- /** Actions for the chat message composer component */
21- export enum MessageComposerAction {
22- /** Send a message */
23- Send = 'KeyBinding.sendMessageInComposer' ,
24- /** Go backwards through the send history and use the message in composer view */
25- SelectPrevSendHistory = 'KeyBinding.previousMessageInComposerHistory' ,
26- /** Go forwards through the send history */
27- SelectNextSendHistory = 'KeyBinding.nextMessageInComposerHistory' ,
28- /** Start editing the user's last sent message */
29- EditPrevMessage = 'KeyBinding.editPreviousMessage' ,
30- /** Start editing the user's next sent message */
31- EditNextMessage = 'KeyBinding.editNextMessage' ,
32- /** Cancel editing a message or cancel replying to a message */
33- CancelEditing = 'KeyBinding.cancelReplyInComposer' ,
34-
35- /** Set bold format the current selection */
36- FormatBold = 'KeyBinding.toggleBoldInComposer' ,
37- /** Set italics format the current selection */
38- FormatItalics = 'KeyBinding.toggleItalicsInComposer' ,
39- /** Format the current selection as quote */
40- FormatQuote = 'KeyBinding.toggleQuoteInComposer' ,
41- /** Undo the last editing */
42- EditUndo = 'KeyBinding.editUndoInComposer' ,
43- /** Redo editing */
44- EditRedo = 'KeyBinding.editRedoInComposer' ,
45- /** Insert new line */
46- NewLine = 'KeyBinding.newLineInComposer' ,
47- /** Move the cursor to the start of the message */
48- MoveCursorToStart = 'KeyBinding.jumpToStartInComposer' ,
49- /** Move the cursor to the end of the message */
50- MoveCursorToEnd = 'KeyBinding.jumpToEndInComposer' ,
51- }
52-
53- /** Actions for text editing autocompletion */
54- export enum AutocompleteAction {
55- /** Accepts chosen autocomplete selection */
56- Complete = 'KeyBinding.completeAutocomplete' ,
57- /** Accepts chosen autocomplete selection or,
58- * if the autocompletion window is not shown, open the window and select the first selection */
59- ForceComplete = 'KeyBinding.forceCompleteAutocomplete' ,
60- /** Move to the previous autocomplete selection */
61- PrevSelection = 'KeyBinding.previousOptionInAutoComplete' ,
62- /** Move to the next autocomplete selection */
63- NextSelection = 'KeyBinding.nextOptionInAutoComplete' ,
64- /** Close the autocompletion window */
65- Cancel = 'KeyBinding.cancelAutoComplete' ,
66- }
67-
68- /** Actions for the room list sidebar */
69- export enum RoomListAction {
70- /** Clear room list filter field */
71- ClearSearch = 'KeyBinding.clearRoomFilter' ,
72- /** Navigate up/down in the room list */
73- PrevRoom = 'KeyBinding.downerRoom' ,
74- /** Navigate down in the room list */
75- NextRoom = 'KeyBinding.upperRoom' ,
76- /** Select room from the room list */
77- SelectRoom = 'KeyBinding.selectRoomInRoomList' ,
78- /** Collapse room list section */
79- CollapseSection = 'KeyBinding.collapseSectionInRoomList' ,
80- /** Expand room list section, if already expanded, jump to first room in the selection */
81- ExpandSection = 'KeyBinding.expandSectionInRoomList' ,
82- }
83-
84- /** Actions for the current room view */
85- export enum RoomAction {
86- /** Scroll up in the timeline */
87- ScrollUp = 'KeyBinding.scrollUpInTimeline' ,
88- /** Scroll down in the timeline */
89- RoomScrollDown = 'KeyBinding.scrollDownInTimeline' ,
90- /** Dismiss read marker and jump to bottom */
91- DismissReadMarker = 'KeyBinding.dismissReadMarkerAndJumpToBottom' ,
92- /** Jump to oldest unread message */
93- JumpToOldestUnread = 'KeyBinding.jumpToOldestUnreadMessage' ,
94- /** Upload a file */
95- UploadFile = 'KeyBinding.uploadFileToRoom' ,
96- /** Focus search message in a room (must be enabled) */
97- FocusSearch = 'KeyBinding.searchInRoom' ,
98- /** Jump to the first (downloaded) message in the room */
99- JumpToFirstMessage = 'KeyBinding.jumpToFirstMessageInTimeline' ,
100- /** Jump to the latest message in the room */
101- JumpToLatestMessage = 'KeyBinding.jumpToLastMessageInTimeline' ,
102- }
103-
104- /** Actions for navigating do various menus, dialogs or screens */
105- export enum NavigationAction {
106- /** Jump to room search (search for a room) */
107- FocusRoomSearch = 'KeyBinding.filterRooms' ,
108- /** Toggle the space panel */
109- ToggleSpacePanel = 'KeyBinding.toggleSpacePanel' ,
110- /** Toggle the room side panel */
111- ToggleRoomSidePanel = 'KeyBinding.toggleRightPanel' ,
112- /** Toggle the user menu */
113- ToggleUserMenu = 'KeyBinding.toggleTopLeftMenu' ,
114- /** Toggle the short cut help dialog */
115- OpenShortCutDialog = 'KeyBinding.showKeyBindingsSettings' ,
116- /** Got to the Element home screen */
117- GoToHome = 'KeyBinding.goToHomeView' ,
118- /** Select prev room */
119- SelectPrevRoom = 'KeyBinding.previousRoom' ,
120- /** Select next room */
121- SelectNextRoom = 'KeyBinding.nextRoom' ,
122- /** Select prev room with unread messages */
123- SelectPrevUnreadRoom = 'KeyBinding.previousUnreadRoom' ,
124- /** Select next room with unread messages */
125- SelectNextUnreadRoom = 'KeyBinding.nextUnreadRoom' ,
126- }
127-
128- /** Actions only available when labs are enabled */
129- export enum LabsAction {
130- /** Toggle visibility of hidden events */
131- ToggleHiddenEventVisibility = 'KeyBinding.toggleHiddenEventVisibility' ,
132- }
133-
134- export type KeyBindingAction = (
135- MessageComposerAction | AutocompleteAction | RoomListAction | RoomAction | NavigationAction | LabsAction
136- ) ;
137-
13822/**
13923 * Represent a key combination.
14024 *
@@ -144,16 +28,16 @@ export type KeyCombo = {
14428 key ?: string ;
14529
14630 /** On PC: ctrl is pressed; on Mac: meta is pressed */
147- ctrlOrCmd ?: boolean ;
31+ ctrlOrCmdKey ?: boolean ;
14832
14933 altKey ?: boolean ;
15034 ctrlKey ?: boolean ;
15135 metaKey ?: boolean ;
15236 shiftKey ?: boolean ;
15337} ;
15438
155- export type KeyBinding < T extends string > = {
156- action : T ;
39+ export type KeyBinding = {
40+ action : KeyBindingAction ;
15741 keyCombo : KeyCombo ;
15842} ;
15943
@@ -186,7 +70,7 @@ export function isKeyComboMatch(ev: KeyboardEvent | React.KeyboardEvent, combo:
18670 const evShift = ev . shiftKey ?? false ;
18771 const evMeta = ev . metaKey ?? false ;
18872 // When ctrlOrCmd is set, the keys need do evaluated differently on PC and Mac
189- if ( combo . ctrlOrCmd ) {
73+ if ( combo . ctrlOrCmdKey ) {
19074 if ( onMac ) {
19175 if ( ! evMeta
19276 || evCtrl !== comboCtrl
@@ -215,15 +99,10 @@ export function isKeyComboMatch(ev: KeyboardEvent | React.KeyboardEvent, combo:
21599 return true ;
216100}
217101
218- export type KeyBindingGetter < T extends string > = ( ) => KeyBinding < T > [ ] ;
102+ export type KeyBindingGetter = ( ) => KeyBinding [ ] ;
219103
220104export interface IKeyBindingsProvider {
221- getMessageComposerBindings : KeyBindingGetter < MessageComposerAction > ;
222- getAutocompleteBindings : KeyBindingGetter < AutocompleteAction > ;
223- getRoomListBindings : KeyBindingGetter < RoomListAction > ;
224- getRoomBindings : KeyBindingGetter < RoomAction > ;
225- getNavigationBindings : KeyBindingGetter < NavigationAction > ;
226- getLabsBindings : KeyBindingGetter < LabsAction > ;
105+ [ key : string ] : KeyBindingGetter ;
227106}
228107
229108export class KeyBindingsManager {
@@ -242,10 +121,10 @@ export class KeyBindingsManager {
242121 /**
243122 * Finds a matching KeyAction for a given KeyboardEvent
244123 */
245- private getAction < T extends string > (
246- getters : KeyBindingGetter < T > [ ] ,
124+ private getAction (
125+ getters : KeyBindingGetter [ ] ,
247126 ev : KeyboardEvent | React . KeyboardEvent ,
248- ) : T | undefined {
127+ ) : KeyBindingAction | undefined {
249128 for ( const getter of getters ) {
250129 const bindings = getter ( ) ;
251130 const binding = bindings . find ( it => isKeyComboMatch ( ev , it . keyCombo , isMac ) ) ;
@@ -256,27 +135,27 @@ export class KeyBindingsManager {
256135 return undefined ;
257136 }
258137
259- getMessageComposerAction ( ev : KeyboardEvent | React . KeyboardEvent ) : MessageComposerAction | undefined {
138+ getMessageComposerAction ( ev : KeyboardEvent | React . KeyboardEvent ) : KeyBindingAction | undefined {
260139 return this . getAction ( this . bindingsProviders . map ( it => it . getMessageComposerBindings ) , ev ) ;
261140 }
262141
263- getAutocompleteAction ( ev : KeyboardEvent | React . KeyboardEvent ) : AutocompleteAction | undefined {
142+ getAutocompleteAction ( ev : KeyboardEvent | React . KeyboardEvent ) : KeyBindingAction | undefined {
264143 return this . getAction ( this . bindingsProviders . map ( it => it . getAutocompleteBindings ) , ev ) ;
265144 }
266145
267- getRoomListAction ( ev : KeyboardEvent | React . KeyboardEvent ) : RoomListAction | undefined {
146+ getRoomListAction ( ev : KeyboardEvent | React . KeyboardEvent ) : KeyBindingAction | undefined {
268147 return this . getAction ( this . bindingsProviders . map ( it => it . getRoomListBindings ) , ev ) ;
269148 }
270149
271- getRoomAction ( ev : KeyboardEvent | React . KeyboardEvent ) : RoomAction | undefined {
150+ getRoomAction ( ev : KeyboardEvent | React . KeyboardEvent ) : KeyBindingAction | undefined {
272151 return this . getAction ( this . bindingsProviders . map ( it => it . getRoomBindings ) , ev ) ;
273152 }
274153
275- getNavigationAction ( ev : KeyboardEvent | React . KeyboardEvent ) : NavigationAction | undefined {
154+ getNavigationAction ( ev : KeyboardEvent | React . KeyboardEvent ) : KeyBindingAction | undefined {
276155 return this . getAction ( this . bindingsProviders . map ( it => it . getNavigationBindings ) , ev ) ;
277156 }
278157
279- getLabsAction ( ev : KeyboardEvent | React . KeyboardEvent ) : LabsAction | undefined {
158+ getLabsAction ( ev : KeyboardEvent | React . KeyboardEvent ) : KeyBindingAction | undefined {
280159 return this . getAction ( this . bindingsProviders . map ( it => it . getLabsBindings ) , ev ) ;
281160 }
282161}
0 commit comments