remove: global variables

This commit is contained in:
windingwind 2024-09-24 12:39:55 +02:00
parent 07ab7f0902
commit 00b88f394b
4 changed files with 23 additions and 32 deletions

View File

@ -33,7 +33,9 @@ async function onStartup() {
UIExampleFactory.registerReaderItemPaneSection();
await onMainWindowLoad(window);
await Promise.all(
Zotero.getMainWindows().map((win) => onMainWindowLoad(win)),
);
}
async function onMainWindowLoad(win: Window): Promise<void> {
@ -41,7 +43,7 @@ async function onMainWindowLoad(win: Window): Promise<void> {
addon.data.ztoolkit = createZToolkit();
// @ts-ignore This is a moz feature
window.MozXULElement.insertFTLIfNeeded(`${config.addonRef}-mainWindow.ftl`);
win.MozXULElement.insertFTLIfNeeded(`${config.addonRef}-mainWindow.ftl`);
const popupWin = new ztoolkit.ProgressWindow(config.addonName, {
closeOnClick: true,
@ -60,11 +62,11 @@ async function onMainWindowLoad(win: Window): Promise<void> {
text: `[30%] ${getString("startup-begin")}`,
});
UIExampleFactory.registerStyleSheet();
UIExampleFactory.registerStyleSheet(win);
UIExampleFactory.registerRightClickMenuItem();
UIExampleFactory.registerRightClickMenuPopup();
UIExampleFactory.registerRightClickMenuPopup(win);
UIExampleFactory.registerWindowMenuWithSeparator();

View File

@ -5,10 +5,6 @@ import { config } from "../package.json";
const basicTool = new BasicTool();
if (!basicTool.getGlobal("Zotero")[config.addonInstance]) {
defineGlobal("window");
defineGlobal("document");
defineGlobal("ZoteroPane");
defineGlobal("Zotero_Tabs");
_globalThis.addon = new Addon();
defineGlobal("ztoolkit", () => {
return _globalThis.addon.data.ztoolkit;

View File

@ -44,14 +44,11 @@ export class BasicExampleFactory {
"file",
]);
// Unregister callback when the window closes (important to avoid a memory leak)
window.addEventListener(
"unload",
(e: Event) => {
Zotero.Plugins.addObserver({
shutdown: ({ id: pluginID }) => {
this.unregisterNotifier(notifierID);
},
false,
);
});
}
@example
@ -126,18 +123,17 @@ export class KeyExampleFactory {
export class UIExampleFactory {
@example
static registerStyleSheet() {
const styles = ztoolkit.UI.createElement(document, "link", {
static registerStyleSheet(win: Window) {
const doc = win.document;
const styles = ztoolkit.UI.createElement(doc, "link", {
properties: {
type: "text/css",
rel: "stylesheet",
href: `chrome://${config.addonRef}/content/zoteroPane.css`,
},
});
document.documentElement.appendChild(styles);
document
.getElementById("zotero-item-pane-content")
?.classList.add("makeItRed");
doc.documentElement.appendChild(styles);
doc.getElementById("zotero-item-pane-content")?.classList.add("makeItRed");
}
@example
@ -154,7 +150,7 @@ export class UIExampleFactory {
}
@example
static registerRightClickMenuPopup() {
static registerRightClickMenuPopup(win: Window) {
ztoolkit.Menu.register(
"item",
{
@ -169,7 +165,7 @@ export class UIExampleFactory {
],
},
"before",
document.querySelector(
win.document.querySelector(
"#zotero-itemmenu-addontemplate-test",
) as XUL.MenuItem,
);
@ -214,7 +210,7 @@ export class UIExampleFactory {
},
renderCell(index, data, column) {
ztoolkit.log("Custom column cell is rendered!");
const span = document.createElementNS(
const span = Zotero.getMainWindow().document.createElementNS(
"http://www.w3.org/1999/xhtml",
"span",
);
@ -406,7 +402,8 @@ export class PromptExampleFactory {
if (i != 0) str += ", ";
if (typeof node === "object") {
const label = document.createElement("label");
const label =
Zotero.getMainWindow().document.createElement("label");
label.setAttribute("value", str);
label.setAttribute("crop", "end");
str = "";
@ -500,8 +497,8 @@ export class PromptExampleFactory {
type: "click",
listener: () => {
prompt.promptNode.style.display = "none";
Zotero_Tabs.select("zotero-pane");
ZoteroPane.selectItem(item.id);
ztoolkit.getGlobal("Zotero_Tabs").select("zotero-pane");
ztoolkit.getGlobal("ZoteroPane").selectItem(item.id);
},
},
],
@ -556,12 +553,12 @@ export class PromptExampleFactory {
label: "Plugin Template",
// The when function is executed when Prompt UI is woken up by `Shift + P`, and this command does not display when false is returned.
when: () => {
const items = ZoteroPane.getSelectedItems();
const items = ztoolkit.getGlobal("ZoteroPane").getSelectedItems();
return items.length > 0;
},
callback(prompt) {
prompt.inputNode.placeholder = "Hello World!";
const items = ZoteroPane.getSelectedItems();
const items = ztoolkit.getGlobal("ZoteroPane").getSelectedItems();
ztoolkit.getGlobal("alert")(
`You select ${items.length} items!\n\n${items
.map(

4
typings/global.d.ts vendored
View File

@ -1,10 +1,6 @@
declare const _globalThis: {
[key: string]: any;
Zotero: _ZoteroTypes.Zotero;
ZoteroPane: _ZoteroTypes.ZoteroPane;
Zotero_Tabs: typeof Zotero_Tabs;
window: Window;
document: Document;
ztoolkit: ZToolkit;
addon: typeof addon;
};