Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
fix: gloabl window & document
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Jul 24, 2023
1 parent dcf23b6 commit 55cbf80
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 52 deletions.
40 changes: 20 additions & 20 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ async function onMainWindowLoad(win: Window): Promise<void> {
registerPreviewTab();
// Call `viewItem` to initialize #zotero-editpane-item-box
ztoolkit.getGlobal("ZoteroItemPane").viewItem(null, null, 0);
await registerSplit(doc, PreviewType.info);
await registerSplit(doc, PreviewType.attachment);
updatePreviewTab(doc);
updateSplit(doc, PreviewType.info);
updateSplit(doc, PreviewType.attachment);
await initContainer(doc, PreviewType.preview, "after");
await initContainer(doc, PreviewType.info, "before");
await initContainer(doc, PreviewType.info, "after");
await initContainer(doc, PreviewType.attachment, "before");
await initContainer(doc, PreviewType.attachment, "after");
initItemSelectListener(doc);
initTabSelectListener(doc);
initPreviewResizeListener(doc);
initWindowResizeListener(win);
onPreview(doc, true);
await registerSplit(PreviewType.info);
await registerSplit(PreviewType.attachment);
updatePreviewTab();
updateSplit(PreviewType.info);
updateSplit(PreviewType.attachment);
await initContainer(PreviewType.preview, "after");
await initContainer(PreviewType.info, "before");
await initContainer(PreviewType.info, "after");
await initContainer(PreviewType.attachment, "before");
await initContainer(PreviewType.attachment, "after");
initItemSelectListener();
initTabSelectListener();
initPreviewResizeListener();
initWindowResizeListener();
onPreview(true);
} catch (e) {
ztoolkit.log(e);
}
Expand All @@ -74,19 +74,19 @@ function onShutdown(): void {
delete Zotero[config.addonInstance];
}

function onPreview(document: Document, forceUpdate = false) {
updatePreviewTab(document);
function onPreview(forceUpdate = false) {
updatePreviewTab();
const previewType = getPreviewType(document);
ztoolkit.log(previewType);
updateSplit(document, PreviewType.info);
updateSplit(PreviewType.info);
if (previewType === PreviewType.null) {
//
} else if (previewType === PreviewType.preview) {
//
} else {
updateSplit(document, previewType);
updateSplit(previewType);
}
preview(document, previewType, forceUpdate);
preview(previewType, forceUpdate);
}

const onInitContainer = initContainer;
Expand Down
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ const basicTool = new BasicTool();
if (!basicTool.getGlobal("Zotero")[config.addonInstance]) {
// Set global variables
_globalThis.Zotero = basicTool.getGlobal("Zotero");
_globalThis.ZoteroPane = basicTool.getGlobal("ZoteroPane");
_globalThis.Zotero_Tabs = basicTool.getGlobal("Zotero_Tabs");
defineGlobal("window");
defineGlobal("document");
defineGlobal("ZoteroPane");
defineGlobal("Zotero_Tabs");
_globalThis.addon = new Addon();
_globalThis.ztoolkit = addon.data.ztoolkit;
ztoolkit.basicOptions.log.prefix = `[${config.addonName}]`;
Expand All @@ -23,3 +25,11 @@ if (!basicTool.getGlobal("Zotero")[config.addonInstance]) {
// Trigger addon hook for initialization
addon.hooks.onStartup();
}

function defineGlobal(name: Parameters<BasicTool["getGlobal"]>[0]) {
Object.defineProperty(_globalThis, name, {
get() {
return basicTool.getGlobal(name);
},
});
}
6 changes: 1 addition & 5 deletions src/modules/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ export { initContainer };
* @param type
* @param position
*/
async function initContainer(
document: Document,
type: PreviewType,
position: "before" | "after",
) {
async function initContainer(type: PreviewType, position: "before" | "after") {
addon.data.state.initPromise = Zotero.Promise.defer();
const containerId = getContainerId(type, position);
const container = document.querySelector(`#${containerId}`);
Expand Down
25 changes: 12 additions & 13 deletions src/modules/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ export {
// Use a global variable to prevent multiple resize events from triggering
let resizing = 0;

function initItemSelectListener(document: Document) {
function initItemSelectListener() {
const listener = () => {
if (!addon.data.alive) {
ZoteroPane.itemsView.onSelect.removeListener(listener);
}
ztoolkit.log("Preview triggered by selection change");
addon.hooks.onPreview(document);
addon.hooks.onPreview();
};
ZoteroPane.itemsView.onSelect.addListener(listener);
}

function initPreviewResizeListener(document: Document) {
function initPreviewResizeListener() {
const splitter = document.querySelector(
"#zotero-items-splitter",
) as HTMLElement;
splitter.addEventListener("mouseup", getOnItemBoxResize(document));
splitter.addEventListener("mouseup", getOnItemBoxResize());
}

function initWindowResizeListener(window: Window) {
const onWindowResize = getOnWindowResize(window);
function initWindowResizeListener() {
const onWindowResize = getOnWindowResize();
window.addEventListener("resize", onWindowResize);
onWindowResize();
}

function initTabSelectListener(document: Document) {
function initTabSelectListener() {
const tabbox = document.querySelector("#zotero-view-tabbox") as XUL.TabBox;
const triggeredIds = [
"item-box-container",
Expand All @@ -46,13 +46,13 @@ function initTabSelectListener(document: Document) {
}
if (triggeredIds.includes(tabbox.selectedPanel?.id)) {
ztoolkit.log("Preview triggered by tab change");
addon.hooks.onPreview(document, true);
addon.hooks.onPreview(true);
}
};
tabbox.addEventListener("select", listener);
}

function getOnItemBoxResize(document: Document) {
function getOnItemBoxResize() {
function onItemBoxResize() {
const splitter = document.querySelector(
"#zotero-items-splitter",
Expand Down Expand Up @@ -88,16 +88,15 @@ function getOnItemBoxResize(document: Document) {
resizing -= 1;
if (resizing === 0) {
ztoolkit.log("Preview triggered by resize");
addon.hooks.onPreview(document, true);
addon.hooks.onPreview(true);
}
}, 100);
}
return onItemBoxResize;
}

function getOnWindowResize(window: Window) {
const document = window.document;
const onItemBoxResize = getOnItemBoxResize(document);
function getOnWindowResize() {
const onItemBoxResize = getOnItemBoxResize();
function onWindowResize(e?: UIEvent) {
if (!addon.data.alive) {
window.removeEventListener("resize", onWindowResize);
Expand Down
10 changes: 4 additions & 6 deletions src/modules/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export { preview };
* This function update item and decide whether to render preview
* @param forceRender
*/
async function updatePreviewItem(document: Document, forceRender = false) {
async function updatePreviewItem(forceRender = false) {
const items = ZoteroPane.getSelectedItems();
if (items.length !== 1) {
// Do not preview multiple selection
Expand All @@ -26,12 +26,12 @@ async function updatePreviewItem(document: Document, forceRender = false) {
}
isDev() && ztoolkit.log(items, item);
if (!item || !item.isPDFAttachment()) {
addon.hooks.onCollapse(document, true, true);
addon.hooks.onCollapse(true, true);
// Do not preview non-pdf item
return false;
} else {
if (!addon.data.state.splitCollapsed) {
addon.hooks.onCollapse(document, false, true);
addon.hooks.onCollapse(false, true);
}
}
if (!forceRender && addon.data.state.item?.id === item.id) {
Expand All @@ -44,7 +44,7 @@ async function updatePreviewItem(document: Document, forceRender = false) {
return true;
}

async function preview(document: Document, type: PreviewType, force = false) {
async function preview(type: PreviewType, force = false) {
if (
getPref(type === PreviewType.preview ? "enableTab" : "enableSplit") ===
false
Expand All @@ -56,7 +56,6 @@ async function preview(document: Document, type: PreviewType, force = false) {
if (addon.data.state.previewCounts[type] >= RELOAD_COUNT) {
addon.data.state.previewCounts[type] = 0;
await addon.hooks.onInitContainer(
document,
type,
type === PreviewType.preview ? "after" : addon.data.state.splitPosition,
);
Expand All @@ -79,7 +78,6 @@ async function preview(document: Document, type: PreviewType, force = false) {
3. force. user clicked the preview button
*/
await updatePreviewItem(
document,
type !== addon.data.state.lastType ||
// @ts-ignore cachedData is not a standard property
addon.data.state.item?.id !== iframe.contentWindow?.cachedData?.itemID ||
Expand Down
7 changes: 2 additions & 5 deletions src/modules/split.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function getSplitterId(type: PreviewType, position: "before" | "after") {
return `${getContainerId(type, position)}-splitter`;
}

async function registerSplit(document: Document, type: PreviewType) {
async function registerSplit(type: PreviewType) {
let zitembox: XUL.Box;
switch (type) {
case PreviewType.info: {
Expand Down Expand Up @@ -66,7 +66,6 @@ async function registerSplit(document: Document, type: PreviewType) {
?.getAttribute("height") || "0",
);
setSplitCollapsed(
doc,
doc
.querySelector(`#${splitterBeforeId}`)
?.getAttribute("state") === "collapsed",
Expand Down Expand Up @@ -101,7 +100,6 @@ async function registerSplit(document: Document, type: PreviewType) {
"0",
);
setSplitCollapsed(
doc,
doc
.querySelector(`#${splitterAfterId}`)
?.getAttribute("state") === "collapsed",
Expand All @@ -120,7 +118,6 @@ async function registerSplit(document: Document, type: PreviewType) {
}

function setSplitCollapsed(
document: Document,
collapsed: boolean | undefined = undefined,
quietly = false,
) {
Expand Down Expand Up @@ -149,7 +146,7 @@ function setSplitCollapsed(
* Update split hidden status
* @param type
*/
function updateSplit(document: Document, type: PreviewType) {
function updateSplit(type: PreviewType) {
// Check BBT layout
// const BBTBox = (
// document.getElementById("zotero-editpane-item-box") as Element
Expand Down
2 changes: 1 addition & 1 deletion src/modules/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function registerPreviewTab() {
);
}

function updatePreviewTab(document: Document) {
function updatePreviewTab() {
const hidden = !getPref("enableTab");
const previewTab = document.querySelector(`#${tabId}`) as XUL.Tab;
if (hidden) {
Expand Down

0 comments on commit 55cbf80

Please sign in to comment.