Skip to content

Commit

Permalink
Fix submenu registration by using ZoteroToolkit (#304)
Browse files Browse the repository at this point in the history
Fixes #302
  • Loading branch information
thebluepotato authored Oct 3, 2024
1 parent 7b23e68 commit 1c83e04
Showing 1 changed file with 35 additions and 59 deletions.
94 changes: 35 additions & 59 deletions src/cita/zoteroOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Root, createRoot } from "react-dom/client";

import { initLocale, getLocaleID } from "../utils/locale";
import { getPrefGlobalName } from "../utils/prefs";
import { MenuitemOptions } from "zotero-plugin-toolkit/dist/managers/menu";

const TRANSLATORS_PATH = `chrome://${config.addonRef}/content/translators`;
const TRANSLATOR_LABELS = [
Expand Down Expand Up @@ -1084,47 +1085,24 @@ class ZoteroOverlay {
// /******************************************/
// // Create XUL for Zotero menu elements
zoteroPopup(menuName: MenuSelectionType, doc: Document) {
const ns =
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
const zoteroMenu = doc.getElementById(`zotero-${menuName}menu`);
if (zoteroMenu === null) {
// Don't do anything if elements not loaded yet
return;
}

const wikiciteSeparator = doc.createElementNS(ns, "menuseparator");
const wikiciteSeparatorID = `wikicite-${menuName}submenu-separator`;
wikiciteSeparator.setAttribute("id", wikiciteSeparatorID);
zoteroMenu.appendChild(wikiciteSeparator);
WikiciteChrome.registerXUL(wikiciteSeparatorID, doc);

// Wikicite submenu
const wikiciteSubmenu = doc.createElementNS(ns, "menu");
const wikiciteSubmenuID = `wikicite-${menuName}submenu`;
wikiciteSubmenu.setAttribute("id", wikiciteSubmenuID);
wikiciteSubmenu.setAttribute(
"label",
Wikicite.getString(`wikicite.submenu.label`),
);
zoteroMenu.appendChild(wikiciteSubmenu);
WikiciteChrome.registerXUL(wikiciteSubmenuID, doc);

// Wikicite submenu popup
const wikiciteSubmenuPopup = doc.createElementNS(ns, "menupopup");
wikiciteSubmenuPopup.setAttribute(
"id",
`wikicite-${menuName}submenu-popup`,
);
wikiciteSubmenu.appendChild(wikiciteSubmenuPopup);
ztoolkit.Menu.register(menuName, {
tag: "menuseparator",
id: `wikicite-${menuName}submenu-separator`,
});

this.createMenuItems(
const menuItems = this.createMenuItems(
menuName,
wikiciteSubmenuPopup,
`wikicite-${menuName}submenu-`,
false,
doc,
);

ztoolkit.Menu.register(menuName, {
tag: "menu",
id: `wikicite-${menuName}submenu`,
label: Wikicite.getString(`wikicite.submenu.label`),
children: menuItems,
});

this.refreshZoteroPopup(menuName, doc);
}

Expand Down Expand Up @@ -1176,11 +1154,8 @@ class ZoteroOverlay {
// Create Zotero item menu items as children of menuPopup
createMenuItems(
menuName: MenuSelectionType,
menuPopup: Element,
IDPrefix: string,
elementsAreRoot: boolean,
doc: Document,
) {
): MenuitemOptions[] {
const menuFunctions: Map<
MenuFunction,
(menuName: MenuSelectionType) => void
Expand All @@ -1193,6 +1168,8 @@ class ZoteroOverlay {
["addAsCitations", () => this.addAsCitations(menuName)],
["localCitationNetwork", () => this.localCitationNetwork(menuName)],
]);

const options: MenuitemOptions[] = [];
for (const [functionName, func] of menuFunctions) {
if (
menuName === "collection" &&
Expand All @@ -1208,13 +1185,10 @@ class ZoteroOverlay {
functionName,
func,
IDPrefix,
doc,
);
menuPopup.appendChild(menuFunc);
if (elementsAreRoot) {
WikiciteChrome.registerXUL(menuFunc.id, doc);
}
options.push(menuFunc);
}
return options;
}

// Create Zotero item menu item
Expand All @@ -1223,25 +1197,27 @@ class ZoteroOverlay {
functionName: MenuFunction,
func: (menuName: MenuSelectionType) => void,
IDPrefix: string,
doc: Document,
) {
const ns =
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
const menuFunc = doc.createElementNS(ns, "menuitem");
menuFunc.setAttribute("id", IDPrefix + functionName);
menuFunc.setAttribute(
"label",
Wikicite.getString(`wikicite.submenu.${functionName}`),
);
menuFunc.addEventListener(
"command",
(event) => {
let label: string;
Zotero.log(`Building menu for ${functionName}`);
if (functionName.includes("getFromIndexer.")) {
const indexerName = functionName.split(".")[1];
label = Wikicite.formatString(
"wikicite.submenu.get-from-indexer",
indexerName,
);
Zotero.log(label);
} else label = Wikicite.getString(`wikicite.submenu.${functionName}`);
const menuOptions: MenuitemOptions = {
tag: "menuitem",
id: IDPrefix + functionName,
label: label,
commandListener: (event) => {
event.stopPropagation();
func(menuName);
},
false,
);
return menuFunc;
};
return menuOptions;
}
}

Expand Down

0 comments on commit 1c83e04

Please sign in to comment.