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
23742374 if (!params.isUser && newMessageId !== 0 && newMessageId !== chat.length - 1) {
23752375 const swipesNum = chat[newMessageId].swipes?.length;
23762376 const swipeId = chat[newMessageId].swipe_id + 1;
23772377 newMessage.find('.swipes-counter').text(`${formatSwipeCounter(swipeId}\u200B/\u200b${, swipesNum}`));
23782378 }
23792379
23802380 if (showSwipes) {
@@ -7487,7 +7487,7 @@ export function showSwipeButtons() {
74877487
74887488 const currentMessage = $('#chat').children().filter(`[mesid="${chat.length - 1}"]`);
74897489 const swipeId = chat[chat.length - 1].swipe_id;
74907490 const swipeCounterText = formatSwipeCounter(`${(swipeId + 1)}\u200B/\u200b${(, chat[chat.length - 1].swipes.length)}`);
74917491 const swipeRight = currentMessage.find('.swipe_right');
74927492 const swipeLeft = currentMessage.find('.swipe_left');
74937493 const swipeCounter = currentMessage.find('.swipes-counter');
@@ -8165,6 +8165,20 @@ window['SillyTavern'].getContext = function () {
81658165 };
81668166};
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+ */
8174+function formatSwipeCounter(current, total) {
8175+ if (isNaN(current) || isNaN(total)) {
8176+ return '';
8177+ }
8178+
8179+ return `${current}\u200b/\u200b${total}`;
8180+}
8181+
81688182function swipe_left() { // when we swipe left..but no generation.
81698183 if (chat.length - 1 === Number(this_edit_mes_id)) {
81708184 closeMessageEditor();