Fix group VN updates on switching to chat
| @@ -178,6 +178,7 @@ async function visualNovelRemoveInactive(container) { | ||
| 178 | 178 | * @returns {Promise<Array>} - An array of promises that resolve when the sprites are set |
| 179 | 179 | */ |
| 180 | 180 | async function visualNovelSetCharacterSprites(vnContainer, spriteFolderName, expression) { |
| 181 | + const originalExpression = expression; | |
| 181 | 182 | const context = getContext(); |
| 182 | 183 | const group = context.groups.find(x => x.id == context.groupId); |
| 183 | 184 | |
| @@ -208,6 +209,10 @@ async function visualNovelSetCharacterSprites(vnContainer, spriteFolderName, exp | ||
| 208 | 209 | |
| 209 | 210 | const prevExpressionSrc = expressionImage.find('img').attr('src') || null; |
| 210 | 211 | |
| 212 | + if (!originalExpression && Array.isArray(spriteCache[memberSpriteFolderName]) && spriteCache[memberSpriteFolderName].length > 0) { | |
| 213 | + expression = await getLastMessageSprite(avatar); | |
| 214 | + } | |
| 215 | + | |
| 211 | 216 | const spriteFile = chooseSpriteForExpression(memberSpriteFolderName, expression, { prevExpressionSrc: prevExpressionSrc }); |
| 212 | 217 | if (expressionImage.length) { |
| 213 | 218 | if (!spriteFolderName || spriteFolderName == memberSpriteFolderName) { |
| @@ -251,6 +256,23 @@ async function visualNovelSetCharacterSprites(vnContainer, spriteFolderName, exp | ||
| 251 | 256 | return setSpritePromises; |
| 252 | 257 | } |
| 253 | 258 | |
| 259 | +/** | |
| 260 | + * Classifies the text of the latest message and returns the expression label. | |
| 261 | + * @param {string} avatar - The avatar of the character to get the last message for | |
| 262 | + * @returns {Promise<string>} - The expression label | |
| 263 | + */ | |
| 264 | +async function getLastMessageSprite(avatar) { | |
| 265 | + const context = getContext(); | |
| 266 | + const lastMessage = context.chat.slice().reverse().find(x => x.original_avatar == avatar || (x.force_avatar && x.force_avatar.includes(encodeURIComponent(avatar)))); | |
| 267 | + | |
| 268 | + if (lastMessage) { | |
| 269 | + const text = lastMessage.mes || ''; | |
| 270 | + return await getExpressionLabel(text); | |
| 271 | + } | |
| 272 | + | |
| 273 | + return null; | |
| 274 | +} | |
| 275 | + | |
| 254 | 276 | async function visualNovelUpdateLayers(container) { |
| 255 | 277 | const context = getContext(); |
| 256 | 278 | const group = context.groups.find(x => x.id == context.groupId); |
| @@ -445,7 +467,7 @@ async function setImage(img, path) { | ||
| 445 | 467 | }); |
| 446 | 468 | } |
| 447 | 469 | |
| 448 | 470 | async function moduleWorker({ newChat = false } = {}) { |
| 449 | 471 | const context = getContext(); |
| 450 | 472 | |
| 451 | 473 | // non-characters not supported |
| @@ -514,6 +536,10 @@ async function moduleWorker() { | ||
| 514 | 536 | offlineMode.css('display', 'none'); |
| 515 | 537 | } |
| 516 | 538 | |
| 539 | + if (context.groupId && vnMode && newChat) { | |
| 540 | + await forceUpdateVisualNovelMode(); | |
| 541 | + } | |
| 542 | + | |
| 517 | 543 | // Don't bother classifying if current char has no sprites and no default expressions are enabled |
| 518 | 544 | if ((!Array.isArray(spriteCache[spriteFolderName]) || spriteCache[spriteFolderName].length === 0) && !extension_settings.expressions.showDefault) { |
| 519 | 545 | return; |
| @@ -2134,7 +2160,7 @@ function migrateSettings() { | ||
| 2134 | 2160 | $('#visual-novel-wrapper').empty(); |
| 2135 | 2161 | } |
| 2136 | 2162 | |
| 2137 | 2163 | updateFunction({ newChat: true }); |
| 2138 | 2164 | }); |
| 2139 | 2165 | eventSource.on(event_types.MOVABLE_PANELS_RESET, updateVisualNovelModeDebounced); |
| 2140 | 2166 | eventSource.on(event_types.GROUP_UPDATED, updateVisualNovelModeDebounced); |