From 4a201db84431c501824824e510935e78675e73a8 Mon Sep 17 00:00:00 2001 From: Maksim Karmatskikh Date: Thu, 5 Oct 2023 15:54:22 +0600 Subject: [PATCH] UBER-987: Fix emojis in the middle of something (URLs) (#3790) Signed-off-by: Maxim Karmatskikh --- .../src/components/extension/emoji.ts | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/packages/text-editor/src/components/extension/emoji.ts b/packages/text-editor/src/components/extension/emoji.ts index ffb20d3f29..308dafda72 100644 --- a/packages/text-editor/src/components/extension/emoji.ts +++ b/packages/text-editor/src/components/extension/emoji.ts @@ -19,7 +19,6 @@ const emojiReplaceDict = { ":'-)": '😂', ':)': '😊', ':-)': '😄', - ':]': '😄', ':^)': '😄', ':o)': '😄', ':}': '😄', @@ -28,19 +27,12 @@ const emojiReplaceDict = { ';)': '😉', ';-)': '😉', ';-]': '😉', - ';]': '😉', ';^)': '😉', ':-|': '😐', - ':|': '😐', - ':(': '😞', ':-(': '😒', ':-<': '😒', ':-[': '😒', ':-c': '😒', - ':<': '😒', - ':[': '😒', - ':{': '😒', - '%)': '😖', '%-)': '😖', ':-P': '😜', ':-p': '😜', @@ -48,7 +40,6 @@ const emojiReplaceDict = { ':-||': '😠', ':-.': '😡', ':-/': '😡', - ':/': '😐', ":'(": '😢', ":'-(": '😢', ':-O': '😲', @@ -58,16 +49,20 @@ const emojiReplaceDict = { } function escapeRegExp (text: string): string { - return text.replace(/[[\]{}()*+?.\\^$|#]/g, '\\$&') + return text.replace(/[:[\]{}()*+?.\\^$|#]/g, '\\$&') } export const EmojiExtension = Extension.create({ addInputRules () { return Object.keys(emojiReplaceDict).map((pattern) => { return { - find: new RegExp(escapeRegExp(pattern)), + find: new RegExp(`(?:^|\\s)(${escapeRegExp(pattern)})`), handler: ({ range, match, commands }) => { - commands.insertContentAt(range, [ + let replaceRange = range + if (match[0] !== match[1]) { + replaceRange = { from: range.from + 1, to: range.to } + } + commands.insertContentAt(replaceRange, [ { type: 'text', text: emojiReplaceDict[pattern as keyof typeof emojiReplaceDict]