Refactored `mes_edit_up` and `mes_edit_down` into `messageEditMove`. (#4704) * Refactored `mes_edit_up` and `mes_edit_down` into `messageEditMove`. * Fixed: https://github.com/SillyTavern/SillyTavern/pull/4704#pullrequestreview-3385227819 * Fixed https://github.com/SillyTavern/SillyTavern/pull/4704#pullrequestreview-3385647876 and refactored `updateEditArrowClasses`. * Update this_edit_mes_id in messageEditMove * Don't set this_edit_mes_id if unset --------- Co-authored-by: user <user@exmaple.com> Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>

6757a0375b726f751ca9f1a6141355a4db4326f2

DeclineThyself <FallenHaze@tutamail.com>

Signed
1 files changed, +70 -55Showing whitespace changes
public/script.js+70 -55
@@ -7389,6 +7389,55 @@ async function messageEditCancel(messageId = this_edit_mes_id) {
73897389 showSwipeButtons();
73907390}
73917391
7392+/**
7393+ * Swaps chat[sourceId] with chat[targetId]. They must be adjacent.
7394+ * @param {number} sourceId Index of the message to move
7395+ * @param {number} targetId Index of the target message
7396+ * @returns {Promise<boolean>} True if the messages were moved, false otherwise
7397+ */
7398+async function messageEditMove(sourceId, targetId) {
7399+ if (is_send_press) {
7400+ console.warn(`The message #${sourceId} was not moved to #${targetId} because a generation is in progress.`);
7401+ return false;
7402+ }
7403+
7404+ if (Math.abs(sourceId - targetId) !== 1) {
7405+ console.error(`Message #${sourceId} and #${targetId} are not adjacent.`);
7406+ return false;
7407+ }
7408+
7409+ const targetMessageDiv = chatElement.find(`.mes[mesid="${targetId}"]`);
7410+ const sourceMessageDiv = chatElement.find(`.mes[mesid="${sourceId}"]`);
7411+
7412+ if (sourceMessageDiv.length === 0 || targetMessageDiv.length === 0) {
7413+ console.error(`Message #${sourceId} or #${targetId} were not found.`);
7414+ return false;
7415+ }
7416+
7417+ if (sourceId <= targetId) {
7418+ sourceMessageDiv.insertAfter(targetMessageDiv);
7419+ }
7420+ else {
7421+ sourceMessageDiv.insertBefore(targetMessageDiv);
7422+ }
7423+
7424+ //Swap Ids.
7425+ targetMessageDiv.attr('mesid', sourceId);
7426+ sourceMessageDiv.attr('mesid', targetId);
7427+
7428+ // Swap chat array entries.
7429+ [chat[sourceId], chat[targetId]] = [chat[targetId], chat[sourceId]];
7430+
7431+ // Update edited message id
7432+ if (this_edit_mes_id === sourceId) {
7433+ this_edit_mes_id = targetId;
7434+ }
7435+
7436+ updateViewMessageIds();
7437+ await saveChatConditional();
7438+ return true;
7439+}
7440+
73927441async function messageEditDone(div) {
73937442 let { mesBlock, text, mes, bias } = updateMessage(div);
73947443 if (this_edit_mes_id == 0) {
@@ -8294,7 +8343,7 @@ async function importCharacterChat(formData, { refresh = true } = {}) {
82948343 return [];
82958344}
82968345
82978346export function updateViewMessageIds(startIndex = null) {
82988347 const minId = startIndex ?? getFirstDisplayedMessageId();
82998348
83008349 chatElement.find('.mes').each(function (index, element) {
@@ -8314,24 +8363,27 @@ export function getFirstDisplayedMessageId() {
83148363 return minId;
83158364}
83168365
83178366export function updateEditArrowClasses() {
8318- chatElement.find('.mes .mes_edit_up').removeClass('disabled');
8367+ if (!(this_edit_mes_id >= 0)) {
8319- chatElement.find('.mes .mes_edit_down').removeClass('disabled');
8368+ return;
8369+ }
83208370
8321- if (this_edit_mes_id >= 0) {
8371+ const message = chatElement.find(`.mes[mesid="${this_edit_mes_id}"]`);
8322- const down = chatElement.find(`.mes[mesid="${this_edit_mes_id}"] .mes_edit_down`);
8372+
8323- const up = chatElement.find(`.mes[mesid="${this_edit_mes_id}"] .mes_edit_up`);
8373+ const downButton = message.find('.mes_edit_down');
8374+ const upButton = message.find('.mes_edit_up');
8375+ const copyButton = message.find('.mes_edit_copy');
8376+ const deleteButton = message.find('.mes_edit_delete');
83248377 const lastId = Number(chatElement.find('.mes').last().attr('mesid'));
83258378 const firstId = Number(chatElement.find('.mes').first().attr('mesid'));
83268379
8327- if (lastId == Number(this_edit_mes_id)) {
8380+ copyButton.removeClass('disabled');
83288381 down deleteButton.addClassremoveClass('disabled');
8329- }
83308382
8331- if (firstId == Number(this_edit_mes_id)) {
8383+ // The last message cannot be moved down.
8332- up.addClass('disabled');
8384+ downButton.toggleClass('disabled', lastId === Number(this_edit_mes_id));
8333- }
8385+ // The first message cannot be moved up.
8334- }
8386+ upButton.toggleClass('disabled', firstId === Number(this_edit_mes_id));
83358387}
83368388
83378389/**
@@ -10618,57 +10670,20 @@ jQuery(async function () {
1061810670 });
1061910671
1062010672 $(document).on('click', '.mes_edit_up', async function () {
1062110673 if (is_send_press || this_edit_mes_id <= 0) {
1062210674 return;
1062310675 }
10624-
1062510676 const targetId = Number(this_edit_mes_id) - 1;
10626- const target = chatElement.find(`.mes[mesid="${targetId}"]`);
10677+ await messageEditMove(this_edit_mes_id, targetId);
10627- const root = $(this).closest('.mes');
10628-
10629- if (root.length === 0 || target.length === 0) {
10630- return;
10631- }
10632-
10633- root.insertBefore(target);
10634-
10635- target.attr('mesid', this_edit_mes_id);
10636- root.attr('mesid', targetId);
10637-
10638- const temp = chat[targetId];
10639- chat[targetId] = chat[this_edit_mes_id];
10640- chat[this_edit_mes_id] = temp;
10641-
10642- this_edit_mes_id = targetId;
10643- updateViewMessageIds();
10644- await saveChatConditional();
1064510678 });
1064610679
1064710680 $(document).on('click', '.mes_edit_down', async function () {
1064810681 if (is_send_press || this_edit_mes_id >= chat.length - 1) {
1064910682 return;
1065010683 }
1065110684
1065210685 const targetId = Number(this_edit_mes_id) + 1;
10653- const target = chatElement.find(`.mes[mesid="${targetId}"]`);
10686+ await messageEditMove(this_edit_mes_id, targetId);
10654- const root = $(this).closest('.mes');
10655-
10656- if (root.length === 0 || target.length === 0) {
10657- return;
10658- }
10659-
10660- root.insertAfter(target);
10661-
10662- target.attr('mesid', this_edit_mes_id);
10663- root.attr('mesid', targetId);
10664-
10665- const temp = chat[targetId];
10666- chat[targetId] = chat[this_edit_mes_id];
10667- chat[this_edit_mes_id] = temp;
10668-
10669- this_edit_mes_id = targetId;
10670- updateViewMessageIds();
10671- await saveChatConditional();
1067210687 });
1067310688
1067410689 $(document).on('click', '.mes_edit_copy', async function () {