From c172ff6b0ba856fb1a0675b67c3640816b531947 Mon Sep 17 00:00:00 2001 From: ebolam Date: Wed, 14 Sep 2022 11:58:01 -0400 Subject: [PATCH] Initial commit of better editing method. Has some visual bugs that need to be worked on --- aiserver.py | 2 +- static/koboldai.css | 3 +- static/koboldai.js | 63 +++++++++++++++++++++++++++++++--------- templates/index_new.html | 6 ++-- 4 files changed, 54 insertions(+), 20 deletions(-) diff --git a/aiserver.py b/aiserver.py index be4b5263..8fad41db 100644 --- a/aiserver.py +++ b/aiserver.py @@ -7144,7 +7144,7 @@ def UI_2_save_to_json(): #==================================================================# @socketio.on('Set Selected Text') def UI_2_Set_Selected_Text(data): - if koboldai_vars.quiet: + if not koboldai_vars.quiet: print("Updating Selected Text: {}".format(data)) koboldai_vars.actions[int(data['id'])] = data['text'] diff --git a/static/koboldai.css b/static/koboldai.css index 1f9928e7..fc770080 100644 --- a/static/koboldai.css +++ b/static/koboldai.css @@ -1188,8 +1188,7 @@ body { display: none; } -[contenteditable="true"]:active, -[contenteditable="true"]:focus{ +.editing{ border:none; outline:none; color: var(--text_edit); diff --git a/static/koboldai.js b/static/koboldai.js index 33e229d2..50d18358 100644 --- a/static/koboldai.js +++ b/static/koboldai.js @@ -48,6 +48,7 @@ var on_colab; var colab_cookies = null; var wi_finder_data = []; var wi_finder_offset = 0; +var selected_game_chunk = null; // name, desc, icon, func const finder_actions = [ @@ -272,17 +273,8 @@ function do_story_text_updates(data) { var span = document.createElement("span"); span.id = 'Selected Text Chunk '+data.value.id; span.classList.add("rawtext"); - span.chunk = data.value.id; + span.setAttribute("chunk", data.value.id); span.original_text = data.value.action['Selected Text']; - span.setAttribute("contenteditable", true); - span.onblur = function () { - if (this.textContent != this.original_text) { - socket.emit("Set Selected Text", {"id": this.chunk, "text": this.textContent}); - this.original_text = this.textContent; - this.classList.add("pulse"); - } - } - span.onkeydown = detect_enter_text; new_span = document.createElement("span"); new_span.textContent = data.value.action['Selected Text']; span.append(new_span); @@ -306,9 +298,7 @@ function do_prompt(data) { } span = document.createElement("span"); - span2 = document.createElement("span"); - span2.textContent = data.value; - span.append(span2); + span.textContent = data.value; item.append(span); item.setAttribute("old_text", data.value) item.classList.remove("pulse"); @@ -2044,7 +2034,6 @@ function save_tweaks() { setCookie("enabledTweaks", JSON.stringify(out)); } - function load_tweaks() { let enabledTweaks = JSON.parse(getCookie("enabledTweaks", "[]")); @@ -2075,6 +2064,52 @@ function toggle_adventure_mode(button) { } +function select_game_text(event) { + if ((event == null) || (event.code == 'ArrowRight') || (event.code == 'ArrowLeft') || (event.code == 'ArrowDown') || (event.code == 'ArrowUp')) { + let new_selected_game_chunk = null; + if (document.selection) { + if (document.selection.createRange().parentElement().id == 'story_prompt') { + new_selected_game_chunk = document.selection.createRange().parentElement(); + } else { + new_selected_game_chunk = document.selection.createRange().parentElement().parentElement(); + } + } else { + if (window.getSelection().anchorNode.parentNode.id == 'story_prompt') { + new_selected_game_chunk = window.getSelection().anchorNode.parentNode; + } else { + new_selected_game_chunk = window.getSelection().anchorNode.parentNode.parentNode; + } + } + //if we've moved to a new game chunk we need to save the old chunk + if ((new_selected_game_chunk != selected_game_chunk) && (selected_game_chunk != null)) { + edit_game_text(); + } + if (new_selected_game_chunk != selected_game_chunk) { + selected_game_chunk = new_selected_game_chunk + } + + //set editting class + for (item of document.getElementsByClassName("editing")) { + item.classList.remove("editing"); + } + selected_game_chunk.classList.add("editing"); + } +} + +function edit_game_text() { + if ((selected_game_chunk != null) && (selected_game_chunk.textContent != selected_game_chunk.original_text)) { + console.log("selected_chunk_id: "+selected_game_chunk.id); + console.log(selected_game_chunk); + if (selected_game_chunk.id == "story_prompt") { + sync_to_server(selected_game_chunk); + } else { + socket.emit("Set Selected Text", {"id": selected_game_chunk.getAttribute("chunk"), "text": selected_game_chunk.textContent}); + } + selected_game_chunk.original_text = selected_game_chunk.textContent; + selected_game_chunk.classList.add("pulse"); + } +} + //--------------------------------------------General UI Functions------------------------------------ function autoResize(element) { element.style.height = 'auto'; diff --git a/templates/index_new.html b/templates/index_new.html index 8498777e..112547bf 100644 --- a/templates/index_new.html +++ b/templates/index_new.html @@ -6,7 +6,7 @@ var debug_info = {errors: []}; window.addEventListener('error', function(event) { - debug_info.push({msg: message, url: url, line: line}); + // debug_info.push({msg: message, url: url, line: line}); }); @@ -47,8 +47,8 @@

Disconnected

-
- +
+