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 {
180 shiftUpByOne,180 shiftUpByOne,
181 shiftDownByOne,181 shiftDownByOne,
182 canUseNegativeLookbehind,182 canUseNegativeLookbehind,
183 trimSpaces,
183 clamp,184 clamp,
184} from './scripts/utils.js';185} from './scripts/utils.js';
185import { debounce_timeout, GENERATION_TYPE_TRIGGERS, IGNORE_SYMBOL, inject_ids, SWIPE_DIRECTION } from './scripts/constants.js';186import { debounce_timeout, GENERATION_TYPE_TRIGGERS, IGNORE_SYMBOL, inject_ids, SWIPE_DIRECTION } from './scripts/constants.js';
@@ -7277,59 +7278,61 @@ function messageEditAuto(div) {
72777278
7278/**7279/**
7279 * Create the message edit UI.7280 * Create the message edit UI.
7280 * @param {number} edit_mes_id7281 * @param {number} editMessageId The ID of the message to edit
7281 */7282 */
7282async function messageEdit(edit_mes_id) {7283export 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 }
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
7286 this_edit_mes_id = edit_mes_id;7296 this_edit_mes_id = editMessageId;
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
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');
72927305
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');
72967309
7297 // Also edit reasoning, if it exists7310 // 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 }
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
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);
73207324
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 text7333 // 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 }
@@ -8840,8 +8843,7 @@ export async function swipe(_event, direction, { source, repeated, message = cha
88408843
8841 async function standardSwipe() {8844 async function standardSwipe() {
8842 //If swipe_id has changed, or the source is being deleted.8845 //If swipe_id has changed, or the source is being deleted.
8843 if (newSwipeId !== originalSwipeId || source == 'delete')8846 if (newSwipeId !== originalSwipeId || source == 'delete') {
8844 {
8845 //Update the chat.8847 //Update the chat.
8846 await loadFromSwipeId(mesId, newSwipeId);8848 await loadFromSwipeId(mesId, newSwipeId);
8847 //Transition to the new chat.8849 //Transition to the new chat.
@@ -9091,8 +9093,8 @@ export async function swipe(_event, direction, { source, repeated, message = cha
9091 chat[mesId]['swipe_id'] = originalSwipeId;9093 chat[mesId]['swipe_id'] = originalSwipeId;
9092 await endSwipe();9094 await endSwipe();
9093 return;9095 return;
9094 //Generate.
9095 } else {9096 } else {
9097 //Generate.
9096 await loadFromSwipeId(mesId, newSwipeId);9098 await loadFromSwipeId(mesId, newSwipeId);
9097 let run_generate = true;9099 let run_generate = true;
9098 await animateSwipe(run_generate);9100 await animateSwipe(run_generate);