interweave-emoji
Version:
Emoji support for Interweave.
175 lines (129 loc) • 4.42 kB
JavaScript
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
// Bundled with Packemon: https://packemon.dev
// Platform: browser, Support: stable, Format: esm
import { TEXT, generateEmoticonPermutations, EMOTICON_OPTIONS, fromCodepointToUnicode, fromHexcodeToCodepoint } from 'emojibase';
const instances = new Map();
function resetInstances() {
if (process.env.NODE_ENV !== "production") {
instances.clear();
}
}
class EmojiDataManager {
constructor(locale, version) {
_defineProperty(this, "EMOJIS", {});
_defineProperty(this, "EMOTICON_TO_HEXCODE", {});
_defineProperty(this, "SHORTCODE_TO_HEXCODE", {});
_defineProperty(this, "UNICODE_TO_HEXCODE", {});
_defineProperty(this, "GROUPS_BY_KEY", {});
_defineProperty(this, "SKIN_TONES_BY_KEY", {});
_defineProperty(this, "SUBGROUPS_BY_KEY", {});
_defineProperty(this, "data", []);
_defineProperty(this, "flatData", []);
_defineProperty(this, "locale", 'en');
_defineProperty(this, "version", '0.0.0');
this.locale = locale;
this.version = version;
}
/**
* Return or create a singleton instance per locale.
*/
static getInstance(locale, version) {
const key = `${locale}:${version}`;
if (!instances.has(key)) {
instances.set(key, new EmojiDataManager(locale, version));
}
return instances.get(key);
}
/**
* Return dataset as a list.
*/
getData() {
return this.data;
}
/**
* Return dataset as a flattened list.
*/
getFlatData() {
return this.flatData;
}
/**
* Package the emoji object with additional data,
* while also extracting and partitioning relevant information.
*/
packageEmoji(baseEmoji) {
const {
emoticon,
hexcode,
shortcodes = []
} = baseEmoji;
const emoji = { ...baseEmoji,
canonical_shortcodes: [],
primary_shortcode: '',
skins: [],
unicode: ''
}; // Make our lives easier
if (!emoji.unicode) {
emoji.unicode = emoji.text && emoji.type === TEXT ? emoji.text : emoji.emoji;
} // Canonicalize the shortcodes for easy reuse
emoji.canonical_shortcodes = shortcodes.map(code => `:${code}:`);
emoji.primary_shortcode = emoji.canonical_shortcodes[0]; // Support all shortcodes
emoji.canonical_shortcodes.forEach(shortcode => {
this.SHORTCODE_TO_HEXCODE[shortcode] = hexcode;
}); // Support all emoticons
if (emoticon) {
const emoticons = Array.isArray(emoticon) ? emoticon : [emoticon];
emoticons.forEach(emo => {
generateEmoticonPermutations(emo, EMOTICON_OPTIONS[emo]).forEach(e => {
this.EMOTICON_TO_HEXCODE[e] = hexcode;
});
});
} // Support all presentations (even no variation selectors)
this.UNICODE_TO_HEXCODE[fromCodepointToUnicode(fromHexcodeToCodepoint(hexcode))] = hexcode;
if (emoji.emoji) {
this.UNICODE_TO_HEXCODE[emoji.emoji] = hexcode;
}
if (emoji.text) {
this.UNICODE_TO_HEXCODE[emoji.text] = hexcode;
} // Map each emoji
this.EMOJIS[hexcode] = emoji; // Apply same logic to all variations
if (baseEmoji.skins) {
emoji.skins = baseEmoji.skins.map(skinEmoji => this.packageEmoji(skinEmoji));
}
return emoji;
}
/**
* Parse and generate emoji datasets.
*/
parseEmojiData(data) {
data.forEach(emoji => {
const packagedEmoji = this.packageEmoji(emoji);
this.data.push(packagedEmoji);
this.flatData.push(packagedEmoji); // Flatten and package skins as well
if (packagedEmoji.skins) {
packagedEmoji.skins.forEach(skin => {
this.flatData.push(skin);
});
}
});
return this.data;
}
parseMessageData(data) {
if (data.groups) {
data.groups.forEach(group => {
this.GROUPS_BY_KEY[group.key] = group.message;
});
}
if (data.subgroups) {
data.subgroups.forEach(group => {
this.SUBGROUPS_BY_KEY[group.key] = group.message;
});
}
if (data.skinTones) {
data.skinTones.forEach(skinTone => {
this.SKIN_TONES_BY_KEY[skinTone.key] = skinTone.message;
});
}
}
}
export { EmojiDataManager as E, resetInstances as r };
//# sourceMappingURL=bundle-2bf6afbb.js.map