Skip to content

Commit

Permalink
feat: export & import settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Zephyruso committed Dec 3, 2024
1 parent 8ffe0c4 commit 3617a07
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]: '中文',
}
2 changes: 2 additions & 0 deletions src/i18n/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ export default {
auto: '自动',
dots: '点',
bar: '条',
exportSettings: '导出设置',
importSettings: '导入设置',
[LANG.EN_US]: 'English',
[LANG.ZH_CN]: '中文',
}
53 changes: 52 additions & 1 deletion src/views/SettingsPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,26 @@
/>
</select>
</div>
<div v-if="!isSingBox">
<div class="flex items-center gap-2">
<button
v-if="!isSingBox"
:class="twMerge('btn btn-primary btn-sm', isUIUpgrading ? 'animate-pulse' : '')"
@click="handlerClickUpgradeUI"
>
{{ $t('upgradeUI') }}
</button>
<button
class="btn btn-sm"
@click="handlerClickExport"
>
{{ $t('exportSettings') }}
</button>
<button
class="btn btn-sm"
@click="handlerClickImport"
>
{{ $t('importSettings') }}
</button>
</div>
</div>
</div>
Expand Down Expand Up @@ -314,4 +327,42 @@ const themes = [
'nord',
'sunset',
]
const handlerClickExport = () => {
const settings: Record<string, string | null> = {}
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()
}
</script>

0 comments on commit 3617a07

Please sign in to comment.