mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 09:46:22 +00:00
embed hotkey, config to preferences modal & fix datasaving sequence(Async, Queue)
This commit is contained in:
53
lib/config.js
Normal file
53
lib/config.js
Normal file
@@ -0,0 +1,53 @@
|
||||
const electron = require('electron')
|
||||
const app = electron.app
|
||||
const ipc = electron.ipcMain
|
||||
const jetpack = require('fs-jetpack')
|
||||
const mainWindow = require('./main-window')
|
||||
|
||||
const defaultConfig = {
|
||||
'editor-font-size': '14',
|
||||
'editor-font-family': 'Monaco, Ubuntu Mono, Consolas, source-code-pro, monospace',
|
||||
'editor-indent-type': 'space',
|
||||
'editor-indent-size': '4',
|
||||
'preview-font-size': '14',
|
||||
'preview-font-family': 'Lato, helvetica, arial, sans-serif',
|
||||
'disable-direct-write': false
|
||||
}
|
||||
const configFile = 'config.json'
|
||||
|
||||
var userDataPath = app.getPath('userData')
|
||||
|
||||
function getConfig () {
|
||||
var userDataPath = app.getPath('userData')
|
||||
if (jetpack.cwd(userDataPath).exists(configFile)) {
|
||||
try {
|
||||
return JSON.parse(jetpack.cwd(userDataPath).read(configFile, 'utf-8'))
|
||||
} catch (err) {}
|
||||
}
|
||||
return {}
|
||||
}
|
||||
|
||||
function saveConfig () {
|
||||
var content
|
||||
try {
|
||||
content = JSON.stringify(global.config)
|
||||
} catch (e) {
|
||||
global.config = {}
|
||||
content = JSON.stringify(global.config)
|
||||
}
|
||||
jetpack.cwd(userDataPath).file(configFile, { content })
|
||||
}
|
||||
|
||||
// Init
|
||||
global.config = Object.assign({}, defaultConfig, getConfig())
|
||||
|
||||
function applyConfig () {
|
||||
mainWindow.webContents.send('config-apply')
|
||||
}
|
||||
|
||||
ipc.on('configUpdated', function (event, newConfig) {
|
||||
global.config = Object.assign({}, defaultConfig, global.config, newConfig)
|
||||
saveConfig()
|
||||
applyConfig()
|
||||
})
|
||||
|
||||
126
lib/hotkey.js
126
lib/hotkey.js
@@ -1,54 +1,40 @@
|
||||
const electron = require('electron')
|
||||
const app = electron.app
|
||||
const ipc = electron.ipcMain
|
||||
const Menu = electron.Menu
|
||||
const globalShortcut = electron.globalShortcut
|
||||
const jetpack = require('fs-jetpack')
|
||||
const mainWindow = require('./main-window')
|
||||
const nodeIpc = require('@rokt33r/node-ipc')
|
||||
|
||||
const defaultKeymap = {
|
||||
toggleFinder: 'Cmd + alt + s',
|
||||
toggleMain: 'Cmd + alt + v'
|
||||
}
|
||||
const keymapFilename = 'keymap.json'
|
||||
|
||||
var userDataPath = app.getPath('userData')
|
||||
if (!jetpack.cwd(userDataPath).exists('keymap.json')) {
|
||||
jetpack.cwd(userDataPath).file('keymap.json', {content: '{}'})
|
||||
}
|
||||
try {
|
||||
global.keymap = JSON.parse(jetpack.cwd(userDataPath).read('keymap.json', 'utf-8'))
|
||||
} catch (err) {
|
||||
jetpack.cwd(userDataPath).file('keymap.json', {content: '{}'})
|
||||
global.keymap = {}
|
||||
}
|
||||
if (global.keymap.toggleFinder == null) global.keymap.toggleFinder = 'ctrl+tab+shift'
|
||||
var toggleFinderKey = global.keymap.toggleFinder
|
||||
|
||||
try {
|
||||
globalShortcut.register(toggleFinderKey, function () {
|
||||
emitToFinder('open-finder')
|
||||
mainWindow.webContents.send('open-finder', {})
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(err.name)
|
||||
}
|
||||
|
||||
ipc.on('hotkeyUpdated', function (event, newKeymap) {
|
||||
console.log('got new keymap')
|
||||
console.log(newKeymap)
|
||||
globalShortcut.unregisterAll()
|
||||
global.keymap = newKeymap
|
||||
jetpack.cwd(userDataPath).file('keymap.json', {content: JSON.stringify(global.keymap)})
|
||||
|
||||
var toggleFinderKey = global.keymap.toggleFinder != null ? global.keymap.toggleFinder : 'ctrl+tab+shift'
|
||||
try {
|
||||
globalShortcut.register(toggleFinderKey, function () {
|
||||
emitToFinder('open-finder')
|
||||
mainWindow.webContents.send('open-finder', {})
|
||||
})
|
||||
mainWindow.webContents.send('APP_SETTING_DONE', {})
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
mainWindow.webContents.send('APP_SETTING_ERROR', {
|
||||
message: 'Failed to apply hotkey: Invalid format'
|
||||
})
|
||||
function getKeymap () {
|
||||
var userDataPath = app.getPath('userData')
|
||||
if (jetpack.cwd(userDataPath).exists(keymapFilename)) {
|
||||
try {
|
||||
return JSON.parse(jetpack.cwd(userDataPath).read(keymapFilename, 'utf-8'))
|
||||
} catch (err) {}
|
||||
}
|
||||
})
|
||||
return {}
|
||||
}
|
||||
|
||||
function saveKeymap () {
|
||||
var content
|
||||
try {
|
||||
content = JSON.stringify(global.keymap)
|
||||
} catch (e) {
|
||||
global.keymap = {}
|
||||
content = JSON.stringify(global.keymap)
|
||||
}
|
||||
jetpack.cwd(userDataPath).file(keymapFilename, { content })
|
||||
}
|
||||
|
||||
function emitToFinder (type, data) {
|
||||
var payload = {
|
||||
@@ -58,3 +44,63 @@ function emitToFinder (type, data) {
|
||||
|
||||
nodeIpc.server.broadcast('message', payload)
|
||||
}
|
||||
|
||||
function toggleFinder () {
|
||||
emitToFinder('open-finder')
|
||||
mainWindow.webContents.send('open-finder', {})
|
||||
}
|
||||
|
||||
function toggleMain () {
|
||||
if (mainWindow.isFocused()) {
|
||||
if (process.platform === 'darwin') {
|
||||
Menu.sendActionToFirstResponder('hide:')
|
||||
} else {
|
||||
mainWindow.minimize()
|
||||
}
|
||||
} else {
|
||||
if (process.platform === 'darwin') {
|
||||
mainWindow.show()
|
||||
} else {
|
||||
mainWindow.minimize()
|
||||
mainWindow.restore()
|
||||
}
|
||||
mainWindow.webContents.send('list-focus')
|
||||
}
|
||||
}
|
||||
|
||||
// Init
|
||||
global.keymap = Object.assign({}, defaultKeymap, getKeymap())
|
||||
|
||||
function registerKey (name, callback, broadcast) {
|
||||
if (broadcast == null) broadcast = true
|
||||
|
||||
try {
|
||||
globalShortcut.register(global.keymap[name], callback)
|
||||
if (broadcast) {
|
||||
mainWindow.webContents.send('APP_SETTING_DONE', {})
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
if (broadcast) {
|
||||
mainWindow.webContents.send('APP_SETTING_ERROR', {
|
||||
message: 'Failed to apply hotkey: Invalid format'
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function registerAllKeys (broadcast) {
|
||||
if (broadcast == null) broadcast = true
|
||||
registerKey('toggleFinder', toggleFinder, broadcast)
|
||||
registerKey('toggleMain', toggleMain, broadcast)
|
||||
}
|
||||
|
||||
registerAllKeys(false)
|
||||
|
||||
ipc.on('hotkeyUpdated', function (event, newKeymap) {
|
||||
global.keymap = Object.assign({}, defaultKeymap, global.keymap, newKeymap)
|
||||
saveKeymap()
|
||||
globalShortcut.unregisterAll()
|
||||
registerAllKeys()
|
||||
})
|
||||
|
||||
|
||||
@@ -295,6 +295,7 @@ app.on('ready', function () {
|
||||
})
|
||||
|
||||
require('./hotkey')
|
||||
require('./config')
|
||||
})
|
||||
|
||||
module.exports = app
|
||||
|
||||
Reference in New Issue
Block a user