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 {
117117} from './scripts/nai-settings.js';
118118
119119import {
120- createNewBookmark,
121120 showBookmarksButtons,
122121 createBranchupdateBookmarkDisplay,
123122} from './scripts/bookmarks.js';
124123
125124import {
@@ -2102,7 +2101,7 @@ function getMessageFromTemplate({
21022101 tokenCount && mes.find('.tokenCounterDisplay').text(`${tokenCount}t`);
21032102 title && mes.attr('title', title);
21042103 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
21072106 if (power_user.timestamp_model_icon && extra?.api) {
21082107 insertSVGIcon(mes, extra);
@@ -8220,29 +8219,6 @@ function swipe_left() { // when we swipe left..but no generation.
82208219 }
82218220}
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- */
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-
82468222// when we click swipe right button
82478223const swipe_right = () => {
82488224 if (chat.length - 1 === Number(this_edit_mes_id)) {
@@ -10548,51 +10524,6 @@ jQuery(async function () {
1054810524 await duplicateCharacter();
1054910525 });
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-
1059610527 $(document).on('click', '.mes_stop', function () {
1059710528 stopGeneration();
1059810529 });
public/scripts/bookmarks.js+81 -2
@@ -24,6 +24,7 @@ import {
2424 saveGroupBookmarkChat,
2525 selected_group,
2626} from './group-chats.js';
27+import { hideLoader, showLoader } from './loader.js';
2728import { Popup } from './popup.js';
2829import { createTagMapFromList } from './tags.js';
2930
@@ -212,13 +213,23 @@ async function createNewBookmark(mesId) {
212213 lastMes.extra['bookmark_link'] = name;
213214
214215 const mes = $(`.mes[mesid="${mesId}"]`);
215216 mes.attrupdateBookmarkDisplay('bookmark_link'mes, name);
216- mes.find('.mes_bookmark').attr('title', `Checkpoint${name}\n\n${(mes.find('.mes_bookmark')).data('tooltip')}`);
217217
218218 await saveChatConditional();
219219 toastr.success('Click the flag icon next to the message to open the checkpoint chat.', 'Checkpoint created', { timeOut: 10000 });
220220}
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+ */
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+
222233async function backToMainChat() {
223234 const mainChatName = getMainChatName();
224235 const allChats = await getExistingChatNames();
@@ -349,8 +360,76 @@ async function convertSoloToGroupChat() {
349360 toastr.success('The chat has been successfully converted!');
350361}
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+ */
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+
352386jQuery(function () {
353387 $('#option_new_bookmark').on('click', saveBookmarkMenu);
354388 $('#option_back_to_main').on('click', backToMainChat);
355389 $('#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+ });
356435});