update: toolkit 1.0.0

This commit is contained in:
xiangyu 2023-01-09 17:23:02 +08:00
parent 5a1800254d
commit c0f7694480
6 changed files with 44 additions and 70 deletions

View File

@ -31,7 +31,7 @@
}, },
"homepage": "https://github.com/windingwind/zotero-addon-template#readme", "homepage": "https://github.com/windingwind/zotero-addon-template#readme",
"dependencies": { "dependencies": {
"zotero-plugin-toolkit": "^0.1.6" "zotero-plugin-toolkit": "^1.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^18.11.17", "@types/node": "^18.11.17",

View File

@ -1,4 +1,4 @@
import ZoteroToolkit from "zotero-plugin-toolkit"; import ZoteroToolkit from "zotero-plugin-toolkit/dist/index";
import hooks from "./hooks"; import hooks from "./hooks";
class Addon { class Addon {

View File

@ -14,12 +14,12 @@ async function onStartup() {
Zotero.uiReadyPromise, Zotero.uiReadyPromise,
]); ]);
initLocale(); initLocale();
ztoolkit.Tool.setIconURI( ztoolkit.ProgressWindow.setIconURI(
"default", "default",
`chrome://${config.addonRef}/content/icons/favicon.png` `chrome://${config.addonRef}/content/icons/favicon.png`
); );
const popupWin = ztoolkit.Tool.createProgressWindow(config.addonName, { const popupWin = new ztoolkit.ProgressWindow(config.addonName, {
closeOnClick: true, closeOnClick: true,
closeTime: -1, closeTime: -1,
}) })
@ -70,8 +70,7 @@ async function onStartup() {
} }
function onShutdown(): void { function onShutdown(): void {
BasicExampleFactory.unregisterPrefs(); ztoolkit.unregisterAll();
UIExampleFactory.unregisterUIExamples();
// Remove addon object // Remove addon object
addon.data.alive = false; addon.data.alive = false;
delete Zotero.AddonTemplate; delete Zotero.AddonTemplate;
@ -88,7 +87,7 @@ async function onNotify(
extraData: { [key: string]: any } extraData: { [key: string]: any }
) { ) {
// You can add your code to the corresponding notify type // You can add your code to the corresponding notify type
ztoolkit.Tool.log("notify", event, type, ids, extraData); ztoolkit.log("notify", event, type, ids, extraData);
if ( if (
event == "select" && event == "select" &&
type == "tab" && type == "tab" &&

View File

@ -1,21 +1,20 @@
import { ZoteroCompat } from "zotero-plugin-toolkit"; import { BasicTool } from "zotero-plugin-toolkit/dist/basic";
import Addon from "./addon"; import Addon from "./addon";
import { config } from "../package.json"; import { config } from "../package.json";
const compat = new ZoteroCompat(); const basicTool = new BasicTool();
if (!compat.getGlobal("Zotero").AddonTemplate) { if (!basicTool.getGlobal("Zotero").AddonTemplate) {
// Set global variables // Set global variables
_globalThis.Zotero = compat.getGlobal("Zotero"); _globalThis.Zotero = basicTool.getGlobal("Zotero");
_globalThis.ZoteroPane = compat.getGlobal("ZoteroPane"); _globalThis.ZoteroPane = basicTool.getGlobal("ZoteroPane");
_globalThis.Zotero_Tabs = compat.getGlobal("Zotero_Tabs"); _globalThis.Zotero_Tabs = basicTool.getGlobal("Zotero_Tabs");
_globalThis.window = compat.getGlobal("window"); _globalThis.window = basicTool.getGlobal("window");
_globalThis.document = compat.getGlobal("document"); _globalThis.document = basicTool.getGlobal("document");
_globalThis.addon = new Addon(); _globalThis.addon = new Addon();
_globalThis.ztoolkit = addon.data.ztoolkit; _globalThis.ztoolkit = addon.data.ztoolkit;
ztoolkit.Tool.logOptionsGlobal.prefix = `[${config.addonName}]`; ztoolkit.basicOptions.log.prefix = `[${config.addonName}]`;
ztoolkit.Tool.logOptionsGlobal.disableConsole = ztoolkit.basicOptions.log.disableConsole = addon.data.env === "production";
addon.data.env === "production";
Zotero.AddonTemplate = addon; Zotero.AddonTemplate = addon;
// Trigger addon hook for initialization // Trigger addon hook for initialization
addon.hooks.onStartup(); addon.hooks.onStartup();

View File

@ -9,15 +9,10 @@ function example(
const original = descriptor.value; const original = descriptor.value;
descriptor.value = function (...args: any) { descriptor.value = function (...args: any) {
try { try {
ztoolkit.Tool.log( ztoolkit.log(`Calling example ${target.name}.${String(propertyKey)}`);
`Calling example ${target.name}.${String(propertyKey)}`
);
return original.apply(this, args); return original.apply(this, args);
} catch (e) { } catch (e) {
ztoolkit.Tool.log( ztoolkit.log(`Error in example ${target.name}.${String(propertyKey)}`, e);
`Error in example ${target.name}.${String(propertyKey)}`,
e
);
throw e; throw e;
} }
}; };
@ -61,7 +56,7 @@ export class BasicExampleFactory {
@example @example
static exampleNotifierCallback() { static exampleNotifierCallback() {
ztoolkit.Tool.createProgressWindow(config.addonName) new ztoolkit.ProgressWindow(config.addonName)
.createLine({ .createLine({
text: "Open Tab Detected!", text: "Open Tab Detected!",
type: "success", type: "success",
@ -85,18 +80,7 @@ export class BasicExampleFactory {
extraDTD: [`chrome://${config.addonRef}/locale/overlay.dtd`], extraDTD: [`chrome://${config.addonRef}/locale/overlay.dtd`],
defaultXUL: true, defaultXUL: true,
}; };
if (ztoolkit.Compat.isZotero7()) { ztoolkit.PreferencePane.register(prefOptions);
Zotero.PreferencePanes.register(prefOptions);
} else {
ztoolkit.Compat.registerPrefPane(prefOptions);
}
}
@example
static unregisterPrefs() {
if (!ztoolkit.Compat.isZotero7()) {
ztoolkit.Compat.unregisterPrefPane();
}
} }
} }
@ -107,7 +91,7 @@ export class KeyExampleFactory {
const cmdsetId = `${config.addonRef}-cmdset`; const cmdsetId = `${config.addonRef}-cmdset`;
const cmdSmallerId = `${config.addonRef}-cmd-smaller`; const cmdSmallerId = `${config.addonRef}-cmd-smaller`;
// Register an event key for Alt+L // Register an event key for Alt+L
ztoolkit.KeyTool.registerKey("event", { ztoolkit.Shortcut.register("event", {
id: `${config.addonRef}-key-larger`, id: `${config.addonRef}-key-larger`,
key: "L", key: "L",
modifiers: "alt", modifiers: "alt",
@ -116,7 +100,7 @@ export class KeyExampleFactory {
}, },
}); });
// Register an element key using <key> for Alt+S // Register an element key using <key> for Alt+S
ztoolkit.KeyTool.registerKey("element", { ztoolkit.Shortcut.register("element", {
id: `${config.addonRef}-key-smaller`, id: `${config.addonRef}-key-smaller`,
key: "S", key: "S",
modifiers: "alt", modifiers: "alt",
@ -135,16 +119,16 @@ export class KeyExampleFactory {
// Here we register an conflict key for Alt+S // Here we register an conflict key for Alt+S
// just to show how the confliction check works. // just to show how the confliction check works.
// This is something you should avoid in your plugin. // This is something you should avoid in your plugin.
ztoolkit.KeyTool.registerKey("event", { ztoolkit.Shortcut.register("event", {
id: `${config.addonRef}-key-smaller-conflict`, id: `${config.addonRef}-key-smaller-conflict`,
key: "S", key: "S",
modifiers: "alt", modifiers: "alt",
callback: (keyOptions) => { callback: (keyOptions) => {
ztoolkit.Compat.getGlobal("alert")("Smaller! This is a conflict key."); ztoolkit.getGlobal("alert")("Smaller! This is a conflict key.");
}, },
}); });
// Register an event key to check confliction // Register an event key to check confliction
ztoolkit.KeyTool.registerKey("event", { ztoolkit.Shortcut.register("event", {
id: `${config.addonRef}-key-check-conflict`, id: `${config.addonRef}-key-check-conflict`,
key: "C", key: "C",
modifiers: "alt", modifiers: "alt",
@ -152,7 +136,7 @@ export class KeyExampleFactory {
addon.hooks.onShortcuts("confliction"); addon.hooks.onShortcuts("confliction");
}, },
}); });
ztoolkit.Tool.createProgressWindow(config.addonName) new ztoolkit.ProgressWindow(config.addonName)
.createLine({ .createLine({
text: "Example Shortcuts: Alt+L/S/C", text: "Example Shortcuts: Alt+L/S/C",
type: "success", type: "success",
@ -162,7 +146,7 @@ export class KeyExampleFactory {
@example @example
static exampleShortcutLargerCallback() { static exampleShortcutLargerCallback() {
ztoolkit.Tool.createProgressWindow(config.addonName) new ztoolkit.ProgressWindow(config.addonName)
.createLine({ .createLine({
text: "Larger!", text: "Larger!",
type: "default", type: "default",
@ -172,7 +156,7 @@ export class KeyExampleFactory {
@example @example
static exampleShortcutSmallerCallback() { static exampleShortcutSmallerCallback() {
ztoolkit.Tool.createProgressWindow(config.addonName) new ztoolkit.ProgressWindow(config.addonName)
.createLine({ .createLine({
text: "Smaller!", text: "Smaller!",
type: "default", type: "default",
@ -182,17 +166,17 @@ export class KeyExampleFactory {
@example @example
static exampleShortcutConflictionCallback() { static exampleShortcutConflictionCallback() {
const conflictionGroups = ztoolkit.KeyTool.checkAllKeyConfliction(); const conflictionGroups = ztoolkit.Shortcut.checkAllKeyConfliction();
ztoolkit.Tool.createProgressWindow("Check Key Confliction") new ztoolkit.ProgressWindow("Check Key Confliction")
.createLine({ .createLine({
text: `${conflictionGroups.length} groups of confliction keys found. Details are in the debug output/console.`, text: `${conflictionGroups.length} groups of confliction keys found. Details are in the debug output/console.`,
}) })
.show(-1); .show(-1);
ztoolkit.Tool.log( ztoolkit.log(
"Conflictions:", "Conflictions:",
conflictionGroups, conflictionGroups,
"All keys:", "All keys:",
ztoolkit.KeyTool.getAllKeys() ztoolkit.Shortcut.getAll()
); );
} }
} }
@ -218,7 +202,7 @@ export class UIExampleFactory {
static registerRightClickMenuItem() { static registerRightClickMenuItem() {
const menuIcon = `chrome://${config.addonRef}/content/icons/favicon@0.5x.png`; const menuIcon = `chrome://${config.addonRef}/content/icons/favicon@0.5x.png`;
// item menuitem with icon // item menuitem with icon
ztoolkit.UI.insertMenuItem("item", { ztoolkit.Menu.register("item", {
tag: "menuitem", tag: "menuitem",
id: "zotero-itemmenu-addontemplate-test", id: "zotero-itemmenu-addontemplate-test",
label: getString("menuitem.label"), label: getString("menuitem.label"),
@ -229,7 +213,7 @@ export class UIExampleFactory {
@example @example
static registerRightClickMenuPopup() { static registerRightClickMenuPopup() {
ztoolkit.UI.insertMenuItem( ztoolkit.Menu.register(
"item", "item",
{ {
tag: "menu", tag: "menu",
@ -251,11 +235,11 @@ export class UIExampleFactory {
@example @example
static registerWindowMenuWithSeprator() { static registerWindowMenuWithSeprator() {
ztoolkit.UI.insertMenuItem("menuFile", { ztoolkit.Menu.register("menuFile", {
tag: "menuseparator", tag: "menuseparator",
}); });
// menu->File menuitem // menu->File menuitem
ztoolkit.UI.insertMenuItem("menuFile", { ztoolkit.Menu.register("menuFile", {
tag: "menuitem", tag: "menuitem",
label: getString("menuitem.filemenulabel"), label: getString("menuitem.filemenulabel"),
oncommand: "alert('Hello World! File Menuitem.')", oncommand: "alert('Hello World! File Menuitem.')",
@ -326,7 +310,7 @@ export class UIExampleFactory {
@example @example
static registerLibraryTabPanel() { static registerLibraryTabPanel() {
const tabId = ztoolkit.UI.registerLibraryTabPanel( const tabId = ztoolkit.LibraryTabPanel.register(
getString("tabpanel.lib.tab.label"), getString("tabpanel.lib.tab.label"),
(panel: XUL.Element, win: Window) => { (panel: XUL.Element, win: Window) => {
const elem = ztoolkit.UI.creatElementsFromJSON(win.document, { const elem = ztoolkit.UI.creatElementsFromJSON(win.document, {
@ -357,7 +341,7 @@ export class UIExampleFactory {
{ {
type: "click", type: "click",
listener: () => { listener: () => {
ztoolkit.UI.unregisterLibraryTabPanel(tabId); ztoolkit.LibraryTabPanel.unregister(tabId);
}, },
}, },
], ],
@ -374,7 +358,7 @@ export class UIExampleFactory {
@example @example
static async registerReaderTabPanel() { static async registerReaderTabPanel() {
const tabId = await ztoolkit.UI.registerReaderTabPanel( const tabId = await ztoolkit.ReaderTabPanel.register(
getString("tabpanel.reader.tab.label"), getString("tabpanel.reader.tab.label"),
( (
panel: XUL.TabPanel | undefined, panel: XUL.TabPanel | undefined,
@ -383,12 +367,12 @@ export class UIExampleFactory {
reader: _ZoteroReaderInstance reader: _ZoteroReaderInstance
) => { ) => {
if (!panel) { if (!panel) {
ztoolkit.Tool.log( ztoolkit.log(
"This reader do not have right-side bar. Adding reader tab skipped." "This reader do not have right-side bar. Adding reader tab skipped."
); );
return; return;
} }
ztoolkit.Tool.log(reader); ztoolkit.log(reader);
const elem = ztoolkit.UI.creatElementsFromJSON(win.document, { const elem = ztoolkit.UI.creatElementsFromJSON(win.document, {
tag: "vbox", tag: "vbox",
id: `${config.addonRef}-${reader._instanceID}-extra-reader-tab-div`, id: `${config.addonRef}-${reader._instanceID}-extra-reader-tab-div`,
@ -435,7 +419,7 @@ export class UIExampleFactory {
{ {
type: "click", type: "click",
listener: () => { listener: () => {
ztoolkit.UI.unregisterReaderTabPanel(tabId); ztoolkit.ReaderTabPanel.unregister(tabId);
}, },
}, },
], ],
@ -449,12 +433,4 @@ export class UIExampleFactory {
} }
); );
} }
@example
static unregisterUIExamples() {
ztoolkit.unregisterAll();
}
}
function initPreferences(win: Window) {
throw new Error("Function not implemented.");
} }

View File

@ -22,7 +22,7 @@ function bindPrefEvents() {
`#zotero-prefpane-${config.addonRef}-enable` `#zotero-prefpane-${config.addonRef}-enable`
) )
?.addEventListener("command", (e) => { ?.addEventListener("command", (e) => {
ztoolkit.Tool.log(e); ztoolkit.log(e);
addon.data.prefs!.window.alert( addon.data.prefs!.window.alert(
`Successfully changed to ${(e.target as XUL.Checkbox).checked}!` `Successfully changed to ${(e.target as XUL.Checkbox).checked}!`
); );
@ -33,7 +33,7 @@ function bindPrefEvents() {
`#zotero-prefpane-${config.addonRef}-input` `#zotero-prefpane-${config.addonRef}-input`
) )
?.addEventListener("change", (e) => { ?.addEventListener("change", (e) => {
ztoolkit.Tool.log(e); ztoolkit.log(e);
addon.data.prefs!.window.alert( addon.data.prefs!.window.alert(
`Successfully changed to ${(e.target as HTMLInputElement).value}!` `Successfully changed to ${(e.target as HTMLInputElement).value}!`
); );