mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
refactored main JS code and RAmods functions
This commit is contained in:
1547
public/script.js
1547
public/script.js
File diff suppressed because it is too large
Load Diff
@ -39,7 +39,21 @@ var create_save_scenario;
|
||||
var create_save_mes_example;
|
||||
var count_tokens;
|
||||
var perm_tokens;
|
||||
var ALC_Done;
|
||||
|
||||
const observerConfig = { childList: true, subtree: true };
|
||||
|
||||
const observer = new MutationObserver(function(mutations) {
|
||||
mutations.forEach(function(mutation) {
|
||||
if (mutation.target.id === "online_status_text2") {
|
||||
RA_checkOnlineStatus();
|
||||
} else if (mutation.target.parentNode === SelectedCharacterTab) {
|
||||
setTimeout(RA_CountCharTokens, 200);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
observer.observe(document.documentElement, observerConfig);
|
||||
|
||||
|
||||
//RossAscends: Added function to format dates used in files and chat timestamps to a humanized format.
|
||||
//Mostly I wanted this to be for file names, but couldn't figure out exactly where the filename save code was as everything seemed to be connected.
|
||||
@ -63,7 +77,6 @@ export function humanizedDateTime() {
|
||||
return HumanizedDateTime;
|
||||
}
|
||||
|
||||
//RA_CountCharTokens -- faster counting characters. Works for unsaved characters, and counts permanent and ephemeral tokens separately.
|
||||
|
||||
// triggers:
|
||||
$("#rm_button_create").on("click", function () { //when "+New Character" is clicked
|
||||
@ -78,15 +91,8 @@ $("#rm_button_create").on("click", function () { //when "+New Character" is
|
||||
create_save_mes_example="";
|
||||
$("#result_info").html('Type to start counting tokens!');
|
||||
});
|
||||
$(SelectedCharacterTab).children("h2").on("DOMSubtreeModified", function () { // when the nav's 3rd panel tab header changes (when a char is selected from the char list)
|
||||
//console.log('3rd tab name changed - counting tokens');
|
||||
//SaveLocal('ActiveChar',active_character);
|
||||
setTimeout(RA_CountCharTokens,200); //delay to allow textareas to fill out, so we don't show a previous char's count
|
||||
});
|
||||
|
||||
$("#rm_ch_create_block").on("input", function () {RA_CountCharTokens();}); //when any input is made to the create/edit character form textareas
|
||||
$("#character_popup").on("input", function () {RA_CountCharTokens();}); //when any input is made to the advanced editing popup textareas
|
||||
|
||||
//function:
|
||||
function RA_CountCharTokens() {
|
||||
$("#result_info").html("");
|
||||
@ -150,9 +156,6 @@ function RA_CountCharTokens() {
|
||||
$("#result_info").html(count_tokens + " Tokens (" + perm_tokens + " Permanent Tokens)"); //display normal if both counts are under 1024
|
||||
} else {$("#result_info").html("<font color=red>" +count_tokens +" Tokens (" +perm_tokens +" Permanent Tokens)(TOO MANY)</font>");} //warn if either are over 1024
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Auto Load Last Charcter -- (fires when active_character is defined and auto_load_chat is true)
|
||||
async function RA_autoloadchat() {
|
||||
if (document.getElementById('CharID0') !== null){
|
||||
@ -169,7 +172,6 @@ async function RA_autoloadchat() {
|
||||
setTimeout(RA_autoloadchat,100) // if the charcter list hadn't been loaded yet, try again.
|
||||
}
|
||||
}
|
||||
|
||||
//only triggers when AutoLoadChat is enabled, consider adding this as an independent feature later.
|
||||
function RestoreNavTab(){
|
||||
if($(rm_button_selected_ch).children("h2").text()!==''){ //check for a change in the character edit tab name
|
||||
@ -179,9 +181,7 @@ function RestoreNavTab(){
|
||||
setTimeout(RestoreNavTab,100) //if not changed yet, check again after 100ms
|
||||
}
|
||||
}
|
||||
|
||||
//changes input bar and send button display depending on connection status
|
||||
$("#online_status_text2").on("DOMSubtreeModified", function () {RA_checkOnlineStatus();});
|
||||
function RA_checkOnlineStatus() {
|
||||
if (online_status == "no_connection") {
|
||||
$("#send_textarea").attr("placeholder", "Not connected to API!"); //Input bar placeholder tells users they are not connected
|
||||
@ -195,66 +195,44 @@ function RA_checkOnlineStatus() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Auto-connect to API (when set to kobold, API URL exists, and auto_connect is true)
|
||||
function RA_autoconnect() {
|
||||
//console.log('RA_AC -- starting..')
|
||||
if(online_status !== undefined && api_server !== ''){
|
||||
if (online_status == "no_connection") {
|
||||
if (LoadLocalBool('AutoConnectEnabled') === true) {
|
||||
if (main_api === "kobold") {
|
||||
if (api_server !== "") {
|
||||
$("#api_url_text").val(api_server);
|
||||
console.log('clicking API-button');
|
||||
$("#api_button").click();
|
||||
//console.log("clicked KAI connect for you");
|
||||
} else {
|
||||
console.log(main_api+' '+api_server);
|
||||
console.log("RA_AC - KAI API not specificed");
|
||||
}
|
||||
} else {
|
||||
//console.log("RA_AC - not kobold. skipping to novel.");
|
||||
}
|
||||
|
||||
if (main_api === "novel") {
|
||||
if (api_key_novel !== "") {
|
||||
$("#api_key_novel").val(api_key_novel);
|
||||
console.log('clicking novel API-button');
|
||||
$("#api_button").click();
|
||||
// console.log("clicked NAI connect for you");
|
||||
} else {
|
||||
console.log("RA_AC - no novel API key");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log("RA_AC - is disabled. stopping.");
|
||||
|
||||
function RA_autoconnect() {
|
||||
if (typeof online_status !== 'undefined' && (api_server !== '' || api_key_novel !== '')) {
|
||||
if (online_status === "no_connection" && LoadLocalBool('AutoConnectEnabled')) {
|
||||
if (isUrlOrAPIKey(api_server) && main_api === "kobold") {
|
||||
$("#api_url_text").val(api_server);
|
||||
$("#api_button").click();
|
||||
//} else if (isUrlOrAPIKey(api_key_novel) && main_api === "novel") {
|
||||
// $("#api_key_novel").val(api_key_novel);
|
||||
// $("#api_button").click();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log("RA_AC -- Already online, nothing to do.");
|
||||
setTimeout(RA_autoconnect, 100);
|
||||
}
|
||||
}else{
|
||||
console.log('RA_AC -- settings not loaded yet...trying again..');
|
||||
setTimeout(RA_autoconnect,100);
|
||||
} // if onlinbe_staus hadn't been declared yet, try again..
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function isUrlOrAPIKey(string) {
|
||||
//const pattern = /^\d{3}-\d{3}-\d{3}-\d{3}$/; //need a sample novelAI key to set this format
|
||||
try {
|
||||
new URL(string);
|
||||
return true;
|
||||
} catch (_) {
|
||||
// return pattern.test(string);
|
||||
}
|
||||
}
|
||||
|
||||
$("document").ready(function () {
|
||||
|
||||
// read the state of Nav Lock and whether the nav was open or not before page load.
|
||||
$(PanelPin).prop('checked', LoadLocalBool("NavLockOn"));
|
||||
if (LoadLocalBool("NavLockOn") == true){
|
||||
$(NavToggle).prop("checked", LoadLocalBool("NavOpened"));
|
||||
}
|
||||
if (LoadLocalBool("NavLockOn") == true){$(NavToggle).prop("checked", LoadLocalBool("NavOpened"));}
|
||||
// read the state of AutoConnect and AutoLoadChat.
|
||||
$(AutoConnectCheckbox).prop("checked",LoadLocalBool("AutoConnectEnabled"));
|
||||
$(AutoLoadChatCheckbox).prop("checked",LoadLocalBool("AutoLoadChatEnabled"));
|
||||
|
||||
|
||||
if (LoadLocalBool('AutoLoadChatEnabled') == true) {
|
||||
console.log('calling RA_ALC');
|
||||
RA_autoloadchat();
|
||||
}
|
||||
if (LoadLocalBool('AutoLoadChatEnabled') == true) {RA_autoloadchat();}
|
||||
//Autoconnect on page load if enabled, or when api type is changed
|
||||
if (LoadLocalBool("AutoConnectEnabled") == true) {RA_autoconnect()}
|
||||
$("#main_api").change(function () {RA_autoconnect();});
|
||||
@ -262,60 +240,38 @@ $("document").ready(function () {
|
||||
|
||||
//close the RightNav panel when user clicks outside of it or related panels (adv editing popup, or dialog popups)
|
||||
$("html").click(function (e) {
|
||||
//console.log('clicking'+$(this));
|
||||
if ($(NavToggle).prop("checked") === true && $(PanelPin).prop("checked") === false) {
|
||||
if ($(e.target).attr("id") !== "nav-toggle") {
|
||||
if (RightNavPanel.contains(e.target) === false) {
|
||||
if (AdvancedCharDefsPopup.contains(e.target) === false) {
|
||||
if (ConfirmationPopup.contains(e.target) === false) {
|
||||
if ($(NavToggle).prop("checked") &&
|
||||
!$(PanelPin).prop("checked") &&
|
||||
$(e.target).attr("id") !== "nav-toggle" &&
|
||||
!RightNavPanel.contains(e.target) &&
|
||||
!AdvancedCharDefsPopup.contains(e.target) &&
|
||||
!ConfirmationPopup.contains(e.target)) {
|
||||
NavToggle.click();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//save NavLock prefs and record state of the Nav being open or closed
|
||||
$(NavToggle).on("change", function () {
|
||||
SaveLocal("NavOpened", $(NavToggle).prop("checked"));
|
||||
});
|
||||
$(PanelPin).on("change",function () {
|
||||
SaveLocal("NavLockOn", $(PanelPin).prop("checked"));
|
||||
});
|
||||
$(NavToggle).on("change", function () {SaveLocal("NavOpened", $(NavToggle).prop("checked")); });
|
||||
$(PanelPin).on("change",function () {SaveLocal("NavLockOn", $(PanelPin).prop("checked")); });
|
||||
|
||||
//save AutoConnect and AutoLoadChat prefs
|
||||
$(AutoConnectCheckbox).on("change",function () {
|
||||
SaveLocal("AutoConnectEnabled", $(AutoConnectCheckbox).prop("checked"));
|
||||
});
|
||||
$(AutoLoadChatCheckbox).on("change",function () {
|
||||
SaveLocal("AutoLoadChatEnabled", $(AutoLoadChatCheckbox).prop("checked"));
|
||||
});
|
||||
$(AutoConnectCheckbox).on("change",function () {SaveLocal("AutoConnectEnabled", $(AutoConnectCheckbox).prop("checked")); });
|
||||
$(AutoLoadChatCheckbox).on("change",function () {SaveLocal("AutoLoadChatEnabled", $(AutoLoadChatCheckbox).prop("checked")); });
|
||||
|
||||
//save the clicked Nav Tab as the tab to auto-open on page reload
|
||||
$("#rm_button_settings").click( function (){
|
||||
SaveLocal('SelectedNavTab','rm_button_settings');
|
||||
});
|
||||
$(SelectedCharacterTab).click(function () {
|
||||
console.log('3rd tab clicked - saving selnavtab');
|
||||
//SaveLocal('ActiveChar',this_chid);
|
||||
SaveLocal('SelectedNavTab','rm_button_selected_ch');
|
||||
});
|
||||
$("#rm_button_settings").click( function (){SaveLocal('SelectedNavTab','rm_button_settings'); });
|
||||
$(SelectedCharacterTab).click(function () {SaveLocal('SelectedNavTab','rm_button_selected_ch'); });
|
||||
$("#rm_button_characters").click( function () { //if char list is clicked, in addition to saving it...
|
||||
SaveLocal('SelectedNavTab','rm_button_characters');
|
||||
characters.sort(Intl.Collator().compare); // we sort the list
|
||||
});
|
||||
// when a char is selected from the list, save them as the auto-load character for next page load
|
||||
$(document).on("click", ".character_select",function () {
|
||||
console.log('char clicked in charlist - saving local activechar');
|
||||
SaveLocal('ActiveChar',$(this).attr('chid'));
|
||||
});
|
||||
$(document).on("click", ".character_select",function () {SaveLocal('ActiveChar',$(this).attr('chid')); });
|
||||
|
||||
//this makes the chat input text area resize vertically to match the text size (limited by CSS at 50% window height)
|
||||
$('#send_textarea').on('input', function () {
|
||||
this.style.height = '40px';
|
||||
this.style.height =
|
||||
(this.scrollHeight) + 'px';
|
||||
this.style.height = (this.scrollHeight) + 'px';
|
||||
});
|
||||
|
||||
//Regenerate if user swipes on the last mesage in chat
|
||||
@ -324,18 +280,20 @@ $("document").ready(function () {
|
||||
//2. find a way to make the chat slide down smoothly when the last mes div gets .remove()-d
|
||||
|
||||
document.addEventListener('swiped-left', function(e) {
|
||||
var SwipeButR = $('.swipe_right:last');
|
||||
var SwipeTargetMesClassParent = e.target.closest('.last_mes');
|
||||
if (SwipeTargetMesClassParent !== null){
|
||||
if($('.swipe_right:last').attr('style')=='display: flex;' == true){
|
||||
$('.swipe_right:last').click();
|
||||
if(SwipeButR.attr('style')=='display: flex;' == true){
|
||||
SwipeButR.click();
|
||||
}
|
||||
}
|
||||
});
|
||||
document.addEventListener('swiped-right', function(e) {
|
||||
var SwipeButL = $('.swipe_left:last');
|
||||
var SwipeTargetMesClassParent = e.target.closest('.last_mes');
|
||||
if (SwipeTargetMesClassParent !== null){
|
||||
if($('.swipe_left:last').attr('style')=='display: flex;' == true){
|
||||
$('.swipe_left:last').click();
|
||||
if(SwipeButL.attr('style')=='display: flex;' == true){
|
||||
SwipeButL.click();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user