| 7277 | | 7278 | |
| 7278 | /** | 7279 | /** |
| 7279 | * Create the message edit UI. | 7280 | * Create the message edit UI. |
| 7280 | * @param {number} edit_mes_id | 7281 | * @param {number} editMessageId The ID of the message to edit |
| 7281 | */ | 7282 | */ |
| 7282 | async function messageEdit(edit_mes_id) { | 7283 | export async function messageEdit(editMessageId) { |
| 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 | } |
| 7285 | | 7289 | |
| 7286 | this_edit_mes_id = edit_mes_id; | 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 | } |
| | 7295 | |
| | 7296 | this_edit_mes_id = editMessageId; |
| | 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 | let thisMesBlock = thisMesDiv.find('.mes_block'); | 7302 | const chatScrollPosition = chatElement.scrollTop(); |
| 7291 | let thisMesText = thisMesBlock.find('.mes_text'); | 7303 | const messageBlock = messageElement.find('.mes_block'); |
| | 7304 | const messageText = messageBlock.find('.mes_text'); |
| 7292 | | 7305 | |
| 7293 | thisMesText.empty(); | 7306 | messageText.empty(); |
| 7294 | thisMesBlock.find('.mes_buttons').css('display', 'none'); | 7307 | messageBlock.find('.mes_buttons').css('display', 'none'); |
| 7295 | thisMesBlock.find('.mes_edit_buttons').css('display', 'inline-flex'); | 7308 | messageBlock.find('.mes_edit_buttons').css('display', 'inline-flex'); |
| 7296 | | 7309 | |
| 7297 | // Also edit reasoning, if it exists | 7310 | // Also edit reasoning, if it exists |
| 7298 | const reasoningEdit = thisMesBlock.find('.mes_reasoning_edit:visible'); | 7311 | const reasoningEdit = messageBlock.find('.mes_reasoning_edit:visible'); |
| 7299 | if (reasoningEdit.length > 0) { | 7312 | if (reasoningEdit.length > 0) { |
| 7300 | reasoningEdit.trigger('click'); | 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 | let edit_textarea = thisMesBlock.find('.edit_textarea'); | 7321 | const text = trimSpaces(editMessage.mes || ''); |
| 7319 | edit_textarea.val(text); | 7322 | const $editTextArea = $(editTextArea); |
| | 7323 | $editTextArea.val(text); |
| 7320 | | 7324 | |
| 7321 | const cssAutofit = CSS.supports('field-sizing', 'content'); | 7325 | const cssAutofit = CSS.supports('field-sizing', 'content'); |
| 7322 | if (!cssAutofit) { | 7326 | if (!cssAutofit) { |
| 7323 | edit_textarea.height(0); | 7327 | $editTextArea.height(0); |
| 7324 | edit_textarea.height(edit_textarea[0].scrollHeight); | 7328 | $editTextArea.height(editTextArea.scrollHeight); |
| 7325 | } | 7329 | } |
| 7326 | edit_textarea.trigger('focus'); | 7330 | |
| 7327 | const textAreaElement = /** @type {HTMLTextAreaElement} */ (edit_textarea[0]); | 7331 | $editTextArea.trigger('focus'); |
| | 7332 | |
| 7328 | // Sets the cursor at the end of the text | 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 | if (Number(this_edit_mes_id) === chat.length - 1) { | 7336 | if (Number(this_edit_mes_id) === chat.length - 1) { |
| 7334 | chatElement.scrollTop(chatScrollPosition); | 7337 | chatElement.scrollTop(chatScrollPosition); |
| 7335 | } | 7338 | } |