Skip to content

Commit

Permalink
fix: bug with adding mention on new line
Browse files Browse the repository at this point in the history
fixes #10
  • Loading branch information
dabakovich committed Dec 20, 2020
1 parent b372ed7 commit 3a1bede
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/components/mentions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ const Mentions: FC<MentionsProps> = (

const triggerIndex = plainText.lastIndexOf(trigger, selection.end);
const spaceIndex = plainText.lastIndexOf(' ', selection.end - 1);
const newLineIndex = plainText.lastIndexOf('\n', selection.end - 1);

switch (true) {
case triggerIndex == -1:
Expand All @@ -182,13 +183,18 @@ const Mentions: FC<MentionsProps> = (
case triggerIndex < spaceIndex:
return undefined;

case spaceIndex == -1 || spaceIndex + trigger.length === triggerIndex: {
// When we have a mention at the very beginning of text
case spaceIndex == -1 && newLineIndex == -1:

// When we have a mention on the new line
case newLineIndex + trigger.length === triggerIndex:

// When we have a mention just after space
case spaceIndex + trigger.length === triggerIndex:
return plainText.substring(
triggerIndex + trigger.length,
selection.end,
);
}
}
}, [parts, plainText, selection, trigger]);

Expand Down Expand Up @@ -225,7 +231,7 @@ const Mentions: FC<MentionsProps> = (
};

const isInsertSpaceToNextPart = isInsertSpaceAfterMention
// Cursor it at the very of parts or text row
// Cursor is at the very end of parts or text row
&& (plainText.length === selection.end || parts[currentPartIndex]?.text.startsWith('\n', newMentionPartPosition.end));

const newParts = [
Expand Down

0 comments on commit 3a1bede

Please sign in to comment.