-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"autoMaximize": false, | ||
"showTrafficLights": false | ||
} |