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.

functions.js 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // @flow
  2. import aliases from 'react-emoji-render/data/aliases';
  3. import emojiAsciiAliases from 'react-emoji-render/data/asciiAliases';
  4. import { escapeRegexp } from '../base/util';
  5. /**
  6. * An ASCII emoticon regexp array to find and replace old-style ASCII
  7. * emoticons (such as :O) to new Unicode representation, so then devices
  8. * and browsers that support them can render these natively without
  9. * a 3rd party component.
  10. *
  11. * NOTE: this is currently only used on mobile, but it can be used
  12. * on web too once we drop support for browsers that don't support
  13. * unicode emoji rendering.
  14. */
  15. const EMOTICON_REGEXP_ARRAY: Array<Array<Object>> = [];
  16. (function() {
  17. for (const [ key, value ] of Object.entries(aliases)) {
  18. let escapedValues;
  19. const asciiEmojies = emojiAsciiAliases[key];
  20. // Adding ascii emoticons
  21. if (asciiEmojies) {
  22. escapedValues = asciiEmojies.map(v => escapeRegexp(v));
  23. } else {
  24. escapedValues = [];
  25. }
  26. // Adding slack-type emoji format
  27. escapedValues.push(escapeRegexp(`:${key}:`));
  28. const regexp = `\\B(${escapedValues.join('|')})\\B`;
  29. EMOTICON_REGEXP_ARRAY.push([ new RegExp(regexp, 'g'), value ]);
  30. }
  31. })();
  32. /**
  33. * Replaces ascii and other non-unicode emoticons with unicode emojis to let the emojis be rendered
  34. * by the platform native renderer.
  35. *
  36. * @param {string} message - The message to parse and replace.
  37. * @returns {string}
  38. */
  39. export function replaceNonUnicodeEmojis(message: string) {
  40. let replacedMessage = message;
  41. for (const [ regexp, replaceValue ] of EMOTICON_REGEXP_ARRAY) {
  42. replacedMessage = replacedMessage.replace(regexp, replaceValue);
  43. }
  44. return replacedMessage;
  45. }
  46. /**
  47. * Selector for calculating the number of unread chat messages.
  48. *
  49. * @param {Object} state - The redux state.
  50. * @returns {number} The number of unread messages.
  51. */
  52. export function getUnreadCount(state: Object) {
  53. const { lastReadMessage, messages } = state['features/chat'];
  54. const messagesCount = messages.length;
  55. if (!messagesCount) {
  56. return 0;
  57. }
  58. if (navigator.product === 'ReactNative') {
  59. // React native stores the messages in a reversed order.
  60. return messages.indexOf(lastReadMessage);
  61. }
  62. const lastReadIndex = messages.lastIndexOf(lastReadMessage);
  63. return messagesCount - (lastReadIndex + 1);
  64. }