From abac61e41a57a9ccfe7116680ff68e62a67163be Mon Sep 17 00:00:00 2001 From: ebolam Date: Thu, 6 Oct 2022 19:22:33 -0400 Subject: [PATCH] Fix for scroll --- aiserver.py | 5 +++++ koboldai_settings.py | 2 -- static/koboldai.js | 40 ++++++++++++++++++++++++++++++++++------ 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/aiserver.py b/aiserver.py index 9e83d765..dec57f01 100644 --- a/aiserver.py +++ b/aiserver.py @@ -7060,10 +7060,12 @@ def load_story_v1(js): if(koboldai_vars.gamestarted): #We set the action count higher so that we don't trigger a scroll in the UI. #Once all but the last is loaded we can bring it back down and do the last one so we scroll to it + logger.debug("Created temp story class") temp_story_class = koboldai_settings.KoboldStoryRegister(None, None, koboldai_vars, tokenizer=None) for i in range(len(js["actions"])): temp_story_class.append(js["actions"][i], recalc=False) + logger.debug("Added actions to temp story class") if "actions_metadata" in js: @@ -7075,6 +7077,8 @@ def load_story_v1(js): data[i]["text"] = data[i].pop("Text") temp_story_class.set_options(data, int(key)) koboldai_vars.actions.load_json(temp_story_class.to_json()) + logger.debug("Saved temp story class") + time.sleep(30) del temp_story_class # Try not to break older save files @@ -9076,6 +9080,7 @@ def UI_2_get_next_100_actions(data): break data_to_send.append({"id": i, "action": koboldai_vars.actions.actions[i]}) sent += 1 + logger.debug("data_to_send length: {}".format(len(data_to_send))) emit("var_changed", {"classname": "story", "name": "actions", "old_value": None, "value":data_to_send}) #==================================================================# diff --git a/koboldai_settings.py b/koboldai_settings.py index d5ca9ac6..5c62ea1e 100644 --- a/koboldai_settings.py +++ b/koboldai_settings.py @@ -1038,7 +1038,6 @@ class system_settings(settings): self._horde_pid.terminate() self._horde_pid = None - class KoboldStoryRegister(object): def __init__(self, socketio, story_settings, koboldai_vars, tokenizer=None, sequence=[]): self.socketio = socketio @@ -1512,7 +1511,6 @@ class KoboldStoryRegister(object): if name == 'action_count' and not new_variable: process_variable_changes(self.socketio, "actions", "Action Count", value, old_value) - class KoboldWorldInfo(object): def __init__(self, socketio, story_settings, koboldai_vars, tokenizer=None): diff --git a/static/koboldai.js b/static/koboldai.js index 70059876..fc722c36 100644 --- a/static/koboldai.js +++ b/static/koboldai.js @@ -61,6 +61,8 @@ var control_held = false; var actions_data = {}; var setup_wi_toggles = []; var scroll_trigger_element = undefined; +var first_scroll_occurred = false; +var temp_counter = 0; const on_colab = $el("#on_colab").textContent == "true"; // name, desc, icon, func @@ -157,6 +159,7 @@ function reset_story() { current_chunk_number = null; console.log("resetting scroll_trigger_element"); scroll_trigger_element = undefined; + first_scroll_occurred = false; var story_area = document.getElementById('Selected Text'); let temp = [] for (child of story_area.children) { @@ -545,6 +548,10 @@ function var_changed(data) { //Special Case for Actions if ((data.classname == "story") && (data.name == "actions")) { start_time = Date.now(); + temp_counter += 1; + if (temp_counter > 10) { + return; + } if (document.getElementById("Selected Text").firstElementChild.id == "story_prompt") { first_story_element = document.getElementById("Selected Text").firstElementChild.nextElementSibling; } else { @@ -556,6 +563,12 @@ function var_changed(data) { actions = [data.value]; } if (actions.length == 0) {return;} + let seen_action = false; + if ((actions[0].id in actions_data) && (actions[actions.length-1].id in actions_data)) { + seen_action = true; + } + console.log("Loading actions "+actions[0].id+" to "+actions[actions.length-1].id); + console.log(actions); for (action of actions) { if (action.length != 0) { actions_data[action.id] = action.action; @@ -572,25 +585,39 @@ function var_changed(data) { } } } - //if we hit the top, unhide the prompt and move it to the top - if (actions[actions.length-1].id == 0) { + if (seen_action) { + //We've seen this data before, no scrolling needed + console.log("Already seen actions, doing nothing"); + } else if ((actions[actions.length-1].id == 0) || (actions[0].id == 0)) { + //if we hit the top, unhide the prompt and move it to the top + console.log("Hit the prompt, unhiding and killing scrolling"); + scroll_trigger_element = null; prompt_span = document.getElementById("story_prompt"); document.getElementById("Selected Text").prepend(prompt_span); prompt_span.classList.remove("hidden"); } else if (actions[actions.length-1].id != current_action) { //we are bringing in old data (not adding to the end of the game text), so set the scroll event back up //and scroll so that the old top is at the top again (IE new text added is before the top of the screen) + console.log("got old actions, ading them and scrolling"); if (first_story_element) { first_story_element.scrollIntoView(true); } - scroll_trigger_element = document.getElementById('Selected Text Chunk '+actions[actions.length-1].id); + if (scroll_trigger_element != null) { + console.log("Setting scroll trigger to Selected Text Chunk "+actions[actions.length-1].id); + scroll_trigger_element = document.getElementById('Selected Text Chunk '+actions[actions.length-1].id); + } } else { + console.log("New action added, scrolling to it"); //We are adding new game text to the screen (not past actions) console.log("Scrolling to 'Selected Text Chunk '"+actions[actions.length-1].id); console.log(document.getElementById('Selected Text Chunk '+actions[actions.length-1].id)); - setTimeout(function() {document.getElementById('Selected Text Chunk '+actions[actions.length-1].id).scrollIntoView(false);}, 20); + setTimeout(function() { + document.getElementById('Selected Text Chunk '+actions[actions.length-1].id).scrollIntoView(false); + first_scroll_occurred = true; + }, 20); //If this is the first add, then we need to set our scroll trigger up - if (document.getElementsByClassName("rawtext").length == actions.length+1) { + if ((document.getElementsByClassName("rawtext").length == actions.length+1) && (scroll_trigger_element != null)) { + console.log("Setting scroll trigger to Selected Text Chunk "+actions[0].id); scroll_trigger_element = document.getElementById('Selected Text Chunk '+actions[0].id); } } @@ -4804,9 +4831,10 @@ document.addEventListener("keydown", function(event) { //function to load more actions if nessisary document.getElementById("Selected Text").onscroll = function(){ //TOP - if ((scroll_trigger_element != undefined) && (scroll_trigger_element != null)) { + if ((scroll_trigger_element != undefined) && (scroll_trigger_element != null) && (first_scroll_occurred == true)) { if(scroll_trigger_element.getBoundingClientRect().bottom >= 0){ console.log("Scrolling action: "+scroll_trigger_element.getAttribute("chunk")); + console.log("sending emit"); socket.emit("get_next_100_actions", parseInt(scroll_trigger_element.getAttribute("chunk"))); scroll_trigger_element == null; }