🔨 Refactor: db class

This commit is contained in:
PiEgg
2019-09-11 19:30:08 +08:00
parent 1274893154
commit 554cc0dbf8
30 changed files with 214 additions and 162 deletions

View File

@@ -69,9 +69,9 @@ function createContextMenu () {
return {
label: item.name,
type: 'radio',
checked: db.read().get('picBed.current').value() === item.type,
checked: db.get('picBed.current') === item.type,
click () {
db.read().set('picBed.current', item.type).write()
db.set('picBed.current', item.type)
if (settingWindow) {
settingWindow.webContents.send('syncPicBed')
}
@@ -112,10 +112,10 @@ function createContextMenu () {
{
label: '打开更新助手',
type: 'checkbox',
checked: db.get('settings.showUpdateTip').value(),
checked: db.get('settings.showUpdateTip'),
click () {
const value = db.read().get('settings.showUpdateTip').value()
db.read().set('settings.showUpdateTip', !value).write()
const value = db.get('settings.showUpdateTip')
db.set('settings.showUpdateTip', !value)
}
},
{
@@ -189,7 +189,7 @@ function createTray () {
})
tray.on('drop-files', async (event, files) => {
const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown'
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
const imgs = await new Uploader(files, window.webContents).upload()
if (imgs !== false) {
for (let i in imgs) {
@@ -202,7 +202,7 @@ function createTray () {
setTimeout(() => {
notification.show()
}, i * 100)
db.read().get('uploaded').insert(imgs[i]).write()
db.insert('uploaded', imgs[i])
}
window.webContents.send('dragFiles', imgs)
}
@@ -263,7 +263,7 @@ const createMiniWidow = () => {
}
}
if (db.read().get('settings.miniWindowOntop').value()) {
if (db.get('settings.miniWindowOntop')) {
obj.alwaysOnTop = true
}
@@ -370,7 +370,7 @@ const uploadClipboardFiles = async () => {
let img = await new Uploader(undefined, win.webContents).upload()
if (img !== false) {
if (img.length > 0) {
const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown'
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
clipboard.writeText(pasteTemplate(pasteStyle, img[0]))
const notification = new Notification({
title: '上传成功',
@@ -378,7 +378,7 @@ const uploadClipboardFiles = async () => {
icon: img[0].imgUrl
})
notification.show()
db.read().get('uploaded').insert(img[0]).write()
db.insert('uploaded', img[0])
window.webContents.send('clipboardFiles', [])
window.webContents.send('uploadFiles', img)
if (settingWindow) {
@@ -398,7 +398,7 @@ const uploadChoosedFiles = async (webContents, files) => {
const input = files.map(item => item.path)
const imgs = await new Uploader(input, webContents).upload()
if (imgs !== false) {
const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown'
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
let pasteText = ''
for (let i in imgs) {
pasteText += pasteTemplate(pasteStyle, imgs[i]) + '\r\n'
@@ -410,7 +410,7 @@ const uploadChoosedFiles = async (webContents, files) => {
setTimeout(() => {
notification.show()
}, i * 100)
db.read().get('uploaded').insert(imgs[i]).write()
db.insert('uploaded', imgs[i])
}
clipboard.writeText(pasteText)
window.webContents.send('uploadFiles', imgs)
@@ -426,7 +426,7 @@ picgoCoreIPC(app, ipcMain)
ipcMain.on('uploadClipboardFiles', async (evt, file) => {
const img = await new Uploader(undefined, window.webContents).upload()
if (img !== false) {
const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown'
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
clipboard.writeText(pasteTemplate(pasteStyle, img[0]))
const notification = new Notification({
title: '上传成功',
@@ -435,7 +435,7 @@ ipcMain.on('uploadClipboardFiles', async (evt, file) => {
icon: img[0].imgUrl
})
notification.show()
db.read().get('uploaded').insert(img[0]).write()
db.insert('uploaded', img[0])
window.webContents.send('clipboardFiles', [])
if (settingWindow) {
settingWindow.webContents.send('updateGallery')
@@ -558,13 +558,13 @@ app.on('ready', () => {
if (process.platform === 'darwin' || process.platform === 'win32') {
createTray()
}
db.read().set('needReload', false).write()
db.set('needReload', false)
updateChecker()
initEventCenter()
// 不需要阻塞
process.nextTick(() => {
updateShortKeyFromVersion212(db, db.read().get('settings.shortKey').value())
initShortKeyRegister(globalShortcut, db.read().get('settings.shortKey').value())
updateShortKeyFromVersion212(db, db.get('settings.shortKey'))
initShortKeyRegister(globalShortcut, db.get('settings.shortKey'))
})
if (process.env.NODE_ENV !== 'development') {
@@ -606,7 +606,7 @@ app.on('will-quit', () => {
})
app.setLoginItemSettings({
openAtLogin: db.read().get('settings.autoStart').value() || false
openAtLogin: db.get('settings.autoStart') || false
})
function initEventCenter () {

View File

@@ -12,7 +12,7 @@ const updateShortKeyFromVersion212 = (db, shortKeyConfig) => {
delete shortKeyConfig.upload
}
if (needUpgrade) {
db.read().set('settings.shortKey', shortKeyConfig).write()
db.set('settings.shortKey', shortKeyConfig)
return shortKeyConfig
} else {
return false

View File

@@ -1,13 +1,28 @@
import fs from 'fs-extra'
import path from 'path'
import os from 'os'
import { remote, app } from 'electron'
if (process.env.NODE_ENV !== 'development') {
global.__static = path.join(__dirname, '/static').replace(/\\/g, '\\\\')
}
if (process.env.DEBUG_ENV === 'debug') {
global.__static = path.join(__dirname, '../../../static').replace(/\\/g, '\\\\')
}
const APP = process.type === 'renderer' ? remote.app : app
const STORE_PATH = APP.getPath('userData')
function beforeOpen () {
if (process.platform === 'darwin') {
resolveMacWorkFlow()
}
resolveClipboardImageGenerator()
}
/**
* macOS 右键菜单
*/
function resolveMacWorkFlow () {
const dest = `${os.homedir}/Library/Services/Upload pictures with PicGo.workflow`
if (fs.existsSync(dest)) {
return true
@@ -20,4 +35,45 @@ function beforeOpen () {
}
}
/**
* 初始化剪贴板生成图片的脚本
*/
function resolveClipboardImageGenerator () {
let clipboardFiles = getClipboardFiles()
if (!fs.pathExistsSync(path.join(STORE_PATH, 'windows10.ps1'))) {
clipboardFiles.forEach(item => {
fs.copyFileSync(item.origin, item.dest)
})
} else {
clipboardFiles.forEach(item => {
diffFilesAndUpdate(item.origin, item.dest)
})
}
function diffFilesAndUpdate (filePath1, filePath2) {
let file1 = fs.readFileSync(filePath1)
let file2 = fs.readFileSync(filePath2)
if (!file1.equals(file2)) {
fs.copyFileSync(filePath1, filePath2)
}
}
function getClipboardFiles () {
let files = [
'/linux.sh',
'/mac.applescript',
'/windows.ps1',
'/windows10.ps1'
]
return files.map(item => {
return {
origin: path.join(__static, item),
dest: path.join(STORE_PATH, item)
}
})
}
}
export default beforeOpen

View File

@@ -9,7 +9,7 @@ const getPicBeds = (app) => {
const CONFIG_PATH = path.join(STORE_PATH, '/data.json')
const picgo = new PicGo(CONFIG_PATH)
const picBedTypes = picgo.helper.uploader.getIdList()
const picBedFromDB = db.read().get('picBed.list').value() || []
const picBedFromDB = db.get('picBed.list') || []
const picBeds = picBedTypes.map(item => {
const visible = picBedFromDB.find(i => i.type === item) // object or undefined
return {

View File

@@ -60,7 +60,7 @@ class GuiApi {
async upload (input) {
const imgs = await new Uploader(input, this[WEBCONTENTS], this[PICGO]).upload()
if (imgs !== false) {
const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown'
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
let pasteText = ''
for (let i in imgs) {
pasteText += pasteTemplate(pasteStyle, imgs[i]) + '\r\n'
@@ -72,7 +72,7 @@ class GuiApi {
setTimeout(() => {
notification.show()
}, i * 100)
db.read().get('uploaded').insert(imgs[i]).write()
db.insert('uploaded', imgs[i])
}
clipboard.writeText(pasteText)
this[WEBCONTENTS].send('uploadFiles', imgs)

View File

@@ -19,7 +19,7 @@ const formatCustomLink = (customLink, item) => {
export default (style, item) => {
let url = item.url || item.imgUrl
const customLink = db.read().get('settings.customLink').value() || '$url'
const customLink = db.get('settings.customLink') || '$url'
const tpl = {
'markdown': `![](${url})`,
'HTML': `<img src="${url}"/>`,

View File

@@ -1,6 +1,7 @@
import path from 'path'
import GuiApi from './guiApi'
import { dialog, shell } from 'electron'
import db from '../../datastore'
// eslint-disable-next-line
const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require
@@ -187,6 +188,17 @@ const handleRemoveFiles = (ipcMain, CONFIG_PATH) => {
})
}
const handlePluginShortKeyRegister = (plugin) => {
if (plugin.shortKeys && plugin.shortKeys.length > 0) {
let shortKeyConfig = db.get('settings.shortKey')
plugin.shortKeys.forEach(item => {
if (!shortKeyConfig[item.name]) {
shortKeyConfig[item.name] = item
}
})
}
}
export default (app, ipcMain) => {
const STORE_PATH = app.getPath('userData')
const CONFIG_PATH = path.join(STORE_PATH, '/data.json')
@@ -197,4 +209,5 @@ export default (app, ipcMain) => {
handleGetPicBedConfig(ipcMain, CONFIG_PATH)
handlePluginActions(ipcMain, CONFIG_PATH)
handleRemoveFiles(ipcMain, CONFIG_PATH)
handlePluginShortKeyRegister({})
}

View File

@@ -7,9 +7,9 @@ const release = 'https://api.github.com/repos/Molunerfinn/PicGo/releases/latest'
const downloadUrl = 'https://github.com/Molunerfinn/PicGo/releases/latest'
const checkVersion = async () => {
let showTip = db.read().get('settings.showUpdateTip').value()
let showTip = db.get('settings.showUpdateTip')
if (showTip === undefined) {
db.read().set('settings.showUpdateTip', true).write()
db.set('settings.showUpdateTip', true)
showTip = true
}
if (showTip) {
@@ -29,7 +29,7 @@ const checkVersion = async () => {
if (res === 0) { // if selected yes
shell.openExternal(downloadUrl)
}
db.read().set('settings.showUpdateTip', !checkboxChecked).write()
db.set('settings.showUpdateTip', !checkboxChecked)
})
}
} else {