refactor: change setting

This commit is contained in:
purocean
2021-12-12 00:07:41 +08:00
parent 6682f2ac2f
commit f24ea9b07d
5 changed files with 35 additions and 20 deletions
+6 -1
View File
@@ -208,7 +208,12 @@ const userPlugin = async (ctx: any, next: any) => {
const setting = async (ctx: any, next: any) => {
if (ctx.path.startsWith('/api/settings')) {
if (ctx.method === 'GET') {
ctx.body = result('ok', 'success', config.getAll())
if (ctx.path.endsWith('js')) {
ctx.type = 'application/javascript; charset=utf-8'
ctx.body = '_INIT_SETTINGS = ' + JSON.stringify(config.getAll())
} else {
ctx.body = result('ok', 'success', config.getAll())
}
} else if (ctx.method === 'POST') {
const data = { ...config.getAll(), ...ctx.request.body }
config.setAll(data)
+1
View File
@@ -19,6 +19,7 @@
// 设置初始主题
window.document.documentElement.setAttribute('app-theme', JSON.parse(localStorage['app.theme'] || '"system"'))
</script>
<script src="/api/settings/js"></script>
</head>
<body>
<div id="app"></div>
+26 -18
View File
@@ -99,7 +99,10 @@ const schema: Schema = {
required: ['theme', 'language'],
}
const settings = getDefaultSetting()
const settings = {
...getDefaultSetting(),
...transformSettings(window._INIT_SETTINGS)
}
if (FLAG_DISABLE_XTERM) {
delete (schema.properties as any).shell
@@ -181,24 +184,31 @@ export async function fetchSettings () {
/**
* Write settings.
* @param data settings
* @param settings
* @returns settings
*/
export async function writeSettings (data: Record<string, any>) {
const repositories: any = {}
data.repos.forEach(({ name, path }: any) => {
name = name.trim()
path = path.trim()
if (name && path) {
repositories[name] = path
}
})
export async function writeSettings (settings: Record<string, any>) {
const data = cloneDeep(settings)
delete data.repos
delete data.theme
data.repositories = repositories
if (data.repos) {
const repositories: any = {}
data.repos.forEach(({ name, path }: any) => {
name = name.trim()
path = path.trim()
if (name && path) {
repositories[name] = path
}
})
triggerHook('SETTING_BEFORE_WRITE', { settings })
delete data.repos
data.repositories = repositories
}
if (data.theme) {
delete data.theme
}
triggerHook('SETTING_BEFORE_WRITE', { settings } as any)
await api.writeSettings(data)
return await fetchSettings()
@@ -236,9 +246,7 @@ export function getSetting<T extends keyof BuildInSettings> (key: T, defaultVal:
* @returns
*/
export async function setSetting<T extends keyof BuildInSettings> (key: T, val: BuildInSettings[T]) {
const settings = getSettings()
settings[key] = val
await writeSettings(settings)
await writeSettings({ [key]: val })
}
/**
+1
View File
@@ -30,6 +30,7 @@ declare interface Window {
kityminder: any;
kityminderM: any;
kityM: any;
_INIT_SETTINGS: any;
}
declare const __APP_VERSION__: string
+1 -1
View File
@@ -91,7 +91,7 @@ const doc = getLastOpenFile()
switchDoc(doc)
function changeLanguage ({ settings }: { settings: BuildInSettings }) {
if (settings.language !== getLanguage()) {
if (settings.language && settings.language !== getLanguage()) {
setLanguage(settings.language)
}
}