refactored main JS code and RAmods functions

This commit is contained in:
RossAsscends
2023-03-17 17:37:59 +09:00
parent c0a285eaa2
commit b20b865887
2 changed files with 718 additions and 991 deletions

File diff suppressed because it is too large Load Diff

View File

@ -39,7 +39,21 @@ var create_save_scenario;
var create_save_mes_example; var create_save_mes_example;
var count_tokens; var count_tokens;
var perm_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. //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. //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; return HumanizedDateTime;
} }
//RA_CountCharTokens -- faster counting characters. Works for unsaved characters, and counts permanent and ephemeral tokens separately.
// triggers: // triggers:
$("#rm_button_create").on("click", function () { //when "+New Character" is clicked $("#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=""; create_save_mes_example="";
$("#result_info").html('Type to start counting tokens!'); $("#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 $("#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 $("#character_popup").on("input", function () {RA_CountCharTokens();}); //when any input is made to the advanced editing popup textareas
//function: //function:
function RA_CountCharTokens() { function RA_CountCharTokens() {
$("#result_info").html(""); $("#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 $("#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 } 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) //Auto Load Last Charcter -- (fires when active_character is defined and auto_load_chat is true)
async function RA_autoloadchat() { async function RA_autoloadchat() {
if (document.getElementById('CharID0') !== null){ 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. 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. //only triggers when AutoLoadChat is enabled, consider adding this as an independent feature later.
function RestoreNavTab(){ function RestoreNavTab(){
if($(rm_button_selected_ch).children("h2").text()!==''){ //check for a change in the character edit tab name 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 setTimeout(RestoreNavTab,100) //if not changed yet, check again after 100ms
} }
} }
//changes input bar and send button display depending on connection status //changes input bar and send button display depending on connection status
$("#online_status_text2").on("DOMSubtreeModified", function () {RA_checkOnlineStatus();});
function RA_checkOnlineStatus() { function RA_checkOnlineStatus() {
if (online_status == "no_connection") { if (online_status == "no_connection") {
$("#send_textarea").attr("placeholder", "Not connected to API!"); //Input bar placeholder tells users they are not connected $("#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) //Auto-connect to API (when set to kobold, API URL exists, and auto_connect is true)
function RA_autoconnect() {
//console.log('RA_AC -- starting..') function RA_autoconnect() {
if(online_status !== undefined && api_server !== ''){ if (typeof online_status !== 'undefined' && (api_server !== '' || api_key_novel !== '')) {
if (online_status == "no_connection") { if (online_status === "no_connection" && LoadLocalBool('AutoConnectEnabled')) {
if (LoadLocalBool('AutoConnectEnabled') === true) { if (isUrlOrAPIKey(api_server) && main_api === "kobold") {
if (main_api === "kobold") {
if (api_server !== "") {
$("#api_url_text").val(api_server); $("#api_url_text").val(api_server);
console.log('clicking API-button');
$("#api_button").click(); $("#api_button").click();
//console.log("clicked KAI connect for you"); //} else if (isUrlOrAPIKey(api_key_novel) && main_api === "novel") {
} else { // $("#api_key_novel").val(api_key_novel);
console.log(main_api+' '+api_server); // $("#api_button").click();
console.log("RA_AC - KAI API not specificed"); }
} }
} else { } else {
//console.log("RA_AC - not kobold. skipping to novel."); setTimeout(RA_autoconnect, 100);
}
} }
if (main_api === "novel") { function isUrlOrAPIKey(string) {
if (api_key_novel !== "") { //const pattern = /^\d{3}-\d{3}-\d{3}-\d{3}$/; //need a sample novelAI key to set this format
$("#api_key_novel").val(api_key_novel); try {
console.log('clicking novel API-button'); new URL(string);
$("#api_button").click(); return true;
// console.log("clicked NAI connect for you"); } catch (_) {
} else { // return pattern.test(string);
console.log("RA_AC - no novel API key");
} }
} }
} else {
console.log("RA_AC - is disabled. stopping.");
}
} else {
console.log("RA_AC -- Already online, nothing to do.");
}
}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..
}
$("document").ready(function () { $("document").ready(function () {
// read the state of Nav Lock and whether the nav was open or not before page load. // read the state of Nav Lock and whether the nav was open or not before page load.
$(PanelPin).prop('checked', LoadLocalBool("NavLockOn")); $(PanelPin).prop('checked', LoadLocalBool("NavLockOn"));
if (LoadLocalBool("NavLockOn") == true){ if (LoadLocalBool("NavLockOn") == true){$(NavToggle).prop("checked", LoadLocalBool("NavOpened"));}
$(NavToggle).prop("checked", LoadLocalBool("NavOpened"));
}
// read the state of AutoConnect and AutoLoadChat. // read the state of AutoConnect and AutoLoadChat.
$(AutoConnectCheckbox).prop("checked",LoadLocalBool("AutoConnectEnabled")); $(AutoConnectCheckbox).prop("checked",LoadLocalBool("AutoConnectEnabled"));
$(AutoLoadChatCheckbox).prop("checked",LoadLocalBool("AutoLoadChatEnabled")); $(AutoLoadChatCheckbox).prop("checked",LoadLocalBool("AutoLoadChatEnabled"));
if (LoadLocalBool('AutoLoadChatEnabled') == true) {RA_autoloadchat();}
if (LoadLocalBool('AutoLoadChatEnabled') == true) {
console.log('calling RA_ALC');
RA_autoloadchat();
}
//Autoconnect on page load if enabled, or when api type is changed //Autoconnect on page load if enabled, or when api type is changed
if (LoadLocalBool("AutoConnectEnabled") == true) {RA_autoconnect()} if (LoadLocalBool("AutoConnectEnabled") == true) {RA_autoconnect()}
$("#main_api").change(function () {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) //close the RightNav panel when user clicks outside of it or related panels (adv editing popup, or dialog popups)
$("html").click(function (e) { $("html").click(function (e) {
//console.log('clicking'+$(this)); if ($(NavToggle).prop("checked") &&
if ($(NavToggle).prop("checked") === true && $(PanelPin).prop("checked") === false) { !$(PanelPin).prop("checked") &&
if ($(e.target).attr("id") !== "nav-toggle") { $(e.target).attr("id") !== "nav-toggle" &&
if (RightNavPanel.contains(e.target) === false) { !RightNavPanel.contains(e.target) &&
if (AdvancedCharDefsPopup.contains(e.target) === false) { !AdvancedCharDefsPopup.contains(e.target) &&
if (ConfirmationPopup.contains(e.target) === false) { !ConfirmationPopup.contains(e.target)) {
NavToggle.click(); NavToggle.click();
} }
}
}
}
}
}); });
//save NavLock prefs and record state of the Nav being open or closed //save NavLock prefs and record state of the Nav being open or closed
$(NavToggle).on("change", function () { $(NavToggle).on("change", function () {SaveLocal("NavOpened", $(NavToggle).prop("checked")); });
SaveLocal("NavOpened", $(NavToggle).prop("checked")); $(PanelPin).on("change",function () {SaveLocal("NavLockOn", $(PanelPin).prop("checked")); });
});
$(PanelPin).on("change",function () {
SaveLocal("NavLockOn", $(PanelPin).prop("checked"));
});
//save AutoConnect and AutoLoadChat prefs //save AutoConnect and AutoLoadChat prefs
$(AutoConnectCheckbox).on("change",function () { $(AutoConnectCheckbox).on("change",function () {SaveLocal("AutoConnectEnabled", $(AutoConnectCheckbox).prop("checked")); });
SaveLocal("AutoConnectEnabled", $(AutoConnectCheckbox).prop("checked")); $(AutoLoadChatCheckbox).on("change",function () {SaveLocal("AutoLoadChatEnabled", $(AutoLoadChatCheckbox).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 //save the clicked Nav Tab as the tab to auto-open on page reload
$("#rm_button_settings").click( function (){ $("#rm_button_settings").click( function (){SaveLocal('SelectedNavTab','rm_button_settings'); });
SaveLocal('SelectedNavTab','rm_button_settings'); $(SelectedCharacterTab).click(function () {SaveLocal('SelectedNavTab','rm_button_selected_ch'); });
});
$(SelectedCharacterTab).click(function () {
console.log('3rd tab clicked - saving selnavtab');
//SaveLocal('ActiveChar',this_chid);
SaveLocal('SelectedNavTab','rm_button_selected_ch');
});
$("#rm_button_characters").click( function () { //if char list is clicked, in addition to saving it... $("#rm_button_characters").click( function () { //if char list is clicked, in addition to saving it...
SaveLocal('SelectedNavTab','rm_button_characters'); SaveLocal('SelectedNavTab','rm_button_characters');
characters.sort(Intl.Collator().compare); // we sort the list 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 // 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 () { $(document).on("click", ".character_select",function () {SaveLocal('ActiveChar',$(this).attr('chid')); });
console.log('char clicked in charlist - saving local activechar');
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) //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 () { $('#send_textarea').on('input', function () {
this.style.height = '40px'; this.style.height = '40px';
this.style.height = this.style.height = (this.scrollHeight) + 'px';
(this.scrollHeight) + 'px';
}); });
//Regenerate if user swipes on the last mesage in chat //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 //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) { document.addEventListener('swiped-left', function(e) {
var SwipeButR = $('.swipe_right:last');
var SwipeTargetMesClassParent = e.target.closest('.last_mes'); var SwipeTargetMesClassParent = e.target.closest('.last_mes');
if (SwipeTargetMesClassParent !== null){ if (SwipeTargetMesClassParent !== null){
if($('.swipe_right:last').attr('style')=='display: flex;' == true){ if(SwipeButR.attr('style')=='display: flex;' == true){
$('.swipe_right:last').click(); SwipeButR.click();
} }
} }
}); });
document.addEventListener('swiped-right', function(e) { document.addEventListener('swiped-right', function(e) {
var SwipeButL = $('.swipe_left:last');
var SwipeTargetMesClassParent = e.target.closest('.last_mes'); var SwipeTargetMesClassParent = e.target.closest('.last_mes');
if (SwipeTargetMesClassParent !== null){ if (SwipeTargetMesClassParent !== null){
if($('.swipe_left:last').attr('style')=='display: flex;' == true){ if(SwipeButL.attr('style')=='display: flex;' == true){
$('.swipe_left:last').click(); SwipeButL.click();
} }
} }
}); });