Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): support export & import settings JSON #499

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/client/src/composables/state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { RemovableRef } from '@vueuse/core'
import { showVueNotification } from '@vue/devtools-ui'
import type { GraphSettings } from './graph'
import type { TabSettings } from './state-tab'
import { downloadFile, readFileAsText } from '~/utils'

interface DevtoolsClientState {
isFirstVisit: boolean
Expand Down Expand Up @@ -65,4 +67,48 @@ watch(() => devtoolsClientState.value.splitScreen.enabled, (enabled, o) => {
devtoolsClientState.value.splitScreen.size = [50, 50]
}
})

const DEVTOOLS_STATE_KEY = '__VUE_DEVTOOLS_CLIENT_STATE__'

export function useExportDevtoolsClientState() {
return {
exportDevtoolsClientState: () => {
const blob = new Blob([
JSON.stringify({ [DEVTOOLS_STATE_KEY]: devtoolsClientState.value }, null, 2),
], { type: 'application/json' })
downloadFile(blob, 'vue-devtools-client-state.json')
},
}
}

export function useImportDevtoolsClientState() {
const { open, onChange } = useFileDialog({ accept: '.json', multiple: false })

onChange((fileList) => {
const jsonFile = fileList?.[0]
if (!jsonFile)
return
readFileAsText(jsonFile)
.then((file) => {
const data = JSON.parse(file as string)[DEVTOOLS_STATE_KEY]
if (!data)
throw new Error('Invalid file')
devtoolsClientState.value = data
showVueNotification({
message: 'Import successful',
type: 'success',
})
})
.catch(() => {
showVueNotification({
type: 'error',
message: 'Invalid file',
})
})
})

return {
openImportDialog: open,
}
}
// #endregion
17 changes: 17 additions & 0 deletions packages/client/src/pages/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ const minimizePanelInteractiveLabel = computed(() => {
const option = minimizePanelInteractiveOptions.find(i => i.value === minimizePanelInteractive.value)
return `${option?.label ?? 'Select...'}`
})

const { openImportDialog } = useImportDevtoolsClientState()
const { exportDevtoolsClientState } = useExportDevtoolsClientState()
</script>

<template>
Expand Down Expand Up @@ -213,6 +216,20 @@ const minimizePanelInteractiveLabel = computed(() => {
</VueCard>
</template>

<h3 mt2 text-lg>
Data
</h3>
<div flex="~ gap-2 wrap">
<VueButton outlined @click="() => exportDevtoolsClientState()">
<div i-ph-export />
Export Settings
</VueButton>
<VueButton outlined @click="() => openImportDialog()">
<div i-ph-download-simple />
Import Settings
</VueButton>
</div>

<h3 mt2 text-lg>
Debug
</h3>
Expand Down
17 changes: 17 additions & 0 deletions packages/client/src/utils/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function readFileAsText(file: Blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = () => resolve(reader.result)
reader.onerror = () => reject(new Error('Failed to read file'))
reader.readAsText(file)
})
}

export function downloadFile(content: Blob, filename: string) {
const url = URL.createObjectURL(content)
const a = document.createElement('a')
a.href = url
a.download = filename
a.click()
URL.revokeObjectURL(url)
}
1 change: 1 addition & 0 deletions packages/client/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './color'
export * from './time'
export * from './file'