Skip to content

Commit

Permalink
add: utils
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangyu committed Dec 15, 2022
1 parent 7de5e38 commit 6f752cd
Show file tree
Hide file tree
Showing 8 changed files with 440 additions and 38 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
"devDependencies": {
"@types/node": "^18.7.20",
"release-it": "^14.14.0",
"zotero-types": "^0.0.7"
"zotero-types": "^0.0.8"
}
}
33 changes: 5 additions & 28 deletions src/addon.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,23 @@
import AddonEvents from "./events";
import AddonPrefs from "./prefs";
import AddonUtils from "./utils";
import AddonViews from "./views";

const { addonName } = require("../package.json");

class Addon {
public Zotero: _ZoteroConstructable;
public events: AddonEvents;
public views: AddonViews;
public prefs: AddonPrefs;
public utils: AddonUtils;
// root path to access the resources
public rootURI: string;

constructor() {
this.events = new AddonEvents(this);
this.views = new AddonViews(this);
this.prefs = new AddonPrefs(this);
this.utils = new AddonUtils(this);
}
}

function getZotero(): _ZoteroConstructable {
if (typeof Zotero === "undefined") {
return Components.classes["@zotero.org/Zotero;1"].getService(
Components.interfaces.nsISupports
).wrappedJSObject;
}
return Zotero;
}

function isZotero7(): boolean {
return Zotero.platformMajorVersion >= 102;
}

function createXULElement(doc: Document, type: string): XUL.Element {
if (isZotero7()) {
// @ts-ignore
return doc.createXULElement(type);
} else {
return doc.createElementNS(
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
type
) as XUL.Element;
}
}

export { addonName, Addon, getZotero, isZotero7, createXULElement };
export default Addon;
9 changes: 5 additions & 4 deletions src/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Addon, addonName, getZotero } from "./addon";
import Addon from "./addon";
import AddonModule from "./module";
import { addonName } from "../package.json";

class AddonEvents extends AddonModule {
private notifierCallback: any;
Expand Down Expand Up @@ -27,8 +28,8 @@ class AddonEvents extends AddonModule {
};
}

public async onInit() {
const Zotero = getZotero();
public async onInit(_Zotero: _ZoteroConstructable) {
this._Addon.Zotero = _Zotero;
// This function is the setup code of the addon
Zotero.debug(`${addonName}: init called`);
// alert(112233);
Expand Down Expand Up @@ -70,7 +71,7 @@ class AddonEvents extends AddonModule {
}

public onUnInit(): void {
const Zotero = getZotero();
const Zotero = this._Addon.Zotero;
Zotero.debug(`${addonName}: uninit called`);
// Remove elements and do clean up
this._Addon.views.unInitViews();
Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Addon, getZotero } from "./addon";
import Addon from "./addon";

const Zotero = getZotero();
const Zotero = Components.classes["@zotero.org/Zotero;1"].getService(
Components.interfaces.nsISupports
).wrappedJSObject;

if (!Zotero.AddonTemplate) {
Zotero.AddonTemplate = new Addon();
Zotero.AddonTemplate.events.onInit();
Zotero.AddonTemplate.events.onInit(Zotero);
}
6 changes: 4 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Addon from "./addon";

class AddonModule {
protected _Addon: any;
constructor(parent: any) {
protected _Addon: Addon;
constructor(parent: Addon) {
this._Addon = parent;
}
}
Expand Down
Loading

0 comments on commit 6f752cd

Please sign in to comment.