From f4f40ce55837fa961a78ac6afaaba067b996e5d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Thu, 16 Sep 2021 21:00:43 +0200 Subject: [PATCH 1/2] Fix code to move end of range more simply and safely MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/components/views/rooms/BasicMessageComposer.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/components/views/rooms/BasicMessageComposer.tsx b/src/components/views/rooms/BasicMessageComposer.tsx index d83e2e964a..7afd39a5b1 100644 --- a/src/components/views/rooms/BasicMessageComposer.tsx +++ b/src/components/views/rooms/BasicMessageComposer.tsx @@ -182,15 +182,16 @@ export default class BasicMessageEditor extends React.Component if (data) { const { partCreator } = model; const moveStart = emoticonMatch[0][0] === " " ? 1 : 0; - const moveEnd = emoticonMatch[0].length - emoticonMatch.length - moveStart; // we need the range to only comprise of the emoticon // because we'll replace the whole range with an emoji, // so move the start forward to the start of the emoticon. // Take + 1 because index is reported without the possible preceding space. range.moveStartForwards(emoticonMatch.index + moveStart); - // and move end backwards so that we don't replace the trailing space/newline - range.moveEndBackwards(moveEnd); + // If the end is a trailing space/newline move end backwards, so that we don't replace it + if (["\n", " "].includes(emoticonMatch[0][emoticonMatch[0].length - 1])) { + range.moveEndBackwards(1); + } // this returns the amount of added/removed characters during the replace // so the caret position can be adjusted. From a5ee20febff716234ed058e816a6499480ba1fe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Thu, 16 Sep 2021 21:04:55 +0200 Subject: [PATCH 2/2] Simplifie code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/components/views/rooms/BasicMessageComposer.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/components/views/rooms/BasicMessageComposer.tsx b/src/components/views/rooms/BasicMessageComposer.tsx index 7afd39a5b1..edf4f515d2 100644 --- a/src/components/views/rooms/BasicMessageComposer.tsx +++ b/src/components/views/rooms/BasicMessageComposer.tsx @@ -181,7 +181,8 @@ export default class BasicMessageEditor extends React.Component if (data) { const { partCreator } = model; - const moveStart = emoticonMatch[0][0] === " " ? 1 : 0; + const firstMatch = emoticonMatch[0]; + const moveStart = firstMatch[0] === " " ? 1 : 0; // we need the range to only comprise of the emoticon // because we'll replace the whole range with an emoji, @@ -189,7 +190,7 @@ export default class BasicMessageEditor extends React.Component // Take + 1 because index is reported without the possible preceding space. range.moveStartForwards(emoticonMatch.index + moveStart); // If the end is a trailing space/newline move end backwards, so that we don't replace it - if (["\n", " "].includes(emoticonMatch[0][emoticonMatch[0].length - 1])) { + if (["\n", " "].includes(firstMatch[firstMatch.length - 1])) { range.moveEndBackwards(1); }