Skip to content

Commit

Permalink
fix(frontend/search-emoji): Unicodeの絵文字と同じ名前のカスタム絵文字がMkAutocompleteなど…
Browse files Browse the repository at this point in the history
…で表示されない問題を修正 (#485)
  • Loading branch information
u1-liquid authored Feb 27, 2024
1 parent c8b1ef0 commit 3068801
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions packages/frontend/src/scripts/search-emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ export function searchEmoji(query: string | null, emojiDb: EmojiDef[], max = 30)
// 完全一致(エイリアスなし)
emojiDb.some(x => {
if (x.name === query && !x.aliasOf) {
matched.set(x.name, { emoji: x, score: query.length + 3 });
matched.set(x.emoji, { emoji: x, score: query.length + 3 });
}
return matched.size === max;
});

// 完全一致(エイリアス込み)
if (matched.size < max) {
emojiDb.some(x => {
if (x.name === query && !matched.has(x.aliasOf ?? x.name)) {
matched.set(x.aliasOf ?? x.name, { emoji: x, score: query.length + 2 });
if (x.name === query && !matched.has(x.emoji)) {
matched.set(x.emoji, { emoji: x, score: query.length + 2 });
}
return matched.size === max;
});
Expand All @@ -38,8 +38,8 @@ export function searchEmoji(query: string | null, emojiDb: EmojiDef[], max = 30)
// 前方一致(エイリアスなし)
if (matched.size < max) {
emojiDb.some(x => {
if (x.name.startsWith(query) && !x.aliasOf && !matched.has(x.name)) {
matched.set(x.name, { emoji: x, score: query.length + 1 });
if (x.name.startsWith(query) && !x.aliasOf && !matched.has(x.emoji)) {
matched.set(x.emoji, { emoji: x, score: query.length + 1 });
}
return matched.size === max;
});
Expand All @@ -48,8 +48,8 @@ export function searchEmoji(query: string | null, emojiDb: EmojiDef[], max = 30)
// 前方一致(エイリアス込み)
if (matched.size < max) {
emojiDb.some(x => {
if (x.name.startsWith(query) && !matched.has(x.aliasOf ?? x.name)) {
matched.set(x.aliasOf ?? x.name, { emoji: x, score: query.length });
if (x.name.startsWith(query) && !matched.has(x.emoji)) {
matched.set(x.emoji, { emoji: x, score: query.length });
}
return matched.size === max;
});
Expand All @@ -58,14 +58,14 @@ export function searchEmoji(query: string | null, emojiDb: EmojiDef[], max = 30)
// 部分一致(エイリアス込み)
if (matched.size < max) {
emojiDb.some(x => {
if (x.name.includes(query) && !matched.has(x.aliasOf ?? x.name)) {
matched.set(x.aliasOf ?? x.name, { emoji: x, score: query.length - 1 });
if (x.name.includes(query) && !matched.has(x.emoji)) {
matched.set(x.emoji, { emoji: x, score: query.length - 1 });
}
return matched.size === max;
});
}

// 簡易あいまい検索(3文字以上
// 簡易あいまい検索(4文字以上
if (matched.size < max && query.length > 3) {
const queryChars = [...query];
const hitEmojis = new Map<string, EmojiScore>();
Expand All @@ -82,16 +82,16 @@ export function searchEmoji(query: string | null, emojiDb: EmojiDef[], max = 30)
}

// 半分以上の文字が含まれていればヒットとする
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 });
if (hit > Math.ceil(queryChars.length / 2) && hit - 2 > (matched.get(x.emoji)?.score ?? 0)) {
hitEmojis.set(x.emoji, { emoji: x, score: hit - 2 });
}
}

// ヒットしたものを全部追加すると雑多になるので、先頭の6件程度だけにしておく(6件=オートコンプリートのポップアップのサイズ分)
[...hitEmojis.values()]
.sort((x, y) => y.score - x.score)
.slice(0, 6)
.forEach(it => matched.set(it.emoji.name, it));
.forEach(it => matched.set(it.emoji.emoji, it));
}

return [...matched.values()]
Expand Down

0 comments on commit 3068801

Please sign in to comment.