Merge branch 'SL-TAI-RA-Mods' into dev

This commit is contained in:
SillyLossy
2023-03-15 21:36:20 +02:00
38 changed files with 344 additions and 81 deletions

View File

@ -2,22 +2,25 @@ import { encode } from "../scripts/gpt-2-3-tokenizer/mod.js";
import {
Generate,
getSettings,
/* getSettings,
saveSettings,
printMessages,
clearChat,
getChat,
this_chid,
settings,
chat
settings, */
this_chid,
characters,
online_status,
main_api,
api_server,
api_key_novel,
getCharacters,
is_send_press
is_send_press,
} from "../script.js";
import { LoadLocal, SaveLocal, ClearLocal, CheckLocal, LoadLocalBool } from "./f-localStorage.js";
var NavToggle = document.getElementById("nav-toggle");
var PanelPin = document.getElementById("rm_button_panel_pin");
var SelectedCharacterTab = document.getElementById("rm_button_selected_ch");
@ -38,30 +41,6 @@ var count_tokens;
var perm_tokens;
var ALC_Done;
////////////////// LOCAL STORAGE HANDLING /////////////////////
function SaveLocal(target, val) {
localStorage.setItem(target, val);
console.log('SaveLocal -- '+target+' : '+val);
}
function LoadLocal(target) {
return localStorage.getItem(target);
}
function LoadLocalBool(target){
let result = localStorage.getItem(target) === 'true';
return result;
}
function CheckLocal() {
console.log("----------local storage---------");
var i;
for (i = 0; i < localStorage.length; i++) {
console.log(localStorage.key(i) +" : " +localStorage.getItem(localStorage.key(i)));
}
console.log("------------------------------");
}
function ClearLocal() {localStorage.clear();console.log('Removed All Local Storage');}
/////////////////////////////////////////////////////////////////////////
//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.
//Does not break old characters/chats, as the code just uses whatever timestamp exists in the chat.
@ -271,18 +250,14 @@ $("document").ready(function () {
$(AutoConnectCheckbox).prop("checked",LoadLocalBool("AutoConnectEnabled"));
$(AutoLoadChatCheckbox).prop("checked",LoadLocalBool("AutoLoadChatEnabled"));
//AutoLoadChat and AutoConnect must be loaded on a small delay after page load to allow getSettings to fill out what they need
if (LoadLocalBool('AutoLoadChatEnabled') == true) {
console.log('calling RA_ALC');
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();});
$("#api_button").click(function () {setTimeout(RA_checkOnlineStatus, 100);});
//close the RightNav panel when user clicks outside of it or related panels (adv editing popup, or dialog popups)
@ -343,6 +318,37 @@ $("document").ready(function () {
(this.scrollHeight) + 'px';
});
//Regenerate if user swipes on the last mesage in chat
//TODO:
//1. Make it detect if the last message is from user, and ignore swipes then...
//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 SwipeTargetMesClassParent = e.target.closest('.mes');
if (is_send_press == false){
if (SwipeTargetMesClassParent !== null && SwipeTargetMesClassParent.nextSibling == null ){
$('#chat').children().last().css({'transition':'all 0.5s ease-in-out'});
$('#chat').children().last().css({'transform':'translateX(-100vw) scale(0,0)','overflow':'hidden'});
$('#chat').children().last().css({'opacity':'0'});
Generate('regenerate');
}
}
});
document.addEventListener('swiped-right', function(e) {
var SwipeTargetMesClassParent = e.target.closest('.mes');
console.log(is_send_press);
if (is_send_press === false){
if (SwipeTargetMesClassParent !== null && SwipeTargetMesClassParent.nextSibling == null){
$('#chat').children().last().css({'transition':'all 0.5s ease-in-out'});
$('#chat').children().last().css({'transform':'translateX(100vh) scale(0,0)','overflow':'hidden'});
$('#chat').children().last().css({'opacity':'0'});
Generate('regenerate');
console.log(is_send_press);
}
}
});
//Additional hotkeys CTRL+ENTER and CTRL+UPARROW
document.addEventListener("keydown", (event) => {
if (event.ctrlKey && event.key == "Enter") {

View File

@ -0,0 +1,25 @@
////////////////// LOCAL STORAGE HANDLING /////////////////////
export function SaveLocal(target, val) {
localStorage.setItem(target, val);
console.log('SaveLocal -- '+target+' : '+val);
}
export function LoadLocal(target) {
return localStorage.getItem(target);
}
export function LoadLocalBool(target){
let result = localStorage.getItem(target) === 'true';
return result;
}
export function CheckLocal() {
console.log("----------local storage---------");
var i;
for (i = 0; i < localStorage.length; i++) {
console.log(localStorage.key(i) +" : " +localStorage.getItem(localStorage.key(i)));
}
console.log("------------------------------");
}
export function ClearLocal() {localStorage.clear();console.log('Removed All Local Storage');}
/////////////////////////////////////////////////////////////////////////

View File

@ -0,0 +1,165 @@
/*!
* swiped-events.js - v@version@
* Pure JavaScript swipe events
* https://github.com/john-doherty/swiped-events
* @inspiration https://stackoverflow.com/questions/16348031/disable-scrolling-when-touch-moving-certain-element
* @author John Doherty <www.johndoherty.info>
* @license MIT
*/
(function (window, document) {
'use strict';
// patch CustomEvent to allow constructor creation (IE/Chrome)
if (typeof window.CustomEvent !== 'function') {
window.CustomEvent = function (event, params) {
params = params || { bubbles: false, cancelable: false, detail: undefined };
var evt = document.createEvent('CustomEvent');
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
return evt;
};
window.CustomEvent.prototype = window.Event.prototype;
}
document.addEventListener('touchstart', handleTouchStart, false);
document.addEventListener('touchmove', handleTouchMove, false);
document.addEventListener('touchend', handleTouchEnd, false);
var xDown = null;
var yDown = null;
var xDiff = null;
var yDiff = null;
var timeDown = null;
var startEl = null;
/**
* Fires swiped event if swipe detected on touchend
* @param {object} e - browser event object
* @returns {void}
*/
function handleTouchEnd(e) {
// if the user released on a different target, cancel!
if (startEl !== e.target) return;
var swipeThreshold = parseInt(getNearestAttribute(startEl, 'data-swipe-threshold', '20'), 10); // default 20 units
var swipeUnit = getNearestAttribute(startEl, 'data-swipe-unit', 'px'); // default px
var swipeTimeout = parseInt(getNearestAttribute(startEl, 'data-swipe-timeout', '500'), 10); // default 500ms
var timeDiff = Date.now() - timeDown;
var eventType = '';
var changedTouches = e.changedTouches || e.touches || [];
if (swipeUnit === 'vh') {
swipeThreshold = Math.round((swipeThreshold / 100) * document.documentElement.clientHeight); // get percentage of viewport height in pixels
}
if (swipeUnit === 'vw') {
swipeThreshold = Math.round((swipeThreshold / 100) * document.documentElement.clientWidth); // get percentage of viewport height in pixels
}
if (Math.abs(xDiff) > Math.abs(yDiff)) { // most significant
if (Math.abs(xDiff) > swipeThreshold && timeDiff < swipeTimeout) {
if (xDiff > 0) {
eventType = 'swiped-left';
}
else {
eventType = 'swiped-right';
}
}
}
else if (Math.abs(yDiff) > swipeThreshold && timeDiff < swipeTimeout) {
if (yDiff > 0) {
eventType = 'swiped-up';
}
else {
eventType = 'swiped-down';
}
}
if (eventType !== '') {
var eventData = {
dir: eventType.replace(/swiped-/, ''),
touchType: (changedTouches[0] || {}).touchType || 'direct',
xStart: parseInt(xDown, 10),
xEnd: parseInt((changedTouches[0] || {}).clientX || -1, 10),
yStart: parseInt(yDown, 10),
yEnd: parseInt((changedTouches[0] || {}).clientY || -1, 10)
};
// fire `swiped` event event on the element that started the swipe
startEl.dispatchEvent(new CustomEvent('swiped', { bubbles: true, cancelable: true, detail: eventData }));
// fire `swiped-dir` event on the element that started the swipe
startEl.dispatchEvent(new CustomEvent(eventType, { bubbles: true, cancelable: true, detail: eventData }));
}
// reset values
xDown = null;
yDown = null;
timeDown = null;
}
/**
* Records current location on touchstart event
* @param {object} e - browser event object
* @returns {void}
*/
function handleTouchStart(e) {
// if the element has data-swipe-ignore="true" we stop listening for swipe events
if (e.target.getAttribute('data-swipe-ignore') === 'true') return;
startEl = e.target;
timeDown = Date.now();
xDown = e.touches[0].clientX;
yDown = e.touches[0].clientY;
xDiff = 0;
yDiff = 0;
}
/**
* Records location diff in px on touchmove event
* @param {object} e - browser event object
* @returns {void}
*/
function handleTouchMove(e) {
if (!xDown || !yDown) return;
var xUp = e.touches[0].clientX;
var yUp = e.touches[0].clientY;
xDiff = xDown - xUp;
yDiff = yDown - yUp;
}
/**
* Gets attribute off HTML element or nearest parent
* @param {object} el - HTML element to retrieve attribute from
* @param {string} attributeName - name of the attribute
* @param {any} defaultValue - default value to return if no match found
* @returns {any} attribute value or defaultValue
*/
function getNearestAttribute(el, attributeName, defaultValue) {
// walk up the dom tree looking for attributeName
while (el && el !== document.documentElement) {
var attributeValue = el.getAttribute(attributeName);
if (attributeValue) {
return attributeValue;
}
el = el.parentNode;
}
return defaultValue;
}
}(window, document));