Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/decorators/Mention/Suggestion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<span
className="rdw-suggestion-wrapper"
Expand All @@ -260,7 +262,7 @@ function getSuggestionComponent() {
<span
key={index}
spellCheck={false}
onClick={this.addMention}
onClick={() => this.addMention(mentionIndex)}
data-index={index}
onMouseEnter={this.onOptionMouseEnter}
onMouseLeave={this.onOptionMouseLeave}
Expand Down
8 changes: 2 additions & 6 deletions src/decorators/Mention/addMention.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down