Commit a57d7814 authored by Harrison Healey's avatar Harrison Healey Committed by Joram Wilander

PLT-8306 Removed completePending/suggestionsPending flags from SuggestionStore (#537)

* PLT-8306 Removed code to wait for results while autocompleting

* Reorganized some code in the SuggestionBox
parent c3f94084
......@@ -275,12 +275,11 @@ export function emitSelectPreviousSuggestion(suggestionId) {
});
}
export function emitCompleteWordSuggestion(suggestionId, term = '', override = false) {
export function emitCompleteWordSuggestion(suggestionId, term = '') {
AppDispatcher.handleViewAction({
type: Constants.ActionTypes.SUGGESTION_COMPLETE_WORD,
id: suggestionId,
term,
override
term
});
}
......
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import SuggestionStore from 'stores/suggestion_store.jsx';
export default class Provider {
constructor() {
this.latestPrefix = '';
......@@ -17,9 +15,6 @@ export default class Provider {
startNewRequest(suggestionId, prefix) {
this.latestPrefix = prefix;
this.latestComplete = false;
// Don't use the dispatcher here since this is only called while handling an event
SuggestionStore.setSuggestionsPending(suggestionId, true);
}
shouldCancelDispatch(prefix) {
......
......@@ -242,10 +242,10 @@ export default class SuggestionBox extends React.Component {
insertText = ' ' + insertText;
}
this.handleCompleteWord(insertText, '', false);
this.addTextAtCaret(insertText, '');
}
handleCompleteWord(term, matchedPretext, shouldEmitWordSuggestion = true) {
addTextAtCaret(term, matchedPretext) {
const textbox = this.getTextbox();
const caret = textbox.selectionEnd;
const text = this.props.value;
......@@ -285,6 +285,17 @@ export default class SuggestionBox extends React.Component {
this.props.onChange(e);
}
// set the caret position after the next rendering
window.requestAnimationFrame(() => {
if (textbox.value === newValue) {
Utils.setCaretPosition(textbox, prefix.length + term.length + 1);
}
});
}
handleCompleteWord(term, matchedPretext) {
this.addTextAtCaret(term, matchedPretext);
if (this.props.onItemSelected) {
const items = SuggestionStore.getItems(this.suggestionId);
const terms = SuggestionStore.getTerms(this.suggestionId);
......@@ -296,14 +307,7 @@ export default class SuggestionBox extends React.Component {
}
}
textbox.focus();
// set the caret position after the next rendering
window.requestAnimationFrame(() => {
if (textbox.value === newValue) {
Utils.setCaretPosition(textbox, prefix.length + term.length + 1);
}
});
this.getTextbox().focus();
for (const provider of this.props.providers) {
if (provider.handleCompleteWord) {
......@@ -311,11 +315,7 @@ export default class SuggestionBox extends React.Component {
}
}
// override if user finished typing term before results returned from API
if (shouldEmitWordSuggestion) {
const override = pretext.endsWith(term);
GlobalActions.emitCompleteWordSuggestion(this.suggestionId, '', override);
}
GlobalActions.emitCompleteWordSuggestion(this.suggestionId);
}
handleKeyDown(e) {
......
......@@ -239,22 +239,6 @@ class SuggestionStore extends EventEmitter {
return pretext.endsWith(matchedPretext);
}
setSuggestionsPending(id, pending) {
this.suggestions.get(id).suggestionsPending = pending;
}
areSuggestionsPending(id) {
return this.suggestions.get(id).suggestionsPending;
}
setCompletePending(id, pending) {
this.suggestions.get(id).completePending = pending;
}
isCompletePending(id) {
return this.suggestions.get(id).completePending;
}
completeWord(id, term = '', matchedPretext = '') {
this.emitCompleteWord(id, term || this.getSelection(id), matchedPretext || this.getSelectedMatchedPretext(id));
......@@ -292,14 +276,7 @@ class SuggestionStore extends EventEmitter {
this.addSuggestions(id, other.terms, other.items, other.component, other.matchedPretext);
this.ensureSelectionExists(id);
this.setSuggestionsPending(id, false);
if (this.isCompletePending(id)) {
this.setCompletePending(id, false);
this.completeWord(id);
} else {
this.emitSuggestionsChanged(id);
}
this.emitSuggestionsChanged(id);
break;
case ActionTypes.SUGGESTION_CLEAR_SUGGESTIONS:
this.setPretext(id, '');
......@@ -316,11 +293,7 @@ class SuggestionStore extends EventEmitter {
this.emitSuggestionsChanged(id);
break;
case ActionTypes.SUGGESTION_COMPLETE_WORD:
if (this.areSuggestionsPending(id) && !other.override) {
this.setCompletePending(id, true);
} else {
this.completeWord(id, other.term, other.matchedPretext);
}
this.completeWord(id, other.term, other.matchedPretext);
break;
case ActionTypes.POPOVER_MENTION_KEY_CLICK:
this.emitPopoverMentionKeyClick(other.isRHS, other.mentionKey);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment