Skip to content

Commit

Permalink
refactor: replace global window/document
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Jun 20, 2024
1 parent fcd6f32 commit cb56400
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 35 deletions.
18 changes: 9 additions & 9 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async function onStartup() {
}

async function onMainWindowLoad(win: Window): Promise<void> {
await waitUtilAsync(() => document.readyState === "complete");
await waitUtilAsync(() => win.document.readyState === "complete");

Services.scriptloader.loadSubScript(
`chrome://${config.addonRef}/content/scripts/customElements.js`,
Expand All @@ -84,9 +84,9 @@ async function onMainWindowLoad(win: Window): Promise<void> {
// Create ztoolkit for every window
addon.data.ztoolkit = createZToolkit();

registerNotify(["tab", "item", "item-tag"]);
registerNotify(["tab", "item", "item-tag"], win);

registerMenus();
registerMenus(win);

initTemplates();

Expand Down Expand Up @@ -157,7 +157,7 @@ async function onPrefsEvent(type: string, data: { [key: string]: any }) {
}
}

function onOpenNote(
async function onOpenNote(
noteId: number,
mode: "auto" | "preview" | "tab" | "window" | "builtin" = "auto",
options: {
Expand All @@ -182,9 +182,9 @@ function onOpenNote(
if ((currentWindow as any)?.Zotero_Tabs?.selectedType === "note") {
mode = "preview";
workspaceUID = (
document.querySelector(`#${Zotero_Tabs.selectedID} bn-workspace`) as
| HTMLElement
| undefined
currentWindow?.document.querySelector(
`#${Zotero_Tabs.selectedID} bn-workspace`,
) as HTMLElement | undefined
)?.dataset.uid;
} else if (currentWindow?.document.querySelector("body.workspace-window")) {
mode = "preview";
Expand All @@ -207,10 +207,10 @@ function onOpenNote(
openNotePreview(noteItem, workspaceUID, options);
break;
case "tab":
openWorkspaceTab(noteItem, options);
await openWorkspaceTab(noteItem, options);
break;
case "window":
openWorkspaceWindow(noteItem, options);
await openWorkspaceWindow(noteItem, options);
break;
case "builtin":
ZoteroPane.openNoteWindow(noteId);
Expand Down
8 changes: 5 additions & 3 deletions src/modules/createNote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function createNoteFromTemplate(
const parentItemId =
parentType === "reader" ? getReaderParentId() : getLibraryParentId();
if (!parentItemId) {
window.alert(getString("alert.notValidParentItemError"));
Zotero.getMainWindow().alert(getString("alert.notValidParentItemError"));
return;
}
addon.hooks.onShowTemplatePicker("create", {
Expand All @@ -48,11 +48,13 @@ async function createNoteFromTemplate(
async function createNoteFromMD() {
const currentCollection = ZoteroPane.getSelectedCollection();
if (!currentCollection) {
window.alert(getString("alert.notValidCollectionError"));
Zotero.getMainWindow().alert(getString("alert.notValidCollectionError"));
return;
}

const syncNotes = window.confirm(getString("alert-syncImportedNotes"));
const syncNotes = Zotero.getMainWindow().confirm(
getString("alert-syncImportedNotes"),
);

const filepaths = await new ztoolkit.FilePicker(
"Import MarkDown",
Expand Down
2 changes: 1 addition & 1 deletion src/modules/export/docx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ async function getWorker(): Promise<HTMLIFrameElement> {
position: "absolute",
},
});
window.document.documentElement.appendChild(worker);
Zotero.getMainWindow().document.documentElement.appendChild(worker);
await waitUtilAsync(() => worker.contentDocument?.readyState === "complete");
return worker;
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/export/freemind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function note2mm(
) {
const root = getNoteTree(noteItem, false);
const textNodeForEach = (e: Node, callbackfn: (e: any) => void) => {
if (e.nodeType === document.TEXT_NODE) {
if (e.nodeType === Zotero.getMainWindow().document.TEXT_NODE) {
callbackfn(e);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/export/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function savePDF(noteId: number) {
browser: undefined as any,
url: `chrome://${config.addonRef}/content/printTemplate.xhtml`,
};
const win = window.openDialog(
const win = Zotero.getMainWindow().openDialog(
`chrome://${config.addonRef}/content/printWrapper.xhtml`,
`${config.addonRef}-printWrapper`,
`chrome,centerscreen,resizable,status,width=900,height=650,dialog=no`,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/imageViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function showImageViewer(
Components.utils.isDeadWrapper(addon.data.imageViewer.window) ||
addon.data.imageViewer.window.closed
) {
addon.data.imageViewer.window = window.openDialog(
addon.data.imageViewer.window = Zotero.getMainWindow().openDialog(
`chrome://${config.addonRef}/content/imageViewer.html`,
`${config.addonRef}-imageViewer`,
`chrome,centerscreen,resizable,status,width=500,height=550,dialog=no${
Expand Down
10 changes: 5 additions & 5 deletions src/modules/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getString } from "../utils/locale";
import { getPref } from "../utils/prefs";
import { slice } from "../utils/str";

export function registerMenus() {
export function registerMenus(win: Window) {
// item
ztoolkit.Menu.register("item", { tag: "menuseparator" });
ztoolkit.Menu.register("item", {
Expand All @@ -19,7 +19,7 @@ export function registerMenus() {
});

// menuEdit
const menuEditAnchor = document.querySelector(
const menuEditAnchor = win.document.querySelector(
"#menu_EditPreferencesItem",
) as XUL.MenuItem;
ztoolkit.Menu.register(
Expand Down Expand Up @@ -93,7 +93,7 @@ export function registerMenus() {
});

// menuFile
const menuFileAnchor = document.querySelector(
const menuFileAnchor = win.document.querySelector(
"#menu_newCollection",
) as XUL.MenuItem;

Expand Down Expand Up @@ -140,7 +140,7 @@ export function registerMenus() {
);

// create note menu in library
const newNoteMenu = document
const newNoteMenu = win.document
.querySelector("#zotero-tb-note-add")
?.querySelector("menupopup") as XUL.MenuPopup;
ztoolkit.Menu.register(newNoteMenu, {
Expand All @@ -165,7 +165,7 @@ export function registerMenus() {

// create note menu in reader side panel
ztoolkit.Menu.register(
document.querySelector(
win.document.querySelector(
"#context-pane-add-child-note-button-popup",
) as XUL.MenuPopup,
{
Expand Down
7 changes: 5 additions & 2 deletions src/modules/notify.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export function registerNotify(types: _ZoteroTypes.Notifier.Type[]) {
export function registerNotify(
types: _ZoteroTypes.Notifier.Type[],
win: Window,
) {
const callback = {
notify: async (...data: Parameters<_ZoteroTypes.Notifier.Notify>) => {
if (!addon?.data.alive) {
Expand All @@ -13,7 +16,7 @@ export function registerNotify(types: _ZoteroTypes.Notifier.Type[]) {
const notifyID = Zotero.Notifier.registerObserver(callback, types);

// Unregister callback when the window closes (important to avoid a memory leak)
window.addEventListener(
win.addEventListener(
"unload",
(e: Event) => {
unregisterNotify(notifyID);
Expand Down
4 changes: 2 additions & 2 deletions src/modules/sync/diffWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export async function showSyncDiff(noteId: number, mdPath: string) {
}

if (!isWindowAlive(addon.data.sync.diff.window)) {
addon.data.sync.diff.window = window.open(
addon.data.sync.diff.window = Zotero.getMainWindow().open(
`chrome://${config.addonRef}/content/syncDiff.html`,
`${config.addonRef}-syncDiff`,
`chrome,centerscreen,resizable,status,width=900,height=550`,
Expand Down Expand Up @@ -114,7 +114,7 @@ export async function showSyncDiff(noteId: number, mdPath: string) {

switch (io.type) {
case "skip":
window.alert(
Zotero.getMainWindow().alert(
`Syncing of "${noteItem.getNoteTitle()}" is skipped.\nTo sync manually, go to File->Better Notes Sync Manager.`,
);
addon.data.sync.diff.window?.closed ||
Expand Down
2 changes: 1 addition & 1 deletion src/modules/sync/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function setSyncing() {
}
// Only when Zotero is active and focused
if (
document.hasFocus() &&
Zotero.getMainWindow().document.hasFocus() &&
(getPref("syncPeriodSeconds") as number) > 0
) {
callSyncing(undefined, {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/sync/managerWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function showSyncManager() {
const windowArgs = {
_initPromise: Zotero.Promise.defer(),
};
const win = window.openDialog(
const win = Zotero.getMainWindow().openDialog(
`chrome://${config.addonRef}/content/syncManager.xhtml`,
`${config.addonRef}-syncManager`,
`chrome,centerscreen,resizable,status,width=800,height=400,dialog=no`,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/template/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async function runTemplate(
if (options.dryRun) {
return "Template Preview Error: " + String(e);
}
window.alert(`Template ${key} Error: ${e}`);
Zotero.getMainWindow().alert(`Template ${key} Error: ${e}`);
return "";
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/template/editorWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function showTemplateEditor() {
const windowArgs = {
_initPromise: Zotero.Promise.defer(),
};
const _window = window.openDialog(
const _window = Zotero.getMainWindow().openDialog(
`chrome://${config.addonRef}/content/templateEditor.xhtml`,
`${config.addonRef}-templateEditor`,
`chrome,centerscreen,resizable,status,width=600,height=400,dialog=no`,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/template/picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ async function createTemplateNoteCallback(name: string) {
case "standalone": {
const currentCollection = ZoteroPane.getSelectedCollection();
if (!currentCollection) {
window.alert(getString("alert.notValidCollectionError"));
Zotero.getMainWindow().alert(
getString("alert.notValidCollectionError"),
);
return;
}
const noteID = await ZoteroPane.newNote();
Expand Down
8 changes: 6 additions & 2 deletions src/modules/workspace/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ function scrollTabEditorTo(
sectionName?: string;
} = {},
) {
const tab = Zotero_Tabs._tabs.find((tab) => tab.data?.itemID == item.id);
const tab = ztoolkit
.getGlobal("Zotero_Tabs")
._tabs.find((tab) => tab.data?.itemID == item.id);
if (!tab || tab.type !== TAB_TYPE) return;
const workspace = document.querySelector(`#${tab.id} > bn-workspace`);
const workspace = Zotero.getMainWindow().document.querySelector(
`#${tab.id} > bn-workspace`,
);
if (!workspace) return;
// @ts-ignore
workspace.scrollEditorTo(options);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/workspace/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function openWorkspaceWindow(
const windowArgs = {
_initPromise: Zotero.Promise.defer(),
};
const win = window.openDialog(
const win = Zotero.getMainWindow().openDialog(
`chrome://${config.addonRef}/content/workspaceWindow.xhtml`,
`${config.addonRef}-workspaceWindow`,
`chrome,centerscreen,resizable,status,width=800,height=400,dialog=no`,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/itemPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function itemPicker() {
deferred: Zotero.Promise.defer(),
};

window.openDialog(
Zotero.getMainWindow().openDialog(
"chrome://zotero/content/selectItemsDialog.xhtml",
"",
"chrome,dialog=no,centerscreen,resizable=yes",
Expand Down
2 changes: 1 addition & 1 deletion src/utils/linkCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function openLinkCreator(
currentLineIndex: options?.lineIndex,
deferred: Zotero.Promise.defer(),
} as any;
window.openDialog(
Zotero.getMainWindow().openDialog(
`chrome://${config.addonRef}/content/linkCreator.xhtml`,
"_blank",
"chrome,modal,centerscreen,resizable=yes",
Expand Down

0 comments on commit cb56400

Please sign in to comment.