fix: /cut of the first displayed message

94ba7bcf2dfb3191e75be34a66b85b3e3a7979c8

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

2 files changed, +9 -14Showing whitespace changes
public/script.js+7 -6
@@ -1474,7 +1474,8 @@ export async function deleteLastMessage() {
14741474 * @param {boolean} [askConfirmation=false] Whether to ask for confirmation before deleting.
14751475 */
14761476export async function deleteMessage(id, swipeDeletionIndex = undefined, askConfirmation = false) {
14771477 ifconst (canDeleteSwipe = swipeDeletionIndex !== undefined) {&& swipeDeletionIndex !== null;
1478+ if (canDeleteSwipe) {
14781479 if (swipeDeletionIndex < 0) {
14791480 throw new Error('Swipe index cannot be negative');
14801481 }
@@ -1486,12 +1487,12 @@ export async function deleteMessage(id, swipeDeletionIndex = undefined, askConfi
14861487 }
14871488 }
14881489
1490+ const minId = getFirstDisplayedMessageId();
14891491 const messageElement = chatElement.find(`.mes[mesid="${id}"]`);
14901492 if (messageElement.length === 0) {
14911493 return;
14921494 }
14931495
1494- const canDeleteSwipe = swipeDeletionIndex !== undefined;
14951496 let deleteOnlySwipe = canDeleteSwipe;
14961497 if (askConfirmation) {
14971498 const result = await callGenericPopup(t`Are you sure you want to delete this message?`, POPUP_TYPE.CONFIRM, null, {
@@ -1515,8 +1516,8 @@ export async function deleteMessage(id, swipeDeletionIndex = undefined, askConfi
15151516
15161517 chat_metadata['tainted'] = true;
15171518
1518- const startFromZero = id === 0;
1519+ const startIndex = [0, minId].includes(id) ? id : null;
15191520 updateViewMessageIds(startFromZerostartIndex);
15201521 await saveChatConditional();
15211522
15221523 if (this_edit_mes_id === id) {
@@ -8258,8 +8259,8 @@ async function importCharacterChat(formData, { refresh = true } = {}) {
82588259 return [];
82598260}
82608261
82618262function updateViewMessageIds(startFromZerostartIndex = falsenull) {
82628263 const minId = startFromZerostartIndex ? 0 :? getFirstDisplayedMessageId();
82638264
82648265 chatElement.find('.mes').each(function (index, element) {
82658266 $(element).attr('mesid', minId + index);
public/scripts/power-user.js+2 -8
@@ -29,6 +29,7 @@ import {
2929 messageFormatting,
3030 extension_prompt_types,
3131 extension_prompt_roles,
32+ deleteMessage,
3233} from '../script.js';
3334import { isMobile, initMovingUI, favsToHotswap } from './RossAscends-mods.js';
3435import {
@@ -2766,7 +2767,6 @@ async function doMesCut(_, text) {
27662767
27672768 for (let i = 0; i < totalMesToCut; i++) {
27682769 cutText += (chat[mesIDToCut]?.mes || '') + '\n';
2769- let done = false;
27702770 let mesToCut = $('#chat').find(`.mes[mesid=${mesIDToCut}]`);
27712771
27722772 if (!mesToCut.length) {
@@ -2778,13 +2778,7 @@ async function doMesCut(_, text) {
27782778 }
27792779
27802780 setEditedMessageId(mesIDToCut);
2781- eventSource.once(event_types.MESSAGE_DELETED, () => {
2781+ await deleteMessage(mesIDToCut, null, false);
2782- done = true;
2783- });
2784- mesToCut.find('.mes_edit_delete').trigger('click', { fromSlashCommand: true });
2785- while (!done) {
2786- await delay(1);
2787- }
27882782 }
27892783
27902784 return cutText;