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

View File

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

View File

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

4
typings/global.d.ts vendored
View File

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