Fixes for expressions plugin

This commit is contained in:
SillyLossy
2023-04-17 00:13:31 +03:00
parent 0ac5cc70ca
commit 339f212981

View File

@@ -38,6 +38,7 @@ const DEFAULT_EXPRESSIONS = [
let expressionsList = null; let expressionsList = null;
let lastCharacter = undefined; let lastCharacter = undefined;
let lastMessage = null; let lastMessage = null;
let existingExpressions = [];
let inApiCall = false; let inApiCall = false;
function onExpressionsShowDefaultInput() { function onExpressionsShowDefaultInput() {
@@ -96,11 +97,16 @@ async function moduleWorker() {
$('.expression_settings .offline_mode').css('display', 'none'); $('.expression_settings .offline_mode').css('display', 'none');
} }
// character has no expressions or it is not loaded
if (!context.groupId && existingExpressions.length === 0) {
lastCharacter = context.groupId || context.characterId;
return;
}
// check if last message changed // check if last message changed
const currentLastMessage = getLastCharacterMessage(); const currentLastMessage = getLastCharacterMessage();
if ((lastCharacter === context.characterId || lastCharacter === context.groupId) if ((lastCharacter === context.characterId || lastCharacter === context.groupId)
&& lastMessage === currentLastMessage.mes && lastMessage === currentLastMessage.mes) {
&& $('img.expression').attr('src')) {
return; return;
} }
@@ -163,6 +169,7 @@ async function validateImages() {
} }
imagesValidating = true; imagesValidating = true;
existingExpressions = [];
const context = getContext(); const context = getContext();
$('.expression_settings').show(); $('.expression_settings').show();
$('#image_list').empty(); $('#image_list').empty();
@@ -172,18 +179,19 @@ async function validateImages() {
return; return;
} }
const IMAGE_LIST = (await getExpressionsList()).map(x => `${x}.png`); const IMAGE_LIST = (await getExpressionsList()).map(x => ({ name: x, file: `${x}.png` }));
IMAGE_LIST.forEach((item) => { IMAGE_LIST.forEach((item) => {
const image = document.createElement('img'); const image = document.createElement('img');
image.src = `/characters/${context.name2}/${item}`; image.src = `/characters/${context.name2}/${item.file}`;
image.classList.add('debug-image'); image.classList.add('debug-image');
image.width = '0px'; image.width = '0px';
image.height = '0px'; image.height = '0px';
image.onload = function () { image.onload = function () {
$('#image_list').append(getListItem(item, image.src, 'success')); existingExpressions.push(item.name);
$('#image_list').append(getListItem(item.file, image.src, 'success'));
} }
image.onerror = function () { image.onerror = function () {
$('#image_list').append(getListItem(item, '/img/No-Image-Placeholder.svg', 'failure')); $('#image_list').append(getListItem(item.file, '/img/No-Image-Placeholder.svg', 'failure'));
} }
$('#image_list').prepend(image); $('#image_list').prepend(image);
}); });
@@ -232,16 +240,9 @@ async function getExpressionsList() {
async function setExpression(character, expression, force) { async function setExpression(character, expression, force) {
const filename = `${expression}.png`; const filename = `${expression}.png`;
const debugImageStatus = document.querySelector(`#image_list div[id="${filename}"] span`);
const img = $('img.expression'); const img = $('img.expression');
if (!debugImageStatus && !force) { if (force || (existingExpressions.includes(expression))) {
validateImages();
setTimeout(() => setExpression(character, expression, false), 2000);
return;
}
if (force || (debugImageStatus && !debugImageStatus.classList.contains('failure'))) {
//console.log('setting expression from character images folder'); //console.log('setting expression from character images folder');
const imgUrl = `/characters/${character}/${filename}`; const imgUrl = `/characters/${character}/${filename}`;
img.attr('src', imgUrl); img.attr('src', imgUrl);
@@ -275,6 +276,9 @@ function onClickExpressionImage() {
function addExpressionImage() { function addExpressionImage() {
const html = `<div class="expression-holder"><img class="expression"></div>`; const html = `<div class="expression-holder"><img class="expression"></div>`;
$('body').append(html); $('body').append(html);
$('img.expression').on('error', function () {
$(this).attr('src', '');
});
} }
function addSettings() { function addSettings() {
const html = ` const html = `