From 6a986305a40ee353c87c64d4f99b7a19f4a60739 Mon Sep 17 00:00:00 2001 From: xiangyu <3170102889@zju.edu.cn> Date: Fri, 6 Jan 2023 13:51:41 +0800 Subject: [PATCH] update: toolkit 0.1.3 --- package.json | 2 +- src/hooks.ts | 7 +++- src/modules/examples.ts | 3 +- src/modules/popup.ts | 87 ----------------------------------------- 4 files changed, 7 insertions(+), 92 deletions(-) delete mode 100644 src/modules/popup.ts diff --git a/package.json b/package.json index 5393c96..9bd6fcd 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ }, "homepage": "https://github.com/windingwind/zotero-addon-template#readme", "dependencies": { - "zotero-plugin-toolkit": "^0.1.1" + "zotero-plugin-toolkit": "^0.1.3" }, "devDependencies": { "@types/node": "^18.11.17", diff --git a/src/hooks.ts b/src/hooks.ts index 1475f91..3b31537 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -1,5 +1,4 @@ import { BasicExampleFactory, UIExampleFactory } from "./modules/examples"; -import { PopupWindow } from "./modules/popup"; import { config } from "../package.json"; import { getString, initLocale } from "./modules/locale"; import { registerPrefsScripts } from "./modules/preferenceScript"; @@ -11,8 +10,12 @@ async function onStartup() { Zotero.uiReadyPromise, ]); initLocale(); + ztoolkit.Tool.setIconURI( + "default", + `chrome://${config.addonRef}/content/icons/favicon.png` + ); - const popupWin = new PopupWindow(config.addonName, { + const popupWin = ztoolkit.Tool.createProgressWindow(config.addonName, { closeOnClick: true, closeTime: -1, }) diff --git a/src/modules/examples.ts b/src/modules/examples.ts index a90a3be..fd90ab6 100644 --- a/src/modules/examples.ts +++ b/src/modules/examples.ts @@ -1,6 +1,5 @@ import { config } from "../../package.json"; import { getString } from "./locale"; -import { PopupWindow } from "./popup"; function example( target: any, @@ -62,7 +61,7 @@ export class BasicExampleFactory { @example static exampleNotifierCallback() { - new PopupWindow(config.addonName) + ztoolkit.Tool.createProgressWindow(config.addonName) .createLine({ text: "Open Tab Detected!", type: "success", diff --git a/src/modules/popup.ts b/src/modules/popup.ts deleted file mode 100644 index 2c2dd77..0000000 --- a/src/modules/popup.ts +++ /dev/null @@ -1,87 +0,0 @@ -import { config } from "../../package.json"; - -const progressWindowIcon = { - success: "chrome://zotero/skin/tick.png", - fail: "chrome://zotero/skin/cross.png", - default: `chrome://${config.addonRef}/content/icons/favicon.png`, -}; - -interface LineOptions { - type?: keyof typeof progressWindowIcon; - icon?: string; - text?: string; - progress?: number; - idx?: number; -} - -export class PopupWindow extends Zotero.ProgressWindow { - private lines: _ZoteroItemProgress[]; - private closeTime: number | undefined; - private originalShow: typeof Zotero.ProgressWindow.prototype.show; - // @ts-ignore - public show!: typeof this.showWithTimer; - - constructor( - header: string, - options: { - window?: Window; - closeOnClick?: boolean; - closeTime?: number; - } = { - closeOnClick: true, - closeTime: 5000, - } - ) { - super(options); - this.lines = []; - this.closeTime = options.closeTime || 5000; - this.changeHeadline(header); - this.originalShow = this - .show as unknown as typeof Zotero.ProgressWindow.prototype.show; - this.show = this.showWithTimer; - } - - createLine(options: LineOptions) { - const icon = this.getIcon(options.type, options.icon); - const line = new this.ItemProgress(icon || "", options.text || ""); - if (typeof options.progress === "number") { - line.setProgress(options.progress); - } - this.lines.push(line); - return this; - } - - changeLine(options: LineOptions) { - if (this.lines?.length === 0) { - return this; - } - const idx = - typeof options.idx !== "undefined" && - options.idx >= 0 && - options.idx < this.lines.length - ? options.idx - : 0; - const icon = this.getIcon(options.type, options.icon); - options.text && this.lines[idx].setText(options.text); - icon && this.lines[idx].setIcon(icon); - typeof options.progress === "number" && - this.lines[idx].setProgress(options.progress); - return this; - } - - protected showWithTimer(closeTime: number | undefined = undefined) { - this.originalShow(); - typeof closeTime !== "undefined" && (this.closeTime = closeTime); - if (this.closeTime && this.closeTime > 0) { - this.startCloseTimer(this.closeTime); - } - return this; - } - - protected getIcon( - type: keyof typeof progressWindowIcon | undefined, - defaulIcon?: string | undefined - ) { - return type ? progressWindowIcon[type] : defaulIcon; - } -}