soapbox/app/soapbox/features/emoji/search.ts

55 wiersze
1.2 KiB
TypeScript
Czysty Zwykły widok Historia

2022-07-05 02:08:47 +00:00
import { Index } from 'flexsearch';
import data from './data';
import type { Emoji, CustomEmoji } from 'emoji-mart';
const index = new Index({
tokenize: 'forward',
optimize: true,
});
for (const [key, emoji] of Object.entries(data.emojis)) {
index.add(key, emoji.name);
}
2022-07-04 23:07:04 +00:00
2022-07-04 20:30:35 +00:00
export interface searchOptions {
maxResults?: number;
}
2022-07-05 02:08:47 +00:00
export const addCustomToPool = (customEmojis: Emoji<CustomEmoji>[]) => {
let i = 0;
2022-07-04 20:30:35 +00:00
2022-07-05 02:08:47 +00:00
for (const emoji of customEmojis) {
index.add(i++, emoji.id);
}
2022-07-04 20:30:35 +00:00
};
2022-07-05 02:08:47 +00:00
const search = (str: string, options: searchOptions, custom_emojis: any) => {
return index.search(str, options.maxResults)
.flatMap(id => {
if (Number.isInteger(id)) {
const { shortcode, static_url } = custom_emojis.get(id).toJS();
return {
id: shortcode,
colons: ':' + shortcode + ':',
emoticons: [],
custom: true,
imageUrl: static_url,
};
}
const { name, skins } = data.emojis[id];
return {
id: name,
colons: ':' + name + ':',
emoticons: [],
unified: skins[0].unified,
native: skins[0].native,
};
});
2022-07-04 20:30:35 +00:00
};
export default search;