2017-11-02 18:01:28 +00:00
|
|
|
/*
|
|
|
|
|
Copyright 2016 Aviral Dasgupta
|
|
|
|
|
Copyright 2017 New Vector Ltd
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-06-29 13:11:58 +01:00
|
|
|
import React, { createRef, KeyboardEvent } from 'react';
|
2016-06-21 18:33:39 +05:30
|
|
|
import classNames from 'classnames';
|
2021-06-29 13:11:58 +01:00
|
|
|
import { flatMap } from "lodash";
|
|
|
|
|
import { ICompletion, ISelectionRange, IProviderCompletions } from '../../../autocomplete/Autocompleter';
|
|
|
|
|
import { Room } from 'matrix-js-sdk/src/models/room';
|
2016-06-01 16:54:21 +05:30
|
|
|
|
2017-10-29 16:53:00 -06:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2017-11-02 17:51:08 +00:00
|
|
|
import Autocompleter from '../../../autocomplete/Autocompleter';
|
2021-06-29 13:11:58 +01:00
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
2016-06-01 16:54:21 +05:30
|
|
|
|
2021-05-12 12:18:56 +01:00
|
|
|
const MAX_PROVIDER_MATCHES = 20;
|
2016-09-13 15:41:52 +05:30
|
|
|
|
2019-09-30 14:04:39 +01:00
|
|
|
export const generateCompletionDomId = (number) => `mx_Autocomplete_Completion_${number}`;
|
|
|
|
|
|
2020-04-20 19:00:54 +01:00
|
|
|
interface IProps {
|
2020-04-21 18:08:01 +01:00
|
|
|
// the query string for which to show autocomplete suggestions
|
2020-04-20 19:00:54 +01:00
|
|
|
query: string;
|
2020-04-21 18:08:01 +01:00
|
|
|
// method invoked with range and text content when completion is confirmed
|
2021-05-20 22:25:19 +01:00
|
|
|
onConfirm: (completion: ICompletion) => void;
|
2020-04-21 18:08:01 +01:00
|
|
|
// method invoked when selected (if any) completion changes
|
2021-05-20 22:25:19 +01:00
|
|
|
onSelectionChange?: (partIndex: number) => void;
|
2020-04-20 19:00:54 +01:00
|
|
|
selection: ISelectionRange;
|
2020-04-21 18:08:01 +01:00
|
|
|
// The room in which we're autocompleting
|
2020-04-20 19:00:54 +01:00
|
|
|
room: Room;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
|
completions: IProviderCompletions[];
|
|
|
|
|
completionList: ICompletion[];
|
|
|
|
|
selectionOffset: number;
|
|
|
|
|
shouldShowCompletions: boolean;
|
|
|
|
|
hide: boolean;
|
|
|
|
|
forceComplete: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 20:12:00 -07:00
|
|
|
@replaceableComponent("views.rooms.Autocomplete")
|
2020-04-20 19:00:54 +01:00
|
|
|
export default class Autocomplete extends React.PureComponent<IProps, IState> {
|
|
|
|
|
autocompleter: Autocompleter;
|
|
|
|
|
queryRequested: string;
|
2021-07-12 09:02:46 +01:00
|
|
|
debounceCompletionsRequest: number;
|
2020-05-29 21:42:33 +01:00
|
|
|
private containerRef = createRef<HTMLDivElement>();
|
2020-04-20 19:00:54 +01:00
|
|
|
|
2016-06-01 16:54:21 +05:30
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
2016-07-03 22:15:13 +05:30
|
|
|
|
2017-11-02 17:51:08 +00:00
|
|
|
this.autocompleter = new Autocompleter(props.room);
|
2016-07-03 22:15:13 +05:30
|
|
|
|
2016-06-01 16:54:21 +05:30
|
|
|
this.state = {
|
2016-07-03 22:15:13 +05:30
|
|
|
// list of completionResults, each containing completions
|
2016-06-21 15:46:20 +05:30
|
|
|
completions: [],
|
|
|
|
|
|
2016-07-03 22:15:13 +05:30
|
|
|
// array of completions, so we can look up current selection by offset quickly
|
|
|
|
|
completionList: [],
|
|
|
|
|
|
2016-09-13 15:41:52 +05:30
|
|
|
// how far down the completion list we are (THIS IS 1-INDEXED!)
|
2021-02-16 17:56:09 +00:00
|
|
|
selectionOffset: 1,
|
2016-09-13 15:41:52 +05:30
|
|
|
|
|
|
|
|
// whether we should show completions if they're available
|
|
|
|
|
shouldShowCompletions: true,
|
|
|
|
|
|
|
|
|
|
hide: false,
|
|
|
|
|
|
|
|
|
|
forceComplete: false,
|
2016-06-01 16:54:21 +05:30
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 14:57:09 +02:00
|
|
|
componentDidMount() {
|
2020-04-21 10:01:05 +01:00
|
|
|
this.applyNewProps();
|
2019-05-09 14:57:09 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-15 10:09:24 +01:00
|
|
|
private applyNewProps(oldQuery?: string, oldRoom?: Room): void {
|
2019-05-09 14:57:09 +02:00
|
|
|
if (oldRoom && this.props.room.roomId !== oldRoom.roomId) {
|
2017-11-02 17:51:08 +00:00
|
|
|
this.autocompleter.destroy();
|
2019-05-09 14:57:09 +02:00
|
|
|
this.autocompleter = new Autocompleter(this.props.room);
|
2017-11-02 17:51:08 +00:00
|
|
|
}
|
|
|
|
|
|
2017-06-28 17:27:21 +01:00
|
|
|
// Query hasn't changed so don't try to complete it
|
2019-05-09 14:57:09 +02:00
|
|
|
if (oldQuery === this.props.query) {
|
2016-07-03 01:11:34 +05:30
|
|
|
return;
|
|
|
|
|
}
|
2016-06-17 04:58:09 +05:30
|
|
|
|
2019-05-09 14:57:09 +02:00
|
|
|
this.complete(this.props.query, this.props.selection);
|
2017-06-28 17:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
2017-11-02 17:51:08 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
|
this.autocompleter.destroy();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 10:09:24 +01:00
|
|
|
private complete(query: string, selection: ISelectionRange): Promise<void> {
|
2017-07-07 15:30:31 +01:00
|
|
|
this.queryRequested = query;
|
2017-06-28 17:27:21 +01:00
|
|
|
if (this.debounceCompletionsRequest) {
|
|
|
|
|
clearTimeout(this.debounceCompletionsRequest);
|
|
|
|
|
}
|
|
|
|
|
if (query === "") {
|
|
|
|
|
this.setState({
|
|
|
|
|
// Clear displayed completions
|
|
|
|
|
completions: [],
|
|
|
|
|
completionList: [],
|
|
|
|
|
// Reset selected completion
|
2021-02-16 17:56:09 +00:00
|
|
|
selectionOffset: 1,
|
2017-06-28 17:27:21 +01:00
|
|
|
// Hide the autocomplete box
|
|
|
|
|
hide: true,
|
|
|
|
|
});
|
2017-07-12 14:02:00 +01:00
|
|
|
return Promise.resolve(null);
|
2017-06-28 17:27:21 +01:00
|
|
|
}
|
2017-10-29 16:53:00 -06:00
|
|
|
let autocompleteDelay = SettingsStore.getValue("autocompleteDelay");
|
2017-06-28 17:27:21 +01:00
|
|
|
|
|
|
|
|
// Don't debounce if we are already showing completions
|
2017-07-04 17:49:50 +01:00
|
|
|
if (this.state.completions.length > 0 || this.state.forceComplete) {
|
2017-06-28 17:27:21 +01:00
|
|
|
autocompleteDelay = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-12 11:40:38 +00:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
this.debounceCompletionsRequest = setTimeout(() => {
|
|
|
|
|
resolve(this.processQuery(query, selection));
|
|
|
|
|
}, autocompleteDelay);
|
|
|
|
|
});
|
2017-06-28 17:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-15 10:09:24 +01:00
|
|
|
private processQuery(query: string, selection: ISelectionRange): Promise<void> {
|
2017-11-02 17:51:08 +00:00
|
|
|
return this.autocompleter.getCompletions(
|
2021-05-12 12:18:56 +01:00
|
|
|
query, selection, this.state.forceComplete, MAX_PROVIDER_MATCHES,
|
2017-07-07 15:30:31 +01:00
|
|
|
).then((completions) => {
|
|
|
|
|
// Only ever process the completions for the most recent query being processed
|
|
|
|
|
if (query !== this.queryRequested) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.processCompletions(completions);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 10:09:24 +01:00
|
|
|
private processCompletions(completions: IProviderCompletions[]): void {
|
2017-02-10 03:40:57 +05:30
|
|
|
const completionList = flatMap(completions, (provider) => provider.completions);
|
2016-06-12 17:02:46 +05:30
|
|
|
|
2016-09-13 15:41:52 +05:30
|
|
|
// Reset selection when completion list becomes empty.
|
2021-02-16 17:56:09 +00:00
|
|
|
let selectionOffset = 1;
|
2016-09-13 15:41:52 +05:30
|
|
|
if (completionList.length > 0) {
|
|
|
|
|
/* If the currently selected completion is still in the completion list,
|
|
|
|
|
try to find it and jump to it. If not, select composer.
|
|
|
|
|
*/
|
2021-02-16 17:56:09 +00:00
|
|
|
const currentSelection = this.state.selectionOffset <= 1 ? null :
|
2016-09-13 15:41:52 +05:30
|
|
|
this.state.completionList[this.state.selectionOffset - 1].completion;
|
|
|
|
|
selectionOffset = completionList.findIndex(
|
2017-02-10 03:40:57 +05:30
|
|
|
(completion) => completion.completion === currentSelection);
|
2016-09-13 15:41:52 +05:30
|
|
|
if (selectionOffset === -1) {
|
2021-02-16 17:56:09 +00:00
|
|
|
selectionOffset = 1;
|
2016-09-13 15:41:52 +05:30
|
|
|
} else {
|
|
|
|
|
selectionOffset++; // selectionOffset is 1-indexed!
|
2016-06-12 17:02:46 +05:30
|
|
|
}
|
2016-09-13 15:41:52 +05:30
|
|
|
}
|
|
|
|
|
|
2021-02-16 17:56:09 +00:00
|
|
|
let hide = true;
|
2017-07-07 15:30:31 +01:00
|
|
|
// If `completion.command.command` is truthy, then a provider has matched with the query
|
|
|
|
|
const anyMatches = completions.some((completion) => !!completion.command.command);
|
2021-02-16 17:56:09 +00:00
|
|
|
if (anyMatches) {
|
|
|
|
|
hide = false;
|
|
|
|
|
if (this.props.onSelectionChange) {
|
2021-05-20 22:25:19 +01:00
|
|
|
this.props.onSelectionChange(selectionOffset - 1);
|
2021-02-16 17:56:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-09-13 15:41:52 +05:30
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
|
completions,
|
|
|
|
|
completionList,
|
|
|
|
|
selectionOffset,
|
|
|
|
|
hide,
|
2017-06-28 17:27:21 +01:00
|
|
|
// Force complete is turned off each time since we can't edit the query in that case
|
|
|
|
|
forceComplete: false,
|
2016-06-01 16:54:21 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 10:09:24 +01:00
|
|
|
public hasSelection(): boolean {
|
2019-05-31 15:05:09 +02:00
|
|
|
return this.countCompletions() > 0 && this.state.selectionOffset !== 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 10:09:24 +01:00
|
|
|
public countCompletions(): number {
|
2016-09-13 15:41:52 +05:30
|
|
|
return this.state.completionList.length;
|
2016-07-03 01:11:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// called from MessageComposerInput
|
2021-07-15 10:09:24 +01:00
|
|
|
public moveSelection(delta: number): void {
|
2016-09-13 15:41:52 +05:30
|
|
|
const completionCount = this.countCompletions();
|
2019-05-14 10:13:04 +02:00
|
|
|
if (completionCount === 0) return; // there are no items to move the selection through
|
2016-06-21 18:33:39 +05:30
|
|
|
|
2019-05-14 10:13:04 +02:00
|
|
|
// Note: selectionOffset 0 represents the unsubstituted text, while 1 means first pill selected
|
2021-02-16 17:56:09 +00:00
|
|
|
const index = (this.state.selectionOffset + delta + completionCount - 1) % completionCount;
|
|
|
|
|
this.setSelection(1 + index);
|
2016-09-13 15:41:52 +05:30
|
|
|
}
|
|
|
|
|
|
2021-07-15 10:09:24 +01:00
|
|
|
public onEscape(e: KeyboardEvent): boolean {
|
2016-09-13 15:41:52 +05:30
|
|
|
const completionCount = this.countCompletions();
|
|
|
|
|
if (completionCount === 0) {
|
|
|
|
|
// autocomplete is already empty, so don't preventDefault
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
// selectionOffset = 0, so we don't end up completing when autocomplete is hidden
|
2016-09-21 07:28:07 +05:30
|
|
|
this.hide();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 10:09:24 +01:00
|
|
|
private hide = (): void => {
|
2020-04-20 19:00:54 +01:00
|
|
|
this.setState({
|
|
|
|
|
hide: true,
|
2021-02-16 17:56:09 +00:00
|
|
|
selectionOffset: 1,
|
2020-04-20 19:00:54 +01:00
|
|
|
completions: [],
|
|
|
|
|
completionList: [],
|
|
|
|
|
});
|
|
|
|
|
};
|
2016-09-13 15:41:52 +05:30
|
|
|
|
2021-07-15 10:09:24 +01:00
|
|
|
public forceComplete(): Promise<number> {
|
2019-11-12 11:40:38 +00:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
this.setState({
|
|
|
|
|
forceComplete: true,
|
|
|
|
|
hide: false,
|
|
|
|
|
}, () => {
|
|
|
|
|
this.complete(this.props.query, this.props.selection).then(() => {
|
|
|
|
|
resolve(this.countCompletions());
|
|
|
|
|
});
|
2016-09-13 18:02:33 +05:30
|
|
|
});
|
2016-09-13 15:41:52 +05:30
|
|
|
});
|
2016-06-21 18:33:39 +05:30
|
|
|
}
|
|
|
|
|
|
2021-07-15 10:09:24 +01:00
|
|
|
public onConfirmCompletion = (): void => {
|
2021-02-16 17:56:09 +00:00
|
|
|
this.onCompletionClicked(this.state.selectionOffset);
|
2021-07-15 10:09:24 +01:00
|
|
|
};
|
2021-02-16 17:56:09 +00:00
|
|
|
|
2021-07-15 10:09:24 +01:00
|
|
|
private onCompletionClicked = (selectionOffset: number): boolean => {
|
2021-02-16 17:56:09 +00:00
|
|
|
const count = this.countCompletions();
|
|
|
|
|
if (count === 0 || selectionOffset < 1 || selectionOffset > count) {
|
2016-07-03 22:15:13 +05:30
|
|
|
return false;
|
2016-07-04 21:44:35 +05:30
|
|
|
}
|
2016-07-03 22:15:13 +05:30
|
|
|
|
2018-07-24 13:48:05 +01:00
|
|
|
this.props.onConfirm(this.state.completionList[selectionOffset - 1]);
|
2016-09-21 07:40:48 +05:30
|
|
|
this.hide();
|
2016-07-03 22:15:13 +05:30
|
|
|
|
|
|
|
|
return true;
|
2020-04-20 19:00:54 +01:00
|
|
|
};
|
2016-07-03 22:15:13 +05:30
|
|
|
|
2021-07-15 10:09:24 +01:00
|
|
|
private setSelection(selectionOffset: number): void {
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ selectionOffset, hide: false });
|
2017-08-23 16:22:14 +01:00
|
|
|
if (this.props.onSelectionChange) {
|
2021-05-20 22:25:19 +01:00
|
|
|
this.props.onSelectionChange(selectionOffset - 1);
|
2017-08-23 16:22:14 +01:00
|
|
|
}
|
2016-07-03 22:15:13 +05:30
|
|
|
}
|
|
|
|
|
|
2020-04-20 20:35:57 +01:00
|
|
|
componentDidUpdate(prevProps: IProps) {
|
2020-04-21 10:01:05 +01:00
|
|
|
this.applyNewProps(prevProps.query, prevProps.room);
|
2016-08-17 17:27:19 +05:30
|
|
|
// this is the selected completion, so scroll it into view if needed
|
2020-05-30 13:30:59 +01:00
|
|
|
const selectedCompletion = this.refs[`completion${this.state.selectionOffset}`] as HTMLElement;
|
2020-05-29 21:42:33 +01:00
|
|
|
|
2020-05-30 13:30:59 +01:00
|
|
|
if (selectedCompletion) {
|
|
|
|
|
selectedCompletion.scrollIntoView({
|
2020-05-29 21:42:33 +01:00
|
|
|
behavior: "auto",
|
|
|
|
|
block: "nearest",
|
|
|
|
|
});
|
2020-06-03 10:39:28 +01:00
|
|
|
} else if (this.containerRef.current) {
|
2020-05-29 21:42:33 +01:00
|
|
|
this.containerRef.current.scrollTo({ top: 0 });
|
2016-08-17 17:27:19 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 16:54:21 +05:30
|
|
|
render() {
|
2016-09-13 15:41:52 +05:30
|
|
|
let position = 1;
|
2017-02-10 03:40:57 +05:30
|
|
|
const renderedCompletions = this.state.completions.map((completionResult, i) => {
|
2020-04-20 19:04:55 +01:00
|
|
|
const completions = completionResult.completions.map((completion, j) => {
|
2019-09-30 14:04:39 +01:00
|
|
|
const selected = position === this.state.selectionOffset;
|
2021-06-29 13:11:58 +01:00
|
|
|
const className = classNames('mx_Autocomplete_Completion', { selected });
|
2017-02-10 03:40:57 +05:30
|
|
|
const componentPosition = position;
|
2016-06-21 18:33:39 +05:30
|
|
|
position++;
|
2016-07-03 01:11:34 +05:30
|
|
|
|
2017-02-10 03:40:57 +05:30
|
|
|
const onClick = () => {
|
2018-07-24 13:48:05 +01:00
|
|
|
this.onCompletionClicked(componentPosition);
|
2016-07-04 21:44:35 +05:30
|
|
|
};
|
2016-07-03 22:15:13 +05:30
|
|
|
|
2016-08-17 17:27:19 +05:30
|
|
|
return React.cloneElement(completion.component, {
|
2020-04-20 19:04:55 +01:00
|
|
|
"key": j,
|
2019-09-30 14:04:39 +01:00
|
|
|
"ref": `completion${componentPosition}`,
|
|
|
|
|
"id": generateCompletionDomId(componentPosition - 1), // 0 index the completion IDs
|
2016-08-17 17:27:19 +05:30
|
|
|
className,
|
|
|
|
|
onClick,
|
2019-09-30 14:04:39 +01:00
|
|
|
"aria-selected": selected,
|
2016-08-17 17:27:19 +05:30
|
|
|
});
|
2016-06-12 17:02:46 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return completions.length > 0 ? (
|
2021-02-18 10:55:24 +00:00
|
|
|
<div key={i} className="mx_Autocomplete_ProviderSection" role="presentation">
|
2019-05-19 15:23:43 +01:00
|
|
|
<div className="mx_Autocomplete_provider_name">{ completionResult.provider.getName() }</div>
|
2017-10-11 17:56:17 +01:00
|
|
|
{ completionResult.provider.renderCompletions(completions) }
|
2016-06-01 16:54:21 +05:30
|
|
|
</div>
|
2016-06-12 17:02:46 +05:30
|
|
|
) : null;
|
2017-02-10 03:40:57 +05:30
|
|
|
}).filter((completion) => !!completion);
|
2016-06-01 16:54:21 +05:30
|
|
|
|
2016-09-13 15:41:52 +05:30
|
|
|
return !this.state.hide && renderedCompletions.length > 0 ? (
|
2021-02-18 10:55:24 +00:00
|
|
|
<div id="mx_Autocomplete" className="mx_Autocomplete" ref={this.containerRef} role="listbox">
|
2017-10-11 17:56:17 +01:00
|
|
|
{ renderedCompletions }
|
2016-06-01 16:54:21 +05:30
|
|
|
</div>
|
2016-09-08 00:36:23 +05:30
|
|
|
) : null;
|
2016-06-01 16:54:21 +05:30
|
|
|
}
|
|
|
|
|
}
|