From af4ad1361f8e7968c9d11bd0eaa8129b531a6d79 Mon Sep 17 00:00:00 2001 From: Aevann Date: Thu, 23 May 2024 05:54:15 +0300 Subject: [PATCH] fix this https://watchpeopledie.tv/h/meta/post/61549/megathread-for-bugs-and-suggestions/3335934#context --- .../assets/js/emoji_modal/inline_emoji_modal.js | 17 +++++++++-------- .../assets/js/emoji_modal/inline_group_modal.js | 2 +- .../assets/js/emoji_modal/inline_user_modal.js | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/files/assets/js/emoji_modal/inline_emoji_modal.js b/files/assets/js/emoji_modal/inline_emoji_modal.js index 150fcae76..3d6e8d552 100644 --- a/files/assets/js/emoji_modal/inline_emoji_modal.js +++ b/files/assets/js/emoji_modal/inline_emoji_modal.js @@ -38,6 +38,7 @@ document.body.appendChild(inline_carot_modal); let e let current_word = ""; +let start_index = 0; let selecting; let emoji_index = 0; @@ -52,15 +53,11 @@ function close_inline_emoji_modal() { } -function replaceText(input, current_word, new_text) { +function replaceText(input, new_text) { close_inline_emoji_modal() - const match = new RegExp(current_word+"(?=[^\\w-]|$)", "gi").exec(input.value.substring(0, input.selectionStart)) - if (!match) return input.focus() - const start_index = match.index; - const end_index = input.selectionStart; - input.setRangeText(new_text, start_index, end_index, "end"); + input.setRangeText(new_text, start_index, input.selectionStart, "end"); if (typeof markdown === "function" && input.dataset.preview) markdown(input) @@ -109,7 +106,7 @@ function populate_inline_emoji_modal(results, textbox) if (current_word.includes("!")) name = `!${name}` emoji_option.addEventListener('click', () => { - replaceText(textbox, current_word, `:${name}: `) + replaceText(textbox, `:${name}: `) }); // Pack emoji_option.appendChild(emoji_option_img); @@ -146,7 +143,11 @@ function update_inline_emoji_modal(event) // Get current word at string, such as ":marse" or "word" let coords = text.indexOf(' ', box_coords.pos); current_word = /(^|\s|\+|-)([:!@][!#\w-]{2,}(?=\n|$))/.exec(text.slice(0, coords === -1 ? text.length : coords)); - if (current_word) current_word = current_word[2].toLowerCase(); + + if (current_word) { + current_word = current_word[2].toLowerCase(); + start_index = event.target.selectionStart - current_word.length + } if (current_word && curr_word_is_emoji() && current_word != ":") { diff --git a/files/assets/js/emoji_modal/inline_group_modal.js b/files/assets/js/emoji_modal/inline_group_modal.js index 22425b693..a3961c410 100644 --- a/files/assets/js/emoji_modal/inline_group_modal.js +++ b/files/assets/js/emoji_modal/inline_group_modal.js @@ -104,7 +104,7 @@ function populate_inline_group_modal(results, textbox) group_option_text.textContent = name; group_option.addEventListener('click', () => { - replaceText(textbox, current_word, `!${name} `) + replaceText(textbox, `!${name} `) }); group_option.appendChild(group_option_text); inline_carot_modal.appendChild(group_option); diff --git a/files/assets/js/emoji_modal/inline_user_modal.js b/files/assets/js/emoji_modal/inline_user_modal.js index 3a4c98adf..37437a280 100644 --- a/files/assets/js/emoji_modal/inline_user_modal.js +++ b/files/assets/js/emoji_modal/inline_user_modal.js @@ -108,7 +108,7 @@ function populate_inline_user_modal(results, textbox) user_option_text.textContent = name; user_option.addEventListener('click', () => { - replaceText(textbox, current_word, `@${name} `) + replaceText(textbox, `@${name} `) }); user_option.appendChild(user_option_img); user_option.appendChild(user_option_text);