🔨 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

@@ -213,7 +213,7 @@ export default {
this.changeZIndexForGallery(false)
},
copy (item) {
const style = this.$db.read().get('settings.pasteStyle').value() || 'markdown'
const style = this.$db.get('settings.pasteStyle') || 'markdown'
const copyLink = pasteStyle(style, item)
const obj = {
title: '复制链接成功',
@@ -232,7 +232,7 @@ export default {
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
const file = this.$db.read().get('uploaded').getById(id).value()
const file = this.$db.get('uploaded').getById(id)
this.$db.read().get('uploaded').removeById(id).write()
this.$electron.ipcRenderer.send('removeFiles', [file])
const obj = {
@@ -319,7 +319,7 @@ export default {
multiCopy () {
if (Object.values(this.choosedList).some(item => item)) {
let copyString = ''
const style = this.$db.read().get('settings.pasteStyle').value() || 'markdown'
const style = this.$db.get('settings.pasteStyle') || 'markdown'
// choosedList -> { [id]: true or false }; true means choosed. false means not choosed.
Object.keys(this.choosedList).forEach(key => {
if (this.choosedList[key]) {
@@ -343,11 +343,10 @@ export default {
this.handleBarActive = !this.handleBarActive
},
getPasteStyle () {
this.pasteStyle = this.$db.read().get('settings.pasteStyle').value() || 'markdown'
this.pasteStyle = this.$db.get('settings.pasteStyle') || 'markdown'
},
handlePasteStyleChange (val) {
this.$db.read().set('settings.pasteStyle', val)
.write()
this.$db.set('settings.pasteStyle', val)
this.pasteStyle = val
}
},

View File

@@ -136,9 +136,9 @@ export default {
return {
label: item.name,
type: 'radio',
checked: this.$db.read().get('picBed.current').value() === item.type,
checked: this.$db.get('picBed.current') === item.type,
click () {
_this.$db.read().set('picBed.current', item.type).write()
_this.$db.set('picBed.current', item.type)
_this.$electron.ipcRenderer.send('syncPicBed')
}
}

View File

@@ -290,7 +290,7 @@ export default {
return callback()
}
}
let logLevel = this.$db.read().get('settings.logLevel').value()
let logLevel = this.$db.get('settings.logLevel')
if (!Array.isArray(logLevel)) {
if (logLevel && logLevel.length > 0) {
logLevel = [logLevel]
@@ -300,13 +300,13 @@ export default {
}
return {
form: {
updateHelper: this.$db.read().get('settings.showUpdateTip').value(),
updateHelper: this.$db.get('settings.showUpdateTip'),
showPicBedList: [],
autoStart: this.$db.read().get('settings.autoStart').value() || false,
rename: this.$db.read().get('settings.rename').value() || false,
autoRename: this.$db.read().get('settings.autoRename').value() || false,
uploadNotification: this.$db.read().get('settings.uploadNotification').value() || false,
miniWindowOntop: this.$db.read().get('settings.miniWindowOntop').value() || false,
autoStart: this.$db.get('settings.autoStart') || false,
rename: this.$db.get('settings.rename') || false,
autoRename: this.$db.get('settings.autoRename') || false,
uploadNotification: this.$db.get('settings.uploadNotification') || false,
miniWindowOntop: this.$db.get('settings.miniWindowOntop') || false,
logLevel
},
picBed: [],
@@ -316,12 +316,12 @@ export default {
checkUpdateVisible: false,
proxyVisible: false,
customLink: {
value: this.$db.read().get('settings.customLink').value() || '$url'
value: this.$db.get('settings.customLink') || '$url'
},
shortKey: {
upload: this.$db.read().get('settings.shortKey.upload').value()
upload: this.$db.get('settings.shortKey.upload')
},
proxy: this.$db.read().get('picBed.proxy').value() || undefined,
proxy: this.$db.get('picBed.proxy') || undefined,
rules: {
value: [
{ validator: customLinkRule, trigger: 'blur' }
@@ -368,22 +368,22 @@ export default {
},
cancelKeyBinding () {
this.keyBindingVisible = false
this.shortKey = this.$db.read().get('settings.shortKey').value()
this.shortKey = this.$db.get('settings.shortKey')
},
confirmKeyBinding () {
const oldKey = this.$db.read().get('settings.shortKey').value()
this.$db.read().set('settings.shortKey', this.shortKey).write()
const oldKey = this.$db.get('settings.shortKey')
this.$db.set('settings.shortKey', this.shortKey)
this.keyBindingVisible = false
this.$electron.ipcRenderer.send('updateShortKey', oldKey)
},
cancelCustomLink () {
this.customLinkVisible = false
this.customLink.value = this.$db.read().get('settings.customLink').value() || '$url'
this.customLink.value = this.$db.get('settings.customLink') || '$url'
},
confirmCustomLink () {
this.$refs.customLink.validate((valid) => {
if (valid) {
this.$db.read().set('settings.customLink', this.customLink.value).write()
this.$db.set('settings.customLink', this.customLink.value)
this.customLinkVisible = false
this.$electron.ipcRenderer.send('updateCustomLink')
} else {
@@ -393,11 +393,11 @@ export default {
},
cancelProxy () {
this.proxyVisible = false
this.proxy = this.$db.read().get('picBed.proxy').value() || undefined
this.proxy = this.$db.get('picBed.proxy') || undefined
},
confirmProxy () {
this.proxyVisible = false
this.$db.read().set('picBed.proxy', this.proxy).write()
this.$db.set('picBed.proxy', this.proxy)
const successNotification = new window.Notification('设置代理', {
body: '设置成功'
})
@@ -406,7 +406,7 @@ export default {
}
},
updateHelperChange (val) {
this.$db.read().set('settings.showUpdateTip', val).write()
this.$db.set('settings.showUpdateTip', val)
},
handleShowPicBedListChange (val) {
const list = this.picBed.map(item => {
@@ -417,18 +417,18 @@ export default {
}
return item
})
this.$db.read().set('picBed.list', list).write()
this.$db.set('picBed.list', list)
this.$electron.ipcRenderer.send('getPicBeds')
},
handleAutoStartChange (val) {
this.$db.read().set('settings.autoStart', val).write()
this.$db.set('settings.autoStart', val)
this.$electron.ipcRenderer.send('autoStart', val)
},
handleRename (val) {
this.$db.read().set('settings.rename', val).write()
this.$db.set('settings.rename', val)
},
handleAutoRename (val) {
this.$db.read().set('settings.autoRename', val).write()
this.$db.set('settings.autoRename', val)
},
compareVersion2Update (current, latest) {
const currentVersion = current.split('.').map(item => parseInt(item))
@@ -463,17 +463,17 @@ export default {
this.checkUpdateVisible = false
},
handleUploadNotification (val) {
this.$db.read().set('settings.uploadNotification', val).write()
this.$db.set('settings.uploadNotification', val)
},
handleMiniWindowOntop (val) {
this.$db.read().set('settings.miniWindowOntop', val).write()
this.$db.set('settings.miniWindowOntop', val)
this.$message('需要重启生效')
},
confirmLogLevelSetting () {
if (this.form.logLevel.length === 0) {
return this.$message.error('请选择日志记录等级')
}
this.$db.read().set('settings.logLevel', this.form.logLevel).write()
this.$db.set('settings.logLevel', this.form.logLevel)
const successNotification = new window.Notification('设置日志', {
body: '设置成功'
})
@@ -484,7 +484,7 @@ export default {
},
cancelLogLevelSetting () {
this.logFileVisible = false
let logLevel = this.$db.read().get('settings.logLevel').value()
let logLevel = this.$db.get('settings.logLevel')
if (!Array.isArray(logLevel)) {
if (logLevel && logLevel.length > 0) {
logLevel = [logLevel]

View File

@@ -183,7 +183,7 @@ export default {
})
this.getPluginList()
this.getSearchResult = debounce(this.getSearchResult, 50)
this.needReload = this.$db.read().get('needReload').value()
this.needReload = this.$db.get('needReload')
},
methods: {
buildContextMenu (plugin) {
@@ -192,7 +192,7 @@ export default {
label: '启用插件',
enabled: !plugin.enabled,
click () {
_this.$db.read().set(`picgoPlugins.picgo-plugin-${plugin.name}`, true).write()
_this.$db.set(`picgoPlugins.picgo-plugin-${plugin.name}`, true)
plugin.enabled = true
_this.getPicBeds()
}
@@ -200,7 +200,7 @@ export default {
label: '禁用插件',
enabled: plugin.enabled,
click () {
_this.$db.read().set(`picgoPlugins.picgo-plugin-${plugin.name}`, false).write()
_this.$db.set(`picgoPlugins.picgo-plugin-${plugin.name}`, false)
plugin.enabled = false
_this.getPicBeds()
if (plugin.config.transformer.name) {
@@ -238,7 +238,7 @@ export default {
// handle transformer
if (plugin.config.transformer.name) {
let currentTransformer = this.$db.read().get('picBed.transformer').value() || 'path'
let currentTransformer = this.$db.get('picBed.transformer') || 'path'
let pluginTransformer = plugin.config.transformer.name
const obj = {
label: `${currentTransformer === pluginTransformer ? '禁用' : '启用'}transformer - ${plugin.config.transformer.name}`,
@@ -311,7 +311,7 @@ export default {
this.$electron.remote.app.exit(0)
},
handleReload () {
this.$db.read().set('needReload', true).write()
this.$db.set('needReload', true)
this.needReload = true
const successNotification = new window.Notification('更新成功', {
body: '请点击此通知重启应用以生效'
@@ -324,11 +324,11 @@ export default {
this.searchText = ''
},
toggleTransformer (transformer) {
let currentTransformer = this.$db.read().get('picBed.transformer').value() || 'path'
let currentTransformer = this.$db.get('picBed.transformer') || 'path'
if (currentTransformer === transformer) {
this.$db.read().set('picBed.transformer', 'path').write()
this.$db.set('picBed.transformer', 'path')
} else {
this.$db.read().set('picBed.transformer', transformer).write()
this.$db.set('picBed.transformer', transformer)
}
},
async handleConfirmConfig () {
@@ -336,13 +336,13 @@ export default {
if (result !== false) {
switch (this.currentType) {
case 'plugin':
this.$db.read().set(`picgo-plugin-${this.configName}`, result).write()
this.$db.set(`picgo-plugin-${this.configName}`, result)
break
case 'uploader':
this.$db.read().set(`picBed.${this.configName}`, result).write()
this.$db.set(`picBed.${this.configName}`, result)
break
case 'transformer':
this.$db.read().set(`transformer.${this.configName}`, result).write()
this.$db.set(`transformer.${this.configName}`, result)
break
}
const successNotification = new window.Notification('设置结果', {
@@ -393,15 +393,15 @@ export default {
// restore Uploader & Transformer
handleRestoreState (item, name) {
if (item === 'uploader') {
const current = this.$db.read().get('picBed.current').value()
const current = this.$db.get('picBed.current')
if (current === name) {
this.$db.read().set('picBed.current', 'smms').write()
this.$db.set('picBed.current', 'smms')
}
}
if (item === 'transformer') {
const current = this.$db.read().get('picBed.transformer').value()
const current = this.$db.get('picBed.transformer')
if (current === name) {
this.$db.read().set('picBed.transformer', 'path').write()
this.$db.set('picBed.transformer', 'path')
}
}
},

View File

@@ -71,7 +71,7 @@ export default {
}
},
created () {
const shortKeyConfig = this.$db.read().get('settings.shortKey').value()
const shortKeyConfig = this.$db.get('settings.shortKey')
this.list = Object.keys(shortKeyConfig).map(item => shortKeyConfig[item])
},
methods: {
@@ -82,8 +82,8 @@ export default {
toggleEnable (item) {
const status = !item.enable
item.enable = status
this.$db.set(`settings.shortKey.${item.name}.enable`, status).write()
this.$electron.ipcRenderer.send('updateShortKeyConfig', item)
this.$db.set(`settings.shortKey.${item.name}.enable`, status)
this.$electron.ipcRenderer.send('updateShortKey', item)
}
}
}

View File

@@ -53,7 +53,7 @@
this.getData()
this.$electron.ipcRenderer.on('dragFiles', (event, files) => {
files.forEach(item => {
this.$db.read().get('uploaded').insert(item).write()
this.$db.insert('uploaded', item)
})
this.files = this.$db.read().get('uploaded').slice().reverse().slice(0, 5).value()
})
@@ -83,7 +83,7 @@
this.notification.body = item.imgUrl
this.notification.icon = item.imgUrl
const myNotification = new window.Notification(this.notification.title, this.notification)
const pasteStyle = this.$db.read().get('settings.pasteStyle').value() || 'markdown'
const pasteStyle = this.$db.get('settings.pasteStyle') || 'markdown'
this.$electron.clipboard.writeText(pasteTemplate(pasteStyle, item))
myNotification.onclick = () => {
return true

View File

@@ -130,17 +130,16 @@ export default {
this.$electron.ipcRenderer.send('uploadChoosedFiles', sendFiles)
},
getPasteStyle () {
this.pasteStyle = this.$db.read().get('settings.pasteStyle').value() || 'markdown'
this.pasteStyle = this.$db.get('settings.pasteStyle') || 'markdown'
},
handlePasteStyleChange (val) {
this.$db.read().set('settings.pasteStyle', val)
.write()
this.$db.set('settings.pasteStyle', val)
},
uploadClipboardFiles () {
this.$electron.ipcRenderer.send('uploadClipboardFilesFromUploadPage')
},
getDefaultPicBed () {
const current = this.$db.read().get('picBed.current').value()
const current = this.$db.get('picBed.current')
this.picBed.forEach(item => {
if (item.type === current) {
this.picBedName = item.name
@@ -161,9 +160,9 @@ export default {
return {
label: item.name,
type: 'radio',
checked: this.$db.read().get('picBed.current').value() === item.type,
checked: this.$db.get('picBed.current') === item.type,
click () {
_this.$db.read().set('picBed.current', item.type).write()
_this.$db.set('picBed.current', item.type)
_this.$electron.ipcRenderer.send('syncPicBed')
}
}

View File

@@ -82,7 +82,7 @@ export default {
}
},
created () {
const config = this.$db.get('picBed.aliyun').value()
const config = this.$db.get('picBed.aliyun')
if (config) {
for (let i in config) {
this.form[i] = config[i]

View File

@@ -73,7 +73,7 @@ export default {
}
},
created () {
const config = this.$db.get('picBed.github').value()
const config = this.$db.get('picBed.github')
if (config) {
for (let i in config) {
this.form[i] = config[i]

View File

@@ -50,7 +50,7 @@ export default {
}
},
created () {
const config = this.$db.get('picBed.imgur').value()
const config = this.$db.get('picBed.imgur')
if (config) {
for (let i in config) {
this.form[i] = config[i]

View File

@@ -52,7 +52,7 @@ export default {
async handleConfirm () {
const result = await this.$refs.configForm.validate()
if (result !== false) {
this.$db.read().set(`picBed.${this.type}`, result).write()
this.$db.set(`picBed.${this.type}`, result)
const successNotification = new window.Notification('设置结果', {
body: '设置成功'
})
@@ -62,7 +62,7 @@ export default {
}
},
setDefaultPicBed (type) {
this.$db.read().set('picBed.current', type).write()
this.$db.set('picBed.current', type)
this.defaultPicBed = type
const successNotification = new window.Notification('设置默认图床', {
body: '设置成功'

View File

@@ -90,7 +90,7 @@ export default {
}
},
created () {
const config = this.$db.get('picBed.qiniu').value()
const config = this.$db.get('picBed.qiniu')
if (config) {
for (let i in config) {
this.form[i] = config[i]

View File

@@ -105,7 +105,7 @@ export default {
}
},
created () {
const config = this.$db.get('picBed.tcyun').value()
const config = this.$db.get('picBed.tcyun')
if (config) {
for (let i in config) {
this.form[i] = config[i]

View File

@@ -81,7 +81,7 @@ export default {
}
},
created () {
const config = this.$db.get('picBed.upyun').value()
const config = this.$db.get('picBed.upyun')
if (config) {
for (let i in config) {
this.form[i] = config[i]

View File

@@ -78,7 +78,7 @@ export default {
}
},
created () {
const config = this.$db.read().get('picBed.weibo').value()
const config = this.$db.get('picBed.weibo')
if (config) {
this.form.username = config.username
this.form.password = config.password
@@ -91,13 +91,13 @@ export default {
confirm (formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
this.$db.read().set('picBed.weibo', {
this.$db.set('picBed.weibo', {
username: this.form.username,
password: this.form.password,
quality: this.quality,
cookie: this.form.cookie,
chooseCookie: this.chooseCookie
}).write()
})
const successNotification = new window.Notification('设置结果', {
body: '设置成功'
})