From b8bc3c13c7b3ca0f5d90f3f3e86942c0a66ad020 Mon Sep 17 00:00:00 2001 From: Maxim Karmatskikh Date: Thu, 5 Oct 2023 07:37:13 +0200 Subject: [PATCH] UBER-987: Fix emojis in the middle of something (URLs) 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]