You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
4.9 KiB

  1. import { autoPlayGif } from '../../initial_state';
  2. import unicodeMapping from './emoji_unicode_mapping_light';
  3. import Trie from 'substring-trie';
  4. const trie = new Trie(Object.keys(unicodeMapping));
  5. const assetHost = process.env.CDN_HOST || '';
  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 emojify = (str, customEmojis = {}) => {
  18. const tagCharsWithoutEmojis = '<&';
  19. const tagCharsWithEmojis = Object.keys(customEmojis).length ? '<&:' : '<&';
  20. let rtn = '', tagChars = tagCharsWithEmojis, invisible = 0;
  21. for (;;) {
  22. let match, i = 0, tag;
  23. while (i < str.length && (tag = tagChars.indexOf(str[i])) === -1 && (invisible || !(match = trie.search(str.slice(i))))) {
  24. i += str.codePointAt(i) < 65536 ? 1 : 2;
  25. }
  26. let rend, replacement = '';
  27. if (i === str.length) {
  28. break;
  29. } else if (str[i] === ':') {
  30. if (!(() => {
  31. rend = str.indexOf(':', i + 1) + 1;
  32. if (!rend) return false; // no pair of ':'
  33. const lt = str.indexOf('<', i + 1);
  34. if (!(lt === -1 || lt >= rend)) return false; // tag appeared before closing ':'
  35. const shortname = str.slice(i, rend);
  36. // now got a replacee as ':shortname:'
  37. // if you want additional emoji handler, add statements below which set replacement and return true.
  38. if (shortname in customEmojis) {
  39. const filename = autoPlayGif ? customEmojis[shortname].url : customEmojis[shortname].static_url;
  40. 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}" />`;
  41. return true;
  42. }
  43. return false;
  44. })()) rend = ++i;
  45. } else if (tag >= 0) { // <, &
  46. rend = str.indexOf('>;'[tag], i + 1) + 1;
  47. if (!rend) {
  48. break;
  49. }
  50. if (tag === 0) {
  51. if (invisible) {
  52. if (str[i + 1] === '/') { // closing tag
  53. if (!--invisible) {
  54. tagChars = tagCharsWithEmojis;
  55. }
  56. } else if (str[rend - 2] !== '/') { // opening tag
  57. invisible++;
  58. }
  59. } else {
  60. if (str.startsWith('<span class="invisible">', i)) {
  61. // avoid emojifying on invisible text
  62. invisible = 1;
  63. tagChars = tagCharsWithoutEmojis;
  64. }
  65. }
  66. }
  67. i = rend;
  68. } else { // matched to unicode emoji
  69. const { filename, shortCode } = unicodeMapping[match];
  70. const title = shortCode ? `:${shortCode}:` : '';
  71. replacement = `<img draggable="false" class="emojione" alt="${match}" title="${title}" src="${assetHost}/emoji/${emojiFilename(filename)}.svg" />`;
  72. rend = i + match.length;
  73. // If the matched character was followed by VS15 (for selecting text presentation), skip it.
  74. if (str.codePointAt(rend) === 65038) {
  75. rend += 1;
  76. }
  77. }
  78. rtn += str.slice(0, i) + replacement;
  79. str = str.slice(rend);
  80. }
  81. return rtn + str;
  82. };
  83. export default emojify;
  84. export const buildCustomEmojis = (customEmojis) => {
  85. const emojis = [];
  86. customEmojis.forEach(emoji => {
  87. const shortcode = emoji.get('shortcode');
  88. const url = autoPlayGif ? emoji.get('url') : emoji.get('static_url');
  89. const name = shortcode.replace(':', '');
  90. emojis.push({
  91. id: name,
  92. name,
  93. short_names: [name],
  94. text: '',
  95. emoticons: [],
  96. keywords: [name],
  97. imageUrl: url,
  98. custom: true,
  99. customCategory: emoji.get('category'),
  100. });
  101. });
  102. return emojis;
  103. };
  104. export const categoriesFromEmojis = customEmojis => customEmojis.reduce((set, emoji) => set.add(emoji.get('category') ? `custom-${emoji.get('category')}` : 'custom'), new Set(['custom']));