client/misc: add matchingNames function and fall back to partial matches

This commit is contained in:
Eva
2025-04-04 10:23:49 +02:00
parent ffd62a936f
commit 0d2df6db95
3 changed files with 17 additions and 8 deletions

View File

@ -10,8 +10,7 @@ function _poolListToMatches(text, pools, options) {
return pool2.postCount - pool1.postCount; return pool2.postCount - pool1.postCount;
}) })
.map((pool) => { .map((pool) => {
pool.matchingNames = pool.names.filter((name) => misc.wildcardMatch(text + "*", name, false)); pool.matchingNames = misc.matchingNames(text, pool.names);
pool.matchingNames = pool.matchingNames.length ? pool.matchingNames : pool.names;
let cssName = misc.makeCssName(pool.category, "pool"); let cssName = misc.makeCssName(pool.category, "pool");
const caption = const caption =
'<span class="' + '<span class="' +
@ -64,8 +63,7 @@ class PoolAutoCompleteControl extends AutoCompleteControl {
} }
const result = this._results[this._activeResult].value; const result = this._results[this._activeResult].value;
const textToFind = this._options.getTextToFind(); const textToFind = this._options.getTextToFind();
result.matchingNames = result.names.filter((name) => misc.wildcardMatch(textToFind + "*", name, false)); result.matchingNames = misc.matchingNames(textToFind, result.names);
result.matchingNames = result.matchingNames.length ? result.matchingNames : result.names;
return result; return result;
} }
} }

View File

@ -11,8 +11,7 @@ function _tagListToMatches(text, tags, options, negated) {
return tag2.usages - tag1.usages; return tag2.usages - tag1.usages;
}) })
.map((tag) => { .map((tag) => {
tag.matchingNames = tag.names.filter((name) => misc.wildcardMatch(text + "*", name, false)); tag.matchingNames = misc.matchingNames(text, tag.names);
tag.matchingNames = tag.matchingNames.length ? tag.matchingNames : tag.names;
let cssName = misc.makeCssName(tag.category, "tag"); let cssName = misc.makeCssName(tag.category, "tag");
if (options.isTaggedWith(tag.names[0])) { if (options.isTaggedWith(tag.names[0])) {
cssName += " disabled"; cssName += " disabled";
@ -87,8 +86,7 @@ class TagAutoCompleteControl extends AutoCompleteControl {
} }
const result = this._results[this._activeResult].value; const result = this._results[this._activeResult].value;
const textToFind = this._options.getTextToFind(); const textToFind = this._options.getTextToFind();
result.matchingNames = result.names.filter((name) => misc.wildcardMatch(textToFind + "*", name, false)); result.matchingNames = misc.matchingNames(textToFind, result.names);
result.matchingNames = result.matchingNames.length ? result.matchingNames : result.names;
return result; return result;
} }
} }

View File

@ -217,6 +217,18 @@ function wildcardMatch(pattern, str, sensitive = false) {
return re.test(str); return re.test(str);
} }
function matchingNames(text, names) {
const minLengthForPartialSearch = 3;
let matches = names.filter((name) => wildcardMatch(text + "*", name, false));
if (!matches.length && text.length >= minLengthForPartialSearch) {
matches = names.filter((name) => wildcardMatch("*" + text + "*", name, false));
}
matches = matches.length ? matches : names;
return matches;
}
module.exports = { module.exports = {
range: range, range: range,
formatRelativeTime: formatRelativeTime, formatRelativeTime: formatRelativeTime,
@ -236,4 +248,5 @@ module.exports = {
dataURItoBlob: dataURItoBlob, dataURItoBlob: dataURItoBlob,
getPrettyName: getPrettyName, getPrettyName: getPrettyName,
wildcardMatch: wildcardMatch, wildcardMatch: wildcardMatch,
matchingNames: matchingNames,
}; };