From 66d2a67142f7172b2e71db6d9e357a8a401d7e50 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 20 Apr 2020 19:17:58 +0100 Subject: [PATCH] deduplicate emojibase loading Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/autocomplete/EmojiProvider.tsx | 18 ++---------------- src/{emoji.js => emoji.ts} | 26 ++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 20 deletions(-) rename src/{emoji.js => emoji.ts} (79%) diff --git a/src/autocomplete/EmojiProvider.tsx b/src/autocomplete/EmojiProvider.tsx index be6b590248..27e24af205 100644 --- a/src/autocomplete/EmojiProvider.tsx +++ b/src/autocomplete/EmojiProvider.tsx @@ -27,36 +27,22 @@ import _uniq from 'lodash/uniq'; import _sortBy from 'lodash/sortBy'; import SettingsStore from "../settings/SettingsStore"; import { shortcodeToUnicode } from '../HtmlUtils'; +import { EMOJI, IEmoji } from '../emoji'; import EMOTICON_REGEX from 'emojibase-regex/emoticon'; -import * as EMOJIBASE from 'emojibase-data/en/compact.json'; const LIMIT = 20; // Match for ascii-style ";-)" emoticons or ":wink:" shortcodes provided by emojibase const EMOJI_REGEX = new RegExp('(' + EMOTICON_REGEX.source + '|:[+-\\w]*:?)$', 'g'); -interface IEmoji { - annotation: string; - group: number; - hexcode: string; - order: number; - shortcodes: string[]; - tags: string[]; - unicode: string; - emoticon: string; -} - interface IEmojiShort { emoji: IEmoji; shortname: string; _orderBy: number; } -// XXX: it's very unclear why we bother with this generated emojidata file. -// all it means is that we end up bloating the bundle with precomputed stuff -// which would be trivial to calculate and cache on demand. -const EMOJI_SHORTNAMES: IEmojiShort[] = (EMOJIBASE as IEmoji[]).sort((a, b) => { +const EMOJI_SHORTNAMES: IEmojiShort[] = EMOJI.sort((a, b) => { if (a.group === b.group) { return a.order - b.order; } diff --git a/src/emoji.js b/src/emoji.ts similarity index 79% rename from src/emoji.js rename to src/emoji.ts index 20b05531ca..c0a755145a 100644 --- a/src/emoji.js +++ b/src/emoji.ts @@ -14,12 +14,28 @@ See the License for the specific language governing permissions and limitations under the License. */ +// @ts-ignore - import * as EMOJIBASE actually breaks this import EMOJIBASE from 'emojibase-data/en/compact.json'; +export interface IEmoji { + annotation: string; + group: number; + hexcode: string; + order: number; + shortcodes: string[]; + tags: string[]; + unicode: string; + emoticon?: string; +} + +interface IEmojiWithFilterString extends IEmoji { + filterString?: string; +} + // The unicode is stored without the variant selector -const UNICODE_TO_EMOJI = new Map(); // not exported as gets for it are handled by getEmojiFromUnicode -export const EMOTICON_TO_EMOJI = new Map(); -export const SHORTCODE_TO_EMOJI = new Map(); +const UNICODE_TO_EMOJI = new Map(); // not exported as gets for it are handled by getEmojiFromUnicode +export const EMOTICON_TO_EMOJI = new Map(); +export const SHORTCODE_TO_EMOJI = new Map(); export const getEmojiFromUnicode = unicode => UNICODE_TO_EMOJI.get(stripVariation(unicode)); @@ -48,7 +64,7 @@ export const DATA_BY_CATEGORY = { }; // Store various mappings from unicode/emoticon/shortcode to the Emoji objects -EMOJIBASE.forEach(emoji => { +EMOJIBASE.forEach((emoji: IEmojiWithFilterString) => { const categoryId = EMOJIBASE_GROUP_ID_TO_CATEGORY[emoji.group]; if (DATA_BY_CATEGORY.hasOwnProperty(categoryId)) { DATA_BY_CATEGORY[categoryId].push(emoji); @@ -89,3 +105,5 @@ EMOJIBASE.forEach(emoji => { function stripVariation(str) { return str.replace(/[\uFE00-\uFE0F]$/, ""); } + +export const EMOJI: IEmoji[] = EMOJIBASE;