mirror of
https://github.com/purocean/yn.git
synced 2026-10-13 07:38:54 +08:00
refactor: change setting
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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 })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Vendored
+1
@@ -30,6 +30,7 @@ declare interface Window {
|
||||
kityminder: any;
|
||||
kityminderM: any;
|
||||
kityM: any;
|
||||
_INIT_SETTINGS: any;
|
||||
}
|
||||
|
||||
declare const __APP_VERSION__: string
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user