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

Commit

Permalink
fix: preview bug with z7 beta 28+
Browse files Browse the repository at this point in the history
fix: #120
fix: #117
fix: #115
fix: #113
fix: #110
  • Loading branch information
windingwind committed Aug 27, 2023
1 parent 60d571f commit 27f171e
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 180 deletions.
85 changes: 9 additions & 76 deletions addon/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,80 +5,11 @@
* [2] https://www.zotero.org/support/dev/zotero_7_for_developers
*/

if (typeof Zotero == "undefined") {
var Zotero;
}

var chromeHandle;

var windowListener;

// In Zotero 6, bootstrap methods are called before Zotero is initialized, and using include.js
// to get the Zotero XPCOM service would risk breaking Zotero startup. Instead, wait for the main
// Zotero window to open and get the Zotero object from there.
//
// In Zotero 7, bootstrap methods are not called until Zotero is initialized, and the 'Zotero' is
// automatically made available.
async function waitForZotero() {
await new Promise(async (resolve) => {
if (typeof Zotero != "undefined") {
resolve();
}

const { Services } = ChromeUtils.import(
"resource://gre/modules/Services.jsm",
);
const windows = Services.wm.getEnumerator("navigator:browser");
let found = false;
while (windows.hasMoreElements()) {
let win = windows.getNext();
if (win.Zotero) {
Zotero = win.Zotero;
found = true;
resolve();
break;
}
}
windowListener = {
onOpenWindow: function (aWindow) {
// Wait for the window to finish loading
let domWindow = aWindow
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
domWindow.addEventListener(
"load",
function () {
domWindow.removeEventListener("load", arguments.callee, false);
if (!found && domWindow.Zotero) {
Zotero = domWindow.Zotero;
resolve();
} else if (
domWindow.location.href ===
"chrome://zotero/content/zoteroPane.xhtml"
) {
Zotero.__addonInstance__?.hooks.onMainWindowLoad(domWindow);
domWindow.addEventListener("unload", function () {
domWindow.removeEventListener(
"unload",
arguments.callee,
false,
);
Zotero.__addonInstance__?.hooks.onMainWindowUnload(domWindow);
});
}
},
false,
);
},
};
Services.wm.addListener(windowListener);
});
}

function install(data, reason) {}

async function startup({ id, version, resourceURI, rootURI }, reason) {
await waitForZotero();
await Zotero.initializationPromise;

// String 'rootURI' introduced in Zotero 7
Expand Down Expand Up @@ -111,18 +42,20 @@ async function startup({ id, version, resourceURI, rootURI }, reason) {
);
}

async function onMainWindowLoad({ window }, reason) {
Zotero.__addonInstance__?.hooks.onMainWindowLoad(window);
}

async function onMainWindowUnload({ window }, reason) {
Zotero.__addonInstance__?.hooks.onMainWindowUnload(window);
}

function shutdown({ id, version, resourceURI, rootURI }, reason) {
if (reason === APP_SHUTDOWN) {
return;
}
Services.wm.removeListener(windowListener);

if (typeof Zotero === "undefined") {
Zotero = Components.classes["@zotero.org/Zotero;1"].getService(
Components.interfaces.nsISupports,
).wrappedJSObject;
}
Zotero.__addonInstance__.hooks.onShutdown();
Zotero.__addonInstance__?.hooks.onShutdown();

Cc["@mozilla.org/intl/stringbundle;1"]
.getService(Components.interfaces.nsIStringBundleService)
Expand Down
75 changes: 36 additions & 39 deletions addon/chrome/content/previewPDF.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<meta name="google" content="notranslate" />

<style>
html,
body {
height: 100%;
}
:root {
--page-margin: 8px auto 8px;
}
Expand All @@ -18,13 +22,6 @@
padding: 0;
overflow-y: hidden;
}
#toolbarContainer {
background-color: #f6f6f6;
}
.toolbarViewerBlock {
display: flex;
align-items: center;
}
#viewerContainer {
display: block;
height: inherit;
Expand Down Expand Up @@ -69,44 +66,44 @@
}
</style>

<link rel="stylesheet" href="resource://zotero/pdf-reader/viewer.css" />
<link
rel="stylesheet"
href="resource://zotero/reader/pdf/build/viewer.css"
/>
<link rel="stylesheet" href="resource://zotero/reader/reader.css" />

<script src="chrome://zotero/content/include.js"></script>
<script src="resource://zotero/pdf-reader/pdf.js"></script>
<script src="resource://zotero/reader/pdf/build/pdf.js"></script>
</head>

<body tabindex="1">
<div class="toolbar">
<div id="toolbarContainer">
<div id="toolbarViewer">
<div class="toolbarViewerLeft toolbarViewerBlock"></div>
<div class="toolbarViewerMiddle toolbarViewerBlock">
<div class="splitToolbarButton">
<button
id="zoomOut"
class="toolbarButton zoomOut"
title="Zoom Out"
tabindex="-1"
data-l10n-id="zoom_out"
></button>
<button
id="zoomIn"
class="toolbarButton zoomIn"
title="Zoom In"
tabindex="-1"
data-l10n-id="zoom_in"
></button>
<button
id="zoomAuto"
class="toolbarButton zoomAuto"
title="Zoom Auto"
tabindex="-1"
></button>
</div>
</div>
<div class="toolbarViewerRight toolbarViewerBlock"></div>
<div class="start"></div>
<div class="center">
<div class="splitToolbarButton">
<button
id="zoomOut"
class="toolbarButton zoomOut"
title="Zoom Out"
tabindex="-1"
data-l10n-id="zoom_out"
></button>
<button
id="zoomIn"
class="toolbarButton zoomIn"
title="Zoom In"
tabindex="-1"
data-l10n-id="zoom_in"
></button>
<button
id="zoomAuto"
class="toolbarButton zoomAuto"
title="Zoom Auto"
tabindex="-1"
></button>
</div>
</div>
<div class="end"></div>
</div>
<div id="viewerContainer" class="pdfViewer"></div>

Expand Down Expand Up @@ -498,7 +495,7 @@
cachedData.pdfDocument && cachedData.pdfDocument.destroy();
cachedData.pdfDocument = await pdfjsLib.getDocument({
data: await getBuffer(e.data.itemID),
cMapUrl: "resource://zotero/pdf-reader/cmaps/",
cMapUrl: "resource://zotero/reader/pdf/build/cmaps/",
cMapPacked: true,
}).promise;
cachedData.itemID = e.data.itemID;
Expand Down Expand Up @@ -526,7 +523,7 @@
var MAX_IMAGE_SIZE = 1024 * 1024;

pdfjsLib.GlobalWorkerOptions.workerSrc =
"resource://zotero/pdf-reader/pdf.worker.js";
"resource://zotero/reader/pdf/build/pdf.worker.js";

document
.querySelector("#zoomOut")
Expand Down
46 changes: 6 additions & 40 deletions src/addon.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import ZoteroToolkit from "zotero-plugin-toolkit/dist/index";
import { ColumnOptions } from "zotero-plugin-toolkit/dist/helpers/virtualizedTable";
import hooks from "./hooks";
import { getPref, setPref } from "./utils/prefs";
import { createZToolkit } from "./utils/ztoolkit";
import { DialogHelper } from "zotero-plugin-toolkit/dist/helpers/dialog";
import { InfoPaneMode, PreviewType } from "./utils/type";

class Addon {
public data: {
alive: boolean;
// Env type, see build.js
env: "development" | "production";
// ztoolkit: MyToolkit;
ztoolkit: ZoteroToolkit;
ztoolkit: ZToolkit;
locale?: {
current: any;
};
Expand Down Expand Up @@ -41,7 +44,7 @@ class Addon {
alive: true,
env: __env__,
// ztoolkit: new MyToolkit(),
ztoolkit: new ZoteroToolkit(),
ztoolkit: createZToolkit(),
state: {
splitCollapsed: false,
get splitPosition() {
Expand All @@ -68,43 +71,6 @@ class Addon {
}
}

/**
* Alternatively, import toolkit modules you use to minify the plugin size.
*
* Steps to replace the default `ztoolkit: ZoteroToolkit` with your `ztoolkit: MyToolkit`:
*
* 1. Uncomment this file's line 30: `ztoolkit: new MyToolkit(),`
* and comment line 31: `ztoolkit: new ZoteroToolkit(),`.
* 2. Uncomment this file's line 10: `ztoolkit: MyToolkit;` in this file
* and comment line 11: `ztoolkit: ZoteroToolkit;`.
* 3. Uncomment `./typing/global.d.ts` line 12: `declare const ztoolkit: import("../src/addon").MyToolkit;`
* and comment line 13: `declare const ztoolkit: import("zotero-plugin-toolkit").ZoteroToolkit;`.
*
* You can now add the modules under the `MyToolkit` class.
*/

import { BasicTool, unregister } from "zotero-plugin-toolkit/dist/basic";
import { UITool } from "zotero-plugin-toolkit/dist/tools/ui";
import { PreferencePaneManager } from "zotero-plugin-toolkit/dist/managers/preferencePane";
import { DialogHelper } from "zotero-plugin-toolkit/dist/helpers/dialog";
import { getPref, setPref } from "./utils/prefs";
import { InfoPaneMode, PreviewType } from "./utils/type";

export class MyToolkit extends BasicTool {
UI: UITool;
PreferencePane: PreferencePaneManager;

constructor() {
super();
this.UI = new UITool(this);
this.PreferencePane = new PreferencePaneManager(this);
}

unregisterAll() {
unregister(this);
}
}

export default Addon;

const getResolvedPromise = () => {
Expand Down
25 changes: 17 additions & 8 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,28 @@ async function onStartup() {
Zotero.uiReadyPromise,
]);
initLocale();
ztoolkit.ProgressWindow.setIconURI(
"default",
`chrome://${config.addonRef}/content/icons/favicon.png`,
);
registerPrefPane();
await waitUtilAsync(() => {
const win = ztoolkit.getGlobal("window");
return win && win.document.readyState === "complete";
});
await onMainWindowLoad(ztoolkit.getGlobal("window"));
}

async function onMainWindowLoad(win: Window): Promise<void> {
await new Promise((resolve) => {
if (win.document.readyState !== "complete") {
win.document.addEventListener("readystatechange", () => {
if (win.document.readyState === "complete") {
resolve(void 0);
}
});
}
resolve(void 0);
});

await Promise.all([
Zotero.initializationPromise,
Zotero.unlockPromise,
Zotero.uiReadyPromise,
]);

try {
const doc = win.document;
registerPreviewTab();
Expand Down
21 changes: 10 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,24 @@ if (!basicTool.getGlobal("Zotero")[config.addonInstance]) {
defineGlobal("document");
defineGlobal("ZoteroPane");
defineGlobal("Zotero_Tabs");
defineGlobal("ZoteroContextPane");
defineGlobal("crypto");
defineGlobal("TextEncoder");
_globalThis.addon = new Addon();
_globalThis.ztoolkit = addon.data.ztoolkit;
ztoolkit.basicOptions.log.prefix = `[${config.addonName}]`;
ztoolkit.basicOptions.log.disableConsole = addon.data.env === "production";
ztoolkit.UI.basicOptions.ui.enableElementJSONLog =
addon.data.env === "development";
ztoolkit.UI.basicOptions.ui.enableElementDOMLog =
addon.data.env === "development";
ztoolkit.basicOptions.debug.disableDebugBridgePassword =
addon.data.env === "development";
defineGlobal("ztoolkit", () => {
return _globalThis.addon.data.ztoolkit;
});
Zotero[config.addonInstance] = addon;
// Trigger addon hook for initialization
addon.hooks.onStartup();
}

function defineGlobal(name: Parameters<BasicTool["getGlobal"]>[0]) {
function defineGlobal(name: Parameters<BasicTool["getGlobal"]>[0]): void;
function defineGlobal(name: string, getter: () => any): void;
function defineGlobal(name: string, getter?: () => any) {
Object.defineProperty(_globalThis, name, {
get() {
return basicTool.getGlobal(name);
return getter ? getter() : basicTool.getGlobal(name);
},
});
}
Loading

0 comments on commit 27f171e

Please sign in to comment.