From 0e378d93b2b9d9f4a1e58e15c4940e70a46f7b83 Mon Sep 17 00:00:00 2001 From: maver Date: Tue, 22 Aug 2023 20:37:37 +0200 Subject: [PATCH 1/3] Make quick edit fields static --- public/index.html | 19 +++++- public/scripts/PromptManager.js | 103 +++++++++++++++++++------------- 2 files changed, 77 insertions(+), 45 deletions(-) diff --git a/public/index.html b/public/index.html index f2688ac2d..991a3f513 100644 --- a/public/index.html +++ b/public/index.html @@ -1436,9 +1436,22 @@
-
-
- Select a character to show quick edit options. +
+
Main
+
+ +
+
+
+
NSFW
+
+ +
+
+
+
Jailbreak
+
+
diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js index 9b2e6d99a..af36abb1a 100644 --- a/public/scripts/PromptManager.js +++ b/public/scripts/PromptManager.js @@ -513,14 +513,69 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti }); } + // Fill quick edit fields for the first time + if ('global' === this.configuration.promptOrder.strategy) { + const handleQuickEditSave = (event) => { + const promptId = event.target.dataset.pmPrompt; + const prompt = this.getPromptById(promptId); + + prompt.content = event.target.value; + + // Update edit form if present + // @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetParent + const popupEditFormPrompt = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_prompt'); + if (popupEditFormPrompt.offsetParent) { + popupEditFormPrompt.value = prompt.content; + } + + this.log('Saved prompt: ' + promptId); + this.saveServiceSettings().then(() => this.render()); + }; + + const mainPrompt = this.getPromptById('main'); + const mainElementId = this.updateQuickEdit('main', mainPrompt); + document.getElementById(mainElementId).addEventListener('blur', handleQuickEditSave); + + const nsfwPrompt = this.getPromptById('nsfw'); + const nsfwElementId = this.updateQuickEdit('nsfw', nsfwPrompt); + document.getElementById(nsfwElementId).addEventListener('blur', handleQuickEditSave); + + const jailbreakPrompt = this.getPromptById('jailbreak'); + const jailbreakElementId = this.updateQuickEdit('jailbreak', jailbreakPrompt); + document.getElementById(jailbreakElementId).addEventListener('blur', handleQuickEditSave); + } + // Re-render when chat history changes. eventSource.on(event_types.MESSAGE_DELETED, () => this.renderDebounced()); eventSource.on(event_types.MESSAGE_EDITED, () => this.renderDebounced()); eventSource.on(event_types.MESSAGE_RECEIVED, () => this.renderDebounced()); // Re-render when chatcompletion settings change - eventSource.on(event_types.CHATCOMPLETION_SOURCE_CHANGED, () => this.renderDebounced()); - eventSource.on(event_types.CHATCOMPLETION_MODEL_CHANGED, () => this.renderDebounced()); + eventSource.on(event_types.CHATCOMPLETION_SOURCE_CHANGED, () => { + this.renderDebounced(); + + const mainPrompt = this.getPromptById('main'); + this.updateQuickEdit('main', mainPrompt); + + const nsfwPrompt = this.getPromptById('nsfw'); + this.updateQuickEdit('nsfw', nsfwPrompt); + + const jailbreakPrompt = this.getPromptById('jailbreak'); + this.updateQuickEdit('jailbreak', jailbreakPrompt); + }); + + eventSource.on(event_types.CHATCOMPLETION_MODEL_CHANGED, () => { + this.renderDebounced(); + + const mainPrompt = this.getPromptById('main'); + this.updateQuickEdit('main', mainPrompt); + + const nsfwPrompt = this.getPromptById('nsfw'); + this.updateQuickEdit('nsfw', nsfwPrompt); + + const jailbreakPrompt = this.getPromptById('jailbreak'); + this.updateQuickEdit('jailbreak', jailbreakPrompt); + }); // Re-render when the character changes. eventSource.on('chatLoaded', (event) => { @@ -1016,8 +1071,11 @@ PromptManagerModule.prototype.createQuickEdit = function (identifier, title) { } PromptManagerModule.prototype.updateQuickEdit = function (identifier, prompt) { - const textarea = document.getElementById(`${identifier}_prompt_quick_edit_textarea`); + const elementId = `${identifier}_prompt_quick_edit_textarea`; + const textarea = document.getElementById(elementId); textarea.value = prompt.content; + + return elementId; } /** @@ -1312,48 +1370,9 @@ PromptManagerModule.prototype.renderPromptManager = function () { footerDiv.querySelector('#prompt-manager-export').addEventListener('click', showExportSelection); rangeBlockDiv.querySelector('.export-promptmanager-prompts-full').addEventListener('click', this.handleFullExport); rangeBlockDiv.querySelector('.export-promptmanager-prompts-character')?.addEventListener('click', this.handleCharacterExport); - - const quickEditContainer = document.getElementById('quick-edit-container'); - const heights = this.saveTextAreaHeights(quickEditContainer); - quickEditContainer.innerHTML = ''; - - this.createQuickEdit('jailbreak', 'Jailbreak'); - this.createQuickEdit('nsfw', 'NSFW'); - this.createQuickEdit('main', 'Main'); - - this.restoreTextAreaHeights(quickEditContainer, heights); } }; -/** - * Restores the height of each textarea in the container - * @param container The container to search for textareas - * @param heights An object with textarea ids as keys and heights as values - */ -PromptManagerModule.prototype.restoreTextAreaHeights = function(container, heights) { - if (Object.keys(heights).length === 0) return; - - $(container).find('textarea').each(function () { - const height = heights[this.id]; - if (height > 0) $(this).height(height); - }); -} - -/** - * Saves the current height of each textarea in the container - * @param container The container to search for textareas - * @returns {{}} An object with textarea ids as keys and heights as values - */ -PromptManagerModule.prototype.saveTextAreaHeights = function(container) { - const heights = {}; - - $(container).find('textarea').each(function () { - heights[this.id] = $(this).height(); - }); - - return heights; -} - /** * Empties, then re-assembles the prompt list */ From 48992d4f53a618794b260b9a91320eb8c861c1b2 Mon Sep 17 00:00:00 2001 From: maver Date: Tue, 22 Aug 2023 20:39:46 +0200 Subject: [PATCH 2/3] Prevent context profiler from dangling on error --- public/scripts/PromptManager.js | 1 + 1 file changed, 1 insertion(+) diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js index af36abb1a..fd90dcc4e 100644 --- a/public/scripts/PromptManager.js +++ b/public/scripts/PromptManager.js @@ -664,6 +664,7 @@ PromptManagerModule.prototype.render = function (afterTryGenerate = true) { this.makeDraggable(); this.profileEnd('render'); }).catch(error => { + this.profileEnd('filling context'); this.log('Error caught during render: ' + error); this.renderPromptManager(); this.renderPromptManagerListItems() From 1ea7ad2572fe713e1998c624199831e9247dcaa1 Mon Sep 17 00:00:00 2001 From: maver Date: Tue, 22 Aug 2023 20:49:54 +0200 Subject: [PATCH 3/3] Update quick edit when oai preset changes --- public/scripts/PromptManager.js | 35 +++++++++++---------------------- 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js index fd90dcc4e..59909497f 100644 --- a/public/scripts/PromptManager.js +++ b/public/scripts/PromptManager.js @@ -551,31 +551,9 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti eventSource.on(event_types.MESSAGE_RECEIVED, () => this.renderDebounced()); // Re-render when chatcompletion settings change - eventSource.on(event_types.CHATCOMPLETION_SOURCE_CHANGED, () => { - this.renderDebounced(); + eventSource.on(event_types.CHATCOMPLETION_SOURCE_CHANGED, () => this.renderDebounced()); - const mainPrompt = this.getPromptById('main'); - this.updateQuickEdit('main', mainPrompt); - - const nsfwPrompt = this.getPromptById('nsfw'); - this.updateQuickEdit('nsfw', nsfwPrompt); - - const jailbreakPrompt = this.getPromptById('jailbreak'); - this.updateQuickEdit('jailbreak', jailbreakPrompt); - }); - - eventSource.on(event_types.CHATCOMPLETION_MODEL_CHANGED, () => { - this.renderDebounced(); - - const mainPrompt = this.getPromptById('main'); - this.updateQuickEdit('main', mainPrompt); - - const nsfwPrompt = this.getPromptById('nsfw'); - this.updateQuickEdit('nsfw', nsfwPrompt); - - const jailbreakPrompt = this.getPromptById('jailbreak'); - this.updateQuickEdit('jailbreak', jailbreakPrompt); - }); + eventSource.on(event_types.CHATCOMPLETION_MODEL_CHANGED, () => this.renderDebounced()); // Re-render when the character changes. eventSource.on('chatLoaded', (event) => { @@ -632,6 +610,15 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti this.hidePopup(); this.clearEditForm(); this.renderDebounced(); + + const mainPrompt = this.getPromptById('main'); + this.updateQuickEdit('main', mainPrompt); + + const nsfwPrompt = this.getPromptById('nsfw'); + this.updateQuickEdit('nsfw', nsfwPrompt); + + const jailbreakPrompt = this.getPromptById('jailbreak'); + this.updateQuickEdit('jailbreak', jailbreakPrompt); }); });