Add contextual Character WI button

This commit is contained in:
Cohee
2023-06-25 18:40:13 +03:00
parent da8beeb503
commit 7984e3b818
3 changed files with 84 additions and 33 deletions

View File

@@ -2496,6 +2496,7 @@
<div id="favorite_button" class="menu_button fa-solid fa-star" title="Add to Favorites"></div>
<input type="hidden" id="fav_checkbox" name="fav" />
<div id="advanced_div" class="menu_button fa-solid fa-book " title="Advanced Definitions"></div>
<div id="world_button" class="menu_button fa-solid fa-globe" title="Character Lore"></div>
<!-- <div id="export_button" class="menu_button fa-solid fa-file-export " title="Export and Download"></div> -->
<!-- <div id="set_chat_scenario" class="menu_button fa-solid fa-scroll" title="Set a chat scenario override"></div> -->
<!-- <div id="set_character_world" class="menu_button fa-solid fa-globe" title="Set a character World Info / Lorebook"></div> -->

View File

@@ -30,6 +30,8 @@ import {
world_names,
world_info_character_strategy,
importEmbeddedWorldInfo,
checkEmbeddedWorld,
setWorldInfoButtonClass,
} from "./scripts/world-info.js";
import {
@@ -293,7 +295,7 @@ let currentCroppedAvatar = '';
const durationSaveEdit = 1000;
const saveSettingsDebounced = debounce(() => saveSettings(), durationSaveEdit);
const saveCharacterDebounced = debounce(() => $("#create_button").trigger('click'), durationSaveEdit);
export const saveCharacterDebounced = debounce(() => $("#create_button").trigger('click'), durationSaveEdit);
const getStatusDebounced = debounce(() => getStatus(), 300_000);
const saveChatDebounced = debounce(() => saveChatConditional(), durationSaveEdit);
@@ -5052,39 +5054,13 @@ export function select_selected_character(chid) {
$("#renameCharButton").css("display", "");
$('.open_alternate_greetings').data('chid', chid);
$('#set_character_world').data('chid', chid);
const world = characters[chid]?.data?.extensions?.world;
const worldSet = Boolean(world && world_names.includes(world));
$('#set_character_world').toggleClass('world_set', worldSet);
setWorldInfoButtonClass(chid);
checkEmbeddedWorld(chid);
$("#form_create").attr("actiontype", "editcharacter");
saveSettingsDebounced();
}
function checkEmbeddedWorld(chid) {
$('#import_character_info').hide();
if (chid === undefined) {
return;
}
if (characters[chid]?.data?.character_book) {
$('#import_character_info').data('chid', chid).show();
// Only show the alert once per character
const checkKey = `AlertWI_${characters[chid].avatar}`;
const worldName = characters[chid]?.data?.extensions?.world;
if (!localStorage.getItem(checkKey) && (!worldName || !world_names.includes(worldName))) {
toastr.info(
'To import and use it, select "Import Embedded World Info" in the Options dropdown menu on the character panel.',
`${characters[chid].name} has an embedded World/Lorebook`,
{ timeOut: 10000, extendedTimeOut: 20000, positionClass: 'toast-top-center' },
);
localStorage.setItem(checkKey, 1);
}
}
}
function select_rm_create() {
menu_type = "create";
@@ -5133,7 +5109,7 @@ function select_rm_create() {
$("#name_div").addClass('displayBlock');
$('.open_alternate_greetings').data('chid', undefined);
$('#set_character_world').data('chid', undefined);
$('#set_character_world').toggleClass('world_set', !!create_save.world);
setWorldInfoButtonClass(undefined, !!create_save.world);
updateFavButtonState(false);
checkEmbeddedWorld();
@@ -5530,7 +5506,7 @@ function openCharacterWorldPopup() {
createOrEditCharacter();
}
$('#set_character_world').toggleClass('world_set', !!value);
setWorldInfoButtonClass(undefined, !!value);
}
const name = (menu_type == 'create' ? create_save.name : characters[chid]?.data?.name) || 'Nameless';

View File

@@ -1,4 +1,4 @@
import { saveSettings, callPopup, substituteParams, getTokenCount, getRequestHeaders, chat_metadata, this_chid, characters } from "../script.js";
import { saveSettings, callPopup, substituteParams, getTokenCount, getRequestHeaders, chat_metadata, this_chid, characters, saveCharacterDebounced, menu_type } from "../script.js";
import { download, debounce, initScrollHeight, resetScrollHeight, parseJsonFile, extractDataFromPng, getFileBuffer, delay } from "./utils.js";
import { getContext } from "./extensions.js";
import { NOTE_MODULE_NAME, metadata_keys, shouldWIAddPrompt } from "./extensions/floating-prompt/index.js";
@@ -173,7 +173,7 @@ function hideWorldEditor() {
}
function getWIElement(name) {
const wiElement = $("#world_info").children().filter(function() {
const wiElement = $("#world_info").children().filter(function () {
return $(this).text().toLowerCase() === name.toLowerCase()
});
@@ -630,6 +630,14 @@ async function deleteWorldInfo(worldInfoName) {
await updateWorldInfoList();
$('#world_editor_select').trigger('change');
if ($('#character_world').val() === worldInfoName) {
$('#character_world').val('').trigger('change');
setWorldInfoButtonClass(undefined, false);
if (menu_type != 'create') {
saveCharacterDebounced();
}
}
}
}
@@ -1004,6 +1012,48 @@ function convertCharacterBook(characterBook) {
return result;
}
export function setWorldInfoButtonClass(chid, forceValue = undefined) {
if (forceValue !== undefined) {
$('#set_character_world, #world_button').toggleClass('world_set', forceValue);
return;
}
if (!chid) {
return;
}
const world = characters[chid]?.data?.extensions?.world;
const worldSet = Boolean(world && world_names.includes(world));
$('#set_character_world, #world_button').toggleClass('world_set', worldSet);
}
export function checkEmbeddedWorld(chid) {
$('#import_character_info').hide();
if (chid === undefined) {
return false;
}
if (characters[chid]?.data?.character_book) {
$('#import_character_info').data('chid', chid).show();
// Only show the alert once per character
const checkKey = `AlertWI_${characters[chid].avatar}`;
const worldName = characters[chid]?.data?.extensions?.world;
if (!localStorage.getItem(checkKey) && (!worldName || !world_names.includes(worldName))) {
toastr.info(
'To import and use it, select "Import Embedded World Info" in the Options dropdown menu on the character panel.',
`${characters[chid].name} has an embedded World/Lorebook`,
{ timeOut: 10000, extendedTimeOut: 20000, positionClass: 'toast-top-center' },
);
localStorage.setItem(checkKey, 1);
}
return true;
}
return false;
}
export async function importEmbeddedWorldInfo() {
const chid = $('#import_character_info').data('chid');
@@ -1032,6 +1082,8 @@ export async function importEmbeddedWorldInfo() {
if (newIndex >= 0) {
$("#world_editor_select").val(newIndex).trigger('change');
}
setWorldInfoButtonClass(chid, true);
}
function onWorldInfoChange(_, text) {
@@ -1231,5 +1283,27 @@ jQuery(() => {
$('#world_info_character_strategy').on('change', function () {
world_info_character_strategy = $(this).val();
saveSettingsDebounced();
})
});
$('#world_button').on('click', async function () {
const chid = $('#set_character_world').data('chid');
if (chid) {
const worldName = characters[chid]?.data?.extensions?.world;
const hasEmbed = checkEmbeddedWorld(chid);
if (worldName && world_names.includes(worldName)) {
if (!$('#WorldInfo').is(':visible')) {
$('#WIDrawerIcon').trigger('click');
}
const index = world_names.indexOf(worldName);
$("#world_editor_select").val(index).trigger('change');
} else if (hasEmbed) {
await importEmbeddedWorldInfo();
saveCharacterDebounced();
}
else {
$('#char-management-dropdown').val($('#set_character_world').val()).trigger('change');
}
}
});
});