Skip to content

Commit

Permalink
issue #253 - removed legacy code
Browse files Browse the repository at this point in the history
- added support for refreshing from settings
- update the "Last item" and MRU list to be always current
- improved css selector for hidden label
  • Loading branch information
RealRaven2000 committed Mar 20, 2024
1 parent e01876d commit ff8a653
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 128 deletions.
65 changes: 24 additions & 41 deletions chrome/content/scripts/st-messagePane.js
@@ -1,11 +1,11 @@
var { MailServices } = ChromeUtils.import("resource:///modules/MailServices.jsm");

let patchHeaderMenu; // listener

/* obsolete
let patchHeaderMenu;
var reactNotification;
*/

async function onLoad(activatedWhileWindowOpen) {

// copy namespace (we are in 3pane or about:message)
switch (window.parent.document.URL) {
case "about:3pane":
Expand All @@ -18,7 +18,7 @@ async function onLoad(activatedWhileWindowOpen) {
if (window.document.URL == "about:message") {
window.SmartTemplate4 = window.parent.SmartTemplate4;
}
}
}

window.SmartTemplate4_WLM = WL; // keep a reference to the correct WindowListener. [issue 271]
// it can only patch stuff in its own window!
Expand All @@ -27,10 +27,27 @@ async function onLoad(activatedWhileWindowOpen) {

WL.injectCSS("chrome://smartTemplate4/content/skin/common/smartTemplate-toolButton.css");

const HEADERBARID = "smarttemplate4_thunderbird_extension-messageDisplayAction-toolbarbutton";
const contentDoc = window.document;
let headerButton = contentDoc.getElementById(HEADERBARID);
const HEADERSELECTOR = '[data-extensionid="smarttemplate4@thunderbird.extension"].message-header-view-button';
// [data-extensionid="smarttemplate4@thunderbird.extension"].message-header-view-button
let headerButton = contentDoc.querySelector(HEADERSELECTOR); // getElementById(HEADERBARID);
if (!headerButton) return;
if (window.SmartTemplate4.Preferences.getMyBoolPref("toolbar.hideLabel")) {
headerButton.classList.add("force-label-hidden");
} else {
headerButton.classList.remove("force-label-hidden");
}

// early exit
if (window.SmartTemplate4?.fileTemplates.isAPIpatched) {
return;
}

/* obsolete
// ==================================================================
// old code...
const HEADERBARID = "smarttemplate4_thunderbird_extension-messageDisplayAction-toolbarbutton";
headerButton = contentDoc.getElementById(HEADERBARID);
if (headerButton) { // patch button
window.setTimeout(
(win = window) => {
Expand Down Expand Up @@ -79,46 +96,12 @@ async function onLoad(activatedWhileWindowOpen) {
patchHeaderMenu = async (win = window) => {
win.SmartTemplate4_WLM = WL;
if (win.SmartTemplate4.patchHeaderPane.bind(win.SmartTemplate4)) {
// to do - recreate through API [issue 253]
await win.SmartTemplate4.fileTemplates.initMenus(true, {toolbarType:"messageheader"});
}
}
window.SmartTemplate4.Util.notifyTools.registerListener(reactNotification);

// messenger.messageDisplayAction.setTitle("SmartTemplates")
/*
https://webextension-api.thunderbird.net/en/latest/messageDisplayAction.html#seticon-details
messenger.messageDisplayAction.setIcon(
{
imageData:ImageDataType,
path: IconPath
}
)
John:
The menus are populated via the menus API. The context you need is message_display_action_menu
For the first level you use the mentioned context in menus.create().
You also define an id for each entry. For the sublevels you use the parentId
property to define the menu entries to be children of the previously defined
top level entries. You probably do not need to specify a context for
sublevel entries.
You can hide/show entries in the onShown event, if you need dynamic menu entries,
depending on the current message or UI state.
Or actually update the entries.
For patching, work in main: patchHeaderPane
// access all browsers in 3pane:
window[3].document.childNodes[1].querySelectorAll("browser")
// access messageHeader
window[3].document.childNodes[1].querySelectorAll("browser")[1].contentDocument.querySelector("#messageHeader")
*/

}

function onUnload(isAddOnShutDown) {
Expand Down
20 changes: 17 additions & 3 deletions chrome/content/scripts/st-messenger.js
Expand Up @@ -118,9 +118,6 @@ async function onLoad(activatedWhileWindowOpen) {
case "smartTemplates-templatemenus":
SmartTemplates.Util.notifyTools.notifyBackground({ func: "updateTemplateMenus" });
break;
case "smartTemplates-patchHeaderTools":
SmartTemplates.Util.notifyTools.notifyBackground({ func: "patchHeaderMenu" });
break;
case "smartTemplates-headerMenuAPI":
// to test, first remove the old xul menu items:
if (!SmartTemplates.fileTemplates.isAPIpatched) {
Expand Down Expand Up @@ -198,17 +195,34 @@ async function onLoad(activatedWhileWindowOpen) {

window.SmartTemplate4.WL = WL; // we need this in patchUnifiedToolbar();
window.SmartTemplate4.Util.notifyTools.enable();

util.logDebug("Util.init...");
await window.SmartTemplate4.Util.init();

util.logDebug("startUp...");
window.SmartTemplate4.startUp();

// read custom templates data from disc
await window.SmartTemplate4.fileTemplates.loadCustomMenu(false);
await window.SmartTemplate4.fileTemplates.loadMRU();
await window.SmartTemplate4.Util.notifyTools.notifyBackground({
func: "updateFileTemplates",
Entries: window.SmartTemplate4.fileTemplates.Entries,
MRU_Entries: window.SmartTemplate4.fileTemplates.MRU_Entries
});

// The following will only work if we are currently in a mail pane (ATN update)
// otherwise, we need to call this again in a tab listener
if (window.SmartTemplate4.patchUnifiedToolbar()) {
await window.SmartTemplate4.fileTemplates.initMenus(true, { toolbarType:"unified", isMessenger: true });
}

// [issue 253] update message action menu (API based) - default to this method from now!
await window.SmartTemplate4.Util.notifyTools.notifyBackground({
func: "patchHeaderMenuAPI"
});
window.SmartTemplate4.fileTemplates.isAPIpatched = true;

window.SmartTemplate4.addTabEventListener();

// set up updating the label at midnight
Expand Down
18 changes: 17 additions & 1 deletion chrome/content/settings.js
Expand Up @@ -588,10 +588,26 @@ SmartTemplate4.Settings = {
return true;
} ,

onUnload : function() {
onUnload : async function() {
if (SmartTemplate4.fileTemplates.isModified) {
SmartTemplate4.Util.logDebug("fileTemplates were modified - notify to update all menus...");
SmartTemplate4.Util.notifyTools.notifyBackground({ func: "updateTemplateMenus" });
try {
/* FOR SOME REASONS THIS NEVER RETURNS NOR DOES IT THROW!
await window.SmartTemplate4.Util.notifyTools.notifyBackground({
func: "updateFileTemplates",
Entries: window.SmartTemplate4.fileTemplates.Entries,
MRU_Entries: window.SmartTemplate4.fileTemplates.MRU_Entries
});
*/
await SmartTemplate4.Util.notifyTools.notifyBackground({
func: "patchHeaderMenuAPI"
});
} catch (ex) {
SmartTemplate4.Util.logException("onUnload after calling patchHeaderMenuAPI()", ex);
} finally {

}
}
window.removeEventListener("SmartTemplates.BackgroundUpdate", SmartTemplate4.Settings.validateLicenseFromEvent);
} ,
Expand Down
4 changes: 3 additions & 1 deletion chrome/content/skin/common/smartTemplate-toolButton.css
Expand Up @@ -129,7 +129,9 @@ toolbar:not([iconsize="small"]) #SmartTemplate4Button image {
}

/* use the class force-label-hidden on the button to force hiding the label, but only if icon is visible */
#messageHeader:not(.message-header-buttons-only-text) .SmartTemplates_HeaderBtn.force-label-hidden .toolbarbutton-text {
#messageHeader:not(.message-header-buttons-only-text)
[data-extensionid="smarttemplate4@thunderbird.extension"].force-label-hidden
.toolbarbutton-text {
display: none !important;
}

Expand Down
68 changes: 51 additions & 17 deletions chrome/content/smartTemplate-fileTemplates.js
Expand Up @@ -444,7 +444,7 @@ SmartTemplate4.fileTemplates = {
makeLabel: function(entry) {
let cat = entry.category || "";
// use right pointing guillemet (left-pointing double angle quotation mark) as delimiter
let retval = cat ? (cat + " » " + entry.label) : entry.label;
let retval = cat ? (cat + " » " + entry?.label) : entry?.label;
return retval;
},

Expand Down Expand Up @@ -548,6 +548,11 @@ SmartTemplate4.fileTemplates = {
util.logDebug (`Successfully saved ${fileTemplates.entriesLength} bookmarks [${byteCount} bytes] to file.`);
fileTemplates.isModified = true;
SmartTemplate4.Util.notifyTools.notifyBackground({ func: "updateTemplateMenus" });
SmartTemplate4.Util.notifyTools.notifyBackground({
func: "updateFileTemplates",
Entries: fileTemplates.Entries,
MRU_Entries: fileTemplates.MRU_Entries
});
SmartTemplate4.Util.notifyTools.notifyBackground({ func: "updateSnippetMenus" });
},
function saveReject(fileError) { // IOUtils.Error
Expand Down Expand Up @@ -809,8 +814,7 @@ SmartTemplate4.fileTemplates = {
}
msgPopup.appendChild(menuitem);
}
}
else {
} else {
acKey = getAccessKey(accelerator++);
if (acKey) {
menuitem.setAttribute("accesskey", acKey);
Expand Down Expand Up @@ -947,6 +951,7 @@ SmartTemplate4.fileTemplates = {
SmartTemplate4.fileTemplates.configureMenuMRU(msgPopup); // the last used template to the menu item!
msgPopup.setAttribute("st4configured", true);
} ,


configureMenuMRU: function(msgPopup) {
// uses the classes st-last-rsp st-last-new st-last-fwd for adding the name of the last used templates
Expand Down Expand Up @@ -1029,10 +1034,17 @@ SmartTemplate4.fileTemplates = {
);

if (isAPI) {
// this will also update the data!
await window.SmartTemplate4.Util.notifyTools.notifyBackground({
func: "smartTemplates-headerMenuAPI"
// update the data!
await SmartTemplate4.Util.notifyTools.notifyBackground({
func: "updateFileTemplates",
Entries: this.Entries,
MRU_Entries: this.MRU_Entries
});

await SmartTemplate4.Util.notifyTools.notifyBackground({
func: "patchHeaderMenuAPI"
});

// only on main toolbar item until this is converted for API!
await window.SmartTemplate4.fileTemplates.initMenus(true, {toolbarType:"unified"});
} else {
Expand Down Expand Up @@ -1159,6 +1171,16 @@ SmartTemplate4.fileTemplates = {
if (this.isAPIpatched) {
return;
}


// ==============================================================
// ==============================================================
// ==============================================================
// OBSOLETE
// ==============================================================
// ==============================================================
// ==============================================================

let doc = SmartTemplate4.Util.getMessageBrowserDocument();

if (isHackMessageHeader && doc) {
Expand Down Expand Up @@ -1294,6 +1316,7 @@ SmartTemplate4.fileTemplates = {
return;
}
this.storePreviousTemplate(composeType, entry);
this.enterMRUitem(entry);
// update api menus
// we need to remove all mru- items from the top level!
// not sure how to do it via the API
Expand Down Expand Up @@ -1350,11 +1373,13 @@ SmartTemplate4.fileTemplates = {
},

enterMRUitem(entry) {
SmartTemplate4.Util.logDebug(`enterMRUitem(${this.makeLabel(entry)})`);
let MAX_MRU_ITEMS = SmartTemplate4.Preferences.getMyIntPref("fileTemplates.mru.max") * 2; // default is 10 but can be raised in Pro
let el = SmartTemplate4.fileTemplates.MRU_Entries.find(e =>
e.path == entry.path &&
(e.cmd == entry.cmd ||
e.command == entry.command));

if (!entry) return false;

if (el) {
Expand All @@ -1378,7 +1403,7 @@ SmartTemplate4.fileTemplates = {
},

// origin: "new", "rsp", "fwd"
onItemClick: function (menuitem, menuParent, fileTemplateInstance, composeType, path, label, singleMwindow) {
onItemClick: async function (menuitem, menuParent, fileTemplateInstance, composeType, path, label, singleMwindow) {
/* START NEW CODE - [issue 184] */
// use a pref switch for testing API processing...
if (SmartTemplate4.Preferences.isBackgroundParser()) {
Expand Down Expand Up @@ -1505,20 +1530,30 @@ SmartTemplate4.fileTemplates = {
case "cmd_replylist": cmd = "replyList"; break;
}
}
let mruEntry = {
cmd: cmd,
label: entry.label,
path: entry.path,
composeType: entry.composeType,
command: entry.command
}

if (mruEntry.cmd) {
SmartTemplate4.fileTemplates.enterMRUitem(mruEntry);
if (cmd) {
SmartTemplate4.fileTemplates.enterMRUitem({
cmd: cmd,
label: entry.label,
path: entry.path,
composeType: entry.composeType,
command: entry.command
}
);
}
// notify UI to update _after_ updating the MRU list.
SmartTemplate4.Util.notifyTools.notifyBackground({ func: "updateTemplateMenus" });

// update mru in header
await SmartTemplate4.Util.notifyTools.notifyBackground({
func: "updateFileTemplates",
Entries: this.Entries,
MRU_Entries: this.MRU_Entries
});
SmartTemplate4.Util.notifyTools.notifyBackground({
func: "updateHeaderMenuMRU"
});

} ,

storePreviousTemplate: function(composeType, entry, updateMenus = false) {
Expand Down Expand Up @@ -2084,7 +2119,6 @@ SmartTemplate4.fileTemplates = {
"SmartTemplate4.fileTemplates.loadMRU()",
"Reading the smartTemplates_mru.json file failed\n" + ex);
// no changes to Entries array
// return Promise.reject("loadCustomMenu failed.");
return false;
}
}
Expand Down
5 changes: 3 additions & 2 deletions chrome/content/smartTemplate-main.js
Expand Up @@ -1064,7 +1064,7 @@ var SmartTemplate4 = {
newPopup.remove();
return true;
} else {
SmartTemplate4.Util.logDebug(`patchHeaderPane() - didn't find ${newPopupSelector}, so I couldn't patch the popup menu`);
SmartTemplate4.Util.logDebug(`moveMenuItems() - didn't find ${newPopupSelector}, so I couldn't patch the popup menu`);
console.log(toolbarButton);
}
},
Expand Down Expand Up @@ -1119,7 +1119,6 @@ var SmartTemplate4 = {
<menuitem id="smartTemplates-settings-new" label="__MSG_pref_dialog.title__ (NEW)" class="menuitem-iconic" oncommand="window.SmartTemplate4.doCommand(this);" onclick="event.stopPropagation();"/>
<menuitem id="smartTemplates-headerMenuAPI" label="Create message Actions (API)" class="menuitem-iconic" oncommand="window.SmartTemplate4.doCommand(this);" onclick="event.stopPropagation();"/>
<menuitem id="smartTemplates-MruMenuAPI" label="Update MRU (API)" class="menuitem-iconic" oncommand="window.SmartTemplate4.doCommand(this);" onclick="event.stopPropagation();"/>
<menuitem id="smartTemplates-patchHeaderTools" label="Patch Header Menu (legacy)" class="menuitem-iconic" oncommand="window.SmartTemplate4.doCommand(this);" onclick="event.stopPropagation();"/>
<menuitem id="smartTemplates-installed" label="Splashscreen - After Installation" class="menuitem-iconic" oncommand="window.SmartTemplate4.doCommand(this);" onclick="event.stopPropagation();"/>
<menuitem id="smartTemplates-templatemenus" label="Update Template Menus!" class="menuitem-iconic" oncommand="window.SmartTemplate4.doCommand(this);" onclick="event.stopPropagation();"/>
<menuitem id="smartTemplates-mru-save" label="Save MRU list" class="menuitem-iconic" oncommand="window.SmartTemplate4.doCommand(this);" onclick="event.stopPropagation();"/>
Expand Down Expand Up @@ -1238,8 +1237,10 @@ var SmartTemplate4 = {
const isMailPane = SmartTemplate4.Util.isTabMode (evt.detail.tabInfo, "mail");
if (isMailPane) {
const HEADERBARID = "smarttemplate4_thunderbird_extension-messageDisplayAction-toolbarbutton";

let result = await SmartTemplate4.Util.notifyTools.notifyBackground({func: "patchUnifiedToolbar"});
await SmartTemplate4.fileTemplates.initMenus(true, {toolbarType:"unified"});

if (SmartTemplate4.fileTemplates.isAPIpatched) {
return; // header Patch obsolete for API method
}
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Expand Up @@ -3,7 +3,7 @@
"manifest_version": 2,
"name": "SmartTemplates",
"description": "__MSG_extensionDescription__",
"version": "4.4pre136",
"version": "4.4pre143",
"default_locale": "en",
"developer": {
"name": "Axel Grude (author)",
Expand Down
2 changes: 1 addition & 1 deletion revision.txt
@@ -1 +1 @@
136
143

0 comments on commit ff8a653

Please sign in to comment.