How can I ensure that when a user picks an emoji from the picker, it is inserted at the current cursor position in a textarea? I want to make sure that the cursor remains where it was so that users can continue adding emojis seamlessly.
Currently, I am able to insert emojis at the cursor position but I'm struggling to maintain the cursor's position after the emoji is inserted. It should ideally be placed right after the newly inserted character.
This is my code snippet so far:
addEmoji (emoji) {
const textarea = this.$refs.textarea
const cursorPosition = textarea.selectionEnd
const start = this.content.substring(0, textarea.selectionStart)
const end = this.content.substring(textarea.selectionStart)
const text = start + emoji.native + end
this.content=text;
textarea.focus()
this.$nextTick(() => {
textarea.selectionEnd = cursorPosition + emoji.native.length
})
}
I am using Vue.js for this. Any help would be appreciated.