Refactor hideMessageCallback and unhideMessageCallback to remove unnecessary console logs. Introduce onlyUniqueJson and sortIgnoreCaseAndAccents utility functions
| @@ -3055,10 +3055,6 @@ async function askCharacter(args, text) { | |||
| 3055 | } | 3055 | } |
| 3056 | 3056 | ||
| 3057 | async function hideMessageCallback(args, value) { | 3057 | async function hideMessageCallback(args, value) { |
| 3058 | if (!value) { | ||
| 3059 | console.log('No range provided. Hiding the last message.'); | ||
| 3060 | } | ||
| 3061 | |||
| 3062 | const range = value ? stringToRange(value, 0, chat.length - 1) : { start: chat.length - 1, end: chat.length - 1 }; | 3058 | const range = value ? stringToRange(value, 0, chat.length - 1) : { start: chat.length - 1, end: chat.length - 1 }; |
| 3063 | 3059 | ||
| 3064 | if (!range) { | 3060 | if (!range) { |
| @@ -3072,10 +3068,6 @@ async function hideMessageCallback(args, value) { | |||
| 3072 | } | 3068 | } |
| 3073 | 3069 | ||
| 3074 | async function unhideMessageCallback(args, value) { | 3070 | async function unhideMessageCallback(args, value) { |
| 3075 | if (!value) { | ||
| 3076 | console.log('No range provided. Unhiding the last message'); | ||
| 3077 | } | ||
| 3078 | |||
| 3079 | const range = value ? stringToRange(value, 0, chat.length - 1) : { start: chat.length - 1, end: chat.length - 1 }; | 3071 | const range = value ? stringToRange(value, 0, chat.length - 1) : { start: chat.length - 1, end: chat.length - 1 }; |
| 3080 | 3072 | ||
| 3081 | if (!range) { | 3073 | if (!range) { |
| @@ -3,7 +3,7 @@ import { extension_settings } from '../extensions.js'; | |||
| 3 | import { getGroupMembers, groups } from '../group-chats.js'; | 3 | import { getGroupMembers, groups } from '../group-chats.js'; |
| 4 | import { power_user } from '../power-user.js'; | 4 | import { power_user } from '../power-user.js'; |
| 5 | import { searchCharByName, getTagsList, tags, tag_map } from '../tags.js'; | 5 | import { searchCharByName, getTagsList, tags, tag_map } from '../tags.js'; |
| 6 | import { onlyUnique } from '../utils.js'; | 6 | import { onlyUniqueJson, sortIgnoreCaseAndAccents } from '../utils.js'; |
| 7 | import { world_names } from '../world-info.js'; | 7 | import { world_names } from '../world-info.js'; |
| 8 | import { SlashCommandClosure } from './SlashCommandClosure.js'; | 8 | import { SlashCommandClosure } from './SlashCommandClosure.js'; |
| 9 | import { SlashCommandEnumValue, enumTypes } from './SlashCommandEnumValue.js'; | 9 | import { SlashCommandEnumValue, enumTypes } from './SlashCommandEnumValue.js'; |
| @@ -266,7 +266,14 @@ export const commonEnumProviders = { | |||
| 266 | * | 266 | * |
| 267 | * @returns {SlashCommandEnumValue[]} | 267 | * @returns {SlashCommandEnumValue[]} |
| 268 | */ | 268 | */ |
| 269 | messageNames: () => chat.map((message) => message.name).filter(onlyUnique).sort(Intl.Collator().compare).map(name => new SlashCommandEnumValue(name)), | 269 | messageNames: () => chat |
| 270 | .map(message => ({ | ||
| 271 | name: message.name, | ||
| 272 | icon: message.is_user ? enumIcons.user : enumIcons.assistant, | ||
| 273 | })) | ||
| 274 | .filter(onlyUniqueJson) | ||
| 275 | .sort((a, b) => sortIgnoreCaseAndAccents(a.name, b.name)) | ||
| 276 | .map(name => new SlashCommandEnumValue(name.name, null, null, name.icon)), | ||
| 270 | 277 | ||
| 271 | /** | 278 | /** |
| 272 | * All existing worlds / lorebooks | 279 | * All existing worlds / lorebooks |
| @@ -14,7 +14,7 @@ import { Popup, POPUP_RESULT, POPUP_TYPE } from './popup.js'; | |||
| 14 | import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js'; | 14 | import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js'; |
| 15 | import { getTagsList } from './tags.js'; | 15 | import { getTagsList } from './tags.js'; |
| 16 | import { groups, selected_group } from './group-chats.js'; | 16 | import { groups, selected_group } from './group-chats.js'; |
| 17 | import { getCurrentLocale } from './i18n.js'; | 17 | import { getCurrentLocale, t } from './i18n.js'; |
| 18 | 18 | ||
| 19 | /** | 19 | /** |
| 20 | * Pagination status string template. | 20 | * Pagination status string template. |
| @@ -197,6 +197,17 @@ export function onlyUnique(value, index, array) { | |||
| 197 | } | 197 | } |
| 198 | 198 | ||
| 199 | /** | 199 | /** |
| 200 | * Determines if a value is unique in an array of objects. | ||
| 201 | * @param {any} value Current value. | ||
| 202 | * @param {number} index Current index. | ||
| 203 | * @param {any[]} array The array being processed. | ||
| 204 | * @returns {boolean} True if the value is unique, false otherwise. | ||
| 205 | */ | ||
| 206 | export function onlyUniqueJson(value, index, array) { | ||
| 207 | return array.map(v => JSON.stringify(v)).indexOf(JSON.stringify(value)) === index; | ||
| 208 | } | ||
| 209 | |||
| 210 | /** | ||
| 200 | * Removes the first occurrence of a specified item from an array | 211 | * Removes the first occurrence of a specified item from an array |
| 201 | * | 212 | * |
| 202 | * @param {*[]} array - The array from which to remove the item | 213 | * @param {*[]} array - The array from which to remove the item |
| @@ -1799,8 +1810,9 @@ export function runAfterAnimation(control, callback, timeout = 500) { | |||
| 1799 | * | 1810 | * |
| 1800 | * @param {string} a - The first string to compare. | 1811 | * @param {string} a - The first string to compare. |
| 1801 | * @param {string} b - The second string to compare. | 1812 | * @param {string} b - The second string to compare. |
| 1802 | * @param {(a:string,b:string)=>boolean} comparisonFunction - The function to use for the comparison. | 1813 | * @param {(a:string,b:string)=>T} comparisonFunction - The function to use for the comparison. |
| 1803 | * @returns {*} - The result of the comparison. | 1814 | * @returns {T} - The result of the comparison. |
| 1815 | * @template T | ||
| 1804 | */ | 1816 | */ |
| 1805 | export function compareIgnoreCaseAndAccents(a, b, comparisonFunction) { | 1817 | export function compareIgnoreCaseAndAccents(a, b, comparisonFunction) { |
| 1806 | if (!a || !b) return comparisonFunction(a, b); // Return the comparison result if either string is empty | 1818 | if (!a || !b) return comparisonFunction(a, b); // Return the comparison result if either string is empty |
| @@ -1838,6 +1850,16 @@ export function equalsIgnoreCaseAndAccents(a, b) { | |||
| 1838 | } | 1850 | } |
| 1839 | 1851 | ||
| 1840 | /** | 1852 | /** |
| 1853 | * Performs a case-insensitive and accent-insensitive sort. | ||
| 1854 | * @param {string} a - The first string to compare | ||
| 1855 | * @param {string} b - The second string to compare | ||
| 1856 | * @returns {number} -1 if a < b, 1 if a > b, 0 if a === b | ||
| 1857 | */ | ||
| 1858 | export function sortIgnoreCaseAndAccents(a, b) { | ||
| 1859 | return compareIgnoreCaseAndAccents(a, b, (a, b) => a?.localeCompare(b)); | ||
| 1860 | } | ||
| 1861 | |||
| 1862 | /** | ||
| 1841 | * @typedef {object} Select2Option The option object for select2 controls | 1863 | * @typedef {object} Select2Option The option object for select2 controls |
| 1842 | * @property {string} id - The unique ID inside this select | 1864 | * @property {string} id - The unique ID inside this select |
| 1843 | * @property {string} text - The text for this option | 1865 | * @property {string} text - The text for this option |
| @@ -2231,8 +2253,8 @@ export function findPersona({ name = null, allowAvatar = true, insensitive = tru | |||
| 2231 | // Search for matching personas by name | 2253 | // Search for matching personas by name |
| 2232 | const matchingPersonas = personas.filter(a => matches(a)); | 2254 | const matchingPersonas = personas.filter(a => matches(a)); |
| 2233 | if (matchingPersonas.length > 1) { | 2255 | if (matchingPersonas.length > 1) { |
| 2234 | if (!quiet) toastr.warning('Multiple personas found for given conditions.'); | 2256 | if (!quiet) toastr.warning(t`Multiple personas found for given conditions.`); |
| 2235 | else console.warn('Multiple personas found for given conditions. Returning the first match.'); | 2257 | else console.warn(t`Multiple personas found for given conditions. Returning the first match.`); |
| 2236 | } | 2258 | } |
| 2237 | 2259 | ||
| 2238 | return matchingPersonas[0] || null; | 2260 | return matchingPersonas[0] || null; |
| @@ -2270,8 +2292,8 @@ export function findChar({ name = null, allowAvatar = true, insensitive = true, | |||
| 2270 | if (preferCurrentChar) { | 2292 | if (preferCurrentChar) { |
| 2271 | const preferredCharSearch = currentChars.filter(matches); | 2293 | const preferredCharSearch = currentChars.filter(matches); |
| 2272 | if (preferredCharSearch.length > 1) { | 2294 | if (preferredCharSearch.length > 1) { |
| 2273 | if (!quiet) toastr.warning('Multiple characters found for given conditions.'); | 2295 | if (!quiet) toastr.warning(t`Multiple characters found for given conditions.`); |
| 2274 | else console.warn('Multiple characters found for given conditions. Returning the first match.'); | 2296 | else console.warn(t`Multiple characters found for given conditions. Returning the first match.`); |
| 2275 | } | 2297 | } |
| 2276 | if (preferredCharSearch.length) { | 2298 | if (preferredCharSearch.length) { |
| 2277 | return preferredCharSearch[0]; | 2299 | return preferredCharSearch[0]; |