diff --git a/src/decorators/Mention/Suggestion/index.js b/src/decorators/Mention/Suggestion/index.js index f51b92d13..ff163162e 100644 --- a/src/decorators/Mention/Suggestion/index.js +++ b/src/decorators/Mention/Suggestion/index.js @@ -222,20 +222,22 @@ function getSuggestionComponent() { }); }; - addMention = () => { + addMention = (mentionIndexOnClick) => { const { activeOption } = this.state; const editorState = config.getEditorState(); - const { onChange, separator, trigger } = config; + const { onChange, trigger } = config; const selectedMention = this.filteredSuggestions[activeOption]; + const mentionIndex = mentionIndexOnClick ?? editorState.getSelection().focusOffset - 1; if (selectedMention) { - addMention(editorState, onChange, separator, trigger, selectedMention); + addMention(editorState, onChange, trigger, selectedMention, mentionIndex); } }; render() { const { children } = this.props; const { activeOption, showSuggestions } = this.state; - const { dropdownClassName, optionClassName } = config; + const { dropdownClassName, getEditorState, optionClassName } = config; + const mentionIndex = getEditorState().getSelection().focusOffset - 1; return ( this.addMention(mentionIndex)} data-index={index} onMouseEnter={this.onOptionMouseEnter} onMouseLeave={this.onOptionMouseLeave} diff --git a/src/decorators/Mention/addMention.js b/src/decorators/Mention/addMention.js index d95cbdba7..83d57f80c 100644 --- a/src/decorators/Mention/addMention.js +++ b/src/decorators/Mention/addMention.js @@ -7,9 +7,9 @@ import { getSelectedBlock } from 'draftjs-utils'; export default function addMention( editorState: EditorState, onChange: Function, - separator: string, trigger: string, suggestion: Object, + mentionIndex: number ): void { const { value, url } = suggestion; const entityKey = editorState @@ -18,12 +18,8 @@ export default function addMention( .getLastCreatedEntityKey(); const selectedBlock = getSelectedBlock(editorState); const selectedBlockText = selectedBlock.getText(); - let focusOffset = editorState.getSelection().focusOffset; - const mentionIndex = (selectedBlockText.lastIndexOf(separator + trigger, focusOffset) || 0) + 1; + const focusOffset = mentionIndex + 1 let spaceAlreadyPresent = false; - if (selectedBlockText.length === mentionIndex + 1) { - focusOffset = selectedBlockText.length; - } if (selectedBlockText[focusOffset] === ' ') { spaceAlreadyPresent = true; }