Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 33e8edb

Browse files
authored
Merge pull request #5720 from czeidler/key-bindings
Decouple key bindings from event handling
2 parents 83612dd + 228070f commit 33e8edb

File tree

10 files changed

+1104
-285
lines changed

10 files changed

+1104
-285
lines changed

src/KeyBindingsDefaults.ts

Lines changed: 407 additions & 0 deletions
Large diffs are not rendered by default.

src/KeyBindingsManager.ts

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
/*
2+
Copyright 2021 Clemens Zeidler
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { defaultBindingsProvider } from './KeyBindingsDefaults';
18+
import { isMac } from './Keyboard';
19+
20+
/** Actions for the chat message composer component */
21+
export enum MessageComposerAction {
22+
/** Send a message */
23+
Send = 'Send',
24+
/** Go backwards through the send history and use the message in composer view */
25+
SelectPrevSendHistory = 'SelectPrevSendHistory',
26+
/** Go forwards through the send history */
27+
SelectNextSendHistory = 'SelectNextSendHistory',
28+
/** Start editing the user's last sent message */
29+
EditPrevMessage = 'EditPrevMessage',
30+
/** Start editing the user's next sent message */
31+
EditNextMessage = 'EditNextMessage',
32+
/** Cancel editing a message or cancel replying to a message */
33+
CancelEditing = 'CancelEditing',
34+
35+
/** Set bold format the current selection */
36+
FormatBold = 'FormatBold',
37+
/** Set italics format the current selection */
38+
FormatItalics = 'FormatItalics',
39+
/** Format the current selection as quote */
40+
FormatQuote = 'FormatQuote',
41+
/** Undo the last editing */
42+
EditUndo = 'EditUndo',
43+
/** Redo editing */
44+
EditRedo = 'EditRedo',
45+
/** Insert new line */
46+
NewLine = 'NewLine',
47+
/** Move the cursor to the start of the message */
48+
MoveCursorToStart = 'MoveCursorToStart',
49+
/** Move the cursor to the end of the message */
50+
MoveCursorToEnd = 'MoveCursorToEnd',
51+
}
52+
53+
/** Actions for text editing autocompletion */
54+
export enum AutocompleteAction {
55+
/** Apply the current autocomplete selection */
56+
ApplySelection = 'ApplySelection',
57+
/** Cancel autocompletion */
58+
Cancel = 'Cancel',
59+
/** Move to the previous autocomplete selection */
60+
PrevSelection = 'PrevSelection',
61+
/** Move to the next autocomplete selection */
62+
NextSelection = 'NextSelection',
63+
}
64+
65+
/** Actions for the room list sidebar */
66+
export enum RoomListAction {
67+
/** Clear room list filter field */
68+
ClearSearch = 'ClearSearch',
69+
/** Navigate up/down in the room list */
70+
PrevRoom = 'PrevRoom',
71+
/** Navigate down in the room list */
72+
NextRoom = 'NextRoom',
73+
/** Select room from the room list */
74+
SelectRoom = 'SelectRoom',
75+
/** Collapse room list section */
76+
CollapseSection = 'CollapseSection',
77+
/** Expand room list section, if already expanded, jump to first room in the selection */
78+
ExpandSection = 'ExpandSection',
79+
}
80+
81+
/** Actions for the current room view */
82+
export enum RoomAction {
83+
/** Scroll up in the timeline */
84+
ScrollUp = 'ScrollUp',
85+
/** Scroll down in the timeline */
86+
RoomScrollDown = 'RoomScrollDown',
87+
/** Dismiss read marker and jump to bottom */
88+
DismissReadMarker = 'DismissReadMarker',
89+
/** Jump to oldest unread message */
90+
JumpToOldestUnread = 'JumpToOldestUnread',
91+
/** Upload a file */
92+
UploadFile = 'UploadFile',
93+
/** Focus search message in a room (must be enabled) */
94+
FocusSearch = 'FocusSearch',
95+
/** Jump to the first (downloaded) message in the room */
96+
JumpToFirstMessage = 'JumpToFirstMessage',
97+
/** Jump to the latest message in the room */
98+
JumpToLatestMessage = 'JumpToLatestMessage',
99+
}
100+
101+
/** Actions for navigating do various menus, dialogs or screens */
102+
export enum NavigationAction {
103+
/** Jump to room search (search for a room) */
104+
FocusRoomSearch = 'FocusRoomSearch',
105+
/** Toggle the room side panel */
106+
ToggleRoomSidePanel = 'ToggleRoomSidePanel',
107+
/** Toggle the user menu */
108+
ToggleUserMenu = 'ToggleUserMenu',
109+
/** Toggle the short cut help dialog */
110+
ToggleShortCutDialog = 'ToggleShortCutDialog',
111+
/** Got to the Element home screen */
112+
GoToHome = 'GoToHome',
113+
/** Select prev room */
114+
SelectPrevRoom = 'SelectPrevRoom',
115+
/** Select next room */
116+
SelectNextRoom = 'SelectNextRoom',
117+
/** Select prev room with unread messages */
118+
SelectPrevUnreadRoom = 'SelectPrevUnreadRoom',
119+
/** Select next room with unread messages */
120+
SelectNextUnreadRoom = 'SelectNextUnreadRoom',
121+
}
122+
123+
/**
124+
* Represent a key combination.
125+
*
126+
* The combo is evaluated strictly, i.e. the KeyboardEvent must match exactly what is specified in the KeyCombo.
127+
*/
128+
export type KeyCombo = {
129+
key?: string;
130+
131+
/** On PC: ctrl is pressed; on Mac: meta is pressed */
132+
ctrlOrCmd?: boolean;
133+
134+
altKey?: boolean;
135+
ctrlKey?: boolean;
136+
metaKey?: boolean;
137+
shiftKey?: boolean;
138+
}
139+
140+
export type KeyBinding<T extends string> = {
141+
action: T;
142+
keyCombo: KeyCombo;
143+
}
144+
145+
/**
146+
* Helper method to check if a KeyboardEvent matches a KeyCombo
147+
*
148+
* Note, this method is only exported for testing.
149+
*/
150+
export function isKeyComboMatch(ev: KeyboardEvent | React.KeyboardEvent, combo: KeyCombo, onMac: boolean): boolean {
151+
if (combo.key !== undefined) {
152+
// When shift is pressed, letters are returned as upper case chars. In this case do a lower case comparison.
153+
// This works for letter combos such as shift + U as well for none letter combos such as shift + Escape.
154+
// If shift is not pressed, the toLowerCase conversion can be avoided.
155+
if (ev.shiftKey) {
156+
if (ev.key.toLowerCase() !== combo.key.toLowerCase()) {
157+
return false;
158+
}
159+
} else if (ev.key !== combo.key) {
160+
return false;
161+
}
162+
}
163+
164+
const comboCtrl = combo.ctrlKey ?? false;
165+
const comboAlt = combo.altKey ?? false;
166+
const comboShift = combo.shiftKey ?? false;
167+
const comboMeta = combo.metaKey ?? false;
168+
// Tests mock events may keep the modifiers undefined; convert them to booleans
169+
const evCtrl = ev.ctrlKey ?? false;
170+
const evAlt = ev.altKey ?? false;
171+
const evShift = ev.shiftKey ?? false;
172+
const evMeta = ev.metaKey ?? false;
173+
// When ctrlOrCmd is set, the keys need do evaluated differently on PC and Mac
174+
if (combo.ctrlOrCmd) {
175+
if (onMac) {
176+
if (!evMeta
177+
|| evCtrl !== comboCtrl
178+
|| evAlt !== comboAlt
179+
|| evShift !== comboShift) {
180+
return false;
181+
}
182+
} else {
183+
if (!evCtrl
184+
|| evMeta !== comboMeta
185+
|| evAlt !== comboAlt
186+
|| evShift !== comboShift) {
187+
return false;
188+
}
189+
}
190+
return true;
191+
}
192+
193+
if (evMeta !== comboMeta
194+
|| evCtrl !== comboCtrl
195+
|| evAlt !== comboAlt
196+
|| evShift !== comboShift) {
197+
return false;
198+
}
199+
200+
return true;
201+
}
202+
203+
export type KeyBindingGetter<T extends string> = () => KeyBinding<T>[];
204+
205+
export interface IKeyBindingsProvider {
206+
getMessageComposerBindings: KeyBindingGetter<MessageComposerAction>;
207+
getAutocompleteBindings: KeyBindingGetter<AutocompleteAction>;
208+
getRoomListBindings: KeyBindingGetter<RoomListAction>;
209+
getRoomBindings: KeyBindingGetter<RoomAction>;
210+
getNavigationBindings: KeyBindingGetter<NavigationAction>;
211+
}
212+
213+
export class KeyBindingsManager {
214+
/**
215+
* List of key bindings providers.
216+
*
217+
* Key bindings from the first provider(s) in the list will have precedence over key bindings from later providers.
218+
*
219+
* To overwrite the default key bindings add a new providers before the default provider, e.g. a provider for
220+
* customized key bindings.
221+
*/
222+
bindingsProviders: IKeyBindingsProvider[] = [
223+
defaultBindingsProvider,
224+
];
225+
226+
/**
227+
* Finds a matching KeyAction for a given KeyboardEvent
228+
*/
229+
private getAction<T extends string>(getters: KeyBindingGetter<T>[], ev: KeyboardEvent | React.KeyboardEvent)
230+
: T | undefined {
231+
for (const getter of getters) {
232+
const bindings = getter();
233+
const binding = bindings.find(it => isKeyComboMatch(ev, it.keyCombo, isMac));
234+
if (binding) {
235+
return binding.action;
236+
}
237+
}
238+
return undefined;
239+
}
240+
241+
getMessageComposerAction(ev: KeyboardEvent | React.KeyboardEvent): MessageComposerAction | undefined {
242+
return this.getAction(this.bindingsProviders.map(it => it.getMessageComposerBindings), ev);
243+
}
244+
245+
getAutocompleteAction(ev: KeyboardEvent | React.KeyboardEvent): AutocompleteAction | undefined {
246+
return this.getAction(this.bindingsProviders.map(it => it.getAutocompleteBindings), ev);
247+
}
248+
249+
getRoomListAction(ev: KeyboardEvent | React.KeyboardEvent): RoomListAction | undefined {
250+
return this.getAction(this.bindingsProviders.map(it => it.getRoomListBindings), ev);
251+
}
252+
253+
getRoomAction(ev: KeyboardEvent | React.KeyboardEvent): RoomAction | undefined {
254+
return this.getAction(this.bindingsProviders.map(it => it.getRoomBindings), ev);
255+
}
256+
257+
getNavigationAction(ev: KeyboardEvent | React.KeyboardEvent): NavigationAction | undefined {
258+
return this.getAction(this.bindingsProviders.map(it => it.getNavigationBindings), ev);
259+
}
260+
}
261+
262+
const manager = new KeyBindingsManager();
263+
264+
export function getKeyBindingsManager(): KeyBindingsManager {
265+
return manager;
266+
}

0 commit comments

Comments
 (0)