Refactor move bookmark functionality
| @@ -117,9 +117,8 @@ import { | ||
| 117 | 117 | } from './scripts/nai-settings.js'; |
| 118 | 118 | |
| 119 | 119 | import { |
| 120 | - createNewBookmark, | |
| 121 | 120 | showBookmarksButtons, |
| 122 | 121 | createBranchupdateBookmarkDisplay, |
| 123 | 122 | } from './scripts/bookmarks.js'; |
| 124 | 123 | |
| 125 | 124 | import { |
| @@ -2102,7 +2101,7 @@ function getMessageFromTemplate({ | ||
| 2102 | 2101 | tokenCount && mes.find('.tokenCounterDisplay').text(`${tokenCount}t`); |
| 2103 | 2102 | title && mes.attr('title', title); |
| 2104 | 2103 | timerValue && mes.find('.mes_timer').attr('title', timerTitle).text(timerValue); |
| 2105 | - bookmarkLink && mes.find('.mes_bookmark').attr('title', `Checkpoint\n${bookmarkLink}\n\n${mes.find('.mes_bookmark').data('tooltip')}`); | |
| 2104 | + bookmarkLink && updateBookmarkDisplay(mes); | |
| 2106 | 2105 | |
| 2107 | 2106 | if (power_user.timestamp_model_icon && extra?.api) { |
| 2108 | 2107 | insertSVGIcon(mes, extra); |
| @@ -8220,29 +8219,6 @@ function swipe_left() { // when we swipe left..but no generation. | ||
| 8220 | 8219 | } |
| 8221 | 8220 | } |
| 8222 | 8221 | |
| 8223 | -/** | |
| 8224 | - * Creates a new branch from the message with the given ID | |
| 8225 | - * @param {number} mesId Message ID | |
| 8226 | - * @returns {Promise<string>} Branch file name | |
| 8227 | - */ | |
| 8228 | -async function branchChat(mesId) { | |
| 8229 | - if (this_chid === undefined && !selected_group) { | |
| 8230 | - toastr.info('No character selected.', 'Branch creation aborted'); | |
| 8231 | - return; | |
| 8232 | - } | |
| 8233 | - | |
| 8234 | - const fileName = await createBranch(mesId); | |
| 8235 | - await saveItemizedPrompts(fileName); | |
| 8236 | - | |
| 8237 | - if (selected_group) { | |
| 8238 | - await openGroupChat(selected_group, fileName); | |
| 8239 | - } else { | |
| 8240 | - await openCharacterChat(fileName); | |
| 8241 | - } | |
| 8242 | - | |
| 8243 | - return fileName; | |
| 8244 | -} | |
| 8245 | - | |
| 8246 | 8222 | // when we click swipe right button |
| 8247 | 8223 | const swipe_right = () => { |
| 8248 | 8224 | if (chat.length - 1 === Number(this_edit_mes_id)) { |
| @@ -10548,51 +10524,6 @@ jQuery(async function () { | ||
| 10548 | 10524 | await duplicateCharacter(); |
| 10549 | 10525 | }); |
| 10550 | 10526 | |
| 10551 | - $(document).on('click', '.select_chat_block, .bookmark_link, .mes_bookmark', async function (e) { | |
| 10552 | - // If shift is held down, we are not following the bookmark, but creating a new one | |
| 10553 | - if (e.shiftKey) { | |
| 10554 | - var selectedMesId = $(this).closest('.mes').attr('mesid'); | |
| 10555 | - createNewBookmark(selectedMesId); | |
| 10556 | - return; | |
| 10557 | - } | |
| 10558 | - | |
| 10559 | - let file_name = $(this).hasClass('mes_bookmark') | |
| 10560 | - ? $(this).closest('.mes').attr('bookmark_link') | |
| 10561 | - : $(this).attr('file_name').replace('.jsonl', ''); | |
| 10562 | - | |
| 10563 | - if (!file_name) { | |
| 10564 | - return; | |
| 10565 | - } | |
| 10566 | - | |
| 10567 | - try { | |
| 10568 | - showLoader(); | |
| 10569 | - if (selected_group) { | |
| 10570 | - await openGroupChat(selected_group, file_name); | |
| 10571 | - } else { | |
| 10572 | - await openCharacterChat(file_name); | |
| 10573 | - } | |
| 10574 | - } finally { | |
| 10575 | - hideLoader(); | |
| 10576 | - } | |
| 10577 | - | |
| 10578 | - $('#shadow_select_chat_popup').css('display', 'none'); | |
| 10579 | - $('#load_select_chat_div').css('display', 'block'); | |
| 10580 | - }); | |
| 10581 | - | |
| 10582 | - $(document).on('click', '.mes_create_bookmark', async function () { | |
| 10583 | - var selected_mes_id = $(this).closest('.mes').attr('mesid'); | |
| 10584 | - if (selected_mes_id !== undefined) { | |
| 10585 | - createNewBookmark(selected_mes_id); | |
| 10586 | - } | |
| 10587 | - }); | |
| 10588 | - | |
| 10589 | - $(document).on('click', '.mes_create_branch', async function () { | |
| 10590 | - var selected_mes_id = $(this).closest('.mes').attr('mesid'); | |
| 10591 | - if (selected_mes_id !== undefined) { | |
| 10592 | - branchChat(Number(selected_mes_id)); | |
| 10593 | - } | |
| 10594 | - }); | |
| 10595 | - | |
| 10596 | 10527 | $(document).on('click', '.mes_stop', function () { |
| 10597 | 10528 | stopGeneration(); |
| 10598 | 10529 | }); |
| @@ -24,6 +24,7 @@ import { | ||
| 24 | 24 | saveGroupBookmarkChat, |
| 25 | 25 | selected_group, |
| 26 | 26 | } from './group-chats.js'; |
| 27 | +import { hideLoader, showLoader } from './loader.js'; | |
| 27 | 28 | import { Popup } from './popup.js'; |
| 28 | 29 | import { createTagMapFromList } from './tags.js'; |
| 29 | 30 | |
| @@ -212,13 +213,23 @@ async function createNewBookmark(mesId) { | ||
| 212 | 213 | lastMes.extra['bookmark_link'] = name; |
| 213 | 214 | |
| 214 | 215 | const mes = $(`.mes[mesid="${mesId}"]`); |
| 215 | 216 | mes.attrupdateBookmarkDisplay('bookmark_link'mes, name); |
| 216 | - mes.find('.mes_bookmark').attr('title', `Checkpoint${name}\n\n${(mes.find('.mes_bookmark')).data('tooltip')}`); | |
| 217 | 217 | |
| 218 | 218 | await saveChatConditional(); |
| 219 | 219 | toastr.success('Click the flag icon next to the message to open the checkpoint chat.', 'Checkpoint created', { timeOut: 10000 }); |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | + | |
| 223 | +/** | |
| 224 | + * Updates the display of the bookmark on a chat message. | |
| 225 | + * @param {JQuery<HTMLElement>} mes - The message element | |
| 226 | + * @param {string?} [newBookmarkLink=null] - The new bookmark link (optional) | |
| 227 | + */ | |
| 228 | +export function updateBookmarkDisplay(mes, newBookmarkLink = null) { | |
| 229 | + newBookmarkLink && mes.attr('bookmark_link', newBookmarkLink); | |
| 230 | + mes.find('.mes_bookmark').attr('title', `Checkpoint\n${mes.attr('bookmark_link')}\n\n${mes.find('.mes_bookmark').data('tooltip')}`); | |
| 231 | +} | |
| 232 | + | |
| 222 | 233 | async function backToMainChat() { |
| 223 | 234 | const mainChatName = getMainChatName(); |
| 224 | 235 | const allChats = await getExistingChatNames(); |
| @@ -349,8 +360,76 @@ async function convertSoloToGroupChat() { | ||
| 349 | 360 | toastr.success('The chat has been successfully converted!'); |
| 350 | 361 | } |
| 351 | 362 | |
| 363 | +/** | |
| 364 | + * Creates a new branch from the message with the given ID | |
| 365 | + * @param {number} mesId Message ID | |
| 366 | + * @returns {Promise<string>} Branch file name | |
| 367 | + */ | |
| 368 | +async function branchChat(mesId) { | |
| 369 | + if (this_chid === undefined && !selected_group) { | |
| 370 | + toastr.info('No character selected.', 'Branch creation aborted'); | |
| 371 | + return; | |
| 372 | + } | |
| 373 | + | |
| 374 | + const fileName = await createBranch(mesId); | |
| 375 | + await saveItemizedPrompts(fileName); | |
| 376 | + | |
| 377 | + if (selected_group) { | |
| 378 | + await openGroupChat(selected_group, fileName); | |
| 379 | + } else { | |
| 380 | + await openCharacterChat(fileName); | |
| 381 | + } | |
| 382 | + | |
| 383 | + return fileName; | |
| 384 | +} | |
| 385 | + | |
| 352 | 386 | jQuery(function () { |
| 353 | 387 | $('#option_new_bookmark').on('click', saveBookmarkMenu); |
| 354 | 388 | $('#option_back_to_main').on('click', backToMainChat); |
| 355 | 389 | $('#option_convert_to_group').on('click', convertSoloToGroupChat); |
| 390 | + | |
| 391 | + $(document).on('click', '.select_chat_block, .bookmark_link, .mes_bookmark', async function (e) { | |
| 392 | + // If shift is held down, we are not following the bookmark, but creating a new one | |
| 393 | + if (e.shiftKey) { | |
| 394 | + var selectedMesId = $(this).closest('.mes').attr('mesid'); | |
| 395 | + createNewBookmark(selectedMesId); | |
| 396 | + return; | |
| 397 | + } | |
| 398 | + | |
| 399 | + let file_name = $(this).hasClass('mes_bookmark') | |
| 400 | + ? $(this).closest('.mes').attr('bookmark_link') | |
| 401 | + : $(this).attr('file_name').replace('.jsonl', ''); | |
| 402 | + | |
| 403 | + if (!file_name) { | |
| 404 | + return; | |
| 405 | + } | |
| 406 | + | |
| 407 | + try { | |
| 408 | + showLoader(); | |
| 409 | + if (selected_group) { | |
| 410 | + await openGroupChat(selected_group, file_name); | |
| 411 | + } else { | |
| 412 | + await openCharacterChat(file_name); | |
| 413 | + } | |
| 414 | + } finally { | |
| 415 | + hideLoader(); | |
| 416 | + } | |
| 417 | + | |
| 418 | + $('#shadow_select_chat_popup').css('display', 'none'); | |
| 419 | + $('#load_select_chat_div').css('display', 'block'); | |
| 420 | + }); | |
| 421 | + | |
| 422 | + $(document).on('click', '.mes_create_bookmark', async function () { | |
| 423 | + var selected_mes_id = $(this).closest('.mes').attr('mesid'); | |
| 424 | + if (selected_mes_id !== undefined) { | |
| 425 | + createNewBookmark(selected_mes_id); | |
| 426 | + } | |
| 427 | + }); | |
| 428 | + | |
| 429 | + $(document).on('click', '.mes_create_branch', async function () { | |
| 430 | + var selected_mes_id = $(this).closest('.mes').attr('mesid'); | |
| 431 | + if (selected_mes_id !== undefined) { | |
| 432 | + branchChat(Number(selected_mes_id)); | |
| 433 | + } | |
| 434 | + }); | |
| 356 | 435 | }); |