add: custom toolkit modules example

This commit is contained in:
xiangyu 2023-01-11 12:56:53 +08:00
parent d48a628693
commit 4a06cd0535
3 changed files with 39 additions and 0 deletions

View File

@ -7,6 +7,7 @@ class Addon {
alive: boolean;
// Env type, see build.js
env: "development" | "production";
// ztoolkit: MyToolkit;
ztoolkit: ZoteroToolkit;
locale?: {
stringBundle: any;
@ -26,6 +27,7 @@ class Addon {
this.data = {
alive: true,
env: __env__,
// ztoolkit: new MyToolkit(),
ztoolkit: new ZoteroToolkit(),
};
this.hooks = hooks;
@ -33,4 +35,38 @@ 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";
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;

View File

@ -15,6 +15,8 @@ if (!basicTool.getGlobal("Zotero").AddonTemplate) {
_globalThis.ztoolkit = addon.data.ztoolkit;
ztoolkit.basicOptions.log.prefix = `[${config.addonName}]`;
ztoolkit.basicOptions.log.disableConsole = addon.data.env === "production";
ztoolkit.UI.elementOptions.enableElementJSONLog =
addon.data.env === "development";
Zotero.AddonTemplate = addon;
// Trigger addon hook for initialization
addon.hooks.onStartup();

1
typing/global.d.ts vendored
View File

@ -9,6 +9,7 @@ declare const _globalThis: {
addon: typeof addon;
};
// declare const ztoolkit: import("../src/addon").MyToolkit;
declare const ztoolkit: import("zotero-plugin-toolkit").ZoteroToolkit;
declare const rootURI: string;