2017-11-02 21:01:28 +03: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.
|
|
|
|
*/
|
|
|
|
|
2020-05-29 23:42:33 +03:00
|
|
|
import React, {createRef} from 'react';
|
2016-06-21 16:03:39 +03:00
|
|
|
import classNames from 'classnames';
|
2016-07-04 19:26:09 +03:00
|
|
|
import flatMap from 'lodash/flatMap';
|
2020-04-20 21:00:54 +03:00
|
|
|
import {ICompletion, ISelectionRange, IProviderCompletions} from '../../../autocomplete/Autocompleter';
|
|
|
|
import {Room} from 'matrix-js-sdk/src/models/room';
|
2016-06-01 14:24:21 +03:00
|
|
|
|
2017-10-30 01:53:00 +03:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2017-11-02 20:51:08 +03:00
|
|
|
import Autocompleter from '../../../autocomplete/Autocompleter';
|
2020-05-29 23:42:33 +03:00
|
|
|
import { Completion } from '../../../autocomplete/Components';
|
2016-06-01 14:24:21 +03:00
|
|
|
|
2016-09-13 13:11:52 +03:00
|
|
|
const COMPOSER_SELECTED = 0;
|
|
|
|
|
2019-09-30 16:04:39 +03:00
|
|
|
export const generateCompletionDomId = (number) => `mx_Autocomplete_Completion_${number}`;
|
|
|
|
|
2020-04-20 21:00:54 +03:00
|
|
|
interface IProps {
|
2020-04-21 20:08:01 +03:00
|
|
|
// the query string for which to show autocomplete suggestions
|
2020-04-20 21:00:54 +03:00
|
|
|
query: string;
|
2020-04-21 20:08:01 +03:00
|
|
|
// method invoked with range and text content when completion is confirmed
|
2020-04-20 21:00:54 +03:00
|
|
|
onConfirm: (ICompletion) => void;
|
2020-04-21 20:08:01 +03:00
|
|
|
// method invoked when selected (if any) completion changes
|
2020-04-20 21:00:54 +03:00
|
|
|
onSelectionChange?: (ICompletion, number) => void;
|
|
|
|
selection: ISelectionRange;
|
2020-04-21 20:08:01 +03:00
|
|
|
// The room in which we're autocompleting
|
2020-04-20 21:00:54 +03:00
|
|
|
room: Room;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
completions: IProviderCompletions[];
|
|
|
|
completionList: ICompletion[];
|
|
|
|
selectionOffset: number;
|
|
|
|
shouldShowCompletions: boolean;
|
|
|
|
hide: boolean;
|
|
|
|
forceComplete: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Autocomplete extends React.PureComponent<IProps, IState> {
|
|
|
|
autocompleter: Autocompleter;
|
|
|
|
queryRequested: string;
|
|
|
|
debounceCompletionsRequest: NodeJS.Timeout;
|
2020-05-29 23:42:33 +03:00
|
|
|
private containerRef = createRef<HTMLDivElement>();
|
2020-04-20 21:00:54 +03:00
|
|
|
|
2016-06-01 14:24:21 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-07-03 19:45:13 +03:00
|
|
|
|
2017-11-02 20:51:08 +03:00
|
|
|
this.autocompleter = new Autocompleter(props.room);
|
2016-07-03 19:45:13 +03:00
|
|
|
|
2016-06-01 14:24:21 +03:00
|
|
|
this.state = {
|
2016-07-03 19:45:13 +03:00
|
|
|
// list of completionResults, each containing completions
|
2016-06-21 13:16:20 +03:00
|
|
|
completions: [],
|
|
|
|
|
2016-07-03 19:45:13 +03:00
|
|
|
// array of completions, so we can look up current selection by offset quickly
|
|
|
|
completionList: [],
|
|
|
|
|
2016-09-13 13:11:52 +03:00
|
|
|
// how far down the completion list we are (THIS IS 1-INDEXED!)
|
|
|
|
selectionOffset: COMPOSER_SELECTED,
|
|
|
|
|
|
|
|
// whether we should show completions if they're available
|
|
|
|
shouldShowCompletions: true,
|
|
|
|
|
|
|
|
hide: false,
|
|
|
|
|
|
|
|
forceComplete: false,
|
2016-06-01 14:24:21 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-09 15:57:09 +03:00
|
|
|
componentDidMount() {
|
2020-04-21 12:01:05 +03:00
|
|
|
this.applyNewProps();
|
2019-05-09 15:57:09 +03:00
|
|
|
}
|
|
|
|
|
2020-04-21 12:01:05 +03:00
|
|
|
private applyNewProps(oldQuery?: string, oldRoom?: Room) {
|
2019-05-09 15:57:09 +03:00
|
|
|
if (oldRoom && this.props.room.roomId !== oldRoom.roomId) {
|
2017-11-02 20:51:08 +03:00
|
|
|
this.autocompleter.destroy();
|
2019-05-09 15:57:09 +03:00
|
|
|
this.autocompleter = new Autocompleter(this.props.room);
|
2017-11-02 20:51:08 +03:00
|
|
|
}
|
|
|
|
|
2017-06-28 19:27:21 +03:00
|
|
|
// Query hasn't changed so don't try to complete it
|
2019-05-09 15:57:09 +03:00
|
|
|
if (oldQuery === this.props.query) {
|
2017-06-28 19:27:21 +03:00
|
|
|
return;
|
2016-09-13 13:11:52 +03:00
|
|
|
}
|
|
|
|
|
2019-05-09 15:57:09 +03:00
|
|
|
this.complete(this.props.query, this.props.selection);
|
2016-09-13 13:11:52 +03:00
|
|
|
}
|
|
|
|
|
2017-11-02 20:51:08 +03:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.autocompleter.destroy();
|
|
|
|
}
|
|
|
|
|
2020-04-20 22:35:57 +03:00
|
|
|
complete(query: string, selection: ISelectionRange) {
|
2017-07-07 17:30:31 +03:00
|
|
|
this.queryRequested = query;
|
2017-06-28 19:27:21 +03:00
|
|
|
if (this.debounceCompletionsRequest) {
|
|
|
|
clearTimeout(this.debounceCompletionsRequest);
|
|
|
|
}
|
|
|
|
if (query === "") {
|
|
|
|
this.setState({
|
|
|
|
// Clear displayed completions
|
|
|
|
completions: [],
|
|
|
|
completionList: [],
|
|
|
|
// Reset selected completion
|
|
|
|
selectionOffset: COMPOSER_SELECTED,
|
|
|
|
// Hide the autocomplete box
|
|
|
|
hide: true,
|
|
|
|
});
|
2017-07-12 16:02:00 +03:00
|
|
|
return Promise.resolve(null);
|
2017-06-28 19:27:21 +03:00
|
|
|
}
|
2017-10-30 01:53:00 +03:00
|
|
|
let autocompleteDelay = SettingsStore.getValue("autocompleteDelay");
|
2016-09-13 13:11:52 +03:00
|
|
|
|
2017-06-28 19:27:21 +03:00
|
|
|
// Don't debounce if we are already showing completions
|
2017-07-04 19:49:50 +03:00
|
|
|
if (this.state.completions.length > 0 || this.state.forceComplete) {
|
2017-06-28 19:27:21 +03:00
|
|
|
autocompleteDelay = 0;
|
2016-07-02 22:41:34 +03:00
|
|
|
}
|
2016-06-17 02:28:09 +03:00
|
|
|
|
2019-11-12 14:40:38 +03:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
this.debounceCompletionsRequest = setTimeout(() => {
|
|
|
|
resolve(this.processQuery(query, selection));
|
|
|
|
}, autocompleteDelay);
|
|
|
|
});
|
2017-06-28 19:27:21 +03:00
|
|
|
}
|
|
|
|
|
2020-04-20 22:35:57 +03:00
|
|
|
processQuery(query: string, selection: ISelectionRange) {
|
2017-11-02 20:51:08 +03:00
|
|
|
return this.autocompleter.getCompletions(
|
2018-10-12 06:50:18 +03:00
|
|
|
query, selection, this.state.forceComplete,
|
2017-07-07 17:30:31 +03:00
|
|
|
).then((completions) => {
|
|
|
|
// Only ever process the completions for the most recent query being processed
|
|
|
|
if (query !== this.queryRequested) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.processCompletions(completions);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-20 22:35:57 +03:00
|
|
|
processCompletions(completions: IProviderCompletions[]) {
|
2017-02-10 01:10:57 +03:00
|
|
|
const completionList = flatMap(completions, (provider) => provider.completions);
|
2016-09-13 13:11:52 +03:00
|
|
|
|
|
|
|
// Reset selection when completion list becomes empty.
|
|
|
|
let selectionOffset = COMPOSER_SELECTED;
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
const currentSelection = this.state.selectionOffset === 0 ? null :
|
|
|
|
this.state.completionList[this.state.selectionOffset - 1].completion;
|
|
|
|
selectionOffset = completionList.findIndex(
|
2017-02-10 01:10:57 +03:00
|
|
|
(completion) => completion.completion === currentSelection);
|
2016-09-13 13:11:52 +03:00
|
|
|
if (selectionOffset === -1) {
|
|
|
|
selectionOffset = COMPOSER_SELECTED;
|
|
|
|
} else {
|
|
|
|
selectionOffset++; // selectionOffset is 1-indexed!
|
2016-06-12 14:32:46 +03:00
|
|
|
}
|
2016-09-13 13:11:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let hide = this.state.hide;
|
2017-07-07 17:30:31 +03:00
|
|
|
// If `completion.command.command` is truthy, then a provider has matched with the query
|
|
|
|
const anyMatches = completions.some((completion) => !!completion.command.command);
|
|
|
|
hide = !anyMatches;
|
2016-09-13 13:11:52 +03:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
completions,
|
|
|
|
completionList,
|
|
|
|
selectionOffset,
|
|
|
|
hide,
|
2017-06-28 19:27:21 +03:00
|
|
|
// Force complete is turned off each time since we can't edit the query in that case
|
|
|
|
forceComplete: false,
|
2016-06-01 14:24:21 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:00:54 +03:00
|
|
|
hasSelection(): boolean {
|
2019-05-31 16:05:09 +03:00
|
|
|
return this.countCompletions() > 0 && this.state.selectionOffset !== 0;
|
|
|
|
}
|
|
|
|
|
2016-07-02 22:41:34 +03:00
|
|
|
countCompletions(): number {
|
2016-09-13 13:11:52 +03:00
|
|
|
return this.state.completionList.length;
|
2016-07-02 22:41:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// called from MessageComposerInput
|
2020-04-20 22:35:57 +03:00
|
|
|
moveSelection(delta: number) {
|
2016-09-13 13:11:52 +03:00
|
|
|
const completionCount = this.countCompletions();
|
2019-05-14 11:13:04 +03:00
|
|
|
if (completionCount === 0) return; // there are no items to move the selection through
|
2016-06-21 16:03:39 +03:00
|
|
|
|
2019-05-14 11:13:04 +03:00
|
|
|
// Note: selectionOffset 0 represents the unsubstituted text, while 1 means first pill selected
|
|
|
|
const index = (this.state.selectionOffset + delta + completionCount + 1) % (completionCount + 1);
|
|
|
|
this.setSelection(index);
|
2016-09-13 13:11:52 +03:00
|
|
|
}
|
|
|
|
|
2020-04-20 22:35:57 +03:00
|
|
|
onEscape(e: KeyboardEvent): boolean {
|
2016-09-13 13:11:52 +03:00
|
|
|
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 04:58:07 +03:00
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:00:54 +03:00
|
|
|
hide = () => {
|
|
|
|
this.setState({
|
|
|
|
hide: true,
|
|
|
|
selectionOffset: 0,
|
|
|
|
completions: [],
|
|
|
|
completionList: [],
|
|
|
|
});
|
|
|
|
};
|
2016-09-13 13:11:52 +03:00
|
|
|
|
|
|
|
forceComplete() {
|
2019-11-12 14:40:38 +03: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 15:32:33 +03:00
|
|
|
});
|
2016-09-13 13:11:52 +03:00
|
|
|
});
|
2016-06-21 16:03:39 +03:00
|
|
|
}
|
|
|
|
|
2020-04-20 21:00:54 +03:00
|
|
|
onCompletionClicked = (selectionOffset: number): boolean => {
|
2018-07-24 15:48:05 +03:00
|
|
|
if (this.countCompletions() === 0 || selectionOffset === COMPOSER_SELECTED) {
|
2016-07-03 19:45:13 +03:00
|
|
|
return false;
|
2016-07-04 19:14:35 +03:00
|
|
|
}
|
2016-07-03 19:45:13 +03:00
|
|
|
|
2018-07-24 15:48:05 +03:00
|
|
|
this.props.onConfirm(this.state.completionList[selectionOffset - 1]);
|
2016-09-21 05:10:48 +03:00
|
|
|
this.hide();
|
2016-07-03 19:45:13 +03:00
|
|
|
|
|
|
|
return true;
|
2020-04-20 21:00:54 +03:00
|
|
|
};
|
2016-07-03 19:45:13 +03:00
|
|
|
|
|
|
|
setSelection(selectionOffset: number) {
|
2017-02-10 01:10:57 +03:00
|
|
|
this.setState({selectionOffset, hide: false});
|
2017-08-23 18:22:14 +03:00
|
|
|
if (this.props.onSelectionChange) {
|
2019-09-30 16:04:39 +03:00
|
|
|
this.props.onSelectionChange(this.state.completionList[selectionOffset - 1], selectionOffset - 1);
|
2017-08-23 18:22:14 +03:00
|
|
|
}
|
2016-07-03 19:45:13 +03:00
|
|
|
}
|
|
|
|
|
2020-04-20 22:35:57 +03:00
|
|
|
componentDidUpdate(prevProps: IProps) {
|
2020-04-21 12:01:05 +03:00
|
|
|
this.applyNewProps(prevProps.query, prevProps.room);
|
2016-08-17 14:57:19 +03:00
|
|
|
// this is the selected completion, so scroll it into view if needed
|
2020-05-29 23:42:33 +03:00
|
|
|
const selectedCompletion = this.refs[`completion${this.state.selectionOffset}`] as Completion<any>;
|
|
|
|
|
|
|
|
if (selectedCompletion && selectedCompletion.nodeRef.current) {
|
|
|
|
selectedCompletion.nodeRef.current.scrollIntoView({
|
|
|
|
behavior: "auto",
|
|
|
|
block: "nearest",
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.containerRef.current.scrollTo({ top: 0 });
|
2016-08-17 14:57:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 14:24:21 +03:00
|
|
|
render() {
|
2016-09-13 13:11:52 +03:00
|
|
|
let position = 1;
|
2017-02-10 01:10:57 +03:00
|
|
|
const renderedCompletions = this.state.completions.map((completionResult, i) => {
|
2020-04-20 21:04:55 +03:00
|
|
|
const completions = completionResult.completions.map((completion, j) => {
|
2019-09-30 16:04:39 +03:00
|
|
|
const selected = position === this.state.selectionOffset;
|
|
|
|
const className = classNames('mx_Autocomplete_Completion', {selected});
|
2017-02-10 01:10:57 +03:00
|
|
|
const componentPosition = position;
|
2016-06-21 16:03:39 +03:00
|
|
|
position++;
|
2016-07-02 22:41:34 +03:00
|
|
|
|
2017-02-10 01:10:57 +03:00
|
|
|
const onClick = () => {
|
2018-07-24 15:48:05 +03:00
|
|
|
this.onCompletionClicked(componentPosition);
|
2016-07-04 19:14:35 +03:00
|
|
|
};
|
2016-07-03 19:45:13 +03:00
|
|
|
|
2016-08-17 14:57:19 +03:00
|
|
|
return React.cloneElement(completion.component, {
|
2020-04-20 21:04:55 +03:00
|
|
|
"key": j,
|
2019-09-30 16:04:39 +03:00
|
|
|
"ref": `completion${componentPosition}`,
|
|
|
|
"id": generateCompletionDomId(componentPosition - 1), // 0 index the completion IDs
|
2016-08-17 14:57:19 +03:00
|
|
|
className,
|
|
|
|
onClick,
|
2019-09-30 16:04:39 +03:00
|
|
|
"aria-selected": selected,
|
2016-08-17 14:57:19 +03:00
|
|
|
});
|
2016-06-12 14:32:46 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return completions.length > 0 ? (
|
2016-06-17 02:28:09 +03:00
|
|
|
<div key={i} className="mx_Autocomplete_ProviderSection">
|
2019-05-19 17:23:43 +03:00
|
|
|
<div className="mx_Autocomplete_provider_name">{ completionResult.provider.getName() }</div>
|
2017-10-11 19:56:17 +03:00
|
|
|
{ completionResult.provider.renderCompletions(completions) }
|
2016-06-01 14:24:21 +03:00
|
|
|
</div>
|
2016-06-12 14:32:46 +03:00
|
|
|
) : null;
|
2017-02-10 01:10:57 +03:00
|
|
|
}).filter((completion) => !!completion);
|
2016-06-01 14:24:21 +03:00
|
|
|
|
2016-09-13 13:11:52 +03:00
|
|
|
return !this.state.hide && renderedCompletions.length > 0 ? (
|
2020-04-20 21:02:27 +03:00
|
|
|
<div className="mx_Autocomplete" ref={this.containerRef}>
|
2017-10-11 19:56:17 +03:00
|
|
|
{ renderedCompletions }
|
2016-06-01 14:24:21 +03:00
|
|
|
</div>
|
2016-09-07 22:06:23 +03:00
|
|
|
) : null;
|
2016-06-01 14:24:21 +03:00
|
|
|
}
|
|
|
|
}
|