diff --git a/files/assets/js/emoji_modal/inline_emoji_modal.js b/files/assets/js/emoji_modal/inline_emoji_modal.js index 438b4026d..bf17517e7 100644 --- a/files/assets/js/emoji_modal/inline_emoji_modal.js +++ b/files/assets/js/emoji_modal/inline_emoji_modal.js @@ -51,6 +51,16 @@ function close_inline_emoji_modal() { inline_carot_modal.style.display = "none"; } + +function replaceText(input, current_word, new_text) { + const match = new RegExp(current_word+"(?=[^\\w-]|$)", "gi").exec(input.value) + if (!match) return + input.focus() + const start_index = match.index; + const end_index = input.selectionStart; + input.setRangeText(new_text, start_index, end_index, "end"); +} + function populate_inline_emoji_modal(results, textbox) { selecting = true; @@ -95,8 +105,7 @@ function populate_inline_emoji_modal(results, textbox) emoji_option.addEventListener('click', () => { close_inline_emoji_modal() - textbox.value = textbox.value.replace(new RegExp(current_word+"(?=[^\\w-]|$)", "gi"), `:${name}: `) - textbox.focus() + replaceText(textbox, current_word, `:${name}: `) if (typeof markdown === "function" && textbox.dataset.preview) { markdown(textbox) } diff --git a/files/assets/js/emoji_modal/inline_group_modal.js b/files/assets/js/emoji_modal/inline_group_modal.js index 9c5079e8b..b0cf83dbb 100644 --- a/files/assets/js/emoji_modal/inline_group_modal.js +++ b/files/assets/js/emoji_modal/inline_group_modal.js @@ -105,8 +105,7 @@ function populate_inline_group_modal(results, textbox) group_option.addEventListener('click', () => { close_inline_emoji_modal() - textbox.value = textbox.value.replace(new RegExp(current_word+"(?=[^\\w-]|$)", "gi"), `!${name} `) - textbox.focus() + replaceText(textbox, current_word, `:${name}: `) if (typeof markdown === "function" && textbox.dataset.preview) { markdown(textbox) } diff --git a/files/assets/js/emoji_modal/inline_user_modal.js b/files/assets/js/emoji_modal/inline_user_modal.js index 5aaf7b42d..b0576df25 100644 --- a/files/assets/js/emoji_modal/inline_user_modal.js +++ b/files/assets/js/emoji_modal/inline_user_modal.js @@ -109,8 +109,7 @@ function populate_inline_user_modal(results, textbox) user_option.addEventListener('click', () => { close_inline_emoji_modal() - textbox.value = textbox.value.replace(new RegExp(current_word+"(?=[^\\w-]|$)", "gi"), `@${name} `) - textbox.focus() + replaceText(textbox, current_word, `:${name}: `) if (typeof markdown === "function" && textbox.dataset.preview) { markdown(textbox) }