From b2cf34c8652e78e1ba244c4f26b1350872156e8e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=A2=96=E9=80=B8?=
<49649786+Zuoqiu-Yingyi@users.noreply.github.com>
Date: Sun, 22 Oct 2023 17:29:14 +0800
Subject: [PATCH] =?UTF-8?q?feat(keepass):=20=E6=B7=BB=E5=8A=A0=E8=AE=BE?=
=?UTF-8?q?=E7=BD=AE=E9=A1=B9=20`=E5=88=A0=E9=99=A4=20KeeWeb=20=E7=94=A8?=
=?UTF-8?q?=E6=88=B7=E9=85=8D=E7=BD=AE`=20|=20Add=20setting=20item=20`Dele?=
=?UTF-8?q?te=20KeeWeb=20User=20Configuration`.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
public/README.md | 8 ++++++-
public/README_zh_CN.md | 8 ++++++-
public/i18n/en_US.json | 7 ++++++-
public/i18n/zh_CHT.json | 7 ++++++-
public/i18n/zh_CN.json | 7 ++++++-
src/components/Settings.svelte | 26 ++++++++++++++++++++++-
src/index.ts | 38 +++++++++++++++++++++++++++++++---
src/keeweb/locale.ts | 3 +--
8 files changed, 93 insertions(+), 11 deletions(-)
diff --git a/public/README.md b/public/README.md
index d438df7..59363f2 100644
--- a/public/README.md
+++ b/public/README.md
@@ -111,8 +111,14 @@ This is a plugin for [SiYuan](https://github.com/siyuan-note/siyuan), developed
* `Reset Settings`
- * Resets all settings options to their default values.
+ * This is a button.
+ * Resets all settings options of this plugin to their default values.
* Refreshes the page after resetting.
+ * `Delete KeeWeb User Configuration`
+
+ * This is a button.
+ * Delete all KeeWeb user configurations.
+ * If the KeeWeb app does not load properly, you can use this button to initialize the Keeweb app.
* `KeeWeb Settings`
* `Plugins`
diff --git a/public/README_zh_CN.md b/public/README_zh_CN.md
index ee062b8..3c6ffaa 100644
--- a/public/README_zh_CN.md
+++ b/public/README_zh_CN.md
@@ -111,8 +111,14 @@
* `重置设置选项`
- * 重置所有设置选项为默认选项
+ * 这是一个按钮
+ * 重置本插件所有设置选项为默认选项
* 重置后将刷新页面
+ * `删除 KeeWeb 用户配置`
+
+ * 这是一个按钮
+ * 刪除所有 KeeWeb 用户配置
+ * 若 KeeWeb 应用无法正常加载, 可使用该按钮初始化 KeeWeb 应用
* `KeeWeb 设置`
* `插件设置`
diff --git a/public/i18n/en_US.json b/public/i18n/en_US.json
index f27ed94..0d377ef 100644
--- a/public/i18n/en_US.json
+++ b/public/i18n/en_US.json
@@ -15,10 +15,15 @@
"settings": {
"generalSettings": {
"reset": {
- "description": "Reset all settings options to default (page will refresh after reset)",
+ "description": "Reset all settings options of this plugin to default (page will refresh after reset)",
"text": "Reset",
"title": "Reset Settings"
},
+ "deleteKeeWebConfig": {
+ "description": "Delete all KeeWeb user configurations (initialize KeeWeb app)",
+ "text": "Deletem",
+ "title": "Delete KeeWeb User Configuration"
+ },
"title": "General Settings"
},
"keewebSettings": {
diff --git a/public/i18n/zh_CHT.json b/public/i18n/zh_CHT.json
index c8937c7..b34c448 100644
--- a/public/i18n/zh_CHT.json
+++ b/public/i18n/zh_CHT.json
@@ -15,10 +15,15 @@
"settings": {
"generalSettings": {
"reset": {
- "description": "重置所有設置選項為默認選項(重置後將刷新頁面)",
+ "description": "重置本插件所有設置選項為默認選項(重置後將刷新頁面)",
"text": "重置",
"title": "重置設置選項"
},
+ "deleteKeeWebConfig": {
+ "description": "刪除所有 KeeWeb 用戶配置(初始化 KeeWeb 應用)",
+ "text": "刪除",
+ "title": "刪除 KeeWeb 用戶配置"
+ },
"title": "常規設置"
},
"keewebSettings": {
diff --git a/public/i18n/zh_CN.json b/public/i18n/zh_CN.json
index 837a81d..150479b 100644
--- a/public/i18n/zh_CN.json
+++ b/public/i18n/zh_CN.json
@@ -15,10 +15,15 @@
"settings": {
"generalSettings": {
"reset": {
- "description": "重置所有设置选项为默认选项(重置后将刷新页面)",
+ "description": "重置本插件所有设置选项为默认选项(重置后将刷新页面)",
"text": "重置",
"title": "重置设置选项"
},
+ "deleteKeeWebConfig": {
+ "description": "刪除所有 KeeWeb 用户配置(初始化 KeeWeb 应用)",
+ "text": "删除",
+ "title": "删除 KeeWeb 用户配置"
+ },
"title": "常规设置"
},
"keewebSettings": {
diff --git a/src/components/Settings.svelte b/src/components/Settings.svelte
index 1f70cfa..b554d97 100644
--- a/src/components/Settings.svelte
+++ b/src/components/Settings.svelte
@@ -52,6 +52,16 @@
);
}
+ function deleteKeeWebConfig() {
+ plugin.siyuan.confirm(
+ i18n.settings.generalSettings.deleteKeeWebConfig.title, // 标题
+ i18n.settings.generalSettings.deleteKeeWebConfig.description, // 文本
+ async () => {
+ await plugin.deleteKeeWebUserConfig(); // 删除 KeeWeb 用户配置
+ }, // 确认按钮回调
+ );
+ }
+
enum PanelKey {
general, // 常规设置
keeweb, // KeeWeb 设置
@@ -112,11 +122,25 @@
+
+
+ -
+
+
diff --git a/src/index.ts b/src/index.ts
index a1d0220..5118106 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -371,6 +371,27 @@ export default class KeepassPlugin extends siyuan.Plugin {
return this.saveData(KeepassPlugin.GLOBAL_CONFIG_NAME, JSON.stringify(this.config, undefined, 4));
}
+ /* 删除 KeeWeb 用户配置 */
+ public async deleteKeeWebUserConfig(): Promise {
+ /* 删除 localStorage 相关的内容 */
+ this.removeLocalStorageItems();
+
+ await Promise.allSettled([
+ /* 删除 indexedDB 文件 */
+ this.idb.FilesCache.clear(KeepassPlugin.IDB_SCHEMA.FilesCache.stores.files.name),
+ this.idb.PluginFiles.clear(KeepassPlugin.IDB_SCHEMA.PluginFiles.stores.files.name),
+ ]);
+
+ /* 同步删除插件对应的数据 */
+ await Promise.allSettled([
+ this.saveLocalStorage(),
+ this.saveIDB(),
+ ]);
+
+ /* 更新 KeeWeb 插件状态 */
+ await this.updateKeeWebPluginStatus();
+ }
+
/* 更新 keeweb 插件状态 */
public async updateKeeWebPluginStatus(): Promise {
switch (true) {
@@ -476,7 +497,6 @@ export default class KeepassPlugin extends siyuan.Plugin {
/**
* 获取 KeeWeb 相关的 localStorage 项
- * @param prefix 键名前缀
*/
public getLocalStorageItems(): TLocal {
this.local = {};
@@ -488,6 +508,17 @@ export default class KeepassPlugin extends siyuan.Plugin {
return this.local;
}
+ /**
+ * 删除 KeeWeb 相关的 localStorage 项
+ */
+ public removeLocalStorageItems(): void {
+ for (const key of Object.keys(globalThis.localStorage)) {
+ if (this.isLocalStorageKey(key)) {
+ globalThis.localStorage.removeItem(key);
+ }
+ }
+ }
+
/**
* 加载 indexedDB
*/
@@ -525,8 +556,9 @@ export default class KeepassPlugin extends siyuan.Plugin {
}
/**
- * 保存 indexedDB
- * @param names 需要保存的数据库名, 默认保存所有数据库
+ * 保存 indexedDB 中 KeeWeb 相关的数据
+ * 完整同步 `indexedDB` -> `data/storage/petal/keepass/idb`
+ * @param names 需要保存的数据库名列表, 默认保存所有数据库
*/
public async saveIDB(...names: TDBDatabaseName[]) {
const db_name_set = new Set(names);
diff --git a/src/keeweb/locale.ts b/src/keeweb/locale.ts
index 2860a1f..bc55bb0 100644
--- a/src/keeweb/locale.ts
+++ b/src/keeweb/locale.ts
@@ -36,8 +36,7 @@ export function install(context: IContext) {
// this._logger.debug("plugin:siyuan:locale-install");
const lang = mapLang(
- getLocalStorage(LocalStorageKey.app_settings)?.locale
- ?? globalThis.navigator.language
+ getLocalStorage(LocalStorageKey.app_settings)?.locale ?? "en"
);
var i18n: I18N;