Add scroll behavior options for media appending
This commit is contained in:
+11
-8
@@ -183,7 +183,7 @@ import {
|
||||
trimSpaces,
|
||||
clamp,
|
||||
} from './scripts/utils.js';
|
||||
import { debounce_timeout, GENERATION_TYPE_TRIGGERS, IGNORE_SYMBOL, inject_ids, MEDIA_DISPLAY, MEDIA_TYPE, SWIPE_DIRECTION } from './scripts/constants.js';
|
||||
import { debounce_timeout, GENERATION_TYPE_TRIGGERS, IGNORE_SYMBOL, inject_ids, MEDIA_DISPLAY, MEDIA_TYPE, SCROLL_BEHAVIOR, SWIPE_DIRECTION } from './scripts/constants.js';
|
||||
|
||||
import { cancelDebouncedMetadataSave, doDailyExtensionUpdatesCheck, extension_settings, initExtensions, loadExtensionSettings, runGenerationInterceptors, saveMetadataDebounced } from './scripts/extensions.js';
|
||||
import { COMMENT_NAME_DEFAULT, CONNECT_API_MAP, executeSlashCommandsOnChatInput, initDefaultSlashCommands, isExecutingCommandsFromChatInput, pauseScriptExecution, stopScriptExecution, UNIQUE_APIS } from './scripts/slash-commands.js';
|
||||
@@ -2071,9 +2071,9 @@ export function getMediaIndex(mes) {
|
||||
* Appends image or file to the message element.
|
||||
* @param {ChatMessage} mes Message object
|
||||
* @param {JQuery<HTMLElement>} messageElement Message element
|
||||
* @param {boolean} [adjustScroll=true] Whether to adjust the scroll position after appending the media
|
||||
* @param {string} [scrollBehavior] Scroll behavior when adjusting scroll position
|
||||
*/
|
||||
export function appendMediaToMessage(mes, messageElement, adjustScroll = true) {
|
||||
export function appendMediaToMessage(mes, messageElement, scrollBehavior = SCROLL_BEHAVIOR.ADJUST) {
|
||||
ensureMessageMediaIsArray(mes);
|
||||
|
||||
const hasMedia = Array.isArray(mes?.extra?.media) && mes.extra.media.length > 0;
|
||||
@@ -2084,13 +2084,16 @@ export function appendMediaToMessage(mes, messageElement, adjustScroll = true) {
|
||||
const mediaBlocks = [];
|
||||
const mediaPromises = [];
|
||||
|
||||
const chatHeight = adjustScroll && (hasMedia || hasFiles) ? chatElement.prop('scrollHeight') : 0;
|
||||
const scrollPosition = chatElement.scrollTop();
|
||||
const chatHeight = (hasMedia || hasFiles) ? chatElement.prop('scrollHeight') : 0;
|
||||
const scrollPosition = (hasMedia || hasFiles) ? chatElement.scrollTop() : 0;
|
||||
const doAdjustScroll = () => {
|
||||
if (!hasMedia && !hasFiles) {
|
||||
return;
|
||||
}
|
||||
if (!adjustScroll) {
|
||||
if (scrollBehavior === SCROLL_BEHAVIOR.NONE) {
|
||||
return;
|
||||
}
|
||||
if (scrollBehavior === SCROLL_BEHAVIOR.KEEP) {
|
||||
chatElement.scrollTop(scrollPosition);
|
||||
return;
|
||||
}
|
||||
@@ -2397,7 +2400,7 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll
|
||||
swipeMessage.find('.mes_text').html(messageText).attr('title', title);
|
||||
swipeMessage.find('.timestamp').text(timestamp).attr('title', `${params.extra.api} - ${params.extra.model}`);
|
||||
updateReasoningUI(swipeMessage);
|
||||
appendMediaToMessage(mes, swipeMessage);
|
||||
appendMediaToMessage(mes, swipeMessage, scroll ? SCROLL_BEHAVIOR.ADJUST : SCROLL_BEHAVIOR.NONE);
|
||||
if (power_user.timestamp_model_icon && params.extra?.api) {
|
||||
insertSVGIcon(swipeMessage, params.extra);
|
||||
}
|
||||
@@ -2412,7 +2415,7 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll
|
||||
} else {
|
||||
const messageId = forceId ?? chat.length - 1;
|
||||
chatElement.find(`[mesid="${messageId}"] .mes_text`).append(messageText);
|
||||
appendMediaToMessage(mes, newMessage);
|
||||
appendMediaToMessage(mes, newMessage, scroll ? SCROLL_BEHAVIOR.ADJUST : SCROLL_BEHAVIOR.NONE);
|
||||
showSwipes && hideSwipeButtons();
|
||||
}
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ import { renderTemplateAsync } from './templates.js';
|
||||
import { t } from './i18n.js';
|
||||
import { humanizedDateTime } from './RossAscends-mods.js';
|
||||
import { accountStorage } from './util/AccountStorage.js';
|
||||
import { MEDIA_DISPLAY, MEDIA_TYPE, SWIPE_DIRECTION } from './constants.js';
|
||||
import { MEDIA_DISPLAY, MEDIA_TYPE, SCROLL_BEHAVIOR, SWIPE_DIRECTION } from './constants.js';
|
||||
|
||||
/**
|
||||
* @typedef {Object} FileAttachment
|
||||
@@ -418,7 +418,7 @@ async function deleteMessageFile(messageBlock, messageId, fileIndex) {
|
||||
await saveChatConditional();
|
||||
await deleteFileFromServer(url);
|
||||
|
||||
appendMediaToMessage(message, messageBlock, false);
|
||||
appendMediaToMessage(message, messageBlock, SCROLL_BEHAVIOR.KEEP);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -489,7 +489,7 @@ function embedMessageFile(messageId, messageBlock) {
|
||||
|
||||
await populateFileAttachment(message, 'embed_file_input');
|
||||
await eventSource.emit(event_types.MESSAGE_FILE_EMBEDDED, messageId);
|
||||
appendMediaToMessage(message, messageBlock, false);
|
||||
appendMediaToMessage(message, messageBlock, SCROLL_BEHAVIOR.KEEP);
|
||||
await saveChatConditional();
|
||||
}
|
||||
}
|
||||
@@ -1022,7 +1022,7 @@ async function deleteMessageMedia(messageId, mediaIndex, messageBlock) {
|
||||
}
|
||||
|
||||
await saveChatConditional();
|
||||
appendMediaToMessage(message, messageBlock, false);
|
||||
appendMediaToMessage(message, messageBlock, SCROLL_BEHAVIOR.KEEP);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1051,7 +1051,7 @@ async function switchMessageMediaDisplay(messageId, messageBlock, targetDisplay)
|
||||
|
||||
message.extra.media_display = targetDisplay;
|
||||
await saveChatConditional();
|
||||
appendMediaToMessage(message, messageBlock, false);
|
||||
appendMediaToMessage(message, messageBlock, SCROLL_BEHAVIOR.KEEP);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -93,6 +93,17 @@ export const MEDIA_TYPE = {
|
||||
VIDEO: 'video',
|
||||
};
|
||||
|
||||
/**
|
||||
* Scroll behavior options when appending media to messages.
|
||||
* @enum {string}
|
||||
* @readonly
|
||||
*/
|
||||
export const SCROLL_BEHAVIOR = {
|
||||
NONE: 'none',
|
||||
KEEP: 'keep',
|
||||
ADJUST: 'adjust',
|
||||
};
|
||||
|
||||
/**
|
||||
* @type {{readonly LEFT: 'left', readonly RIGHT: 'right'}}
|
||||
*/
|
||||
|
||||
@@ -10,7 +10,7 @@ import { SlashCommand } from '../../slash-commands/SlashCommand.js';
|
||||
import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '../../slash-commands/SlashCommandArgument.js';
|
||||
import { commonEnumProviders } from '../../slash-commands/SlashCommandCommonEnumsProvider.js';
|
||||
import { callGenericPopup, Popup, POPUP_TYPE } from '../../popup.js';
|
||||
import { MEDIA_DISPLAY, MEDIA_TYPE } from '../../constants.js';
|
||||
import { MEDIA_DISPLAY, MEDIA_TYPE, SCROLL_BEHAVIOR } from '../../constants.js';
|
||||
export { MODULE_NAME };
|
||||
|
||||
const MODULE_NAME = 'caption';
|
||||
@@ -692,7 +692,7 @@ jQuery(async function () {
|
||||
const imageIndex = Number(imageBlock.attr('data-index'));
|
||||
const data = getContext().chat[messageId];
|
||||
await captionExistingMessage(data, imageIndex);
|
||||
appendMediaToMessage(data, messageBlock, false);
|
||||
appendMediaToMessage(data, messageBlock, SCROLL_BEHAVIOR.KEEP);
|
||||
await saveChatConditional();
|
||||
} catch (e) {
|
||||
console.error('Message image recaption failed', e);
|
||||
|
||||
@@ -52,7 +52,7 @@ import {
|
||||
SlashCommandArgument,
|
||||
SlashCommandNamedArgument,
|
||||
} from '../../slash-commands/SlashCommandArgument.js';
|
||||
import { debounce_timeout, MEDIA_DISPLAY, MEDIA_TYPE, VIDEO_EXTENSIONS } from '../../constants.js';
|
||||
import { debounce_timeout, MEDIA_DISPLAY, MEDIA_TYPE, SCROLL_BEHAVIOR, VIDEO_EXTENSIONS } from '../../constants.js';
|
||||
import { SlashCommandEnumValue } from '../../slash-commands/SlashCommandEnumValue.js';
|
||||
import { callGenericPopup, Popup, POPUP_TYPE } from '../../popup.js';
|
||||
import { commonEnumProviders } from '../../slash-commands/SlashCommandCommonEnumsProvider.js';
|
||||
@@ -4290,7 +4290,7 @@ async function sdMessageButton(e) {
|
||||
message.extra.media.push(newMediaAttachment);
|
||||
message.extra.media_index = message.extra.media.length - 1;
|
||||
|
||||
appendMediaToMessage(message, messageElement, false);
|
||||
appendMediaToMessage(message, messageElement, SCROLL_BEHAVIOR.KEEP);
|
||||
|
||||
await context.saveChat();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user