Update backend returned sprites list

a072951102f40be153a019ccb4443305654387da

Wolfsblvt <wolfsblvt@gmail.com>

1 files changed, +34 -5Showing whitespace changes
public/scripts/extensions/expressions/index.js+34 -5
@@ -20,7 +20,7 @@ export { MODULE_NAME };
2020/**
2121* @typedef {object} Expression Expression definition with label and file path
2222* @property {string} label The label of the expression
2323 * @property {stringExpressionImage[]} pathfiles TheOne pathor more images to therepresent expressionthis imageexpression
2424*/
2525
2626/**
@@ -86,6 +86,7 @@ let lastCharacter = undefined;
8686let lastMessage = null;
8787let lastTalkingState = false;
8888let lastTalkingStateMessage = null; // last message as seen by `updateTalkingState` (tracked separately, different timer)
89+/** @type {{[characterKey: string]: Expression[]}} */
8990let spriteCache = {};
9091let inApiCall = false;
9192let lastServerResponseTime = 0;
@@ -1307,6 +1308,11 @@ function removeExpression() {
13071308 $('#no_chat_expressions').show();
13081309}
13091310
1311+/**
1312+ * Validate a character's sprites, and redraw the sprites list if not done before or forced to redraw.
1313+ * @param {string} character - The character to validate
1314+ * @param {boolean} forceRedrawCached - Whether to force redrawing the sprites list even if it's already been drawn before
1315+ */
13101316async function validateImages(character, forceRedrawCached) {
13111317 if (!character) {
13121318 return;
@@ -1330,7 +1336,7 @@ async function validateImages(character, forceRedrawCached) {
13301336
13311337/**
13321338 * Takes a given sprite as returned from the server, and enriches it with additional data for display/sorting
13331339 * @param {Expression{ path: string, label: string }} sprite
13341340 * @returns {ExpressionImage}
13351341 */
13361342function getExpressionImageData(sprite) {
@@ -1371,7 +1377,8 @@ async function drawSpritesList(character, labels, sprites) {
13711377 const isCustom = extension_settings.expressions.custom?.includes(expression);
13721378 const images = sprites
13731379 .filter(s => s.label === expression)
13741380 .map(getExpressionImageDatas => s.files)
1381+ .flat()
13751382 .sort((a, b) => a.title.localeCompare(b.title));
13761383
13771384 if (images.length === 0) {
@@ -1384,7 +1391,7 @@ async function drawSpritesList(character, labels, sprites) {
13841391 }
13851392
13861393 // TODO: Fix valid expression lists/caching and group them correctly
13871394 validExpressions.push({ label: expression, pathsfiles: images });
13881395
13891396 // Render main = first file, additional = rest
13901397 let listItem = await getListItem(expression, {
@@ -1408,13 +1415,35 @@ async function getListItem(expression, { images, isCustom = false } = {}) {
14081415 return renderExtensionTemplateAsync(MODULE_NAME, 'list-item', { expression, images, isCustom: isCustom ?? false });
14091416}
14101417
1418+/**
1419+ * Fetches and processes the list of sprites for a given character name.
1420+ * Retrieves sprite data from the server and organizes it into labeled groups.
1421+ *
1422+ * @param {string} name - The character name to fetch sprites for
1423+ * @returns {Promise<Expression[]>} A promise that resolves to an array of grouped expression objects, each containing a label and associated image data
1424+ */
1425+
14111426async function getSpritesList(name) {
14121427 console.debug('getting sprites list');
14131428
14141429 try {
14151430 const result = await fetch(`/api/sprites/get?name=${encodeURIComponent(name)}`);
1431+ /** @type {{ label: string, path: string }[]} */
14161432 let sprites = result.ok ? (await result.json()) : [];
1417- return sprites;
1433+
1434+ /** @type {Expression[]} */
1435+ const grouped = sprites.reduce((acc, sprite) => {
1436+ const imageData = getExpressionImageData(sprite);
1437+ let existingExpression = acc.find(exp => exp.label === sprite.label);
1438+ if (existingExpression) {
1439+ existingExpression.files.push(imageData);
1440+ } else {
1441+ acc.push({ label: sprite.label, files: [imageData] });
1442+ }
1443+
1444+ return acc;
1445+ }, []);
1446+ return grouped;
14181447 }
14191448 catch (err) {
14201449 console.log(err);