Skip to content

Commit

Permalink
remove: global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Sep 24, 2024
1 parent 07ab7f0 commit 00b88f3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 32 deletions.
10 changes: 6 additions & 4 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ async function onStartup() {

UIExampleFactory.registerReaderItemPaneSection();

await onMainWindowLoad(window);
await Promise.all(
Zotero.getMainWindows().map((win) => onMainWindowLoad(win)),
);
}

async function onMainWindowLoad(win: Window): Promise<void> {
// Create ztoolkit for every window
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,
Expand All @@ -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();

Expand Down
4 changes: 0 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 17 additions & 20 deletions src/modules/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -154,7 +150,7 @@ export class UIExampleFactory {
}

@example
static registerRightClickMenuPopup() {
static registerRightClickMenuPopup(win: Window) {
ztoolkit.Menu.register(
"item",
{
Expand All @@ -169,7 +165,7 @@ export class UIExampleFactory {
],
},
"before",
document.querySelector(
win.document.querySelector(
"#zotero-itemmenu-addontemplate-test",
) as XUL.MenuItem,
);
Expand Down Expand Up @@ -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",
);
Expand Down Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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);
},
},
],
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 0 additions & 4 deletions typings/global.d.ts
Original file line number Diff line number Diff line change
@@ -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;
};
Expand Down

0 comments on commit 00b88f3

Please sign in to comment.