Skip to content

Commit 06e9b77

Browse files
authored
feat: Add Stored Key Manage (#1165)
* feat: allow user import and export keys in json * fix: EOF newline * lint
1 parent e9b3790 commit 06e9b77

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

addon/chrome/content/preferences.xhtml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,13 @@
256256
preference="__prefsPrefix__.splitChar"
257257
></html:input>
258258
</hbox>
259+
<hbox align="center">
260+
<label data-l10n-id="pref-service-manageKeys-hint"></label>
261+
<button
262+
id="__addonRef__-manageKeys"
263+
data-l10n-id="pref-service-manageKeys"
264+
></button>
265+
</hbox>
259266
</groupbox>
260267
<groupbox>
261268
<label><html:h2 data-l10n-id="pref-about"></html:h2></label>

addon/locale/en-US/preferences.ftl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ pref-service-langto =
4343
pref-service-hint =
4444
.value = Translation services 🗝️ requires a secret key; see GitHub for details
4545
46+
pref-service-manageKeys =
47+
.label = Manage Secret Keys
48+
pref-service-manageKeys-hint =
49+
.value = Display saved secret keys for bulk import and export
50+
4651
pref-interface = User Interface
4752
4853
pref-interface-fontSize =

addon/locale/zh-CN/preferences.ftl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,8 @@ pref-about-docs =
103103
.value = 文档 (中文)
104104
pref-about-version =
105105
.value = { $name } 版本 { $version } Build { $time }
106+
107+
pref-service-manageKeys =
108+
.label = 密钥管理
109+
pref-service-manageKeys-hint =
110+
.value = 展示已保存的密钥以便批量导入和导出

src/modules/preferenceWindow.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ function buildPrefsPane() {
167167
doc.querySelector(`#${makeId("langto-placeholder")}`)!,
168168
);
169169

170+
doc
171+
.querySelector(`#${makeId("manageKeys")}`)
172+
?.addEventListener("command", (e: Event) => {
173+
onPrefsEvents("manageKeys");
174+
});
175+
170176
doc
171177
.querySelector(`#${makeId("enableAuto")}`)
172178
?.addEventListener("command", (e: Event) => {
@@ -436,6 +442,15 @@ function onPrefsEvents(type: string, fromElement: boolean = true) {
436442
case "updatelineHeight":
437443
addon.api.getTemporaryRefreshHandler()();
438444
break;
445+
case "manageKeys":
446+
{
447+
import("../modules/settings/manageKeys").then(
448+
({ manageKeysDialog }) => {
449+
manageKeysDialog();
450+
},
451+
);
452+
}
453+
break;
439454
default:
440455
return;
441456
}

src/modules/settings/manageKeys.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { getPrefJSON, setPref } from "../../utils/prefs";
2+
3+
export async function manageKeysDialog() {
4+
const dialog = new ztoolkit.Dialog(2, 1);
5+
const secrets = getPrefJSON("secretObj");
6+
7+
const dialogData: {
8+
secrets: string;
9+
updateSuccess: boolean;
10+
unloadLock?: any;
11+
_lastButtonId?: string;
12+
} = {
13+
secrets: JSON.stringify(secrets, null, 2),
14+
updateSuccess: false,
15+
};
16+
17+
dialog
18+
.setDialogData(dialogData)
19+
.addCell(
20+
0,
21+
0,
22+
{
23+
tag: "div",
24+
namespace: "html",
25+
styles: {
26+
display: "flex",
27+
flexDirection: "column",
28+
gap: "10px",
29+
width: "100%",
30+
height: "100%",
31+
},
32+
children: [
33+
{
34+
tag: "label",
35+
namespace: "html",
36+
properties: {
37+
innerHTML:
38+
"Manage all translation service keys. Edit the JSON directly and click Save.",
39+
},
40+
styles: {
41+
marginBottom: "5px",
42+
},
43+
},
44+
{
45+
tag: "textarea",
46+
namespace: "html",
47+
attributes: {
48+
"data-bind": "secrets",
49+
"data-prop": "value",
50+
rows: "25",
51+
cols: "70",
52+
},
53+
styles: {
54+
fontFamily: "monospace",
55+
whiteSpace: "pre",
56+
overflowX: "auto",
57+
padding: "8px",
58+
},
59+
},
60+
],
61+
},
62+
false,
63+
)
64+
.addButton("Save", "save")
65+
.addButton("Cancel", "cancel");
66+
67+
dialog.open("Manage Translation Service Keys");
68+
69+
if (dialogData.unloadLock && dialogData.unloadLock.promise) {
70+
try {
71+
await dialogData.unloadLock.promise;
72+
} catch (error) {
73+
console.error("Error waiting for dialog to close:", error);
74+
}
75+
}
76+
77+
if (dialogData._lastButtonId === "save") {
78+
try {
79+
const parsedSecrets = JSON.parse(dialogData.secrets);
80+
setPref("secretObj", JSON.stringify(parsedSecrets));
81+
dialogData.updateSuccess = true;
82+
83+
// Refresh UI if needed
84+
addon.hooks.onReaderTabPanelRefresh();
85+
} catch (e) {
86+
if (dialog.window) {
87+
Zotero.alert(
88+
dialog.window,
89+
"Error",
90+
`Failed to save keys: ${e instanceof Error ? e.message : String(e)}`,
91+
);
92+
}
93+
}
94+
}
95+
96+
return dialogData.updateSuccess;
97+
}

0 commit comments

Comments
 (0)