From d1d615755c241247a160bfff467e7d351c29f09e Mon Sep 17 00:00:00 2001 From: Blore <1137480882@qq.com> Date: Sun, 30 Apr 2023 22:37:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(main-preferences):=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=81=8F=E5=A5=BD=E8=AE=BE=E7=BD=AE=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/background.js | 1 + src/main/environment/appPaths.js | 19 ++++++++ src/main/filesystem/database.js | 3 +- src/main/helper/container.js | 9 ++++ src/main/preferences/index.js | 76 ++++++++++++++++++++++++++++++++ src/main/preferences/schema.json | 13 ++++++ static/preference.json | 4 ++ 7 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 src/main/environment/appPaths.js create mode 100644 src/main/helper/container.js create mode 100644 src/main/preferences/index.js create mode 100644 src/main/preferences/schema.json create mode 100644 static/preference.json diff --git a/src/background.js b/src/background.js index 835e6091..f8ae0e3f 100644 --- a/src/background.js +++ b/src/background.js @@ -25,6 +25,7 @@ import EAU from './main/update' import path from 'path' import * as url from 'url' import { isOsx, isWindows } from './main/config' +import Preference from './main/preferences' const isDevelopment = process.env.NODE_ENV !== 'production' let ficusPath = '' diff --git a/src/main/environment/appPaths.js b/src/main/environment/appPaths.js new file mode 100644 index 00000000..0238bbe1 --- /dev/null +++ b/src/main/environment/appPaths.js @@ -0,0 +1,19 @@ +import { app } from 'electron' + +class AppPaths { + constructor (userDataPath = '') { + if (!userDataPath) { + // Use default user data path. + userDataPath = app.getPath('userData') + } + + this._userDataPath = userDataPath + app.setPath('userData', this._userDataPath) + } + + get userDataPath () { + return this._userDataPath + } +} + +export default AppPaths diff --git a/src/main/filesystem/database.js b/src/main/filesystem/database.js index 81172f3a..1ec74d24 100644 --- a/src/main/filesystem/database.js +++ b/src/main/filesystem/database.js @@ -56,8 +56,9 @@ exports.initFromFolder = async () => { return { relation: {}, error: -2 } } console.log(result.filePaths[0]) + accessor.menu.addRecentlyUsedDocument(result.filePaths[0]) + const folderName = path.basename(result.filePaths[0]) - accessor.menu.addRecentlyUsedDocument(folderName) const tree = await getProject(result.filePaths[0]) const projectStat = { version: 1, diff --git a/src/main/helper/container.js b/src/main/helper/container.js new file mode 100644 index 00000000..a80481c1 --- /dev/null +++ b/src/main/helper/container.js @@ -0,0 +1,9 @@ +function hasSameKey (a, b) { + const aKeys = Object.keys(a).sort() + const bKeys = Object.keys(b).sort() + return JSON.stringify(aKeys) === JSON.stringify(bKeys) +} + +export { + hasSameKey +} diff --git a/src/main/preferences/index.js b/src/main/preferences/index.js new file mode 100644 index 00000000..084154f2 --- /dev/null +++ b/src/main/preferences/index.js @@ -0,0 +1,76 @@ +import fs from 'fs-extra' +import path from 'path' +import Store from 'electron-store' +import schema from './schema.json' +import { hasSameKey } from '../helper/container' + +/** + * 用户偏好设置 + * 实现参考:https://github.com/marktext/marktext/blob/develop/src/main/preferences + */ +class Preference { + constructor (preferencePath) { + this._preferencePath = path.resolve(preferencePath, 'preferences.json') + this._defaultPreferencePath = path.resolve(__dirname, '..', 'static', 'preferences.json') + // 注:electron-store有性能问题(IO),但支持JSON scheme验证 + this._store = new Store({ + schema, + name: 'preferences' + }) + } + + init () { + let defaultSettings = null + try { + defaultSettings = JSON.parse(fs.readFileSync(this.staticPath, { encoding: 'utf8' }) || '{}') + } catch (err) { + console.log(err) + } + + if (!defaultSettings) { + throw new Error('Can not load static preference.json file') + } + + if (!fs.existsSync(this._preferencePath)) { + this.store.set(defaultSettings) + } else { + const userSetting = this.getAll() + const requiresUpdate = !hasSameKey(defaultSettings, userSetting) + if (requiresUpdate) { + // Remove outdated settings + for (const key of userSetting.keys()) { + if (!defaultSettings.keys().includes(key)) { + delete userSetting[key] + this.store.delete(key) + } + } + + // Add new setting options + let addedNewEntries = false + for (const key in defaultSettings) { + if (!userSetting.keys().includes(key)) { + addedNewEntries = true + userSetting[key] = defaultSettings[key] + } + } + if (addedNewEntries) { + this.store.set(userSetting) + } + } + } + } + + setItem (key, value) { + return this._store.set(key, value) + } + + getItem (key) { + return this._store.get(key) + } + + getAll () { + return this._store.store + } +} + +export default Preference diff --git a/src/main/preferences/schema.json b/src/main/preferences/schema.json new file mode 100644 index 00000000..27d79ad9 --- /dev/null +++ b/src/main/preferences/schema.json @@ -0,0 +1,13 @@ +{ + "autoMaximize": { + "description": "General--Auto maxmize window when start", + "type": "boolean", + "default": false + }, + "showTrafficLights": { + "description": "General--Show traffic lights on MacOS", + "type": "boolean", + "default": false + } + } + \ No newline at end of file diff --git a/static/preference.json b/static/preference.json new file mode 100644 index 00000000..cd954dbc --- /dev/null +++ b/static/preference.json @@ -0,0 +1,4 @@ +{ + "autoMaximize": false, + "showTrafficLights": false +} \ No newline at end of file