From 7b9e139cb81ced3417cdd08c09806e61b5d80c49 Mon Sep 17 00:00:00 2001 From: xiangyu <3170102889@zju.edu.cn> Date: Sat, 28 Jan 2023 11:08:14 +0800 Subject: [PATCH] change: config.addonInstance --- README.md | 19 +++++++----- addon/bootstrap.js | 2 +- addon/chrome/content/preferences.xhtml | 2 +- package.json | 1 + scripts/build.js | 41 +++++++++++--------------- src/hooks.ts | 2 +- src/index.ts | 4 +-- src/modules/examples.ts | 2 +- 8 files changed, 35 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 25297aa..c9213c9 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ See [`src/modules/preferenceScript.ts`](./src/modules/preferenceScript.ts) ### PromptExamples -An Obsidian-style prompt(popup command input) module. It accepts text command to run callback, with optional display in the popup. +An Obsidian-style prompt(popup command input) module. It accepts text command to run callback, with optional display in the popup. Activate with `Shift+P`. @@ -124,18 +124,21 @@ This is also how your plugin will be released and used by others. - Enter the repo folder; - Modify the settings in `./package.json`, including: -``` +```json +{ version, author, description, homepage, config { - releasepage, - updaterdf, - addonName, - addonID, - addonRef + releasepage, // URL to releases(`.xpi`) + updaterdf, // URL to update.json + addonName, // name to be displayed in the plugin manager + addonID, // ID to avoid confliction. IMPORTANT! + addonRef, // e.g. Element ID prefix + addonInstance // the plugin's root instance: Zotero.${addonInstance} } +} ``` > Be careful to set the addonID and addonRef to avoid confliction. @@ -145,7 +148,7 @@ This is also how your plugin will be released and used by others. > What the difference between dev & prod? > -> - This environment variable is stored in `Zotero.AddonTemplate.data.env`. The outputs to console is disabled in prod mode. +> - This environment variable is stored in `Zotero.${addonInstance}.data.env`. The outputs to console is disabled in prod mode. > - You can decide what users cannot see/use based on this variable. ### Release diff --git a/addon/bootstrap.js b/addon/bootstrap.js index b3ad9e6..8e9546b 100644 --- a/addon/bootstrap.js +++ b/addon/bootstrap.js @@ -106,7 +106,7 @@ function shutdown({ id, version, resourceURI, rootURI }, reason) { Components.interfaces.nsISupports ).wrappedJSObject; } - Zotero.AddonTemplate.hooks.onShutdown(); + Zotero.__addonInstance__.hooks.onShutdown(); Cc["@mozilla.org/intl/stringbundle;1"] .getService(Components.interfaces.nsIStringBundleService) diff --git a/addon/chrome/content/preferences.xhtml b/addon/chrome/content/preferences.xhtml index dac9e4e..9f55711 100644 --- a/addon/chrome/content/preferences.xhtml +++ b/addon/chrome/content/preferences.xhtml @@ -1,6 +1,6 @@ diff --git a/package.json b/package.json index e5b6862..1f4ca32 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "addonName": "Zotero Addon Template", "addonID": "addontemplate@euclpts.com", "addonRef": "addontemplate", + "addonInstance": "AddonTemplate", "releasepage": "https://github.com/windingwind/zotero-addon-template/releases/latest/download/zotero-addon-template.xpi", "updaterdf": "https://raw.githubusercontent.com/windingwind/zotero-addon-template/bootstrap/update.json" }, diff --git a/scripts/build.js b/scripts/build.js index 3e25e04..c13b6f4 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -112,6 +112,21 @@ async function main() { console.log("[Build] Run esbuild OK"); + const replaceFrom = [ + /__author__/g, + /__description__/g, + /__homepage__/g, + /__buildVersion__/g, + /__buildTime__/g, + ]; + + const replaceTo = [author, description, homepage, version, buildTime]; + + replaceFrom.push( + ...Object.keys(config).map((k) => new RegExp(`__${k}__`, "g")) + ); + replaceTo.push(...Object.values(config)); + const optionsAddon = { files: [ path.join(buildDir, "**/*.rdf"), @@ -126,30 +141,8 @@ async function main() { "update.json", "update.rdf", ], - from: [ - /__author__/g, - /__description__/g, - /__homepage__/g, - /__releasepage__/g, - /__updaterdf__/g, - /__addonName__/g, - /__addonID__/g, - /__addonRef__/g, - /__buildVersion__/g, - /__buildTime__/g, - ], - to: [ - author, - description, - homepage, - config.releasepage, - config.updaterdf, - config.addonName, - config.addonID, - config.addonRef, - version, - buildTime, - ], + from: replaceFrom, + to: replaceTo, countMatches: true, }; diff --git a/src/hooks.ts b/src/hooks.ts index 021ce36..a9f8817 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -79,7 +79,7 @@ function onShutdown(): void { ztoolkit.unregisterAll(); // Remove addon object addon.data.alive = false; - delete Zotero.AddonTemplate; + delete Zotero[config.addonInstance]; } /** diff --git a/src/index.ts b/src/index.ts index ba1d1de..84198ad 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ import { config } from "../package.json"; const basicTool = new BasicTool(); -if (!basicTool.getGlobal("Zotero").AddonTemplate) { +if (!basicTool.getGlobal("Zotero")[config.addonInstance]) { // Set global variables _globalThis.Zotero = basicTool.getGlobal("Zotero"); _globalThis.ZoteroPane = basicTool.getGlobal("ZoteroPane"); @@ -17,7 +17,7 @@ if (!basicTool.getGlobal("Zotero").AddonTemplate) { ztoolkit.basicOptions.log.disableConsole = addon.data.env === "production"; ztoolkit.UI.basicOptions.ui.enableElementJSONLog = addon.data.env === "development"; - Zotero.AddonTemplate = addon; + Zotero[config.addonInstance] = addon; // Trigger addon hook for initialization addon.hooks.onStartup(); } diff --git a/src/modules/examples.ts b/src/modules/examples.ts index 7ecebe2..c867183 100644 --- a/src/modules/examples.ts +++ b/src/modules/examples.ts @@ -112,7 +112,7 @@ export class KeyExampleFactory { id: cmdSmallerId, document, _parentId: cmdsetId, - oncommand: "Zotero.AddonTemplate.hooks.onShortcuts('smaller')", + oncommand: `Zotero.${config.addonInstance}.hooks.onShortcuts('smaller')`, }, }, });