DEV: makes autocomplete debounced (#9941)

* DEV: makes autocomplete debounced

* better wording
This commit is contained in:
Joffrey JAFFEUX 2020-06-02 07:20:14 +02:00 committed by GitHub
parent 6358e79f0f
commit f9e715672b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
import { cancel, later } from "@ember/runloop";
import { INPUT_DELAY } from "discourse-common/config/environment";
import { debounce, cancel, later } from "@ember/runloop";
import { iconHTML } from "discourse-common/lib/icon-library";
import { setCaretPosition, caretPosition } from "discourse/lib/utilities";
import Site from "discourse/models/site";
@ -413,9 +414,7 @@ export default function(options) {
return options.triggerRule ? options.triggerRule(me[0], opts) : true;
}
$(this).on("keyup.autocomplete", function(e) {
if ([keys.esc, keys.enter].indexOf(e.which) !== -1) return true;
function debouncedInput() {
let cp = caretPosition(me[0]);
const key = me[0].value[cp - 1];
@ -426,7 +425,7 @@ export default function(options) {
completeStart = cp - match[0].length;
completeEnd = completeStart + match[0].length - 1;
let term = match[0].substring(1, match[0].length);
updateAutoComplete(dataSource(term, options));
debouncedUpdateAutocomplete(term, options);
}
}
}
@ -439,15 +438,24 @@ export default function(options) {
(!prevChar || allowedLettersRegex.test(prevChar))
) {
completeStart = completeEnd = cp - 1;
updateAutoComplete(dataSource("", options));
debouncedUpdateAutocomplete("", options);
}
}
} else if (completeStart !== null) {
let term = me.val().substring(completeStart + (options.key ? 1 : 0), cp);
updateAutoComplete(dataSource(term, options));
debouncedUpdateAutocomplete(term, options);
}
}
$(this).on("keyup.autocomplete", function(e) {
if ([keys.esc, keys.enter].indexOf(e.which) !== -1) return true;
debounce(this, debouncedInput, INPUT_DELAY);
});
function debouncedUpdateAutocomplete(t, o) {
updateAutoComplete(dataSource(t, o));
}
$(this).on("keydown.autocomplete", function(e) {
var c, i, initial, prev, prevIsGood, stopFound, term, total, userToComplete;
let cp;
@ -496,7 +504,13 @@ export default function(options) {
) {
completeStart = c;
term = me[0].value.substring(c + 1, initial);
updateAutoComplete(dataSource(term, options));
debounce(
this,
debouncedUpdateAutocomplete,
term,
options,
INPUT_DELAY
);
return true;
}
}
@ -583,7 +597,13 @@ export default function(options) {
closeAutocomplete();
}
updateAutoComplete(dataSource(term, options));
debounce(
this,
debouncedUpdateAutocomplete,
term,
options,
INPUT_DELAY
);
return true;
default:
completeEnd = cp;