Skip to content

Commit

Permalink
Refactored and fixed bug with cursor position after inserting emoji
Browse files Browse the repository at this point in the history
  • Loading branch information
stiansolli committed Aug 17, 2024
1 parent 84f8f14 commit 24de054
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/components/atoms/markdown/MarkdownEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,9 @@ const MarkdownEditor: React.FC<MarkdownEditorProps> = ({
};

const onClickEmoji = (EmojiObject: EmojiClickData) => {
const textarea = textareaRef.current;
if (textarea !== null) {
const position = textarea.selectionStart;
const before = textarea.value.substring(0, position);
const after = textarea.value.substring(position, textarea.value.length);

// Insert the new text at the cursor position
textarea.value = before + EmojiObject.emoji + after;
setInput(textarea.value);
textarea.selectionStart = textarea.selectionEnd =
position + EmojiObject.emoji.length;
insertText(EmojiObject.emoji, 0);
if (textareaRef.current) {
textareaRef.current.focus();
}
};

Expand Down

0 comments on commit 24de054

Please sign in to comment.