Merge pull request #3584 from SillyTavern/chid-unify-type Always use string for this_chid
Signed| @@ -553,6 +553,10 @@ let generatedPromptCache = ''; | ||
| 553 | 553 | let generation_started = new Date(); |
| 554 | 554 | /** @type {import('./scripts/char-data.js').v1CharData[]} */ |
| 555 | 555 | export let characters = []; |
| 556 | +/** | |
| 557 | + * Stringified index of a currently chosen entity in the characters array. | |
| 558 | + * @type {string|undefined} Yes, we hate it as much as you do. | |
| 559 | + */ | |
| 556 | 560 | export let this_chid; |
| 557 | 561 | let saveCharactersPage = 0; |
| 558 | 562 | export const default_avatar = 'img/ai4.png'; |
| @@ -1378,7 +1382,7 @@ export async function selectCharacterById(id) { | ||
| 1378 | 1382 | return; |
| 1379 | 1383 | } |
| 1380 | 1384 | |
| 1381 | 1385 | if (selected_group || String(this_chid) !== String(id)) { |
| 1382 | 1386 | //if clicked on a different character from what was currently selected |
| 1383 | 1387 | if (!is_send_press) { |
| 1384 | 1388 | await clearChat(); |
| @@ -1386,7 +1390,7 @@ export async function selectCharacterById(id) { | ||
| 1386 | 1390 | resetSelectedGroup(); |
| 1387 | 1391 | this_edit_mes_id = undefined; |
| 1388 | 1392 | selected_button = 'character_edit'; |
| 1389 | - this_chid = id; | |
| 1393 | + setCharacterId(id); | |
| 1390 | 1394 | chat.length = 0; |
| 1391 | 1395 | chat_metadata = {}; |
| 1392 | 1396 | await getChat(); |
| @@ -6210,7 +6214,7 @@ export function resetChatState() { | ||
| 6210 | 6214 | // replaces deleted charcter name with system user since it will be displayed next. |
| 6211 | 6215 | name2 = (this_chid === undefined && neutralCharacterName) ? neutralCharacterName : systemUserName; |
| 6212 | 6216 | //unsets expected chid before reloading (related to getCharacters/printCharacters from using old arrays) |
| 6213 | - this_chid = undefined; | |
| 6217 | + setCharacterId(undefined); | |
| 6214 | 6218 | // sets up system user to tell user about having deleted a character |
| 6215 | 6219 | chat.splice(0, chat.length, ...SAFETY_CHAT); |
| 6216 | 6220 | // resets chat metadata |
| @@ -6233,8 +6237,29 @@ export function setExternalAbortController(controller) { | ||
| 6233 | 6237 | abortController = controller; |
| 6234 | 6238 | } |
| 6235 | 6239 | |
| 6240 | +/** | |
| 6241 | + * Sets a character array index. | |
| 6242 | + * @param {number|string|undefined} value | |
| 6243 | + */ | |
| 6236 | 6244 | export function setCharacterId(value) { |
| 6237 | - this_chid = value; | |
| 6245 | + switch (typeof value) { | |
| 6246 | + case 'bigint': | |
| 6247 | + case 'number': | |
| 6248 | + this_chid = String(value); | |
| 6249 | + break; | |
| 6250 | + case 'string': | |
| 6251 | + this_chid = !isNaN(parseInt(value)) ? value : undefined; | |
| 6252 | + break; | |
| 6253 | + case 'object': | |
| 6254 | + this_chid = characters.indexOf(value) !== -1 ? String(characters.indexOf(value)) : undefined; | |
| 6255 | + break; | |
| 6256 | + case 'undefined': | |
| 6257 | + this_chid = undefined; | |
| 6258 | + break; | |
| 6259 | + default: | |
| 6260 | + console.error('Invalid character ID type:', value); | |
| 6261 | + break; | |
| 6262 | + } | |
| 6238 | 6263 | } |
| 6239 | 6264 | |
| 6240 | 6265 | export function setCharacterName(value) { |
| @@ -6350,13 +6375,13 @@ export async function renameCharacter(name = null, { silent = false, renameChats | ||
| 6350 | 6375 | |
| 6351 | 6376 | if (newChId !== -1) { |
| 6352 | 6377 | // Select the character after the renaming |
| 6353 | - this_chid = -1; | |
| 6378 | + setCharacterId(undefined); | |
| 6354 | 6379 | await selectCharacterById(newChId); |
| 6355 | 6380 | |
| 6356 | 6381 | // Async delay to update UI |
| 6357 | 6382 | await delay(1); |
| 6358 | 6383 | |
| 6359 | 6384 | if (this_chid === -1undefined) { |
| 6360 | 6385 | throw new Error('New character not selected'); |
| 6361 | 6386 | } |
| 6362 | 6387 | |
| @@ -7658,7 +7683,7 @@ export function select_rm_info(type, charId, previousCharId = null) { | ||
| 7658 | 7683 | if (previousCharId) { |
| 7659 | 7684 | const newId = characters.findIndex((x) => x.avatar == previousCharId); |
| 7660 | 7685 | if (newId >= 0) { |
| 7661 | - this_chid = newId; | |
| 7686 | + setCharacterId(newId); | |
| 7662 | 7687 | } |
| 7663 | 7688 | } |
| 7664 | 7689 | } |
| @@ -395,7 +395,7 @@ class BulkEditOverlay { | ||
| 395 | 395 | |
| 396 | 396 | /** |
| 397 | 397 | * @typedef {object} LastSelected - An object noting the last selected character and its state. |
| 398 | 398 | * @property {stringnumber} [characterId] - The character id of the last selected character. |
| 399 | 399 | * @property {boolean} [select] - The selected state of the last selected character. <c>true</c> if it was selected, <c>false</c> if it was deselected. |
| 400 | 400 | */ |
| 401 | 401 | |
| @@ -675,7 +675,7 @@ class BulkEditOverlay { | ||
| 675 | 675 | const characterId = Number(currentCharacter.getAttribute('data-chid')); |
| 676 | 676 | const select = !this.selectedCharacters.includes(characterId); |
| 677 | 677 | |
| 678 | 678 | if (this.lastSelected.characterId >= 0 && this.lastSelected.select !== undefined) { |
| 679 | 679 | // Only if select state and the last select state match we execute the range select |
| 680 | 680 | if (select === this.lastSelected.select) { |
| 681 | 681 | this.toggleCharactersInRange(currentCharacter, select); |