chore: load config in addon.data

This commit is contained in:
windingwind 2024-11-29 10:55:23 +01:00
parent deb2cd0e07
commit d842292073
3 changed files with 21 additions and 18 deletions

View File

@ -1,3 +1,4 @@
import { config } from "../package.json";
import { ColumnOptions } from "zotero-plugin-toolkit/dist/helpers/virtualizedTable"; import { ColumnOptions } from "zotero-plugin-toolkit/dist/helpers/virtualizedTable";
import { DialogHelper } from "zotero-plugin-toolkit/dist/helpers/dialog"; import { DialogHelper } from "zotero-plugin-toolkit/dist/helpers/dialog";
import hooks from "./hooks"; import hooks from "./hooks";
@ -6,6 +7,7 @@ import { createZToolkit } from "./utils/ztoolkit";
class Addon { class Addon {
public data: { public data: {
alive: boolean; alive: boolean;
config: typeof config;
// Env type, see build.js // Env type, see build.js
env: "development" | "production"; env: "development" | "production";
ztoolkit: ZToolkit; ztoolkit: ZToolkit;
@ -27,6 +29,7 @@ class Addon {
constructor() { constructor() {
this.data = { this.data = {
alive: true, alive: true,
config,
env: __env__, env: __env__,
ztoolkit: createZToolkit(), ztoolkit: createZToolkit(),
}; };

View File

@ -5,7 +5,6 @@ import {
PromptExampleFactory, PromptExampleFactory,
UIExampleFactory, UIExampleFactory,
} from "./modules/examples"; } from "./modules/examples";
import { config } from "../package.json";
import { getString, initLocale } from "./utils/locale"; import { getString, initLocale } from "./utils/locale";
import { registerPrefsScripts } from "./modules/preferenceScript"; import { registerPrefsScripts } from "./modules/preferenceScript";
import { createZToolkit } from "./utils/ztoolkit"; import { createZToolkit } from "./utils/ztoolkit";
@ -43,9 +42,11 @@ async function onMainWindowLoad(win: Window): Promise<void> {
addon.data.ztoolkit = createZToolkit(); addon.data.ztoolkit = createZToolkit();
// @ts-ignore This is a moz feature // @ts-ignore This is a moz feature
win.MozXULElement.insertFTLIfNeeded(`${config.addonRef}-mainWindow.ftl`); win.MozXULElement.insertFTLIfNeeded(
`${addon.data.config.addonRef}-mainWindow.ftl`,
);
const popupWin = new ztoolkit.ProgressWindow(config.addonName, { const popupWin = new ztoolkit.ProgressWindow(addon.data.config.addonName, {
closeOnClick: true, closeOnClick: true,
closeTime: -1, closeTime: -1,
}) })
@ -97,7 +98,7 @@ function onShutdown(): void {
addon.data.dialog?.window?.close(); addon.data.dialog?.window?.close();
// Remove addon object // Remove addon object
addon.data.alive = false; addon.data.alive = false;
delete Zotero[config.addonInstance]; delete Zotero[addon.data.config.addonInstance];
} }
/** /**

View File

@ -1,4 +1,3 @@
import { config } from "../../package.json";
import { getLocaleID, getString } from "../utils/locale"; import { getLocaleID, getString } from "../utils/locale";
function example( function example(
@ -53,7 +52,7 @@ export class BasicExampleFactory {
@example @example
static exampleNotifierCallback() { static exampleNotifierCallback() {
new ztoolkit.ProgressWindow(config.addonName) new ztoolkit.ProgressWindow(addon.data.config.addonName)
.createLine({ .createLine({
text: "Open Tab Detected!", text: "Open Tab Detected!",
type: "success", type: "success",
@ -70,10 +69,10 @@ export class BasicExampleFactory {
@example @example
static registerPrefs() { static registerPrefs() {
Zotero.PreferencePanes.register({ Zotero.PreferencePanes.register({
pluginID: config.addonID, pluginID: addon.data.config.addonID,
src: rootURI + "content/preferences.xhtml", src: rootURI + "content/preferences.xhtml",
label: getString("prefs-title"), label: getString("prefs-title"),
image: `chrome://${config.addonRef}/content/icons/favicon.png`, image: `chrome://${addon.data.config.addonRef}/content/icons/favicon.png`,
}); });
} }
} }
@ -92,7 +91,7 @@ export class KeyExampleFactory {
} }
}); });
new ztoolkit.ProgressWindow(config.addonName) new ztoolkit.ProgressWindow(addon.data.config.addonName)
.createLine({ .createLine({
text: "Example Shortcuts: Alt+L/S/C", text: "Example Shortcuts: Alt+L/S/C",
type: "success", type: "success",
@ -102,7 +101,7 @@ export class KeyExampleFactory {
@example @example
static exampleShortcutLargerCallback() { static exampleShortcutLargerCallback() {
new ztoolkit.ProgressWindow(config.addonName) new ztoolkit.ProgressWindow(addon.data.config.addonName)
.createLine({ .createLine({
text: "Larger!", text: "Larger!",
type: "default", type: "default",
@ -112,7 +111,7 @@ export class KeyExampleFactory {
@example @example
static exampleShortcutSmallerCallback() { static exampleShortcutSmallerCallback() {
new ztoolkit.ProgressWindow(config.addonName) new ztoolkit.ProgressWindow(addon.data.config.addonName)
.createLine({ .createLine({
text: "Smaller!", text: "Smaller!",
type: "default", type: "default",
@ -129,7 +128,7 @@ export class UIExampleFactory {
properties: { properties: {
type: "text/css", type: "text/css",
rel: "stylesheet", rel: "stylesheet",
href: `chrome://${config.addonRef}/content/zoteroPane.css`, href: `chrome://${addon.data.config.addonRef}/content/zoteroPane.css`,
}, },
}); });
doc.documentElement.appendChild(styles); doc.documentElement.appendChild(styles);
@ -138,7 +137,7 @@ export class UIExampleFactory {
@example @example
static registerRightClickMenuItem() { static registerRightClickMenuItem() {
const menuIcon = `chrome://${config.addonRef}/content/icons/favicon@0.5x.png`; const menuIcon = `chrome://${addon.data.config.addonRef}/content/icons/favicon@0.5x.png`;
// item menuitem with icon // item menuitem with icon
ztoolkit.Menu.register("item", { ztoolkit.Menu.register("item", {
tag: "menuitem", tag: "menuitem",
@ -188,7 +187,7 @@ export class UIExampleFactory {
static async registerExtraColumn() { static async registerExtraColumn() {
const field = "test1"; const field = "test1";
await Zotero.ItemTreeManager.registerColumns({ await Zotero.ItemTreeManager.registerColumns({
pluginID: config.addonID, pluginID: addon.data.config.addonID,
dataKey: field, dataKey: field,
label: "text column", label: "text column",
dataProvider: (item: Zotero.Item, dataKey: string) => { dataProvider: (item: Zotero.Item, dataKey: string) => {
@ -202,7 +201,7 @@ export class UIExampleFactory {
static async registerExtraColumnWithCustomCell() { static async registerExtraColumnWithCustomCell() {
const field = "test2"; const field = "test2";
await Zotero.ItemTreeManager.registerColumns({ await Zotero.ItemTreeManager.registerColumns({
pluginID: config.addonID, pluginID: addon.data.config.addonID,
dataKey: field, dataKey: field,
label: "custom column", label: "custom column",
dataProvider: (item: Zotero.Item, dataKey: string) => { dataProvider: (item: Zotero.Item, dataKey: string) => {
@ -226,7 +225,7 @@ export class UIExampleFactory {
static registerItemPaneSection() { static registerItemPaneSection() {
Zotero.ItemPaneManager.registerSection({ Zotero.ItemPaneManager.registerSection({
paneID: "example", paneID: "example",
pluginID: config.addonID, pluginID: addon.data.config.addonID,
header: { header: {
l10nID: getLocaleID("item-section-example1-head-text"), l10nID: getLocaleID("item-section-example1-head-text"),
icon: "chrome://zotero/skin/16/universal/book.svg", icon: "chrome://zotero/skin/16/universal/book.svg",
@ -249,7 +248,7 @@ export class UIExampleFactory {
static async registerReaderItemPaneSection() { static async registerReaderItemPaneSection() {
Zotero.ItemPaneManager.registerSection({ Zotero.ItemPaneManager.registerSection({
paneID: "reader-example", paneID: "reader-example",
pluginID: config.addonID, pluginID: addon.data.config.addonID,
header: { header: {
l10nID: getLocaleID("item-section-example2-head-text"), l10nID: getLocaleID("item-section-example2-head-text"),
// Optional // Optional
@ -833,7 +832,7 @@ export class HelperExampleFactory {
@example @example
static progressWindowExample() { static progressWindowExample() {
new ztoolkit.ProgressWindow(config.addonName) new ztoolkit.ProgressWindow(addon.data.config.addonName)
.createLine({ .createLine({
text: "ProgressWindow Example!", text: "ProgressWindow Example!",
type: "success", type: "success",