Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(frontend/search-emoji): Unicodeの絵文字と同じ名前のカスタム絵文字がMkAutocompleteなどで表示されない問題を修正 #485

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading