Refactor message edit function (#4679) * Refactor message edit, show counters on last message * fix: add warnings for missing message in chat array and DOM during edit * Export messageEdit function * Formatting fix * Fix comment alignment

593fba5700563974b232b4f5e8176ee3f0b81d69

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
1 files changed, +41 -39Showing whitespace changes
public/script.js+41 -39
@@ -180,6 +180,7 @@ import {
180180 shiftUpByOne,
181181 shiftDownByOne,
182182 canUseNegativeLookbehind,
183+ trimSpaces,
183184 clamp,
184185} from './scripts/utils.js';
185186import { debounce_timeout, GENERATION_TYPE_TRIGGERS, IGNORE_SYMBOL, inject_ids, SWIPE_DIRECTION } from './scripts/constants.js';
@@ -7277,59 +7278,61 @@ function messageEditAuto(div) {
72777278
72787279/**
72797280 * Create the message edit UI.
72807281 * @param {number} edit_mes_ideditMessageId The ID of the message to edit
72817282 */
72827283export 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+ }
72857295
72867296 this_edit_mes_id = edit_mes_ideditMessageId;
7297+ this_edit_mes_chname = editMessage.name || (editMessage.is_user ? name1 : name2);
72877298
7288- let thisMesDiv = chatElement.children().filter(`.mes[mesid="${edit_mes_id}"]`);
7299+ const hideCounters = editMessageId < chat.length - 1;
7300+ hideSwipeButtons({ hideCounters });
72897301
72907302 letconst thisMesBlockchatScrollPosition = thisMesDivchatElement.findscrollTop('.mes_block');
72917303 letconst thisMesTextmessageBlock = thisMesBlockmessageElement.find('.mes_textmes_block');
7304+ const messageText = messageBlock.find('.mes_text');
72927305
72937306 thisMesTextmessageText.empty();
72947307 thisMesBlockmessageBlock.find('.mes_buttons').css('display', 'none');
72957308 thisMesBlockmessageBlock.find('.mes_edit_buttons').css('display', 'inline-flex');
72967309
72977310 // Also edit reasoning, if it exists
72987311 const reasoningEdit = thisMesBlockmessageBlock.find('.mes_reasoning_edit:visible');
72997312 if (reasoningEdit.length > 0) {
73007313 reasoningEdit.trigger('click');
73017314 }
73027315
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- );
73177320
73187321 letconst edit_textareatext = thisMesBlock.findtrimSpaces('editMessage.edit_textareames || '');
7319- edit_textarea.val(text);
7322+ const $editTextArea = $(editTextArea);
7323+ $editTextArea.val(text);
73207324
73217325 const cssAutofit = CSS.supports('field-sizing', 'content');
73227326 if (!cssAutofit) {
73237327 edit_textarea$editTextArea.height(0);
73247328 edit_textarea$editTextArea.height(edit_textarea[0]editTextArea.scrollHeight);
73257329 }
7326- edit_textarea.trigger('focus');
7330+
7327- const textAreaElement = /** @type {HTMLTextAreaElement} */ (edit_textarea[0]);
7331+ $editTextArea.trigger('focus');
7332+
73287333 // 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- );
73337336 if (Number(this_edit_mes_id) === chat.length - 1) {
73347337 chatElement.scrollTop(chatScrollPosition);
73357338 }
@@ -8840,8 +8843,7 @@ export async function swipe(_event, direction, { source, repeated, message = cha
88408843
88418844 async function standardSwipe() {
88428845 //If swipe_id has changed, or the source is being deleted.
88438846 if (newSwipeId !== originalSwipeId || source == 'delete') {
8844- {
88458847 //Update the chat.
88468848 await loadFromSwipeId(mesId, newSwipeId);
88478849 //Transition to the new chat.
@@ -9091,8 +9093,8 @@ export async function swipe(_event, direction, { source, repeated, message = cha
90919093 chat[mesId]['swipe_id'] = originalSwipeId;
90929094 await endSwipe();
90939095 return;
9094- //Generate.
90959096 } else {
9097+ //Generate.
90969098 await loadFromSwipeId(mesId, newSwipeId);
90979099 let run_generate = true;
90989100 await animateSwipe(run_generate);