Refactor move bookmark functionality

6c0ecdef69bfc0a7d5f6740cc1d13e22a06f9c45

Wolfsblvt <wolfsblvt@gmail.com>

2 files changed, +83 -73Showing whitespace changes
public/script.js+2 -71
@@ -117,9 +117,8 @@ import {
117} from './scripts/nai-settings.js';117} from './scripts/nai-settings.js';
118118
119import {119import {
120 createNewBookmark,
121 showBookmarksButtons,120 showBookmarksButtons,
122 createBranch,121 updateBookmarkDisplay,
123} from './scripts/bookmarks.js';122} from './scripts/bookmarks.js';
124123
125import {124import {
@@ -2102,7 +2101,7 @@ function getMessageFromTemplate({
2102 tokenCount && mes.find('.tokenCounterDisplay').text(`${tokenCount}t`);2101 tokenCount && mes.find('.tokenCounterDisplay').text(`${tokenCount}t`);
2103 title && mes.attr('title', title);2102 title && mes.attr('title', title);
2104 timerValue && mes.find('.mes_timer').attr('title', timerTitle).text(timerValue);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);
21062105
2107 if (power_user.timestamp_model_icon && extra?.api) {2106 if (power_user.timestamp_model_icon && extra?.api) {
2108 insertSVGIcon(mes, extra);2107 insertSVGIcon(mes, extra);
@@ -8220,29 +8219,6 @@ function swipe_left() { // when we swipe left..but no generation.
8220 }8219 }
8221}8220}
82228221
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 */
8228async 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// when we click swipe right button8222// when we click swipe right button
8247const swipe_right = () => {8223const swipe_right = () => {
8248 if (chat.length - 1 === Number(this_edit_mes_id)) {8224 if (chat.length - 1 === Number(this_edit_mes_id)) {
@@ -10548,51 +10524,6 @@ jQuery(async function () {
10548 await duplicateCharacter();10524 await duplicateCharacter();
10549 });10525 });
1055010526
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 $(document).on('click', '.mes_stop', function () {10527 $(document).on('click', '.mes_stop', function () {
10597 stopGeneration();10528 stopGeneration();
10598 });10529 });
public/scripts/bookmarks.js+81 -2
@@ -24,6 +24,7 @@ import {
24 saveGroupBookmarkChat,24 saveGroupBookmarkChat,
25 selected_group,25 selected_group,
26} from './group-chats.js';26} from './group-chats.js';
27import { hideLoader, showLoader } from './loader.js';
27import { Popup } from './popup.js';28import { Popup } from './popup.js';
28import { createTagMapFromList } from './tags.js';29import { createTagMapFromList } from './tags.js';
2930
@@ -212,13 +213,23 @@ async function createNewBookmark(mesId) {
212 lastMes.extra['bookmark_link'] = name;213 lastMes.extra['bookmark_link'] = name;
213214
214 const mes = $(`.mes[mesid="${mesId}"]`);215 const mes = $(`.mes[mesid="${mesId}"]`);
215 mes.attr('bookmark_link', name);216 updateBookmarkDisplay(mes, name);
216 mes.find('.mes_bookmark').attr('title', `Checkpoint${name}\n\n${(mes.find('.mes_bookmark')).data('tooltip')}`);
217217
218 await saveChatConditional();218 await saveChatConditional();
219 toastr.success('Click the flag icon next to the message to open the checkpoint chat.', 'Checkpoint created', { timeOut: 10000 });219 toastr.success('Click the flag icon next to the message to open the checkpoint chat.', 'Checkpoint created', { timeOut: 10000 });
220}220}
221221
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 */
228export 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
222async function backToMainChat() {233async function backToMainChat() {
223 const mainChatName = getMainChatName();234 const mainChatName = getMainChatName();
224 const allChats = await getExistingChatNames();235 const allChats = await getExistingChatNames();
@@ -349,8 +360,76 @@ async function convertSoloToGroupChat() {
349 toastr.success('The chat has been successfully converted!');360 toastr.success('The chat has been successfully converted!');
350}361}
351362
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 */
368async 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
352jQuery(function () {386jQuery(function () {
353 $('#option_new_bookmark').on('click', saveBookmarkMenu);387 $('#option_new_bookmark').on('click', saveBookmarkMenu);
354 $('#option_back_to_main').on('click', backToMainChat);388 $('#option_back_to_main').on('click', backToMainChat);
355 $('#option_convert_to_group').on('click', convertSoloToGroupChat);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});