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 <clemens.zeidler@gmail.com>
pull/21833/head
Clemens Zeidler 2021-03-28 19:59:33 +13:00
parent 7d087524a5
commit 57cd8afbc4
4 changed files with 29 additions and 24 deletions

View File

@ -161,27 +161,27 @@ const messageComposerBindings = (): KeyBinding<MessageComposerAction>[] => {
const autocompleteBindings = (): KeyBinding<AutocompleteAction>[] => { const autocompleteBindings = (): KeyBinding<AutocompleteAction>[] => {
return [ return [
{ {
action: AutocompleteAction.ApplySelection, action: AutocompleteAction.CompleteOrNextSelection,
keyCombo: { keyCombo: {
key: Key.TAB, key: Key.TAB,
}, },
}, },
{ {
action: AutocompleteAction.ApplySelection, action: AutocompleteAction.CompleteOrNextSelection,
keyCombo: { keyCombo: {
key: Key.TAB, key: Key.TAB,
ctrlKey: true, ctrlKey: true,
}, },
}, },
{ {
action: AutocompleteAction.ApplySelection, action: AutocompleteAction.CompleteOrPrevSelection,
keyCombo: { keyCombo: {
key: Key.TAB, key: Key.TAB,
shiftKey: true, shiftKey: true,
}, },
}, },
{ {
action: AutocompleteAction.ApplySelection, action: AutocompleteAction.CompleteOrPrevSelection,
keyCombo: { keyCombo: {
key: Key.TAB, key: Key.TAB,
ctrlKey: true, ctrlKey: true,

View File

@ -52,14 +52,19 @@ export enum MessageComposerAction {
/** Actions for text editing autocompletion */ /** Actions for text editing autocompletion */
export enum AutocompleteAction { export enum AutocompleteAction {
/** Apply the current autocomplete selection */ /**
ApplySelection = 'ApplySelection', * Select previous selection or, if the autocompletion window is not shown, open the window and select the first
/** Cancel autocompletion */ * selection.
Cancel = 'Cancel', */
CompleteOrPrevSelection = 'ApplySelection',
/** Select next selection or, if the autocompletion window is not shown, open it and select the first selection */
CompleteOrNextSelection = 'CompleteOrNextSelection',
/** Move to the previous autocomplete selection */ /** Move to the previous autocomplete selection */
PrevSelection = 'PrevSelection', PrevSelection = 'PrevSelection',
/** Move to the next autocomplete selection */ /** Move to the next autocomplete selection */
NextSelection = 'NextSelection', NextSelection = 'NextSelection',
/** Close the autocompletion window */
Cancel = 'Cancel',
} }
/** Actions for the room list sidebar */ /** Actions for the room list sidebar */

View File

@ -485,16 +485,14 @@ export default class BasicMessageEditor extends React.Component<IProps, IState>
if (model.autoComplete && model.autoComplete.hasCompletions()) { if (model.autoComplete && model.autoComplete.hasCompletions()) {
const autoComplete = model.autoComplete; const autoComplete = model.autoComplete;
switch (autocompleteAction) { switch (autocompleteAction) {
case AutocompleteAction.CompleteOrPrevSelection:
case AutocompleteAction.PrevSelection: case AutocompleteAction.PrevSelection:
autoComplete.onUpArrow(event); autoComplete.selectPreviousSelection();
handled = true; handled = true;
break; break;
case AutocompleteAction.CompleteOrNextSelection:
case AutocompleteAction.NextSelection: case AutocompleteAction.NextSelection:
autoComplete.onDownArrow(event); autoComplete.selectNextSelection();
handled = true;
break;
case AutocompleteAction.ApplySelection:
autoComplete.onTab(event);
handled = true; handled = true;
break; break;
case AutocompleteAction.Cancel: case AutocompleteAction.Cancel:
@ -504,8 +502,10 @@ export default class BasicMessageEditor extends React.Component<IProps, IState>
default: default:
return; // don't preventDefault on anything else return; // don't preventDefault on anything else
} }
} else if (autocompleteAction === AutocompleteAction.ApplySelection) { } else if (autocompleteAction === AutocompleteAction.CompleteOrPrevSelection
this.tabCompleteName(event); || autocompleteAction === AutocompleteAction.CompleteOrNextSelection) {
// there is no current autocomplete window, try to open it
this.tabCompleteName();
handled = true; handled = true;
} else if (event.key === Key.BACKSPACE || event.key === Key.DELETE) { } else if (event.key === Key.BACKSPACE || event.key === Key.DELETE) {
this.formatBarRef.current.hide(); this.formatBarRef.current.hide();
@ -517,7 +517,7 @@ export default class BasicMessageEditor extends React.Component<IProps, IState>
} }
}; };
private async tabCompleteName(event: React.KeyboardEvent) { private async tabCompleteName() {
try { try {
await new Promise<void>(resolve => this.setState({showVisualBell: false}, resolve)); await new Promise<void>(resolve => this.setState({showVisualBell: false}, resolve));
const {model} = this.props; const {model} = this.props;
@ -540,7 +540,7 @@ export default class BasicMessageEditor extends React.Component<IProps, IState>
// Don't try to do things with the autocomplete if there is none shown // Don't try to do things with the autocomplete if there is none shown
if (model.autoComplete) { if (model.autoComplete) {
await model.autoComplete.onTab(event); await model.autoComplete.startSelection();
if (!model.autoComplete.hasSelection()) { if (!model.autoComplete.hasSelection()) {
this.setState({showVisualBell: true}); this.setState({showVisualBell: true});
model.autoComplete.close(); model.autoComplete.close();

View File

@ -68,24 +68,24 @@ export default class AutocompleteWrapperModel {
this.updateCallback({close: true}); this.updateCallback({close: true});
} }
public async onTab(e: KeyboardEvent) { /**
* If there is no current autocompletion, start one and move to the first selection.
*/
public async startSelection() {
const acComponent = this.getAutocompleterComponent(); const acComponent = this.getAutocompleterComponent();
if (acComponent.countCompletions() === 0) { if (acComponent.countCompletions() === 0) {
// Force completions to show for the text currently entered // Force completions to show for the text currently entered
await acComponent.forceComplete(); await acComponent.forceComplete();
// Select the first item by moving "down" // Select the first item by moving "down"
await acComponent.moveSelection(+1); await acComponent.moveSelection(+1);
} else {
await acComponent.moveSelection(e.shiftKey ? -1 : +1);
} }
} }
public onUpArrow(e: KeyboardEvent) { public selectPreviousSelection() {
this.getAutocompleterComponent().moveSelection(-1); this.getAutocompleterComponent().moveSelection(-1);
} }
public onDownArrow(e: KeyboardEvent) { public selectNextSelection() {
this.getAutocompleterComponent().moveSelection(+1); this.getAutocompleterComponent().moveSelection(+1);
} }