Extend fuzzy search in autocomplete to all strategies

Fixes #1086
Just building upon the work already done in #1334
This commit is contained in:
Yash Srivastav 2017-08-01 00:59:52 +05:30
parent e292ef2bed
commit 1c8aa7a88f
No known key found for this signature in database
GPG Key ID: 67A09FFAC003E189

View File

@ -51,17 +51,10 @@ $(function() {
id: "emoji", id: "emoji",
match: /\B:([-+\w:?]{2,}):?$/, match: /\B:([-+\w:?]{2,}):?$/,
search(term, callback) { search(term, callback) {
const results = fuzzy.filter(
// Trim colon from the matched term, // Trim colon from the matched term,
// as we are unable to get a clean string from match regex // as we are unable to get a clean string from match regex
term.replace(/:$/, ""), term = term.replace(/:$/, ""),
emojiSearchTerms, callback(fuzzyGrep(term, emojiSearchTerms));
{
pre: "<b>",
post: "</b>"
}
);
callback(results.map((el) => [el.string, el.original]));
}, },
template([string, original]) { template([string, original]) {
return `<span class="emoji">${emojiMap[original]}</span> ${string}`; return `<span class="emoji">${emojiMap[original]}</span> ${string}`;
@ -83,11 +76,11 @@ $(function() {
callback(completeNicks(term)); callback(completeNicks(term));
} }
}, },
template(value) { template([string, ]) {
return value; return string;
}, },
replace(value) { replace([, original]) {
return value; return original;
}, },
index: 1 index: 1
}; };
@ -98,11 +91,11 @@ $(function() {
search(term, callback, match) { search(term, callback, match) {
callback(completeChans(match[0])); callback(completeChans(match[0]));
}, },
template(value) { template([string,]) {
return value; return string;
}, },
replace(value) { replace([, original]) {
return value; return original;
}, },
index: 1 index: 1
}; };
@ -113,11 +106,11 @@ $(function() {
search(term, callback) { search(term, callback) {
callback(completeCommands("/" + term)); callback(completeCommands("/" + term));
}, },
template(value) { template([string, ]) {
return value; return string;
}, },
replace(value) { replace([, original]) {
return value; return original;
}, },
index: 1 index: 1
}; };
@ -278,7 +271,7 @@ $(function() {
chat.find(".chan.active .chat").trigger("msg.sticky"); // fix growing chat.find(".chan.active .chat").trigger("msg.sticky"); // fix growing
}) })
.tab(completeNicks, {hint: false}) .tab(tabCompleteNicks, {hint: false})
.on("autocomplete:on", function() { .on("autocomplete:on", function() {
enableAutocomplete(); enableAutocomplete();
}); });
@ -919,7 +912,19 @@ $(function() {
} }
}()); }());
function completeNicks(word) { function fuzzyGrep(term, array) {
const results = fuzzy.filter(
term,
array,
{
pre: "<b>",
post: "</b>"
}
);
return results.map((el) => [el.string, el.original]);
}
function tabCompleteNicks(word) {
const users = chat.find(".active .users"); const users = chat.find(".active .users");
// Lobbies and private chats do not have an user list // Lobbies and private chats do not have an user list
@ -935,13 +940,23 @@ $(function() {
); );
} }
function completeNicks(word) {
const users = chat.find(".active .users");
// Lobbies and private chats do not have an user list
if (!users.length) {
return [];
}
const words = users.data("nicks");
return fuzzyGrep(word, words);
}
function completeCommands(word) { function completeCommands(word) {
const words = constants.commands.slice(); const words = constants.commands.slice();
return $.grep( return fuzzyGrep(word, words);
words,
(w) => !w.toLowerCase().indexOf(word.toLowerCase())
);
} }
function completeChans(word) { function completeChans(word) {
@ -955,10 +970,7 @@ $(function() {
} }
}); });
return $.grep( return fuzzyGrep(word, words);
words,
(w) => !w.toLowerCase().indexOf(word.toLowerCase())
);
} }
$(document).on("visibilitychange focus click", () => { $(document).on("visibilitychange focus click", () => {