I have created a plugin for Suneditor which integrates Emoji button.
The Idea is to insert the emoji where the cursor is and then keep the cursor at the same position (following the newly inserted emoji). But I am getting the issue where the cursor gets reset to the beginning of the document, which is terrible UX, any ideas how to keep the cursor from jumping to the start?
https://jsfiddle.net/na3yxb51/4/
I’ve looked over the docs but haven’t been able to find much about the cursor placement.
action: function (core) {
const editor = this;
function handleEmoji(emoji) {
const tag = editor.util.createElement("span");
tag.textContent = emoji;
editor.insertNode(tag, null);
picker.off('emoji', handleEmoji);
};
picker.on('emoji', handleEmoji);
picker.togglePicker(document.querySelector('#emojianchor'));
}
2
Answers
Solution: https://jsfiddle.net/r7p92m8d/1/
In Suneditor, you must save the current cursor position before inserting an emoji and then restore it later to prevent the pointer from jumping to the start of the document. Suneditor’s
core.saveRange()
andcore.restoreRange()
methods can be used to perform this.aHere’s a modified version of your plugin to handle cursor position correctly:
In this code:
This should prevent the cursor from jumping to the beginning of the document when an emoji is inserted.