From afa773fe84abf45a6e0c0978faacc672b9f107a4 Mon Sep 17 00:00:00 2001 From: Mic Date: Sun, 15 Jun 2025 23:47:00 +0200 Subject: [PATCH] handling custom placeholders in checking custom prompts features. see #156 --- pages/customprompts/mzta-custom-prompts.js | 23 ++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/pages/customprompts/mzta-custom-prompts.js b/pages/customprompts/mzta-custom-prompts.js index faecf19b..c4126ce4 100644 --- a/pages/customprompts/mzta-custom-prompts.js +++ b/pages/customprompts/mzta-custom-prompts.js @@ -20,7 +20,7 @@ import { prefs_default } from "../../options/mzta-options-default.js"; import { getPrompts, setDefaultPromptsProperties, setCustomPrompts, preparePromptsForExport, preparePromptsForImport } from "../../js/mzta-prompts.js"; import { ChatGPTWeb_models, isThunderbird128OrGreater, getLocalStorageUsedSpace, sanitizeHtml, validateCustomData_ChatGPTWeb, getChatGPTWebModelsList_HTML } from "../../js/mzta-utils.js"; import { taLogger } from "../../js/mzta-logger.js"; -import { getPlaceholders } from "../../js/mzta-placeholders.js"; +import { getPlaceholders, placeholdersUtils } from "../../js/mzta-placeholders.js"; import { textareaAutocomplete } from "../../js/mzta-placeholders-autocomplete.js"; let prefs = null; @@ -88,13 +88,13 @@ document.addEventListener('DOMContentLoaded', async () => { const textareas = document.querySelectorAll('.editor'); autocompleteSuggestions = (await getPlaceholders(true)).map(p => ({command: '{%'+p.id+'%}', type: p.type})); - // console.log('>>>>>>>>>>> suggestions: ' + JSON.stringify(suggestions)); + // console.log('>>>>>>>>>>> autocompleteSuggestions: ' + JSON.stringify(autocompleteSuggestions)); textareas.forEach(textarea => { textareaAutocomplete(textarea, autocompleteSuggestions); textareas.forEach(textarea => { - textarea.addEventListener('input', (e) => { - checkPromptsConfigForPlaceholders(e.target); + textarea.addEventListener('input', async (e) => { + await checkPromptsConfigForPlaceholders(e.target); }); textareaAutocomplete(textarea, autocompleteSuggestions); }); @@ -161,7 +161,6 @@ document.addEventListener('DOMContentLoaded', async () => { } }); - //To add a new item var txtIdNew = document.getElementById('txtIdNew'); var txtNameNew = document.getElementById('txtNameNew'); @@ -588,13 +587,13 @@ function handleConfirmClick(e) { } // Handle checkbox changes and log new state -function handleCheckboxChange(e) { +async function handleCheckboxChange(e) { e.preventDefault(); e.target.setAttribute('checked_val', e.target.checked ? '1' : '0'); //console.log('>>>>>>>> checked_val: ' + e.target.getAttribute('checked_val')); if (e.target.classList.contains('need_selected') || e.target.classList.contains('need_custom_text') || e.target.classList.contains('need_selected_new') || e.target.classList.contains('need_custom_text_new')) { let textarea = e.target.closest('tr').querySelector('.text_output'); - checkPromptsConfigForPlaceholders(textarea); + await checkPromptsConfigForPlaceholders(textarea); } } @@ -950,11 +949,15 @@ if(await isThunderbird128OrGreater()){ }); } -function checkPromptsConfigForPlaceholders(textarea){ +async function checkPromptsConfigForPlaceholders(textarea){ + let curr_text = textarea.value; + // First substitute the custom data placeholders + curr_text = await placeholdersUtils.replaceCustomPlaceholders(curr_text); + console.log('>>>>>>>>>> curr_text after custom placeholders: ' + curr_text); // check additional_text and selected_text placeholders presence and the corrispondent checkboxes let tr_ancestor = textarea.closest('tr'); let need_custom_text_element = tr_ancestor.querySelector('.need_custom_text') || tr_ancestor.querySelector('.need_custom_text_new'); - if(String(textarea.value).indexOf('{%additional_text%}') != -1){ + if(String(curr_text).indexOf('{%additional_text%}') != -1){ if(!need_custom_text_element.checked){ need_custom_text_element.closest('.need_custom_text_span').style.border = '2px solid red'; }else{ @@ -966,7 +969,7 @@ function checkPromptsConfigForPlaceholders(textarea){ let tr_ancestor2 = textarea.closest('tr'); let selected_text_element = tr_ancestor2.querySelector('.need_selected') || tr_ancestor2.querySelector('.need_selected_new'); - if((String(textarea.value).indexOf('{%selected_text%}') != -1)||(String(textarea.value).indexOf('{%selected_html%}') != -1)){ + if((String(curr_text).indexOf('{%selected_text%}') != -1)||(String(curr_text).indexOf('{%selected_html%}') != -1)){ if(!selected_text_element.checked){ selected_text_element.closest('.need_selected_span').style.border = '2px solid red'; }else{