Skip to content

Commit

Permalink
fix!: mv PluginPrefsMap to _ZoteroTypes.Prefs (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
northword authored Jan 8, 2025
1 parent f978592 commit b0650d5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 50 deletions.
25 changes: 8 additions & 17 deletions docs/src/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,25 +238,16 @@ Relies on the `zotero-types` package to provide type declarations for `Zotero.Pr
You can also use the following helper to get or set preferences while omitting the prefix, simplifying your code.

```ts
const PREFS_PREFIX = "extensions.myPlugin";

/**
* Get preference value.
* Wrapper of `Zotero.Prefs.get`.
* @param key
*/
export function getPref<K extends keyof _PluginPrefsMap>(key: K) {
return Zotero.Prefs.get(`${PREFS_PREFIX}.${key}` as PluginPrefKey<K>, true);
const PREF_PREFIX = "extensions.myPlugin";

type PluginPrefsMap = _ZoteroTypes.Prefs["PluginPrefsMap"];

export function getPref<K extends keyof PluginPrefsMap>(key: K) {
return Zotero.Prefs.get(`${PREF_PREFIX}.${key}`, true) as PluginPrefsMap[K];
}

/**
* Set preference value.
* Wrapper of `Zotero.Prefs.set`.
* @param key
* @param value
*/
export function setPref<K extends keyof _PluginPrefsMap>(key: K, value: PluginPrefsMap[PluginPrefKey<K>]) {
return Zotero.Prefs.set(`${PREFS_PREFIX}.${key}` as PluginPrefKey<K>, value, true);
export function setPref<K extends keyof PluginPrefsMap>(key: K, value: PluginPrefsMap[K]) {
return Zotero.Prefs.set(`${PREF_PREFIX}.${key}`, value, true);
}
```

Expand Down
2 changes: 1 addition & 1 deletion packages/scaffold/src/core/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export default class Build extends Base {

// Generate prefs.d.ts
if (dts) {
const dtsContent = renderPluginPrefsDts(prefsWithoutPrefix, prefix);
const dtsContent = renderPluginPrefsDts(prefsWithoutPrefix);
await outputFile(dts, dtsContent, "utf-8");
}

Expand Down
23 changes: 9 additions & 14 deletions packages/scaffold/src/utils/prefs-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,27 +203,22 @@ describe("renderPluginPrefsDts", () => {
"test.string": "hello",
"test.number": 42,
};
const prefix = "prefix";

const result = `// Generated by zotero-plugin-scaffold
/* prettier-ignore */
/* eslint-disable */
// @ts-nocheck
// prettier-ignore
type _PluginPrefsMap = {
"test.string": string;
"test.number": number;
};
// prettier-ignore
type PluginPrefKey<K extends keyof _PluginPrefsMap> = \`prefix.\${K}\`;
// prettier-ignore
type PluginPrefsMap = {
[K in keyof _PluginPrefsMap as PluginPrefKey<K>]: _PluginPrefsMap[K]
};
declare namespace _ZoteroTypes {
interface Prefs {
PluginPrefsMap: {
"test.string": string;
"test.number": number;
};
}
}
`;
expect(renderPluginPrefsDts(prefs, prefix)).toBe(result);
expect(renderPluginPrefsDts(prefs)).toBe(result);
});
});
38 changes: 20 additions & 18 deletions packages/scaffold/src/utils/prefs-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,35 +107,37 @@ export class PrefsManager {
}
}

export function renderPluginPrefsDts(prefs: Prefs, prefix: string) {
const dtsContent = `// Generated by zotero-plugin-scaffold
export function renderPluginPrefsDts(prefs: Prefs) {
return `// Generated by zotero-plugin-scaffold
/* prettier-ignore */
/* eslint-disable */
// @ts-nocheck
// prettier-ignore
type _PluginPrefsMap = {
${Object.entries(prefs).map(([key, value]) => {
return `"${key}": ${typeof value};`;
}).join("\n ")}
};
// prettier-ignore
type PluginPrefKey<K extends keyof _PluginPrefsMap> = \`${prefix}.\${K}\`;
// prettier-ignore
type PluginPrefsMap = {
[K in keyof _PluginPrefsMap as PluginPrefKey<K>]: _PluginPrefsMap[K]
};
declare namespace _ZoteroTypes {
interface Prefs {
PluginPrefsMap: {
${Object.entries(prefs).map(([key, value]) => {
return `"${key}": ${typeof value};`;
}).join("\n ")}
};
}
}
`;
return dtsContent;
}

const _backup = `
/** Backup */
// // prettier-ignore
// type PluginPrefKey<K extends keyof _PluginPrefsMap> = \`${prefix}.\${K}\`;
//
// // prettier-ignore
// type PluginPrefsMap = {
// [K in keyof _PluginPrefsMap as PluginPrefKey<K>]: _PluginPrefsMap[K]
// };
//
// declare namespace _ZoteroTypes {
// interface Prefs {
// get: <K extends keyof PluginPrefsMap>(key: K, global?: boolean) => PluginPrefsMap[K];
// set: <K extends keyof PluginPrefsMap>(key: K, value: PluginPrefsMap[K], global?: boolean) => any;
// }
// }
`;

0 comments on commit b0650d5

Please sign in to comment.