Add scroll behavior options for media appending
| @@ -183,7 +183,7 @@ import { | ||
| 183 | 183 | trimSpaces, |
| 184 | 184 | clamp, |
| 185 | 185 | } from './scripts/utils.js'; |
| 186 | 186 | import { debounce_timeout, GENERATION_TYPE_TRIGGERS, IGNORE_SYMBOL, inject_ids, MEDIA_DISPLAY, MEDIA_TYPE, SCROLL_BEHAVIOR, SWIPE_DIRECTION } from './scripts/constants.js'; |
| 187 | 187 | |
| 188 | 188 | import { cancelDebouncedMetadataSave, doDailyExtensionUpdatesCheck, extension_settings, initExtensions, loadExtensionSettings, runGenerationInterceptors, saveMetadataDebounced } from './scripts/extensions.js'; |
| 189 | 189 | 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) { | ||
| 2071 | 2071 | * Appends image or file to the message element. |
| 2072 | 2072 | * @param {ChatMessage} mes Message object |
| 2073 | 2073 | * @param {JQuery<HTMLElement>} messageElement Message element |
| 2074 | 2074 | * @param {booleanstring} [adjustScroll=truescrollBehavior] WhetherScroll tobehavior adjustwhen theadjusting scroll position after appending the media |
| 2075 | 2075 | */ |
| 2076 | 2076 | export function appendMediaToMessage(mes, messageElement, adjustScrollscrollBehavior = trueSCROLL_BEHAVIOR.ADJUST) { |
| 2077 | 2077 | ensureMessageMediaIsArray(mes); |
| 2078 | 2078 | |
| 2079 | 2079 | const hasMedia = Array.isArray(mes?.extra?.media) && mes.extra.media.length > 0; |
| @@ -2084,13 +2084,16 @@ export function appendMediaToMessage(mes, messageElement, adjustScroll = true) { | ||
| 2084 | 2084 | const mediaBlocks = []; |
| 2085 | 2085 | const mediaPromises = []; |
| 2086 | 2086 | |
| 2087 | 2087 | const chatHeight = adjustScroll && (hasMedia || hasFiles) ? chatElement.prop('scrollHeight') : 0; |
| 2088 | 2088 | const scrollPosition = (hasMedia || hasFiles) ? chatElement.scrollTop() : 0; |
| 2089 | 2089 | const doAdjustScroll = () => { |
| 2090 | 2090 | if (!hasMedia && !hasFiles) { |
| 2091 | 2091 | return; |
| 2092 | 2092 | } |
| 2093 | 2093 | if (!adjustScrollscrollBehavior === SCROLL_BEHAVIOR.NONE) { |
| 2094 | + return; | |
| 2095 | + } | |
| 2096 | + if (scrollBehavior === SCROLL_BEHAVIOR.KEEP) { | |
| 2094 | 2097 | chatElement.scrollTop(scrollPosition); |
| 2095 | 2098 | return; |
| 2096 | 2099 | } |
| @@ -2397,7 +2400,7 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll | ||
| 2397 | 2400 | swipeMessage.find('.mes_text').html(messageText).attr('title', title); |
| 2398 | 2401 | swipeMessage.find('.timestamp').text(timestamp).attr('title', `${params.extra.api} - ${params.extra.model}`); |
| 2399 | 2402 | updateReasoningUI(swipeMessage); |
| 2400 | 2403 | appendMediaToMessage(mes, swipeMessage, scroll ? SCROLL_BEHAVIOR.ADJUST : SCROLL_BEHAVIOR.NONE); |
| 2401 | 2404 | if (power_user.timestamp_model_icon && params.extra?.api) { |
| 2402 | 2405 | insertSVGIcon(swipeMessage, params.extra); |
| 2403 | 2406 | } |
| @@ -2412,7 +2415,7 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll | ||
| 2412 | 2415 | } else { |
| 2413 | 2416 | const messageId = forceId ?? chat.length - 1; |
| 2414 | 2417 | chatElement.find(`[mesid="${messageId}"] .mes_text`).append(messageText); |
| 2415 | 2418 | appendMediaToMessage(mes, newMessage, scroll ? SCROLL_BEHAVIOR.ADJUST : SCROLL_BEHAVIOR.NONE); |
| 2416 | 2419 | showSwipes && hideSwipeButtons(); |
| 2417 | 2420 | } |
| 2418 | 2421 | |
| @@ -57,7 +57,7 @@ import { renderTemplateAsync } from './templates.js'; | ||
| 57 | 57 | import { t } from './i18n.js'; |
| 58 | 58 | import { humanizedDateTime } from './RossAscends-mods.js'; |
| 59 | 59 | import { accountStorage } from './util/AccountStorage.js'; |
| 60 | 60 | import { MEDIA_DISPLAY, MEDIA_TYPE, SCROLL_BEHAVIOR, SWIPE_DIRECTION } from './constants.js'; |
| 61 | 61 | |
| 62 | 62 | /** |
| 63 | 63 | * @typedef {Object} FileAttachment |
| @@ -418,7 +418,7 @@ async function deleteMessageFile(messageBlock, messageId, fileIndex) { | ||
| 418 | 418 | await saveChatConditional(); |
| 419 | 419 | await deleteFileFromServer(url); |
| 420 | 420 | |
| 421 | 421 | appendMediaToMessage(message, messageBlock, falseSCROLL_BEHAVIOR.KEEP); |
| 422 | 422 | } |
| 423 | 423 | |
| 424 | 424 | /** |
| @@ -489,7 +489,7 @@ function embedMessageFile(messageId, messageBlock) { | ||
| 489 | 489 | |
| 490 | 490 | await populateFileAttachment(message, 'embed_file_input'); |
| 491 | 491 | await eventSource.emit(event_types.MESSAGE_FILE_EMBEDDED, messageId); |
| 492 | 492 | appendMediaToMessage(message, messageBlock, falseSCROLL_BEHAVIOR.KEEP); |
| 493 | 493 | await saveChatConditional(); |
| 494 | 494 | } |
| 495 | 495 | } |
| @@ -1022,7 +1022,7 @@ async function deleteMessageMedia(messageId, mediaIndex, messageBlock) { | ||
| 1022 | 1022 | } |
| 1023 | 1023 | |
| 1024 | 1024 | await saveChatConditional(); |
| 1025 | 1025 | appendMediaToMessage(message, messageBlock, falseSCROLL_BEHAVIOR.KEEP); |
| 1026 | 1026 | } |
| 1027 | 1027 | |
| 1028 | 1028 | /** |
| @@ -1051,7 +1051,7 @@ async function switchMessageMediaDisplay(messageId, messageBlock, targetDisplay) | ||
| 1051 | 1051 | |
| 1052 | 1052 | message.extra.media_display = targetDisplay; |
| 1053 | 1053 | await saveChatConditional(); |
| 1054 | 1054 | appendMediaToMessage(message, messageBlock, falseSCROLL_BEHAVIOR.KEEP); |
| 1055 | 1055 | } |
| 1056 | 1056 | |
| 1057 | 1057 | /** |
| @@ -94,6 +94,17 @@ export const MEDIA_TYPE = { | ||
| 94 | 94 | }; |
| 95 | 95 | |
| 96 | 96 | /** |
| 97 | + * Scroll behavior options when appending media to messages. | |
| 98 | + * @enum {string} | |
| 99 | + * @readonly | |
| 100 | + */ | |
| 101 | +export const SCROLL_BEHAVIOR = { | |
| 102 | + NONE: 'none', | |
| 103 | + KEEP: 'keep', | |
| 104 | + ADJUST: 'adjust', | |
| 105 | +}; | |
| 106 | + | |
| 107 | +/** | |
| 97 | 108 | * @type {{readonly LEFT: 'left', readonly RIGHT: 'right'}} |
| 98 | 109 | */ |
| 99 | 110 | export const SWIPE_DIRECTION = { |
| @@ -10,7 +10,7 @@ import { SlashCommand } from '../../slash-commands/SlashCommand.js'; | ||
| 10 | 10 | import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '../../slash-commands/SlashCommandArgument.js'; |
| 11 | 11 | import { commonEnumProviders } from '../../slash-commands/SlashCommandCommonEnumsProvider.js'; |
| 12 | 12 | import { callGenericPopup, Popup, POPUP_TYPE } from '../../popup.js'; |
| 13 | 13 | import { MEDIA_DISPLAY, MEDIA_TYPE, SCROLL_BEHAVIOR } from '../../constants.js'; |
| 14 | 14 | export { MODULE_NAME }; |
| 15 | 15 | |
| 16 | 16 | const MODULE_NAME = 'caption'; |
| @@ -692,7 +692,7 @@ jQuery(async function () { | ||
| 692 | 692 | const imageIndex = Number(imageBlock.attr('data-index')); |
| 693 | 693 | const data = getContext().chat[messageId]; |
| 694 | 694 | await captionExistingMessage(data, imageIndex); |
| 695 | 695 | appendMediaToMessage(data, messageBlock, falseSCROLL_BEHAVIOR.KEEP); |
| 696 | 696 | await saveChatConditional(); |
| 697 | 697 | } catch (e) { |
| 698 | 698 | console.error('Message image recaption failed', e); |
| @@ -52,7 +52,7 @@ import { | ||
| 52 | 52 | SlashCommandArgument, |
| 53 | 53 | SlashCommandNamedArgument, |
| 54 | 54 | } from '../../slash-commands/SlashCommandArgument.js'; |
| 55 | 55 | import { debounce_timeout, MEDIA_DISPLAY, MEDIA_TYPE, SCROLL_BEHAVIOR, VIDEO_EXTENSIONS } from '../../constants.js'; |
| 56 | 56 | import { SlashCommandEnumValue } from '../../slash-commands/SlashCommandEnumValue.js'; |
| 57 | 57 | import { callGenericPopup, Popup, POPUP_TYPE } from '../../popup.js'; |
| 58 | 58 | import { commonEnumProviders } from '../../slash-commands/SlashCommandCommonEnumsProvider.js'; |
| @@ -4290,7 +4290,7 @@ async function sdMessageButton(e) { | ||
| 4290 | 4290 | message.extra.media.push(newMediaAttachment); |
| 4291 | 4291 | message.extra.media_index = message.extra.media.length - 1; |
| 4292 | 4292 | |
| 4293 | 4293 | appendMediaToMessage(message, messageElement, falseSCROLL_BEHAVIOR.KEEP); |
| 4294 | 4294 | |
| 4295 | 4295 | await context.saveChat(); |
| 4296 | 4296 | } |