From 14a266e3551c1359a39f896972c333d5efd5dba2 Mon Sep 17 00:00:00 2001 From: somebody Date: Sun, 28 Aug 2022 21:28:07 -0500 Subject: [PATCH] File upload wip --- static/koboldai.css | 21 ++++++++ static/koboldai.js | 106 +++++++++++++++++++++++++++++++++++++++ templates/index_new.html | 4 ++ 3 files changed, 131 insertions(+) diff --git a/static/koboldai.css b/static/koboldai.css index fe784ae9..a8d78532 100644 --- a/static/koboldai.css +++ b/static/koboldai.css @@ -1638,6 +1638,27 @@ body { .context-an {background-color: var(--context_colors_authors_notes);} .context-action {background-color: var(--context_colors_game_text);} +/* File Drag Indicator */ +#file-upload-notice { + display: flex; + justify-content: center; + align-items: center; + + position: absolute; + left: 0px; + top: 0px; + + background-color: rgba(0, 0, 0, 0.5); + z-index: 20; + + width: 100vw; + height: 100vh; +} + +#file-upload-notice > span { + font-size: 50vh; + opacity: 0.7; +} /*---------------------------------- Global ------------------------------------------------*/ .hidden { display: none; diff --git a/static/koboldai.js b/static/koboldai.js index f974bc6d..b1ca307e 100644 --- a/static/koboldai.js +++ b/static/koboldai.js @@ -2847,6 +2847,75 @@ function detect_key_up(e) { } } +function loadNAILorebook(data) { + let lorebookVersion = data.lorebookVersion; + console.log(`Loading NAI lorebook version ${lorebookVersion}`); + + // TODO: Make folder + let folder = "root"; + + let uid = -1; + for (item of document.getElementsByClassName('world_info_card')) { + if (parseInt(item.getAttribute("uid")) <= uid) { + uid = parseInt(item.getAttribute("uid")) - 1; + } + } + + for (const entry of data.entries) { + console.log(entry); + // contextConfig: Object { suffix: "\n", tokenBudget: 2048, reservedTokens: 0, … } + // displayName: "Aboleth" + // enabled: true + // forceActivation: false + // keys: Array [ "Aboleth" ] + // lastUpdatedAt: 1624443329051 + // searchRange: 1000 + // text + data = { + "uid": uid, + "title": entry.displayName, + "key": entry.keys, + "keysecondary": [], + "folder": folder, + "constant": entry.forceActivation, + "content": "",//entry.text, + "manual_text": entry.text, + "comment": "", + "token_length": 0, + "selective": false, + "wpp": {"name": "", "type": "", "format": "W++", "attributes": {}}, + "use_wpp": false, + }; + uid--; + card = world_info_entry(data); + card.scrollIntoView(false); + console.log(card); + } + +} + +async function processDroppedFile(file) { + let extension = /.*\.(.*)/.exec(file.name)[1]; + console.log("file is", file) + + switch (extension) { + case "png": + // TODO: Support NovelAI's image lorebook cards. The format for those + // is base64-encoded JSON under a TXT key called "naidata". + console.warn("TODO: NAI LORECARDS"); + return; + case "json": + // KoboldAI story (probably, parse to be sure.); + console.warn("TODO: KOBOLD STORY"); + break; + case "lorebook": + // NovelAI lorebook, JSON encoded. + let data = JSON.parse(await file.text()); + loadNAILorebook(data); + break; + } +} + $(document).ready(function(){ create_theming_elements(); document.onkeydown = detect_key_down; @@ -2915,4 +2984,41 @@ $(document).ready(function(){ $(".token_breakdown").click(function() { document.getElementById("context-viewer-container").classList.remove("hidden"); }); + + document.body.addEventListener("drop", function(e) { + e.preventDefault(); + $("#file-upload-notice")[0].classList.add("hidden"); + + // items api + if (e.dataTransfer.items) { + for (const item of e.dataTransfer.items) { + if (item.kind !== "file") continue; + let file = item.getAsFile(); + processDroppedFile(file); + } + } else { + for (const file of e.dataTransfer.files) { + processDroppedFile(file); + } + } + }); + + let lastTarget = null; + + document.body.addEventListener("dragover", function(e) { + e.preventDefault(); + }); + + document.body.addEventListener("dragenter", function(e) { + lastTarget = e.target; + console.log("start"); + $("#file-upload-notice")[0].classList.remove("hidden"); + }); + + document.body.addEventListener("dragleave", function(e) { + if (!(e.target === document || e.target === lastTarget)) return; + + console.log("end") + $("#file-upload-notice")[0].classList.add("hidden"); + }); }); diff --git a/templates/index_new.html b/templates/index_new.html index f21e54f8..a13b4920 100644 --- a/templates/index_new.html +++ b/templates/index_new.html @@ -139,5 +139,9 @@
+ + \ No newline at end of file