2016-06-01 14:24:21 +03:00
|
|
|
import React from 'react';
|
2016-08-17 14:57:19 +03:00
|
|
|
import ReactDOM from 'react-dom';
|
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';
|
2016-09-13 13:11:52 +03:00
|
|
|
import isEqual from 'lodash/isEqual';
|
2016-08-17 14:57:19 +03:00
|
|
|
import sdk from '../../../index';
|
2016-09-13 13:11:52 +03:00
|
|
|
import type {Completion, SelectionRange} from '../../../autocomplete/Autocompleter';
|
2016-09-13 15:32:33 +03:00
|
|
|
import Q from 'q';
|
2016-06-01 14:24:21 +03:00
|
|
|
|
|
|
|
import {getCompletions} from '../../../autocomplete/Autocompleter';
|
|
|
|
|
2016-09-13 13:11:52 +03:00
|
|
|
const COMPOSER_SELECTED = 0;
|
|
|
|
|
2016-06-01 14:24:21 +03:00
|
|
|
export default class Autocomplete extends React.Component {
|
2016-09-13 13:11:52 +03:00
|
|
|
|
2016-06-01 14:24:21 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-07-03 19:45:13 +03:00
|
|
|
|
2016-09-13 14:16:20 +03:00
|
|
|
this.completionPromise = null;
|
2016-07-03 19:45:13 +03:00
|
|
|
this.onConfirm = this.onConfirm.bind(this);
|
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-13 13:11:52 +03:00
|
|
|
async componentWillReceiveProps(props, state) {
|
2016-07-02 22:41:34 +03:00
|
|
|
if (props.query === this.props.query) {
|
2016-09-13 13:11:52 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return await this.complete(props.query, props.selection);
|
|
|
|
}
|
|
|
|
|
|
|
|
async complete(query, selection) {
|
|
|
|
let forceComplete = this.state.forceComplete;
|
|
|
|
const completionPromise = getCompletions(query, selection, forceComplete);
|
|
|
|
this.completionPromise = completionPromise;
|
|
|
|
const completions = await this.completionPromise;
|
|
|
|
|
|
|
|
// There's a newer completion request, so ignore results.
|
|
|
|
if (completionPromise !== this.completionPromise) {
|
2016-07-02 22:41:34 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-06-17 02:28:09 +03:00
|
|
|
|
2016-09-13 13:11:52 +03:00
|
|
|
const completionList = flatMap(completions, provider => provider.completions);
|
|
|
|
|
|
|
|
// 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(
|
|
|
|
completion => completion.completion === currentSelection);
|
|
|
|
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
|
|
|
} else {
|
|
|
|
// If no completions were returned, we should turn off force completion.
|
|
|
|
forceComplete = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let hide = this.state.hide;
|
|
|
|
// These are lists of booleans that indicate whether whether the corresponding provider had a matching pattern
|
|
|
|
const oldMatches = this.state.completions.map(completion => !!completion.command.command),
|
|
|
|
newMatches = completions.map(completion => !!completion.command.command);
|
|
|
|
|
|
|
|
// So, essentially, we re-show autocomplete if any provider finds a new pattern or stops finding an old one
|
|
|
|
if (!isEqual(oldMatches, newMatches)) {
|
|
|
|
hide = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
completions,
|
|
|
|
completionList,
|
|
|
|
selectionOffset,
|
|
|
|
hide,
|
|
|
|
forceComplete,
|
2016-06-01 14:24:21 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
2016-09-13 13:11:52 +03:00
|
|
|
onUpArrow(): ?Completion {
|
|
|
|
const completionCount = this.countCompletions();
|
|
|
|
// completionCount + 1, since 0 means composer is selected
|
|
|
|
const selectionOffset = (completionCount + 1 + this.state.selectionOffset - 1)
|
|
|
|
% (completionCount + 1);
|
2016-08-03 15:34:52 +03:00
|
|
|
if (!completionCount) {
|
2016-09-13 13:11:52 +03:00
|
|
|
return null;
|
2016-08-03 15:34:52 +03:00
|
|
|
}
|
2016-07-03 19:45:13 +03:00
|
|
|
this.setSelection(selectionOffset);
|
2016-09-13 13:11:52 +03:00
|
|
|
return selectionOffset === COMPOSER_SELECTED ? null : this.state.completionList[selectionOffset - 1];
|
2016-06-21 16:03:39 +03:00
|
|
|
}
|
|
|
|
|
2016-07-02 22:41:34 +03:00
|
|
|
// called from MessageComposerInput
|
2016-09-13 13:11:52 +03:00
|
|
|
onDownArrow(): ?Completion {
|
|
|
|
const completionCount = this.countCompletions();
|
|
|
|
// completionCount + 1, since 0 means composer is selected
|
|
|
|
const selectionOffset = (this.state.selectionOffset + 1) % (completionCount + 1);
|
2016-08-03 15:34:52 +03:00
|
|
|
if (!completionCount) {
|
2016-09-13 13:11:52 +03:00
|
|
|
return null;
|
2016-08-03 15:34:52 +03:00
|
|
|
}
|
2016-07-03 19:45:13 +03:00
|
|
|
this.setSelection(selectionOffset);
|
2016-09-13 13:11:52 +03:00
|
|
|
return selectionOffset === COMPOSER_SELECTED ? null : this.state.completionList[selectionOffset - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
onEscape(e): boolean {
|
|
|
|
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
|
|
|
|
this.setState({hide: true, selectionOffset: 0});
|
|
|
|
}
|
|
|
|
|
|
|
|
forceComplete() {
|
2016-09-13 15:32:33 +03:00
|
|
|
const done = Q.defer();
|
2016-09-13 13:11:52 +03:00
|
|
|
this.setState({
|
|
|
|
forceComplete: true,
|
|
|
|
}, () => {
|
2016-09-13 15:32:33 +03:00
|
|
|
this.complete(this.props.query, this.props.selection).then(() => {
|
|
|
|
done.resolve();
|
|
|
|
});
|
2016-09-13 13:11:52 +03:00
|
|
|
});
|
2016-09-13 15:32:33 +03:00
|
|
|
return done.promise;
|
2016-06-21 16:03:39 +03:00
|
|
|
}
|
|
|
|
|
2016-07-03 19:45:13 +03:00
|
|
|
/** called from MessageComposerInput
|
|
|
|
* @returns {boolean} whether confirmation was handled
|
|
|
|
*/
|
|
|
|
onConfirm(): boolean {
|
2016-09-13 13:11:52 +03:00
|
|
|
if (this.countCompletions() === 0 || this.state.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
|
|
|
|
2016-09-13 13:11:52 +03:00
|
|
|
let selectedCompletion = this.state.completionList[this.state.selectionOffset - 1];
|
2016-07-03 19:45:13 +03:00
|
|
|
this.props.onConfirm(selectedCompletion.range, selectedCompletion.completion);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
setSelection(selectionOffset: number) {
|
|
|
|
this.setState({selectionOffset});
|
|
|
|
}
|
|
|
|
|
2016-08-17 14:57:19 +03:00
|
|
|
componentDidUpdate() {
|
|
|
|
// this is the selected completion, so scroll it into view if needed
|
|
|
|
const selectedCompletion = this.refs[`completion${this.state.selectionOffset}`];
|
|
|
|
if (selectedCompletion && this.container) {
|
2016-08-22 22:06:31 +03:00
|
|
|
const domNode = ReactDOM.findDOMNode(selectedCompletion);
|
|
|
|
const offsetTop = domNode && domNode.offsetTop;
|
2016-08-17 14:57:19 +03:00
|
|
|
if (offsetTop > this.container.scrollTop + this.container.offsetHeight ||
|
|
|
|
offsetTop < this.container.scrollTop) {
|
|
|
|
this.container.scrollTop = offsetTop - this.container.offsetTop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 14:24:21 +03:00
|
|
|
render() {
|
2016-08-17 14:57:19 +03:00
|
|
|
const EmojiText = sdk.getComponent('views.elements.EmojiText');
|
|
|
|
|
2016-09-13 13:11:52 +03:00
|
|
|
let position = 1;
|
2016-06-21 13:16:20 +03:00
|
|
|
let renderedCompletions = this.state.completions.map((completionResult, i) => {
|
2016-06-12 14:32:46 +03:00
|
|
|
let completions = completionResult.completions.map((completion, i) => {
|
2016-08-17 14:57:19 +03:00
|
|
|
|
|
|
|
const className = classNames('mx_Autocomplete_Completion', {
|
2016-07-02 22:41:34 +03:00
|
|
|
'selected': position === this.state.selectionOffset,
|
2016-06-21 16:03:39 +03:00
|
|
|
});
|
|
|
|
let componentPosition = position;
|
|
|
|
position++;
|
2016-07-02 22:41:34 +03:00
|
|
|
|
2016-07-04 19:14:35 +03:00
|
|
|
let onMouseOver = () => this.setSelection(componentPosition);
|
|
|
|
let onClick = () => {
|
|
|
|
this.setSelection(componentPosition);
|
|
|
|
this.onConfirm();
|
|
|
|
};
|
2016-07-03 19:45:13 +03:00
|
|
|
|
2016-08-17 14:57:19 +03:00
|
|
|
return React.cloneElement(completion.component, {
|
|
|
|
key: i,
|
2016-09-13 13:11:52 +03:00
|
|
|
ref: `completion${position - 1}`,
|
2016-08-17 14:57:19 +03:00
|
|
|
className,
|
|
|
|
onMouseOver,
|
|
|
|
onClick,
|
|
|
|
});
|
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">
|
2016-08-17 14:57:19 +03:00
|
|
|
<EmojiText element="div" className="mx_Autocomplete_provider_name">{completionResult.provider.getName()}</EmojiText>
|
|
|
|
{completionResult.provider.renderCompletions(completions)}
|
2016-06-01 14:24:21 +03:00
|
|
|
</div>
|
2016-06-12 14:32:46 +03:00
|
|
|
) : null;
|
2016-09-07 22:06:23 +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 ? (
|
2016-08-17 14:57:19 +03:00
|
|
|
<div className="mx_Autocomplete" ref={(e) => this.container = e}>
|
|
|
|
{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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Autocomplete.propTypes = {
|
|
|
|
// the query string for which to show autocomplete suggestions
|
2016-07-02 22:41:34 +03:00
|
|
|
query: React.PropTypes.string.isRequired,
|
2016-07-04 19:14:35 +03:00
|
|
|
|
|
|
|
// method invoked with range and text content when completion is confirmed
|
|
|
|
onConfirm: React.PropTypes.func.isRequired,
|
2016-06-01 14:24:21 +03:00
|
|
|
};
|