Add button to re-caption message image

This commit is contained in:
Cohee 2024-06-30 00:31:54 +03:00
parent 7149f46c9a
commit f181d1a847
4 changed files with 32 additions and 6 deletions

View File

@ -5686,10 +5686,11 @@
<div class="mes_edit_cancel menu_button fa-solid fa-xmark" title="Cancel" data-i18n="[title]Cancel"></div>
</div>
</div>
<div class=mes_text></div>
<div class="mes_text"></div>
<div class="mes_img_container">
<div class="mes_img_controls">
<div title="Enlarge" class="right_menu_button fa-lg fa-solid fa-magnifying-glass mes_img_enlarge" data-i18n="[title]Enlarge"></div>
<div title="Caption" class="right_menu_button fa-lg fa-solid fa-envelope-open-text mes_img_caption" data-i18n="[title]Caption"></div>
<div title="Delete" class="right_menu_button fa-lg fa-solid fa-trash-can mes_img_delete" data-i18n="[title]Delete"></div>
</div>
<img class="mes_img" src="" />

View File

@ -2079,14 +2079,23 @@ export function updateMessageBlock(messageId, message) {
appendMediaToMessage(message, messageElement);
}
export function appendMediaToMessage(mes, messageElement) {
/**
* Appends image or file to the message element.
* @param {object} mes Message object
* @param {JQuery<HTMLElement>} messageElement Message element
* @param {boolean} [adjustScroll=true] Whether to adjust the scroll position after appending the media
*/
export function appendMediaToMessage(mes, messageElement, adjustScroll = true) {
// Add image to message
if (mes.extra?.image) {
const chatHeight = $('#chat').prop('scrollHeight');
const image = messageElement.find('.mes_img');
const text = messageElement.find('.mes_text');
const isInline = !!mes.extra?.inline_image;
image.on('load', function () {
image.off('load').on('load', function () {
if (!adjustScroll) {
return;
}
const scrollPosition = $('#chat').scrollTop();
const newChatHeight = $('#chat').prop('scrollHeight');
const diff = newChatHeight - chatHeight;
@ -5636,7 +5645,7 @@ export async function renameCharacter(name = null, { silent = false, renameChats
}
}
catch (error) {
// Reloading to prevent data corruption
// Reloading to prevent data corruption
if (!silent) await callPopup('Something went wrong. The page will be reloaded.', 'text');
else toastr.error('Something went wrong. The page will be reloaded.', 'Rename Character');

View File

@ -1,6 +1,6 @@
import { ensureImageFormatSupported, getBase64Async, isTrueBoolean, saveBase64AsFile } from '../../utils.js';
import { getContext, getApiUrl, doExtrasFetch, extension_settings, modules, renderExtensionTemplateAsync } from '../../extensions.js';
import { callPopup, eventSource, event_types, getRequestHeaders, saveSettingsDebounced, substituteParamsExtended } from '../../../script.js';
import { appendMediaToMessage, callPopup, eventSource, event_types, getRequestHeaders, saveChatConditional, saveSettingsDebounced, substituteParamsExtended } from '../../../script.js';
import { getMessageTimeStamp } from '../../RossAscends-mods.js';
import { SECRET_KEYS, secret_state } from '../../secrets.js';
import { getMultimodalCaption } from '../shared.js';
@ -514,6 +514,15 @@ jQuery(async function () {
eventSource.on(event_types.MESSAGE_SENT, onMessageEvent);
eventSource.on(event_types.MESSAGE_FILE_EMBEDDED, onMessageEvent);
$(document).on('click', '.mes_img_caption', async function () {
const messageBlock = $(this).closest('.mes');
const index = Number(messageBlock.attr('mesid'));
const data = getContext().chat[index];
await captionExistingMessage(data);
appendMediaToMessage(data, messageBlock, false);
await saveChatConditional();
});
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'caption',
callback: captionCommandCallback,
returns: 'caption',
@ -548,4 +557,6 @@ jQuery(async function () {
</div>
`,
}));
document.body.classList.add('caption');
});

View File

@ -4481,7 +4481,8 @@ a {
}
.mes_img_controls .right_menu_button {
filter: brightness(80%);
filter: brightness(90%);
text-shadow: 1px 1px var(--SmartThemeShadowColor) !important;
padding: 1px;
height: 1.25em;
width: 1.25em;
@ -4506,6 +4507,10 @@ a {
display: flex;
}
body:not(.caption) .mes_img_caption {
display: none;
}
.img_enlarged_holder {
/* Scaling via flex-grow and object-fit only works if we have some kind of base-height set */
min-height: 120px;