Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Prevent text substitutions for suggestions that are outdated (#442)
Browse files Browse the repository at this point in the history
also clear pending flag once message has been submitted
  • Loading branch information
csduarte authored and hmhealey committed Jan 3, 2018
1 parent 39745d8 commit 1156ddd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
5 changes: 3 additions & 2 deletions actions/global_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,12 @@ export function emitSelectPreviousSuggestion(suggestionId) {
});
}

export function emitCompleteWordSuggestion(suggestionId, term = '') {
export function emitCompleteWordSuggestion(suggestionId, term = '', override = false) {
AppDispatcher.handleViewAction({
type: Constants.ActionTypes.SUGGESTION_COMPLETE_WORD,
id: suggestionId,
term
term,
override
});
}

Expand Down
3 changes: 3 additions & 0 deletions components/suggestion/command_provider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export default class CommandProvider {
handlePretextChanged(suggestionId, pretext) {
if (pretext.startsWith('/')) {
getSuggestedCommands(pretext.toLowerCase(), suggestionId, CommandSuggestion, pretext.toLowerCase());

return true;
}
return false;
}
}
19 changes: 17 additions & 2 deletions components/suggestion/suggestion_box.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,19 +252,27 @@ export default class SuggestionBox extends React.Component {
const pretext = textbox.value.substring(0, textbox.selectionEnd);

let prefix;
let keepPretext = false;
if (pretext.endsWith(matchedPretext)) {
prefix = pretext.substring(0, pretext.length - matchedPretext.length);
} else {
// the pretext has changed since we got a term to complete so see if the term still fits the pretext
const termWithoutMatched = term.substring(matchedPretext.length);
const overlap = SuggestionBox.findOverlap(pretext, termWithoutMatched);

keepPretext = overlap.length === 0;
prefix = pretext.substring(0, pretext.length - overlap.length - matchedPretext.length);
}

const suffix = text.substring(caret);

const newValue = prefix + term + ' ' + suffix;
let newValue;
if (keepPretext) {
newValue = pretext;
} else {
newValue = prefix + term + ' ' + suffix;
}

this.refs.textbox.value = newValue;

if (this.props.onChange) {
Expand Down Expand Up @@ -302,8 +310,11 @@ export default class SuggestionBox extends React.Component {
provider.handleCompleteWord(term, matchedPretext);
}
}

// override if user finished typing term before results returned from API
if (shouldEmitWordSuggestion) {
GlobalActions.emitCompleteWordSuggestion(this.suggestionId);
const override = pretext.endsWith(term);
GlobalActions.emitCompleteWordSuggestion(this.suggestionId, '', override);
}
}

Expand Down Expand Up @@ -337,6 +348,10 @@ export default class SuggestionBox extends React.Component {
let handled = false;
for (const provider of this.props.providers) {
handled = provider.handlePretextChanged(this.suggestionId, pretext) || handled;

if (handled) {
break;
}
}

if (!handled) {
Expand Down
3 changes: 2 additions & 1 deletion stores/suggestion_store.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ class SuggestionStore extends EventEmitter {
this.setSuggestionsPending(id, false);

if (this.isCompletePending(id)) {
this.setCompletePending(id, false);
this.completeWord(id);
} else {
this.emitSuggestionsChanged(id);
Expand All @@ -315,7 +316,7 @@ class SuggestionStore extends EventEmitter {
this.emitSuggestionsChanged(id);
break;
case ActionTypes.SUGGESTION_COMPLETE_WORD:
if (this.areSuggestionsPending(id)) {
if (this.areSuggestionsPending(id) && !other.override) {
this.setCompletePending(id, true);
} else {
this.completeWord(id, other.term, other.matchedPretext);
Expand Down

0 comments on commit 1156ddd

Please sign in to comment.