From c9dcfbbb81cbf8d2c2fa6f6aa15b902bc6dedb58 Mon Sep 17 00:00:00 2001 From: Vyrtsev Mikhail Date: Mon, 22 Nov 2021 16:19:56 +0300 Subject: [PATCH] Add support for multicode emoji --- package.json | 1 + src/TwemojiText/index.tsx | 6 +++--- src/emoji-unicode.d.ts | 3 +++ src/util.ts | 11 ----------- yarn.lock | 5 +++++ 5 files changed, 12 insertions(+), 14 deletions(-) create mode 100644 src/emoji-unicode.d.ts delete mode 100644 src/util.ts diff --git a/package.json b/package.json index daabff7..e1d51cc 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "react-native": ">= 0.59.0" }, "dependencies": { + "emoji-unicode": "^2.0.1", "react-string-replace": "^0.4.4" } } diff --git a/src/TwemojiText/index.tsx b/src/TwemojiText/index.tsx index 60b0d5e..8d47f50 100644 --- a/src/TwemojiText/index.tsx +++ b/src/TwemojiText/index.tsx @@ -5,7 +5,7 @@ import {Image, Text} from "react-native"; //Utils import reactStringReplace from "react-string-replace"; -import {emojiUnicode} from "../util"; +import emojiUnicode from 'emoji-unicode'; //Styles import {StyleSheet} from 'react-native'; @@ -18,7 +18,7 @@ type TwemojiTextProps = { children: string; }; -const EMOJI_REGEX = /(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/g; +const EMOJI_REGEX = /((?:\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])+)/g; const TwemojiText: React.VFC = ({ twemojiStyle, @@ -38,7 +38,7 @@ const TwemojiText: React.VFC = ({ source={{ uri: `https://twemoji.maxcdn.com/2/72x72/${emojiUnicode( emoji - )}.png`, + ).replace(/\s/g, '-')}.png`, }} /> )); diff --git a/src/emoji-unicode.d.ts b/src/emoji-unicode.d.ts new file mode 100644 index 0000000..ae51163 --- /dev/null +++ b/src/emoji-unicode.d.ts @@ -0,0 +1,3 @@ +declare module 'emoji-unicode' { + export default function emojiUnicode(emoji: string): string; +} diff --git a/src/util.ts b/src/util.ts deleted file mode 100644 index 31f6b08..0000000 --- a/src/util.ts +++ /dev/null @@ -1,11 +0,0 @@ -export const emojiUnicode = (emoji: string): string => { - let comp; - if (emoji.length === 1) { - comp = emoji.charCodeAt(0); - } - comp = (emoji.charCodeAt(0) - 0xd800) * 0x400 + (emoji.charCodeAt(1) - 0xdc00) + 0x10000; - if (comp < 0) { - comp = emoji.charCodeAt(0); - } - return comp.toString(16); -}; diff --git a/yarn.lock b/yarn.lock index 8ca1ba0..64f3de6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -332,6 +332,11 @@ emoji-regex@^8.0.0: resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-8.0.0.tgz#e818fd69ce5ccfcb404594f842963bf53164cc37" integrity sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A== +emoji-unicode@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/emoji-unicode/-/emoji-unicode-2.0.1.tgz#37b05f0cd20254fd366d1df4442b5c613a3a021a" + integrity sha512-jjswgV5nR3KDqtC9ugzEOYQaBLLQP5hqaH2tiTBaq9maYpMnmvl9T+ogY3u2bN6O5NAP0Ddh9D+Meo7sfW/1uw== + enquirer@^2.3.5: version "2.3.6" resolved "https://registry.yarnpkg.com/enquirer/-/enquirer-2.3.6.tgz#2a7fe5dd634a1e4125a975ec994ff5456dc3734d"