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