From b8a335a16650e6fca2b3eb5835dc55edda057ada Mon Sep 17 00:00:00 2001 From: ewwwwwwww Date: Sat, 9 Jul 2022 16:32:34 -0700 Subject: [PATCH] add comments and fix index --- app/soapbox/features/emoji/index.ts | 2 ++ app/soapbox/features/emoji/search.ts | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/app/soapbox/features/emoji/index.ts b/app/soapbox/features/emoji/index.ts index c7c32451f..4a0193c7f 100644 --- a/app/soapbox/features/emoji/index.ts +++ b/app/soapbox/features/emoji/index.ts @@ -100,10 +100,12 @@ export const emojifyText = (str: string, customEmojis = {}) => { }; for (let c of split(str)) { + // convert FE0E selector to FE0F so it can be found in unimap if (c.codePointAt(c.length - 1) === 65038) { c = c.slice(0, -1) + String.fromCodePoint(65039); } + // unqualified emojis aren't in emoji-mart's mappings so we just add FEOF const unqualified = c + String.fromCodePoint(65039); if (c in unicodeMapping) { diff --git a/app/soapbox/features/emoji/search.ts b/app/soapbox/features/emoji/search.ts index 6f59a4645..c25325217 100644 --- a/app/soapbox/features/emoji/search.ts +++ b/app/soapbox/features/emoji/search.ts @@ -12,7 +12,7 @@ const index = new Index({ }); for (const [key, emoji] of Object.entries(data.emojis)) { - index.add(key, emoji.name); + index.add('n' + key, emoji.name); } export interface searchOptions { @@ -21,18 +21,27 @@ export interface searchOptions { } export const addCustomToPool = (customEmojis: any[]) => { + // @ts-ignore + for (const key in index.register) { + if (key[0] === 'c') { + index.remove(key); // remove old custom emojis + } + } + let i = 0; for (const emoji of customEmojis) { - index.add(i++, emoji.id); + index.add('c' + i++, emoji.id); } }; +// we can share an index by prefixing custom emojis with 'c' and native with 'n' const search = (str: string, { maxResults = 5, custom }: searchOptions = {}, custom_emojis?: any): Emoji[] => { return index.search(str, maxResults) .flatMap(id => { - if (Number.isInteger(id)) { - const { shortcode, static_url } = custom_emojis.get(id).toJS(); + // @ts-ignore + if (id[0] === 'c') { + const { shortcode, static_url } = custom_emojis.get((id as string).substr(1)).toJS(); return { id: shortcode, @@ -42,10 +51,10 @@ const search = (str: string, { maxResults = 5, custom }: searchOptions = {}, cus }; } - const { skins } = data.emojis[id]; + const { skins } = data.emojis[(id as string).substr(1)]; return { - id: id as string, + id: (id as string).substr(1), colons: ':' + id + ':', unified: skins[0].unified, native: skins[0].native,