Skip to content

Commit ef043a5

Browse files
authored
Merge pull request #827 from GetStream/fix-mentions-text-replace
CRS-478 fix text replace algorithm
2 parents 121014d + 97b5f32 commit ef043a5

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/components/AutoCompleteTextarea/Textarea.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,20 @@ class ReactTextareaAutocomplete extends React.Component {
204204

205205
const textToModify = textareaValue.slice(0, selectionEnd);
206206

207-
const startOfTokenPosition = textToModify.search(
208-
/**
209-
* It's important to escape the currentTrigger char for chars like [, (,...
210-
*/
211-
new RegExp(`\\${currentTrigger}${`[^\\${currentTrigger}${'\\s'}]`}*$`),
207+
// It's important to escape the currentTrigger char for chars like [, (,...
208+
const findTriggerRegExp = new RegExp(
209+
`\\${currentTrigger}${`[^\\${currentTrigger}${'\\s'}]`}*`,
212210
);
211+
// reverse the string so we can easily find the first index of the currentTrigger
212+
const reversedString = textToModify.split('').reverse().join('');
213+
// find the first instance of currentTrigger (-1 if not found)
214+
const distanceFromEndOfString =
215+
reversedString.search(findTriggerRegExp) + 1;
216+
// if found, distract from the length of the string to revert the position back to an actual string index.
217+
const startOfTokenPosition =
218+
distanceFromEndOfString === -1
219+
? 0
220+
: textToModify.length - distanceFromEndOfString;
213221

214222
// we add space after emoji is selected if a caret position is next
215223
const newTokenString =

0 commit comments

Comments
 (0)