Bladeren bron

feat: mobile chat emojis

master
Bettenbuk Zoltan 5 jaren geleden
bovenliggende
commit
1941275f93

+ 3
- 3
package-lock.json Bestand weergeven

@@ -13766,9 +13766,9 @@
13766 13766
       }
13767 13767
     },
13768 13768
     "react-emoji-render": {
13769
-      "version": "0.4.6",
13770
-      "resolved": "https://registry.npmjs.org/react-emoji-render/-/react-emoji-render-0.4.6.tgz",
13771
-      "integrity": "sha512-ARB8E4j/dndQxC7Bn4b+Oymt7pqhh9GjP87NYcxC8KONejysnXD5O9KpnJeW/U3Ke3+XsWrWAr9K5riVA6emfg==",
13769
+      "version": "1.0.0",
13770
+      "resolved": "https://registry.npmjs.org/react-emoji-render/-/react-emoji-render-1.0.0.tgz",
13771
+      "integrity": "sha512-14iNQ1bOqijkIwfmuYMnG0lew2GJhRmSEPEYi7Dc3RDgklaXXwbLB0bmJF/0LUpDH1hy3I1H3EyrKb//FdwCmg==",
13772 13772
       "requires": {
13773 13773
         "classnames": "^2.2.5",
13774 13774
         "emoji-regex": "^6.4.1",

+ 1
- 1
package.json Bestand weergeven

@@ -63,7 +63,7 @@
63 63
     "postis": "2.2.0",
64 64
     "react": "16.8.6",
65 65
     "react-dom": "16.8.6",
66
-    "react-emoji-render": "0.4.6",
66
+    "react-emoji-render": "1.0.0",
67 67
     "react-i18next": "10.11.4",
68 68
     "react-linkify": "1.0.0-alpha",
69 69
     "react-native": "0.60.5",

+ 18
- 0
react/features/base/util/helpers.js Bestand weergeven

@@ -18,6 +18,24 @@ export function createDeferred(): Object {
18 18
     return deferred;
19 19
 }
20 20
 
21
+const MATCH_OPERATOR_REGEXP = /[|\\{}()[\]^$+*?.-]/g;
22
+
23
+/**
24
+ * Escape RegExp special characters.
25
+ *
26
+ * Based on https://github.com/sindresorhus/escape-string-regexp.
27
+ *
28
+ * @param {string} s - The regexp string to escape.
29
+ * @returns {string}
30
+ */
31
+export function escapeRegexp(s: string) {
32
+    if (typeof s !== 'string') {
33
+        throw new TypeError('Expected a string');
34
+    }
35
+
36
+    return s.replace(MATCH_OPERATOR_REGEXP, '\\$&');
37
+}
38
+
21 39
 /**
22 40
  * Returns the base URL of the app.
23 41
  *

+ 4
- 1
react/features/chat/components/native/ChatMessage.js Bestand weergeven

@@ -7,7 +7,10 @@ import { Avatar } from '../../../base/avatar';
7 7
 import { translate } from '../../../base/i18n';
8 8
 import { Linkify } from '../../../base/react';
9 9
 
10
+import { replaceNonUnicodeEmojis } from '../../functions';
11
+
10 12
 import AbstractChatMessage, { type Props } from '../AbstractChatMessage';
13
+
11 14
 import styles from './styles';
12 15
 
13 16
 /**
@@ -60,7 +63,7 @@ class ChatMessage extends AbstractChatMessage<Props> {
60 63
                                 && this._renderDisplayName()
61 64
                         }
62 65
                         <Linkify linkStyle = { styles.chatLink }>
63
-                            { messageText }
66
+                            { replaceNonUnicodeEmojis(messageText) }
64 67
                         </Linkify>
65 68
                     </View>
66 69
                     { this.props.showTimestamp && this._renderTimestamp() }

+ 55
- 0
react/features/chat/functions.js Bestand weergeven

@@ -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
  *

Laden…
Annuleren
Opslaan