correctly handling the calendar menu. see #182
This commit is contained in:
parent
bbe6a227ba
commit
dd00856ef6
5 changed files with 164 additions and 48 deletions
|
|
@ -51,7 +51,7 @@ export class mzta_Menus {
|
|||
}
|
||||
|
||||
|
||||
async initialize(also_special = false) {
|
||||
async initialize(also_special = []) { // also_special is an array of active special prompts ids
|
||||
this.allPrompts = [];
|
||||
this.rootMenu = [];
|
||||
this.shortcutMenu = [];
|
||||
|
|
@ -63,7 +63,7 @@ export class mzta_Menus {
|
|||
});
|
||||
}
|
||||
|
||||
async reload(also_special = false) {
|
||||
async reload(also_special = []) {
|
||||
await browser.menus.removeAll().catch(error => {
|
||||
console.error("[ThunderAI] ERROR removing the menus: ", error);
|
||||
});
|
||||
|
|
@ -212,7 +212,7 @@ export class mzta_Menus {
|
|||
//browser.runtime.sendMessage({command: "chatgpt_open", prompt: fullPrompt, action: curr_prompt.action, tabId: tabs[0].id});
|
||||
if(curr_prompt.is_special == '1'){ // Special prompts
|
||||
switch(curr_prompt.id){
|
||||
case 'prompt_add_tags': // Add tags to the email
|
||||
case 'prompt_add_tags': { // Add tags to the email
|
||||
let tags_current_email = '';
|
||||
let prefs_at = await browser.storage.sync.get({add_tags_maxnum: 3, connection_type: '', add_tags_force_lang: true, default_chatgpt_lang: ''});
|
||||
if((prefs_at.connection_type === '')||(prefs_at.connection_type === null)||(prefs_at.connection_type === undefined)||(prefs_at.connection_type === 'chatgpt_web')){
|
||||
|
|
@ -241,10 +241,21 @@ export class mzta_Menus {
|
|||
return {ok:'0'};
|
||||
}
|
||||
this.logger.log("tags_current_email: " + tags_current_email);
|
||||
console.log(">>>>>>>>>>>> tags_full_list: " + JSON.stringify(tags_full_list));
|
||||
// console.log(">>>>>>>>>>>> tags_full_list: " + JSON.stringify(tags_full_list));
|
||||
browser.tabs.sendMessage(tabs[0].id, {command: "getTags", tags: tags_current_email, messageId: curr_message.id});
|
||||
return {ok:'1'};
|
||||
break;
|
||||
break; // Add tags to the email - END
|
||||
}
|
||||
case 'get_calendar_event': { // Get a calendar event info
|
||||
let prefs_at = await browser.storage.sync.get({connection_type: ''});
|
||||
if((prefs_at.connection_type === '')||(prefs_at.connection_type === null)||(prefs_at.connection_type === undefined)||(prefs_at.connection_type === 'chatgpt_web')){
|
||||
console.error("[ThunderAI | GetCalendarEvent] Invalid connection type: " + prefs_at.connection_type);
|
||||
return {ok:'0'};
|
||||
}
|
||||
//TODO
|
||||
return {ok:'1'};
|
||||
break; // Get a calendar event info - END
|
||||
}
|
||||
default:
|
||||
console.error("[ThunderAI] Unknown special prompt id: " + curr_prompt.id);
|
||||
break;
|
||||
|
|
@ -278,7 +289,7 @@ export class mzta_Menus {
|
|||
this.shortcutMenu.push(curr_menu_entry);
|
||||
}
|
||||
|
||||
async loadMenus(also_special = false) {
|
||||
async loadMenus(also_special = []) {
|
||||
await this.initialize(also_special);
|
||||
await this.addMenu(this.rootMenu);
|
||||
this.addClickListener();
|
||||
|
|
|
|||
|
|
@ -198,15 +198,28 @@ const specialPrompts = [
|
|||
];
|
||||
|
||||
|
||||
export async function getPrompts(onlyEnabled = false, includeSpecial = false){
|
||||
export async function getPrompts(onlyEnabled = false, includeSpecial = []){ // includeSpecial is an array of active special prompts ids
|
||||
const _defaultPrompts = await getDefaultPrompts_withProps();
|
||||
// console.log('>>>>>>>>>>>> getPrompts _defaultPrompts: ' + JSON.stringify(_defaultPrompts));
|
||||
const customPrompts = await getCustomPrompts();
|
||||
// console.log('>>>>>>>>>>>> getPrompts customPrompts: ' + JSON.stringify(customPrompts));
|
||||
const specialPrompts = await getSpecialPrompts();
|
||||
let output = specialPrompts.concat(_defaultPrompts).concat(customPrompts);
|
||||
if(!includeSpecial){
|
||||
if(includeSpecial.length == 0){
|
||||
output = output.filter(obj => obj.is_special != 1); // we do not want special prompts
|
||||
}else{
|
||||
// console.log(">>>>>>>>>> getPrompts includeSpecial: " + JSON.stringify(includeSpecial));
|
||||
output = output.filter(obj => includeSpecial.includes(obj.id) || obj.is_special != 1);
|
||||
// output = output.filter(obj => {
|
||||
// const isIncluded = includeSpecial.includes(obj.id);
|
||||
// const isNotSpecial = obj.is_special != 1;
|
||||
|
||||
// console.log(`>>>>>>>>>> Checking obj:`, obj);
|
||||
// console.log(`>>>>>>>>>> isIncluded: ${isIncluded}`);
|
||||
// console.log(`>>>>>>>>>> isNotSpecial: ${isNotSpecial}`);
|
||||
|
||||
// return isIncluded || isNotSpecial;
|
||||
// });
|
||||
}
|
||||
if(onlyEnabled){
|
||||
output = output.filter(obj => obj.enabled != 0);
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ import { mzta_script } from './js/mzta-chatgpt.js';
|
|||
import { prefs_default } from './options/mzta-options-default.js';
|
||||
import { mzta_Menus } from './js/mzta-menus.js';
|
||||
import { taLogger } from './js/mzta-logger.js';
|
||||
import { getCurrentIdentity, getOriginalBody, replaceBody, setBody, i18nConditionalGet, generateCallID, migrateCustomPromptsStorage, migrateDefaultPromptsPropStorage, getGPTWebModelString, getTagsList, createTag, assignTagsToMessage, checkIfTagExists } from './js/mzta-utils.js';
|
||||
import { getCurrentIdentity, getOriginalBody, replaceBody, setBody, i18nConditionalGet, generateCallID, migrateCustomPromptsStorage, migrateDefaultPromptsPropStorage, getGPTWebModelString, getTagsList, createTag, assignTagsToMessage, checkIfTagExists, getActiveSpecialPromptsIDs, checkSparksPresence } from './js/mzta-utils.js';
|
||||
|
||||
await migrateCustomPromptsStorage();
|
||||
await migrateDefaultPromptsPropStorage();
|
||||
|
|
@ -28,9 +28,11 @@ await migrateDefaultPromptsPropStorage();
|
|||
var original_html = '';
|
||||
var modified_html = '';
|
||||
|
||||
let prefs_init = await browser.storage.sync.get({do_debug: false, add_tags: true, connection_type: 'chatgpt_web'});
|
||||
let prefs_init = await browser.storage.sync.get({do_debug: false, add_tags: true, get_calendar_event: true, connection_type: 'chatgpt_web'});
|
||||
let taLog = new taLogger("mzta-background",prefs_init.do_debug);
|
||||
|
||||
let special_prompts_ids = getActiveSpecialPromptsIDs(prefs_init.add_tags, await doGetCalendarEvent(prefs_init.get_calendar_event), (prefs_init.connection_type === "chatgpt_web"));
|
||||
|
||||
browser.composeScripts.register({
|
||||
js: [{file: "/js/mzta-compose-script.js"}]
|
||||
});
|
||||
|
|
@ -211,8 +213,8 @@ messenger.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
|||
break;
|
||||
case 'reload_menus':
|
||||
async function _reload_menus() {
|
||||
let prefs_reload = await browser.storage.sync.get({add_tags: true, connection_type: 'chatgpt_web'});
|
||||
menus.reload(prefs_reload.add_tags && (prefs_reload.connection_type !== "chatgpt_web"));
|
||||
let prefs_reload = await browser.storage.sync.get({add_tags: true, get_calendar_event: false, connection_type: 'chatgpt_web'});
|
||||
menus.reload((prefs_reload.add_tags||prefs_reload.get_calendar_event) && (prefs_reload.connection_type !== "chatgpt_web"));
|
||||
taLog.log("Reloading menus");
|
||||
return true;
|
||||
}
|
||||
|
|
@ -573,23 +575,59 @@ function checkScreenDimensions(prefs){
|
|||
return prefs;
|
||||
}
|
||||
|
||||
// Register the listener for storage changes
|
||||
browser.storage.onChanged.addListener((changes, areaName) => {
|
||||
// Check if the change happened in the 'sync' storage area
|
||||
if (areaName === 'sync') {
|
||||
// Check if 'add_tags' has changed
|
||||
//console.log(">>>>>>>>>>>>> changes: " + JSON.stringify(changes));
|
||||
if (changes.add_tags) {
|
||||
menus.reload(changes.add_tags.newValue && (prefs_init.connection_type !== "chatgpt_web"));
|
||||
}
|
||||
|
||||
// Check if 'connection_type' has changed
|
||||
if (changes.connection_type) {
|
||||
menus.reload(prefs_init.add_tags && (changes.connection_type.newValue !== "chatgpt_web"));
|
||||
}
|
||||
async function doGetCalendarEvent(get_calendar_event) {
|
||||
if(get_calendar_event) {
|
||||
return await checkSparksPresence();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function reload_pref_init(){
|
||||
prefs_init = await await browser.storage.sync.get({do_debug: false, add_tags: true, get_calendar_event: true, connection_type: 'chatgpt_web'});
|
||||
}
|
||||
|
||||
|
||||
// Register the listener for storage changes
|
||||
function setupStorageChangeListener() {
|
||||
browser.storage.onChanged.addListener((changes, areaName) => {
|
||||
// Check if the change happened in the 'sync' storage area
|
||||
if (areaName === 'sync') {
|
||||
// Process 'add_tags' changes
|
||||
if (changes.add_tags) {
|
||||
const newTags = changes.add_tags.newValue;
|
||||
doGetCalendarEvent(prefs_init.get_calendar_event).then(calendarEvent => {
|
||||
const special_prompts_ids = getActiveSpecialPromptsIDs(newTags, calendarEvent, (prefs_init.connection_type === "chatgpt_web"));
|
||||
menus.reload(special_prompts_ids);
|
||||
});
|
||||
}
|
||||
|
||||
// Process 'get_calendar_event' changes
|
||||
if (changes.get_calendar_event) {
|
||||
const newCalendarEvent = changes.get_calendar_event.newValue;
|
||||
doGetCalendarEvent(newCalendarEvent).then(calendarEvent => {
|
||||
const special_prompts_ids = getActiveSpecialPromptsIDs(prefs_init.add_tags, calendarEvent, (prefs_init.connection_type === "chatgpt_web"));
|
||||
menus.reload(special_prompts_ids);
|
||||
});
|
||||
}
|
||||
|
||||
// Process 'connection_type' changes
|
||||
if (changes.connection_type) {
|
||||
const newConnectionType = changes.connection_type.newValue;
|
||||
doGetCalendarEvent(prefs_init.get_calendar_event).then(calendarEvent => {
|
||||
const special_prompts_ids = getActiveSpecialPromptsIDs(prefs_init.add_tags, calendarEvent, (newConnectionType !== "chatgpt_web"));
|
||||
menus.reload(special_prompts_ids);
|
||||
});
|
||||
}
|
||||
reload_pref_init();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Call the function to set up the listener
|
||||
setupStorageChangeListener();
|
||||
|
||||
|
||||
// Menus handling
|
||||
const menus = new mzta_Menus(openChatGPT, prefs_init.do_debug);
|
||||
menus.loadMenus(prefs_init.add_tags && (prefs_init.connection_type !== "chatgpt_web"));
|
||||
menus.loadMenus(special_prompts_ids);
|
||||
|
|
@ -64,7 +64,7 @@ body {
|
|||
background-color: #e9e9e9;
|
||||
}
|
||||
|
||||
.prompt_add_tags, .prompt_get_calendar_event{
|
||||
.special_prompt{
|
||||
background-color: #cfe9ff;
|
||||
}
|
||||
|
||||
|
|
@ -102,7 +102,7 @@ body {
|
|||
background-color: #444444;
|
||||
}
|
||||
|
||||
.prompt_add_tags, .prompt_get_calendar_event{
|
||||
.special_prompt{
|
||||
background-color: #353542;
|
||||
}
|
||||
}
|
||||
|
|
@ -17,15 +17,18 @@
|
|||
*/
|
||||
|
||||
import { taLogger } from "../js/mzta-logger.js";
|
||||
import { checkSparksPresence } from "../js/mzta-utils.js";
|
||||
|
||||
let menuSendImmediately = false;
|
||||
let taLog = console;
|
||||
let connection_type = 'chatgpt_web';
|
||||
let add_tags = false;
|
||||
let get_calendar_event = false;
|
||||
let tabType;
|
||||
let num_special_menu_items = 0;
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
let prefs = await browser.storage.sync.get({do_debug: false, dynamic_menu_force_enter: false, add_tags: false, connection_type: 'chatgpt_web'});
|
||||
let prefs = await browser.storage.sync.get({do_debug: false, dynamic_menu_force_enter: false, add_tags: true, get_calendar_event: true, connection_type: 'chatgpt_web'});
|
||||
taLog = new taLogger("mzta-popup",prefs.do_debug);
|
||||
i18n.updateDocument();
|
||||
let reponse = await browser.runtime.sendMessage({command: "popup_menu_ready"});
|
||||
|
|
@ -40,6 +43,7 @@ document.addEventListener('DOMContentLoaded', async () => {
|
|||
menuSendImmediately = prefs.dynamic_menu_force_enter;
|
||||
connection_type = prefs.connection_type;
|
||||
add_tags = prefs.add_tags;
|
||||
get_calendar_event = prefs.get_calendar_event;
|
||||
searchPrompt(active_prompts, tabId, tabType);
|
||||
i18n.updateDocument();
|
||||
}, { once: true });
|
||||
|
|
@ -95,18 +99,54 @@ async function searchPrompt(allPrompts, tabId, tabType){
|
|||
|
||||
|
||||
// Prepend numbers to the first 10 items
|
||||
// If add_tags is true and connection_type is not 'chatgpt_web' reserve 0 position for prompt_add_tags
|
||||
// If add_tags is true and connection_type is not 'chatgpt_web' reserve 0 position for prompt_add_tags and 1 for prompt_get_calendar_event (0, if no prompt_add_tags is disabled)
|
||||
let max_num_el = 10
|
||||
let first_num_el = 0;
|
||||
if(checkDoAddTags()){
|
||||
max_num_el = 9;
|
||||
first_num_el = 1;
|
||||
filteredData = ensurePromptAddTagsFirst(filteredData);
|
||||
if (!filteredData[0].numberPrepended) {
|
||||
filteredData[0].numberPrepended = 'true';
|
||||
filteredData[0].label = '0. ' + filteredData[0].label;
|
||||
|
||||
let do_add_tags = checkDoAddTags();
|
||||
let do_get_calendar_event = checkDoCalendarEvent();
|
||||
|
||||
// console.log(">>>>>>>>>>> do_add_tags: " + do_add_tags);
|
||||
// console.log(">>>>>>>>>>> do_get_calendar_event: " + do_get_calendar_event);
|
||||
// console.log(">>>>>>>>>>> filteredData: " + JSON.stringify(filteredData));
|
||||
|
||||
if(do_add_tags){
|
||||
num_special_menu_items++;
|
||||
}
|
||||
if(do_get_calendar_event){
|
||||
num_special_menu_items++;
|
||||
}
|
||||
// console.log(">>>>>>>>>>>> num_special_menu_items: " + num_special_menu_items);
|
||||
if(num_special_menu_items > 0){
|
||||
max_num_el -= num_special_menu_items;
|
||||
first_num_el = num_special_menu_items;
|
||||
// console.log(">>>>>>>>>>>>> max_num_el: " + max_num_el);
|
||||
// console.log(">>>>>>>>>>>>> first_num_el: " + first_num_el);
|
||||
if(do_add_tags){
|
||||
filteredData = ensurePromptAddTagsFirst(filteredData);
|
||||
if (!filteredData[0].numberPrepended) {
|
||||
filteredData[0].numberPrepended = 'true';
|
||||
filteredData[0].label = '0. ' + filteredData[0].label;
|
||||
}
|
||||
}
|
||||
if(do_get_calendar_event){
|
||||
filteredData = ensurePromptGetCalendarEventFirst(filteredData, do_add_tags);
|
||||
let gce_curr_pos = do_add_tags ? 1 : 0;
|
||||
if (!filteredData[gce_curr_pos].numberPrepended) {
|
||||
filteredData[gce_curr_pos].numberPrepended = 'true';
|
||||
filteredData[gce_curr_pos].label = gce_curr_pos + '. ' + filteredData[gce_curr_pos].label;
|
||||
}
|
||||
}
|
||||
}
|
||||
// if(checkDoAddTags()){
|
||||
// max_num_el = 9;
|
||||
// first_num_el = 1;
|
||||
// filteredData = ensurePromptAddTagsFirst(filteredData);
|
||||
// if (!filteredData[0].numberPrepended) {
|
||||
// filteredData[0].numberPrepended = 'true';
|
||||
// filteredData[0].label = '0. ' + filteredData[0].label;
|
||||
// }
|
||||
// }
|
||||
Array.from(filteredData).slice(first_num_el, max_num_el).forEach((item, index) => {
|
||||
let number = (index < 9) ? (index + 1).toString() : '0';
|
||||
// Check if the number is already prepended to avoid duplication
|
||||
|
|
@ -118,21 +158,14 @@ async function searchPrompt(allPrompts, tabId, tabType){
|
|||
|
||||
// console.log(">>>>>>>>>>>>> filteredData: " + JSON.stringify(filteredData));
|
||||
|
||||
// add the prompt_add_tags if add_tags is true and connection_type is not 'chatgpt_web'
|
||||
// if(checkDoAddTags()){
|
||||
// let number = '0';
|
||||
// let item = {label: `${number}. ${browser.i18n.getMessage('prompt_add_tags')}`, id: 'prompt_add_tags', numberPrepended: 'true'};
|
||||
// filteredData.unshift(item);
|
||||
// }
|
||||
|
||||
// Create a div for each filtered result
|
||||
filteredData.forEach(item => {
|
||||
const itemDiv = document.createElement('div');
|
||||
itemDiv.classList.add('mzta_autocomplete-item');
|
||||
itemDiv.textContent = item.label;
|
||||
itemDiv.setAttribute('data-id', item.id);
|
||||
if(item.id === 'prompt_add_tags'){
|
||||
itemDiv.className += ' prompt_add_tags';
|
||||
if((item.id === 'prompt_add_tags')||(item.id === 'prompt_get_calendar_event')){
|
||||
itemDiv.className += ' special_prompt';
|
||||
}
|
||||
|
||||
// Add a mousedown event to select the item
|
||||
|
|
@ -293,6 +326,10 @@ function checkDoAddTags(){
|
|||
return add_tags && (connection_type !== "chatgpt_web" && tabType !== 'messageCompose');
|
||||
}
|
||||
|
||||
function checkDoCalendarEvent(){
|
||||
return get_calendar_event && (connection_type !== "chatgpt_web" && tabType !== 'messageCompose') && checkSparksPresence();
|
||||
}
|
||||
|
||||
function ensurePromptAddTagsFirst(arr) {
|
||||
// Find the index of the object with id "prompt_add_tags"
|
||||
const index = arr.findIndex(item => item.id === "prompt_add_tags");
|
||||
|
|
@ -307,3 +344,20 @@ function ensurePromptAddTagsFirst(arr) {
|
|||
|
||||
return arr;
|
||||
}
|
||||
|
||||
function ensurePromptGetCalendarEventFirst(arr, do_add_tags) {
|
||||
// Find the index of the object with id "prompt_get_calendar_event"
|
||||
const index = arr.findIndex(item => item.id === "prompt_get_calendar_event");
|
||||
|
||||
// If found and needs repositioning
|
||||
if (index !== -1 && (do_add_tags ? index !== 1 : index !== 0)) {
|
||||
// Remove it from its current position
|
||||
const [promptAddTags] = arr.splice(index, 1);
|
||||
|
||||
// Add it to the specified position
|
||||
const targetPosition = do_add_tags ? 1 : 0;
|
||||
arr.splice(targetPosition, 0, promptAddTags);
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
Loading…
Reference in a new issue