From 3617a07124925764fa05d532373bc2ad0dec796c Mon Sep 17 00:00:00 2001 From: Zephyruso <176294927+Zephyruso@users.noreply.github.com> Date: Tue, 3 Dec 2024 13:31:03 +0800 Subject: [PATCH] feat: export & import settings --- src/i18n/en.ts | 2 ++ src/i18n/zh.ts | 2 ++ src/views/SettingsPage.vue | 53 +++++++++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/i18n/en.ts b/src/i18n/en.ts index 148fdfb5..a1c3f228 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -81,6 +81,8 @@ export default { auto: 'Auto', dots: 'Dots', bar: 'Bar', + exportSettings: 'Export Settings', + importSettings: 'Import Settings', [LANG.EN_US]: 'English', [LANG.ZH_CN]: '中文', } diff --git a/src/i18n/zh.ts b/src/i18n/zh.ts index 4e18e890..1e71523f 100644 --- a/src/i18n/zh.ts +++ b/src/i18n/zh.ts @@ -81,6 +81,8 @@ export default { auto: '自动', dots: '点', bar: '条', + exportSettings: '导出设置', + importSettings: '导入设置', [LANG.EN_US]: 'English', [LANG.ZH_CN]: '中文', } diff --git a/src/views/SettingsPage.vue b/src/views/SettingsPage.vue index 51a67470..9cfdee43 100644 --- a/src/views/SettingsPage.vue +++ b/src/views/SettingsPage.vue @@ -39,13 +39,26 @@ /> -
+
+ +
@@ -314,4 +327,42 @@ const themes = [ 'nord', 'sunset', ] + +const handlerClickExport = () => { + const settings: Record = {} + + for (const key in localStorage) { + if (key.startsWith('config/') || key.startsWith('setup/')) { + settings[key] = localStorage.getItem(key) + } + } + + const blob = new Blob([JSON.stringify(settings, null, 2)], { type: 'text/plain' }) + const url = URL.createObjectURL(blob) + const a = document.createElement('a') + a.href = url + a.download = 'zashboard-settings.json' + a.click() + URL.revokeObjectURL(url) +} + +const handlerClickImport = () => { + const input = document.createElement('input') + input.type = 'file' + input.accept = '.json' + input.onchange = async () => { + const file = input.files?.[0] + if (!file) return + const reader = new FileReader() + reader.onload = async () => { + const settings = JSON.parse(reader.result as string) + for (const key in settings) { + localStorage.setItem(key, settings[key]) + } + location.reload() + } + reader.readAsText(file) + } + input.click() +}