Skip to content

Commit

Permalink
chore: do not use fuzzy match for emojiComplete
Browse files Browse the repository at this point in the history
  • Loading branch information
anatawa12 committed Mar 24, 2024
1 parent a0ba703 commit 188e2a3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
10 changes: 3 additions & 7 deletions packages/frontend/src/components/MkAutocomplete.vue
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ export default {
</script>

<script lang="ts" setup generic="T extends keyof CompleteInfo">
import {searchEmojiExact} from "@/scripts/search-emoji.js";

Check failure on line 162 in packages/frontend/src/components/MkAutocomplete.vue

View workflow job for this annotation

GitHub Actions / lint (frontend)

A space is required after '{'

Check failure on line 162 in packages/frontend/src/components/MkAutocomplete.vue

View workflow job for this annotation

GitHub Actions / lint (frontend)

A space is required before '}'

type PropsType<T extends keyof CompleteInfo> = {
type: T;
q: CompleteInfo[T]['query'];
Expand Down Expand Up @@ -282,13 +284,7 @@ function exec() {

emojis.value = searchEmoji(props.q, emojiDb.value);
} else if (props.type === 'emojiComplete') {
if (!props.q || props.q === '') {
// 最近使った絵文字をサジェスト
emojis.value = defaultStore.state.recentlyUsedEmojis.map(emoji => unicodeEmojiDB.value.find(dbEmoji => dbEmoji.emoji === emoji)).filter(x => x) as EmojiDef[];
return;
}

emojis.value = searchEmoji(props.q, unicodeEmojiDB.value);
emojis.value = searchEmojiExact(props.q, unicodeEmojiDB.value);
} else if (props.type === 'mfmTag') {
if (!props.q || props.q === '') {
mfmTags.value = MFM_TAGS;
Expand Down
30 changes: 30 additions & 0 deletions packages/frontend/src/scripts/search-emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,33 @@ export function searchEmoji(query: string | null, emojiDb: EmojiDef[], max = 30)
.slice(0, max)
.map(it => it.emoji);
}

export function searchEmojiExact(query: string | null, emojiDb: EmojiDef[], max = 30): EmojiDef[] {
if (!query) {
return [];
}

const matched = new Map<string, EmojiScore>();
// 完全一致(エイリアスなし)
emojiDb.some(x => {
if (x.name === query && !x.aliasOf) {
matched.set(x.name, { 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 });
}
return matched.size === max;
});
}

return [...matched.values()]
.sort((x, y) => y.score - x.score)
.slice(0, max)
.map(it => it.emoji);
}

0 comments on commit 188e2a3

Please sign in to comment.