So, I'm developing an electron port of a messaging service's webpage. The page doesn't offer emoji's yet, so I'm creating a system that will parse over the textbox's content and replace configured emoji strings with the emoji. However, I've run into an issue. Whenever the emoji is replaced, the cursor returns back to the start of the div
Now, I've looked all over the internet trying to solve the issue. I know about $(elem).focus()
/ its vanilla counterpart method. But, the issue I have is that the page defines the element as a div, and sets the div's role to a textbox. Meaning, the element isn't inherently an input so it doesn't inherent the default functions / events that inputs do. I designed the app to be completely backgrounded- which just means I don't want to have to edit the core HTML upon pageload.
You can view the div element here:
$(window).on('keyup', function(e) {
// if the key isn't enter, we will continue
if (!(e.key === 'Enter')) {
// we'll grab the focused element
let focus = $(":focus");
// cross reference the element's class to make sure it's the proper text box
if (focus.attr('class') == "euyIb") {
// now let's grab our focus text value
let text = focus.text();
// now we're going to get a count of special characters in the string
let count = (text.split(":").length - 1);
// if there's two of our special chars in the string, assume there may be an emoji to replace
if (count >= 2) {
// create an array of emojis in the string
let contains = [];
// loop over all of the emojis configured and check if they're in the string
for (emoji in replaceables) {
let target = replaceables[emoji];
if (text.includes(`:${target.name}:`)) {
contains.push({
name: `:${target.name}:`,
emoji: target.emoji
});
}
}
for (inst in contains) {
let item = contains[inst];
$(":focus").text(text.replace(item.name, item.emoji));
// then here we should reset the cursor to the end
}
}
}
}
});
<div role="textbox" dir="auto" class="euyIb" contenteditable="true" placeholder="Send a chat" style="caret-color: rgb(242, 60, 87);" id="enter-box">hi </div>
Any way to alleviate this issue without having to change div.euyIb's core code on preload would be greatly appreciated.