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

Commit 57cd8af

Browse files
committed
Split ApplySelection into CompleteOrPrevSelection and CompleteOrNextSelection
When moving through the autocomplete selection list distinguish between the following cases: 1) When there is no autocomplete window open, only open one and select the first item when the CompleteOrPrevSelection / CompleteOrNextSelection actions are emitted (e.g. by pressing SHIFT + TAB, TAB) 2) Otherwise navigate through the selection list (e.g. SHIFT + TAB, TAB, UP, DOWN) - Remove references to raw keyboard events in autocomplete.ts - Clarify the purpose of startSelection (previously onTab) Signed-off-by: Clemens Zeidler <[email protected]>
1 parent 7d08752 commit 57cd8af

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

src/KeyBindingsDefaults.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,27 +161,27 @@ const messageComposerBindings = (): KeyBinding<MessageComposerAction>[] => {
161161
const autocompleteBindings = (): KeyBinding<AutocompleteAction>[] => {
162162
return [
163163
{
164-
action: AutocompleteAction.ApplySelection,
164+
action: AutocompleteAction.CompleteOrNextSelection,
165165
keyCombo: {
166166
key: Key.TAB,
167167
},
168168
},
169169
{
170-
action: AutocompleteAction.ApplySelection,
170+
action: AutocompleteAction.CompleteOrNextSelection,
171171
keyCombo: {
172172
key: Key.TAB,
173173
ctrlKey: true,
174174
},
175175
},
176176
{
177-
action: AutocompleteAction.ApplySelection,
177+
action: AutocompleteAction.CompleteOrPrevSelection,
178178
keyCombo: {
179179
key: Key.TAB,
180180
shiftKey: true,
181181
},
182182
},
183183
{
184-
action: AutocompleteAction.ApplySelection,
184+
action: AutocompleteAction.CompleteOrPrevSelection,
185185
keyCombo: {
186186
key: Key.TAB,
187187
ctrlKey: true,

src/KeyBindingsManager.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,19 @@ export enum MessageComposerAction {
5252

5353
/** Actions for text editing autocompletion */
5454
export enum AutocompleteAction {
55-
/** Apply the current autocomplete selection */
56-
ApplySelection = 'ApplySelection',
57-
/** Cancel autocompletion */
58-
Cancel = 'Cancel',
55+
/**
56+
* Select previous selection or, if the autocompletion window is not shown, open the window and select the first
57+
* selection.
58+
*/
59+
CompleteOrPrevSelection = 'ApplySelection',
60+
/** Select next selection or, if the autocompletion window is not shown, open it and select the first selection */
61+
CompleteOrNextSelection = 'CompleteOrNextSelection',
5962
/** Move to the previous autocomplete selection */
6063
PrevSelection = 'PrevSelection',
6164
/** Move to the next autocomplete selection */
6265
NextSelection = 'NextSelection',
66+
/** Close the autocompletion window */
67+
Cancel = 'Cancel',
6368
}
6469

6570
/** Actions for the room list sidebar */

src/components/views/rooms/BasicMessageComposer.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -485,16 +485,14 @@ export default class BasicMessageEditor extends React.Component<IProps, IState>
485485
if (model.autoComplete && model.autoComplete.hasCompletions()) {
486486
const autoComplete = model.autoComplete;
487487
switch (autocompleteAction) {
488+
case AutocompleteAction.CompleteOrPrevSelection:
488489
case AutocompleteAction.PrevSelection:
489-
autoComplete.onUpArrow(event);
490+
autoComplete.selectPreviousSelection();
490491
handled = true;
491492
break;
493+
case AutocompleteAction.CompleteOrNextSelection:
492494
case AutocompleteAction.NextSelection:
493-
autoComplete.onDownArrow(event);
494-
handled = true;
495-
break;
496-
case AutocompleteAction.ApplySelection:
497-
autoComplete.onTab(event);
495+
autoComplete.selectNextSelection();
498496
handled = true;
499497
break;
500498
case AutocompleteAction.Cancel:
@@ -504,8 +502,10 @@ export default class BasicMessageEditor extends React.Component<IProps, IState>
504502
default:
505503
return; // don't preventDefault on anything else
506504
}
507-
} else if (autocompleteAction === AutocompleteAction.ApplySelection) {
508-
this.tabCompleteName(event);
505+
} else if (autocompleteAction === AutocompleteAction.CompleteOrPrevSelection
506+
|| autocompleteAction === AutocompleteAction.CompleteOrNextSelection) {
507+
// there is no current autocomplete window, try to open it
508+
this.tabCompleteName();
509509
handled = true;
510510
} else if (event.key === Key.BACKSPACE || event.key === Key.DELETE) {
511511
this.formatBarRef.current.hide();
@@ -517,7 +517,7 @@ export default class BasicMessageEditor extends React.Component<IProps, IState>
517517
}
518518
};
519519

520-
private async tabCompleteName(event: React.KeyboardEvent) {
520+
private async tabCompleteName() {
521521
try {
522522
await new Promise<void>(resolve => this.setState({showVisualBell: false}, resolve));
523523
const {model} = this.props;
@@ -540,7 +540,7 @@ export default class BasicMessageEditor extends React.Component<IProps, IState>
540540

541541
// Don't try to do things with the autocomplete if there is none shown
542542
if (model.autoComplete) {
543-
await model.autoComplete.onTab(event);
543+
await model.autoComplete.startSelection();
544544
if (!model.autoComplete.hasSelection()) {
545545
this.setState({showVisualBell: true});
546546
model.autoComplete.close();

src/editor/autocomplete.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,24 @@ export default class AutocompleteWrapperModel {
6868
this.updateCallback({close: true});
6969
}
7070

71-
public async onTab(e: KeyboardEvent) {
71+
/**
72+
* If there is no current autocompletion, start one and move to the first selection.
73+
*/
74+
public async startSelection() {
7275
const acComponent = this.getAutocompleterComponent();
73-
7476
if (acComponent.countCompletions() === 0) {
7577
// Force completions to show for the text currently entered
7678
await acComponent.forceComplete();
7779
// Select the first item by moving "down"
7880
await acComponent.moveSelection(+1);
79-
} else {
80-
await acComponent.moveSelection(e.shiftKey ? -1 : +1);
8181
}
8282
}
8383

84-
public onUpArrow(e: KeyboardEvent) {
84+
public selectPreviousSelection() {
8585
this.getAutocompleterComponent().moveSelection(-1);
8686
}
8787

88-
public onDownArrow(e: KeyboardEvent) {
88+
public selectNextSelection() {
8989
this.getAutocompleterComponent().moveSelection(+1);
9090
}
9191

0 commit comments

Comments
 (0)