2023-07-11 18:06:44 +08:00
|
|
|
import { defineStore } from 'pinia'
|
|
|
|
import { lang } from '../langs/index.js'
|
2023-07-12 11:48:08 +08:00
|
|
|
import { camelCase, clone, find, isEmpty, isObject, map, set, snakeCase, split } from 'lodash'
|
|
|
|
import {
|
|
|
|
GetFontList,
|
|
|
|
GetPreferences,
|
|
|
|
RestorePreferences,
|
|
|
|
SetPreferences,
|
|
|
|
} from '../../wailsjs/go/services/preferencesService.js'
|
|
|
|
import { useI18n } from 'vue-i18n'
|
2023-07-11 18:06:44 +08:00
|
|
|
|
|
|
|
const usePreferencesStore = defineStore('preferences', {
|
2023-07-12 11:48:08 +08:00
|
|
|
/**
|
|
|
|
* @typedef {Object} FontItem
|
|
|
|
* @property {string} name
|
|
|
|
* @property {string} path
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @typedef {Object} Preferences
|
|
|
|
* @property {Object} general
|
|
|
|
* @property {Object} editor
|
|
|
|
* @property {FontItem[]} fontList
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @returns {Preferences}
|
|
|
|
*/
|
2023-07-11 18:06:44 +08:00
|
|
|
state: () => ({
|
|
|
|
general: {
|
|
|
|
language: 'en',
|
|
|
|
font: '',
|
|
|
|
fontSize: 14,
|
|
|
|
useSysProxy: false,
|
|
|
|
useSysProxyHttp: false,
|
|
|
|
checkUpdate: false,
|
|
|
|
},
|
|
|
|
editor: {
|
|
|
|
font: '',
|
|
|
|
fontSize: 14,
|
|
|
|
},
|
2023-07-12 11:48:08 +08:00
|
|
|
lastPref: {},
|
|
|
|
fontList: [],
|
2023-07-11 18:06:44 +08:00
|
|
|
}),
|
|
|
|
getters: {
|
|
|
|
getSeparator() {
|
|
|
|
return ':'
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* all available language
|
|
|
|
* @returns {{label: string, value: string}[]}
|
|
|
|
*/
|
|
|
|
langOption() {
|
|
|
|
return Object.entries(lang).map(([key, value]) => ({
|
|
|
|
value: key,
|
|
|
|
label: `${value['lang_name']}`,
|
|
|
|
}))
|
|
|
|
},
|
2023-07-12 11:48:08 +08:00
|
|
|
|
|
|
|
fontOption() {
|
|
|
|
const i18n = useI18n()
|
|
|
|
const option = map(this.fontList, (font) => ({
|
|
|
|
value: font.name,
|
|
|
|
label: font.name,
|
|
|
|
path: font.path,
|
|
|
|
}))
|
|
|
|
option.splice(0, 0, {
|
|
|
|
value: '',
|
|
|
|
label: i18n.t('none'),
|
|
|
|
path: '',
|
|
|
|
})
|
|
|
|
return option
|
|
|
|
},
|
|
|
|
|
|
|
|
generalFont() {
|
|
|
|
const fontStyle = {
|
|
|
|
fontSize: this.general.fontSize + 'px',
|
|
|
|
}
|
|
|
|
if (!isEmpty(this.general.font) && this.general.font !== 'none') {
|
|
|
|
const font = find(this.fontList, { name: this.general.font })
|
|
|
|
if (font != null) {
|
|
|
|
fontStyle['fontFamily'] = `${font.name}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fontStyle
|
|
|
|
},
|
2023-07-11 18:06:44 +08:00
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
_applyPreferences(data) {
|
|
|
|
for (const key in data) {
|
|
|
|
const keys = map(split(key, '.'), camelCase)
|
|
|
|
set(this, keys, data[key])
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* load preferences from local
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async loadPreferences() {
|
|
|
|
const { success, data } = await GetPreferences()
|
|
|
|
if (success) {
|
2023-07-12 11:48:08 +08:00
|
|
|
this.lastPref = clone(data)
|
2023-07-11 18:06:44 +08:00
|
|
|
this._applyPreferences(data)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-07-12 11:48:08 +08:00
|
|
|
/**
|
|
|
|
* load system font list
|
|
|
|
* @returns {Promise<string[]>}
|
|
|
|
*/
|
|
|
|
async loadFontList() {
|
|
|
|
const { success, data } = await GetFontList()
|
|
|
|
if (success) {
|
|
|
|
const { fonts = [] } = data
|
|
|
|
this.fontList = fonts
|
|
|
|
} else {
|
|
|
|
this.fontList = []
|
|
|
|
}
|
|
|
|
return this.fontList
|
|
|
|
},
|
|
|
|
|
2023-07-11 18:06:44 +08:00
|
|
|
/**
|
|
|
|
* save preferences to local
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
async savePreferences() {
|
|
|
|
const obj2Map = (prefix, obj) => {
|
|
|
|
const result = {}
|
|
|
|
for (const key in obj) {
|
|
|
|
if (isObject(obj[key])) {
|
2023-07-12 11:48:08 +08:00
|
|
|
const subResult = obj2Map(`${prefix}.${snakeCase(key)}`, obj[key])
|
|
|
|
Object.assign(result, subResult)
|
2023-07-11 18:06:44 +08:00
|
|
|
} else {
|
|
|
|
result[`${prefix}.${snakeCase(key)}`] = obj[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
const pf = Object.assign({}, obj2Map('general', this.general), obj2Map('editor', this.editor))
|
|
|
|
const { success, msg } = await SetPreferences(pf)
|
|
|
|
return success === true
|
|
|
|
},
|
|
|
|
|
2023-07-12 11:48:08 +08:00
|
|
|
/**
|
|
|
|
* reset to last loaded preferences
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async resetToLastPreferences() {
|
|
|
|
if (!isEmpty(this.lastPref)) {
|
|
|
|
this._applyPreferences(this.lastPref)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-07-11 18:06:44 +08:00
|
|
|
/**
|
|
|
|
* restore preferences to default
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
async restorePreferences() {
|
|
|
|
const { success, data } = await RestorePreferences()
|
|
|
|
if (success === true) {
|
|
|
|
const { pref } = data
|
|
|
|
this._applyPreferences(pref)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
export default usePreferencesStore
|