enhance(frontend): 絵文字のオートコンプリートのアルゴリズムの改善 (MisskeyIO#261)

* 実際は同じ絵文字なら重複してサジェストに出ないように
* エイリアスではない絵文字>前方一致>部分一致>あいまい検索順で表示されるようになるように
This commit is contained in:
まっちゃとーにゅ 2023-11-24 06:37:06 +09:00 committed by syuilo
parent 9c84055f50
commit da3064343b

View file

@ -265,7 +265,7 @@ function emojiAutoComplete(query: string | null, emojiDb: EmojiDef[], max = 30):
//
emojiDb.some(x => {
if (x.name.startsWith(query) && !x.aliasOf) {
matched.set(x.name, { emoji: x, score: query.length });
matched.set(x.name, { emoji: x, score: query.length + 1 });
}
return matched.size === max;
});
@ -273,8 +273,8 @@ function emojiAutoComplete(query: string | null, emojiDb: EmojiDef[], max = 30):
//
if (matched.size < max) {
emojiDb.some(x => {
if (x.name.startsWith(query)) {
matched.set(x.name, { emoji: x, score: query.length });
if (x.name.startsWith(query) && !matched.has(x.aliasOf ?? x.name)) {
matched.set(x.aliasOf ?? x.name, { emoji: x, score: query.length });
}
return matched.size === max;
});
@ -283,36 +283,32 @@ function emojiAutoComplete(query: string | null, emojiDb: EmojiDef[], max = 30):
//
if (matched.size < max) {
emojiDb.some(x => {
if (x.name.includes(query)) {
matched.set(x.name, { emoji: x, score: query.length });
if (x.name.includes(query) && !matched.has(x.aliasOf ?? x.name)) {
matched.set(x.aliasOf ?? x.name, { emoji: x, score: query.length - 1 });
}
return matched.size === max;
});
}
//
if (matched.size < max) {
// 3
if (matched.size < max && query.length > 3) {
const queryChars = [...query];
const hitEmojis = new Map<string, EmojiScore>();
for (const x of emojiDb) {
// 1
//
//
let queryCharHitPos = 0;
let queryCharHitCount = 0;
for (let idx = 0; idx < queryChars.length; idx++) {
queryCharHitPos = x.name.indexOf(queryChars[idx], queryCharHitPos);
if (queryCharHitPos <= -1) {
break;
}
queryCharHitCount++;
let pos = 0;
let hit = 0;
for (const c of queryChars) {
pos = x.name.indexOf(c, pos);
if (pos <= -1) break;
hit++;
}
// 調
if (queryCharHitCount > 2) {
hitEmojis.set(x.name, { emoji: x, score: queryCharHitCount });
//
if (hit > Math.ceil(queryChars.length / 2) && hit - 2 > (matched.get(x.aliasOf ?? x.name)?.score ?? 0)) {
hitEmojis.set(x.aliasOf ?? x.name, { emoji: x, score: hit - 2 });
}
}