Add utility function for formatting swipe counters

85b0d135f1d709f92405566ba2e801dcf98ff28b

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

1 files changed, +16 -2Showing whitespace changes
public/script.js+16 -2
@@ -2374,7 +2374,7 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll
2374 if (!params.isUser && newMessageId !== 0 && newMessageId !== chat.length - 1) {2374 if (!params.isUser && newMessageId !== 0 && newMessageId !== chat.length - 1) {
2375 const swipesNum = chat[newMessageId].swipes?.length;2375 const swipesNum = chat[newMessageId].swipes?.length;
2376 const swipeId = chat[newMessageId].swipe_id + 1;2376 const swipeId = chat[newMessageId].swipe_id + 1;
2377 newMessage.find('.swipes-counter').text(`${swipeId}\u200B/\u200b${swipesNum}`);2377 newMessage.find('.swipes-counter').text(formatSwipeCounter(swipeId, swipesNum));
2378 }2378 }
23792379
2380 if (showSwipes) {2380 if (showSwipes) {
@@ -7487,7 +7487,7 @@ export function showSwipeButtons() {
74877487
7488 const currentMessage = $('#chat').children().filter(`[mesid="${chat.length - 1}"]`);7488 const currentMessage = $('#chat').children().filter(`[mesid="${chat.length - 1}"]`);
7489 const swipeId = chat[chat.length - 1].swipe_id;7489 const swipeId = chat[chat.length - 1].swipe_id;
7490 const swipeCounterText = (`${(swipeId + 1)}\u200B/\u200b${(chat[chat.length - 1].swipes.length)}`);7490 const swipeCounterText = formatSwipeCounter((swipeId + 1), chat[chat.length - 1].swipes.length);
7491 const swipeRight = currentMessage.find('.swipe_right');7491 const swipeRight = currentMessage.find('.swipe_right');
7492 const swipeLeft = currentMessage.find('.swipe_left');7492 const swipeLeft = currentMessage.find('.swipe_left');
7493 const swipeCounter = currentMessage.find('.swipes-counter');7493 const swipeCounter = currentMessage.find('.swipes-counter');
@@ -8165,6 +8165,20 @@ window['SillyTavern'].getContext = function () {
8165 };8165 };
8166};8166};
81678167
8168/**
8169 * Formats a counter for a swipe view.
8170 * @param {number} current The current number of items.
8171 * @param {number} total The total number of items.
8172 * @returns {string} The formatted counter.
8173 */
8174function formatSwipeCounter(current, total) {
8175 if (isNaN(current) || isNaN(total)) {
8176 return '';
8177 }
8178
8179 return `${current}\u200b/\u200b${total}`;
8180}
8181
8168function swipe_left() { // when we swipe left..but no generation.8182function swipe_left() { // when we swipe left..but no generation.
8169 if (chat.length - 1 === Number(this_edit_mes_id)) {8183 if (chat.length - 1 === Number(this_edit_mes_id)) {
8170 closeMessageEditor();8184 closeMessageEditor();