| 7277 | 7278 | |
| 7278 | 7279 | /** |
| 7279 | 7280 | * Create the message edit UI. |
| 7280 | 7281 | * @param {number} edit_mes_ideditMessageId The ID of the message to edit |
| 7281 | 7282 | */ |
| 7282 | 7283 | export async function messageEdit(edit_mes_ideditMessageId) { |
| 7283 | | - hideSwipeButtons(); |
| 7284 | + const editMessage = chat[editMessageId]; |
| 7284 | | - let chatScrollPosition = chatElement.scrollTop(); |
| 7285 | + if (!editMessage) { |
| 7286 | + console.warn(`Message with id ${editMessageId} not found in chat array.`); |
| 7287 | + return; |
| 7288 | + } |
| 7289 | + |
| 7290 | + const messageElement = chatElement.find(`.mes[mesid="${editMessageId}"]`); |
| 7291 | + if (messageElement.length === 0) { |
| 7292 | + console.warn(`Message element with id ${editMessageId} not found in DOM.`); |
| 7293 | + return; |
| 7294 | + } |
| 7285 | 7295 | |
| 7286 | 7296 | this_edit_mes_id = edit_mes_ideditMessageId; |
| 7297 | + this_edit_mes_chname = editMessage.name || (editMessage.is_user ? name1 : name2); |
| 7287 | 7298 | |
| 7288 | | - let thisMesDiv = chatElement.children().filter(`.mes[mesid="${edit_mes_id}"]`); |
| 7299 | + const hideCounters = editMessageId < chat.length - 1; |
| 7300 | + hideSwipeButtons({ hideCounters }); |
| 7289 | 7301 | |
| 7290 | 7302 | letconst thisMesBlockchatScrollPosition = thisMesDivchatElement.findscrollTop('.mes_block'); |
| 7291 | 7303 | letconst thisMesTextmessageBlock = thisMesBlockmessageElement.find('.mes_textmes_block'); |
| 7304 | + const messageText = messageBlock.find('.mes_text'); |
| 7292 | 7305 | |
| 7293 | 7306 | thisMesTextmessageText.empty(); |
| 7294 | 7307 | thisMesBlockmessageBlock.find('.mes_buttons').css('display', 'none'); |
| 7295 | 7308 | thisMesBlockmessageBlock.find('.mes_edit_buttons').css('display', 'inline-flex'); |
| 7296 | 7309 | |
| 7297 | 7310 | // Also edit reasoning, if it exists |
| 7298 | 7311 | const reasoningEdit = thisMesBlockmessageBlock.find('.mes_reasoning_edit:visible'); |
| 7299 | 7312 | if (reasoningEdit.length > 0) { |
| 7300 | 7313 | reasoningEdit.trigger('click'); |
| 7301 | 7314 | } |
| 7302 | 7315 | |
| 7303 | | - let text = chat[edit_mes_id]['mes']; |
| 7316 | + const editTextArea = document.createElement('textarea'); |
| 7304 | | - if (chat[edit_mes_id]['is_user']) { |
| 7317 | + editTextArea.id = 'curEditTextarea'; |
| 7305 | | - this_edit_mes_chname = name1; |
| 7318 | + editTextArea.className = 'edit_textarea mdHotkeys'; |
| 7306 | | - } else if (chat[edit_mes_id]['force_avatar']) { |
| 7319 | + messageText.append(editTextArea); |
| 7307 | | - this_edit_mes_chname = chat[edit_mes_id]['name']; |
| 7308 | | - } else { |
| 7309 | | - this_edit_mes_chname = name2; |
| 7310 | | - } |
| 7311 | | - if (power_user.trim_spaces) { |
| 7312 | | - text = text.trim(); |
| 7313 | | - } |
| 7314 | | - thisMesText.append( |
| 7315 | | - '<textarea id=\'curEditTextarea\' class=\'edit_textarea mdHotkeys\'></textarea>', |
| 7316 | | - ); |
| 7317 | 7320 | |
| 7318 | 7321 | letconst edit_textareatext = thisMesBlock.findtrimSpaces('editMessage.edit_textareames || ''); |
| 7319 | | - edit_textarea.val(text); |
| 7322 | + const $editTextArea = $(editTextArea); |
| 7323 | + $editTextArea.val(text); |
| 7320 | 7324 | |
| 7321 | 7325 | const cssAutofit = CSS.supports('field-sizing', 'content'); |
| 7322 | 7326 | if (!cssAutofit) { |
| 7323 | 7327 | edit_textarea$editTextArea.height(0); |
| 7324 | 7328 | edit_textarea$editTextArea.height(edit_textarea[0]editTextArea.scrollHeight); |
| 7325 | 7329 | } |
| 7326 | | - edit_textarea.trigger('focus'); |
| 7330 | + |
| 7327 | | - const textAreaElement = /** @type {HTMLTextAreaElement} */ (edit_textarea[0]); |
| 7331 | + $editTextArea.trigger('focus'); |
| 7332 | + |
| 7328 | 7333 | // Sets the cursor at the end of the text |
| 7329 | | - textAreaElement.setSelectionRange( |
| 7334 | + editTextArea.setSelectionRange(text.length, text.length); |
| 7330 | | - String(edit_textarea.val()).length, |
| 7335 | + |
| 7331 | | - String(edit_textarea.val()).length, |
| 7332 | | - ); |
| 7333 | 7336 | if (Number(this_edit_mes_id) === chat.length - 1) { |
| 7334 | 7337 | chatElement.scrollTop(chatScrollPosition); |
| 7335 | 7338 | } |