Skip to content

Commit

Permalink
calendar event settings page completed. see #182
Browse files Browse the repository at this point in the history
  • Loading branch information
micz committed Jan 11, 2025
1 parent 6d4c5fa commit d264e9e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 92 deletions.
16 changes: 16 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -974,5 +974,21 @@
"prefs_OptionText_btnManageCalendarEventInfo": {
"message": "Manage calendar events settings",
"description": ""
},
"GetCalendarEvent_PageTitle": {
"message": "Manage Calendar Events Settings",
"description": ""
},
"GetCalendarEvent_info_default": {
"message": "In this page you can modify the default prompt used to get a calendar event from selected text.",
"description": ""
},
"GetCalendarEvent_prompt_text_title": {
"message": "Current prompt text",
"description": ""
},
"prefs_OptionText_GetCalendarEvent_infoline2": {
"message": "You can change the prompt as you wish, but the response received from the AI must be in JSON format as specified in the default prompt!",
"description": ""
}
}
20 changes: 6 additions & 14 deletions pages/get-calendar-event/mzta-get-calendar-event.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,28 @@
<html>
<head>
<meta charset="UTF-8">
<title>ThunderAI - __MSG_AddTags_PageTitle__</title>
<title>ThunderAI - __MSG_GetCalendarEvent_PageTitle__</title>
<link rel="stylesheet" href="mzta-get-calendar-event.css">
<link rel="icon" href="../../images/icon-16px.png">
</head>
<body>
<div>
<h1 class="page_title">__MSG_AddTags_GetCalendarEvent__</h1>
<h1 class="page_title">__MSG_GetCalendarEvent_PageTitle__</h1>
<p>__MSG_GetCalendarEvent_info_default__</p>
</div>
<i>Work in progress...</i>
<div id="get_calendar_event_container">
<span class="section_title">__MSG_GetCalendarEvent_prompt_text_title__</span><span id="addtags_prompt_unsaved" class="unsaved hidden"> __MSG_customPrompts_unsaved_changes__</span>
<span class="section_title">__MSG_GetCalendarEvent_prompt_text_title__</span><span id="get_calendar_event_prompt_unsaved" class="unsaved hidden"> __MSG_customPrompts_unsaved_changes__</span>
<br><span class="infoline">__MSG_prefs_OptionText_btnManagePrompts_infoline__ <a href="https://micz.it/thunderbird-addon-thunderai/data-placeholders/">__MSG_customPrompts_managePrompts_help__</a>
<br>__MSG_prefs_OptionText_btnManagePrompts_infoline3__
<br><b>__MSG_prefs_OptionText_btnManagePrompts_infoline2__</b></span>
<br><b>__MSG_prefs_OptionText_GetCalendarEvent_infoline2__</b></span>
<br>
<div class="autocomplete-container">
<textarea id="addtags_prompt_text" rows="15"></textarea>
<textarea id="get_calendar_event_prompt_text" rows="15"></textarea>
<ul class="autocomplete-list hidden"></ul>
</div>
<div id="addtags_info_additional_statements"></div>
<div id="get_calendar_event_info_additional_statements"></div>
<div class="btn_div"><button id="btn_reset_prompt" disabled>__MSG_reset_default__</button><button id="btn_save_prompt" disabled>__MSG_save__</button></div>
</div>
<div id="addtags_excl_list_container">
<span class="section_title">__MSG_AddTags_excl_list_title__</span><span id="excl_list_unsaved" class="unsaved hidden"> __MSG_customPrompts_unsaved_changes__</span>
<br><span class="infoline">__MSG_AddTags_excl_list_infoline__
<br>__MSG_AddTags_excl_list_infoline2__</span>
<br><textarea id="addtags_excl_list" rows="15"></textarea>
<div class="btn_div"><button id="btn_save_excl_list" disabled>__MSG_save__</button></div>
</div>
<script src="mzta-get-calendar-event.js" type="module"></script>
<script src="../../js/mzta-i18n.js"></script>
</body>
Expand Down
102 changes: 24 additions & 78 deletions pages/get-calendar-event/mzta-get-calendar-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import { taLogger } from '../../js/mzta-logger.js';
import { getSpecialPrompts, setSpecialPrompts } from "../../js/mzta-prompts.js";
import { getPlaceholders } from "../../js/mzta-placeholders.js";
import { textareaAutocomplete } from "../../js/mzta-placeholders-autocomplete.js";
import { addTags_getExclusionList, addTags_setExclusionList } from "../../js/mzta-addatags-exclusion-list.js";


let autocompleteSuggestions = [];
let taLog = new taLogger("mzta-addtags-page",true);
let taLog = new taLogger("mzta-get-calendar-event-page",true);

document.addEventListener('DOMContentLoaded', async () => {

Expand All @@ -36,102 +34,50 @@ document.addEventListener('DOMContentLoaded', async () => {
element.addEventListener("change", saveOptions);
});

let addtags_textarea = document.getElementById('addtags_prompt_text');
let addtags_save_btn = document.getElementById('btn_save_prompt');
let addtags_reset_btn = document.getElementById('btn_reset_prompt');
let get_calendar_event_textarea = document.getElementById('get_calendar_event_prompt_text');
let get_calendar_event_save_btn = document.getElementById('btn_save_prompt');
let get_calendar_event_reset_btn = document.getElementById('btn_reset_prompt');

let specialPrompts = await getSpecialPrompts();
let addtags_prompt = specialPrompts.find(prompt => prompt.id === 'prompt_add_tags');
let get_calendar_event_prompt = specialPrompts.find(prompt => prompt.id === 'prompt_get_calendar_event');

addtags_textarea.addEventListener('input', (event) => {
addtags_reset_btn.disabled = (event.target.value === browser.i18n.getMessage('prompt_add_tags_full_text'));
addtags_save_btn.disabled = (event.target.value === addtags_prompt.text);
if(addtags_save_btn.disabled){
document.getElementById('addtags_prompt_unsaved').classList.add('hidden');
get_calendar_event_textarea.addEventListener('input', (event) => {
get_calendar_event_reset_btn.disabled = (event.target.value === browser.i18n.getMessage('prompt_get_calendar_event_full_text'));
get_calendar_event_save_btn.disabled = (event.target.value === get_calendar_event_prompt.text);
if(get_calendar_event_save_btn.disabled){
document.getElementById('get_calendar_event_prompt_unsaved').classList.add('hidden');
} else {
document.getElementById('addtags_prompt_unsaved').classList.remove('hidden');
document.getElementById('get_calendar_event_prompt_unsaved').classList.remove('hidden');
}
});

addtags_reset_btn.addEventListener('click', () => {
addtags_textarea.value = browser.i18n.getMessage('prompt_add_tags_full_text');
addtags_reset_btn.disabled = true;
get_calendar_event_reset_btn.addEventListener('click', () => {
get_calendar_event_textarea.value = browser.i18n.getMessage('prompt_get_calendar_event_full_text');
get_calendar_event_reset_btn.disabled = true;
let event = new Event('input', { bubbles: true, cancelable: true });
addtags_textarea.dispatchEvent(event);
get_calendar_event_textarea.dispatchEvent(event);
});

addtags_save_btn.addEventListener('click', () => {
specialPrompts.find(prompt => prompt.id === 'prompt_add_tags').text = addtags_textarea.value;
get_calendar_event_save_btn.addEventListener('click', () => {
specialPrompts.find(prompt => prompt.id === 'prompt_get_calendar_event').text = get_calendar_event_textarea.value;
setSpecialPrompts(specialPrompts);
addtags_save_btn.disabled = true;
document.getElementById('addtags_prompt_unsaved').classList.add('hidden');
get_calendar_event_save_btn.disabled = true;
document.getElementById('get_calendar_event_prompt_unsaved').classList.add('hidden');
browser.runtime.sendMessage({command: "reload_menus"});
});

if(addtags_prompt.text === 'prompt_add_tags_full_text'){
addtags_prompt.text = browser.i18n.getMessage(addtags_prompt.text);
if(get_calendar_event_prompt.text === 'prompt_get_calendar_event_full_text'){
get_calendar_event_prompt.text = browser.i18n.getMessage(get_calendar_event_prompt.text);
}
addtags_textarea.value = addtags_prompt.text;
addtags_reset_btn.disabled = (addtags_textarea.value === browser.i18n.getMessage('prompt_add_tags_full_text'));

document.getElementById('add_tags_maxnum').addEventListener('change', updateAdditionalPromptStatements);
document.getElementById('add_tags_force_lang').addEventListener('change', updateAdditionalPromptStatements);

updateAdditionalPromptStatements();
get_calendar_event_textarea.value = get_calendar_event_prompt.text;
get_calendar_event_reset_btn.disabled = (get_calendar_event_textarea.value === browser.i18n.getMessage('prompt_get_calendar_event_full_text'));

autocompleteSuggestions = (await getPlaceholders(true)).filter(p => !(p.id === 'additional_text')).map(p => ({command: '{%'+p.id+'%}', type: p.type}));
textareaAutocomplete(addtags_textarea, autocompleteSuggestions, 1); // type_value = 1, only when reading an email

let excl_list_textarea = document.getElementById('addtags_excl_list');
let excl_list_save_btn = document.getElementById('btn_save_excl_list');

let excl_list_value = await addTags_getExclusionList();
let excl_list_string = excl_list_value.join('\n');

excl_list_textarea.value = excl_list_string;

excl_list_textarea.addEventListener('input', (event) => {
excl_list_save_btn.disabled = (event.target.value === excl_list_string);
if(excl_list_save_btn.disabled){
document.getElementById('excl_list_unsaved').classList.add('hidden');
} else {
document.getElementById('excl_list_unsaved').classList.remove('hidden');
}
});

excl_list_save_btn.addEventListener('click', () => {
let excl_array_new = excl_list_textarea.value.split(/[\n,]+/);
excl_array_new = Array.from(new Set(excl_array_new.map(item => item.trim().toLowerCase()))).sort();
addTags_setExclusionList(excl_array_new);
excl_list_save_btn.disabled = true;
excl_list_textarea.value = excl_array_new.join('\n');
document.getElementById('excl_list_unsaved').classList.add('hidden');
});
textareaAutocomplete(get_calendar_event_textarea, autocompleteSuggestions, 1); // type_value = 1, only when reading an email

});


async function updateAdditionalPromptStatements(){
let prefs_ = await browser.storage.sync.get({add_tags_maxnum: 3, add_tags_force_lang: true, default_chatgpt_lang: ''});
let el_tag_limit = document.getElementById('addtags_info_additional_statements');
if((prefs_.add_tags_maxnum > 0)||(prefs_.add_tags_force_lang && prefs_.default_chatgpt_lang !== '')){
el_tag_limit.textContent = browser.i18n.getMessage("addtags_info_additional_statements") + " \""
if(prefs_.add_tags_maxnum > 0){
el_tag_limit.textContent += browser.i18n.getMessage("prompt_add_tags_maxnum") + " " + prefs_.add_tags_maxnum +"."
}
if(prefs_.add_tags_force_lang && prefs_.default_chatgpt_lang !== ''){
if(prefs_.add_tags_maxnum > 0){
el_tag_limit.textContent += " "
}
el_tag_limit.textContent += browser.i18n.getMessage("prompt_add_tags_force_lang") + " " + prefs_.default_chatgpt_lang + "."
}
el_tag_limit.textContent += "\".";
el_tag_limit.style.display = 'block';
}else{
el_tag_limit.style.display = 'none';
}
}


// Methods to manage options, derived from: /options/mzta-options.js

Expand Down

0 comments on commit d264e9e

Please sign in to comment.