emoji.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { autoPlayGif } from '../../initial_state';
  2. import unicodeMapping from './emoji_unicode_mapping_light';
  3. import { assetHost } from 'mastodon/utils/config';
  4. import Trie from 'substring-trie';
  5. const trie = new Trie(Object.keys(unicodeMapping));
  6. // Convert to file names from emojis. (For different variation selector emojis)
  7. const emojiFilenames = (emojis) => {
  8. return emojis.map(v => unicodeMapping[v].filename);
  9. };
  10. // Emoji requiring extra borders depending on theme
  11. const darkEmoji = emojiFilenames(['🎱', '🐜', '⚫', '🖤', '⬛', '◼️', '◾', '◼️', '✒️', '▪️', '💣', '🎳', '📷', '📸', '♣️', '🕶️', '✴️', '🔌', '💂‍♀️', '📽️', '🍳', '🦍', '💂', '🔪', '🕳️', '🕹️', '🕋', '🖊️', '🖋️', '💂‍♂️', '🎤', '🎓', '🎥', '🎼', '♠️', '🎩', '🦃', '📼', '📹', '🎮', '🐃', '🏴', '🐞', '🕺', '📱', '📲', '🚲']);
  12. const lightEmoji = emojiFilenames(['👽', '⚾', '🐔', '☁️', '💨', '🕊️', '👀', '🍥', '👻', '🐐', '❕', '❔', '⛸️', '🌩️', '🔊', '🔇', '📃', '🌧️', '🐏', '🍚', '🍙', '🐓', '🐑', '💀', '☠️', '🌨️', '🔉', '🔈', '💬', '💭', '🏐', '🏳️', '⚪', '⬜', '◽', '◻️', '▫️']);
  13. const emojiFilename = (filename) => {
  14. const borderedEmoji = (document.body && document.body.classList.contains('theme-mastodon-light')) ? lightEmoji : darkEmoji;
  15. return borderedEmoji.includes(filename) ? (filename + '_border') : filename;
  16. };
  17. const emojifyTextNode = (node, customEmojis) => {
  18. const parentElement = node.parentElement;
  19. let str = node.textContent;
  20. for (;;) {
  21. let match, i = 0;
  22. if (customEmojis === null) {
  23. while (i < str.length && !(match = trie.search(str.slice(i)))) {
  24. i += str.codePointAt(i) < 65536 ? 1 : 2;
  25. }
  26. } else {
  27. while (i < str.length && str[i] !== ':' && !(match = trie.search(str.slice(i)))) {
  28. i += str.codePointAt(i) < 65536 ? 1 : 2;
  29. }
  30. }
  31. let rend, replacement = '';
  32. if (i === str.length) {
  33. break;
  34. } else if (str[i] === ':') {
  35. if (!(() => {
  36. rend = str.indexOf(':', i + 1) + 1;
  37. if (!rend) return false; // no pair of ':'
  38. const shortname = str.slice(i, rend);
  39. // now got a replacee as ':shortname:'
  40. // if you want additional emoji handler, add statements below which set replacement and return true.
  41. if (shortname in customEmojis) {
  42. const filename = autoPlayGif ? customEmojis[shortname].url : customEmojis[shortname].static_url;
  43. replacement = `<img draggable="false" class="emojione custom-emoji" alt="${shortname}" title="${shortname}" src="${filename}" data-original="${customEmojis[shortname].url}" data-static="${customEmojis[shortname].static_url}" />`;
  44. return true;
  45. }
  46. return false;
  47. })()) rend = ++i;
  48. } else { // matched to unicode emoji
  49. const { filename, shortCode } = unicodeMapping[match];
  50. const title = shortCode ? `:${shortCode}:` : '';
  51. replacement = `<img draggable="false" class="emojione" alt="${match}" title="${title}" src="${assetHost}/emoji/${emojiFilename(filename)}.svg" />`;
  52. rend = i + match.length;
  53. // If the matched character was followed by VS15 (for selecting text presentation), skip it.
  54. if (str.codePointAt(rend) === 65038) {
  55. rend += 1;
  56. }
  57. }
  58. node.textContent = str.slice(0, i);
  59. parentElement.insertAdjacentHTML('beforeend', replacement);
  60. str = str.slice(rend);
  61. node = document.createTextNode(str);
  62. parentElement.append(node);
  63. }
  64. };
  65. const emojifyNode = (node, customEmojis) => {
  66. for (const child of node.childNodes) {
  67. switch(child.nodeType) {
  68. case Node.TEXT_NODE:
  69. emojifyTextNode(child, customEmojis);
  70. break;
  71. case Node.ELEMENT_NODE:
  72. if (!child.classList.contains('invisible'))
  73. emojifyNode(child, customEmojis);
  74. break;
  75. }
  76. }
  77. };
  78. const emojify = (str, customEmojis = {}) => {
  79. const wrapper = document.createElement('div');
  80. wrapper.innerHTML = str;
  81. if (!Object.keys(customEmojis).length)
  82. customEmojis = null;
  83. emojifyNode(wrapper, customEmojis);
  84. return wrapper.innerHTML;
  85. };
  86. export default emojify;
  87. export const buildCustomEmojis = (customEmojis) => {
  88. const emojis = [];
  89. customEmojis.forEach(emoji => {
  90. const shortcode = emoji.get('shortcode');
  91. const url = autoPlayGif ? emoji.get('url') : emoji.get('static_url');
  92. const name = shortcode.replace(':', '');
  93. emojis.push({
  94. id: name,
  95. name,
  96. short_names: [name],
  97. text: '',
  98. emoticons: [],
  99. keywords: [name],
  100. imageUrl: url,
  101. custom: true,
  102. customCategory: emoji.get('category'),
  103. });
  104. });
  105. return emojis;
  106. };
  107. export const categoriesFromEmojis = customEmojis => customEmojis.reduce((set, emoji) => set.add(emoji.get('category') ? `custom-${emoji.get('category')}` : 'custom'), new Set(['custom']));