🔨 Refactor: upload module && notification module

This commit is contained in:
PiEgg
2021-03-28 17:44:20 +08:00
parent f9d6191d5e
commit 3cad5f378d
11 changed files with 118 additions and 55 deletions
+18 -20
View File
@@ -19,15 +19,15 @@ import { handleCopyUrl } from '~/main/utils/common'
let contextMenu: Menu | null
let menu: Menu | null
let tray: Tray | null
export function createContextMenu() {
export function createContextMenu () {
const picBeds = getPicBeds()
if (process.platform === "darwin" || process.platform === "win32") {
if (process.platform === 'darwin' || process.platform === 'win32') {
const submenu = picBeds.filter(item => item.visible).map(item => {
return {
label: item.name,
type: 'radio',
checked: db.get('picBed.current') === item.type,
click() {
click () {
picgo.saveConfig({
'picBed.current': item.type,
'picBed.uploader': item.type
@@ -41,7 +41,7 @@ export function createContextMenu() {
contextMenu = Menu.buildFromTemplate([
{
label: '关于',
click() {
click () {
dialog.showMessageBox({
title: 'PicGo',
message: 'PicGo',
@@ -51,7 +51,7 @@ export function createContextMenu() {
},
{
label: '打开详细窗口',
click() {
click () {
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)
settingWindow!.show()
settingWindow!.focus()
@@ -71,14 +71,14 @@ export function createContextMenu() {
label: '打开更新助手',
type: 'checkbox',
checked: db.get('settings.showUpdateTip'),
click() {
click () {
const value = db.get('settings.showUpdateTip')
db.set('settings.showUpdateTip', !value)
}
},
{
label: '重启应用',
click() {
click () {
app.relaunch()
app.exit(0)
}
@@ -89,8 +89,7 @@ export function createContextMenu() {
label: '退出'
}
])
}
else if (process.platform === "linux") {
} else if (process.platform === 'linux') {
// TODO 图床选择功能
// 由于在Linux难以像在Mac和Windows上那样在点击时构造ContextMenu
// 暂时取消这个选单,避免引起和设置中启用的图床不一致
@@ -101,7 +100,7 @@ export function createContextMenu() {
contextMenu = Menu.buildFromTemplate([
{
label: '打开详细窗口',
click() {
click () {
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)
settingWindow!.show()
settingWindow!.focus()
@@ -115,20 +114,20 @@ export function createContextMenu() {
label: '打开更新助手',
type: 'checkbox',
checked: db.get('settings.showUpdateTip'),
click() {
click () {
const value = db.get('settings.showUpdateTip')
db.set('settings.showUpdateTip', !value)
}
},
{
label: '关于应用',
click() {
click () {
dialog.showMessageBox({
title: 'PicGo',
message: 'PicGo',
buttons: ['Ok'],
detail: `Version: ${pkg.version}\nAuthor: Molunerfinn\nGithub: https://github.com/Molunerfinn/PicGo`,
});
detail: `Version: ${pkg.version}\nAuthor: Molunerfinn\nGithub: https://github.com/Molunerfinn/PicGo`
})
}
},
// @ts-ignore
@@ -140,11 +139,11 @@ export function createContextMenu() {
}
}
export function createTray() {
export function createTray () {
const menubarPic = process.platform === 'darwin' ? `${__static}/menubar.png` : `${__static}/menubar-nodarwin.png`
tray = new Tray(menubarPic)
// click事件在Mac和Windows上可以触发(在Ubuntu上无法触发,Unity不支持)
if (process.platform === "darwin" || process.platform === "win32") {
if (process.platform === 'darwin' || process.platform === 'win32') {
tray.on('right-click', () => {
if (windowManager.has(IWindowList.TRAY_WINDOW)) {
windowManager.get(IWindowList.TRAY_WINDOW)!.hide()
@@ -220,16 +219,15 @@ export function createTray() {
}
})
// toggleWindow()
}
} else if (process.platform === 'linux') {
// click事件在Ubuntu上无法触发,Unity不支持(在Mac和Windows上可以触发)
// 需要使用 setContextMenu 设置菜单
else if (process.platform === "linux") {
createContextMenu()
tray!.setContextMenu(contextMenu)
}
}
export function createMenu() {
export function createMenu () {
if (process.env.NODE_ENV !== 'development') {
const template = [{
label: 'Edit',
@@ -244,7 +242,7 @@ export function createMenu() {
{
label: 'Quit',
accelerator: 'CmdOrCtrl+Q',
click() {
click () {
app.quit()
}
}
+39 -21
View File
@@ -1,5 +1,4 @@
import {
app,
Notification,
BrowserWindow,
ipcMain,
@@ -11,9 +10,11 @@ import db from '#/datastore'
import windowManager from 'apis/app/window/windowManager'
import { IWindowList } from 'apis/app/window/constants'
import util from 'util'
import { IPicGo } from 'picgo/dist/src/types'
import { showNotification } from '~/main/utils/common'
const waitForShow = (webcontent: WebContents) => {
return new Promise((resolve, reject) => {
return new Promise<void>((resolve, reject) => {
webcontent.on('did-finish-load', () => {
resolve()
})
@@ -37,6 +38,7 @@ const waitForRename = (window: BrowserWindow, id: number): Promise<string|null>
class Uploader {
private webContents: WebContents | null = null
private uploading: boolean = false
constructor () {
this.init()
}
@@ -60,7 +62,7 @@ class Uploader {
}
})
picgo.helper.beforeUploadPlugins.register('renameFn', {
handle: async ctx => {
handle: async (ctx: IPicGo) => {
const rename = db.get('settings.rename')
const autoRename = db.get('settings.autoRename')
if (autoRename || rename) {
@@ -91,28 +93,44 @@ class Uploader {
}
upload (img?: IUploadOption): Promise<ImgInfo[]|false> {
picgo.upload(img)
if (this.uploading) {
showNotification({
title: '上传失败',
body: '前序上传还在继续,请稍后再试'
})
return Promise.resolve(false)
}
return new Promise((resolve) => {
picgo.once('finished', ctx => {
if (ctx.output.every((item: ImgInfo) => item.imgUrl)) {
resolve(ctx.output)
} else {
try {
this.uploading = true
picgo.upload(img)
picgo.once('finished', ctx => {
this.uploading = false
if (ctx.output.every((item: ImgInfo) => item.imgUrl)) {
resolve(ctx.output)
} else {
resolve(false)
}
picgo.removeAllListeners('failed')
})
picgo.once('failed', (e: Error) => {
this.uploading = false
setTimeout(() => {
showNotification({
title: '上传失败',
body: util.format(e.stack),
clickToCopy: true
})
}, 500)
picgo.removeAllListeners('finished')
resolve(false)
}
})
} catch (e) {
this.uploading = false
picgo.removeAllListeners('failed')
})
picgo.once('failed', (e: Error) => {
setTimeout(() => {
const notification = new Notification({
title: '上传失败',
body: util.format(e.stack)
})
notification.show()
}, 500)
picgo.removeAllListeners('finished')
resolve(false)
})
resolve([])
}
})
}
}
+1 -1
View File
@@ -27,7 +27,7 @@ class GuiApi implements IGuiApi {
return true
}
settingWindow.show()
return new Promise((resolve, reject) => {
return new Promise<void>((resolve, reject) => {
setTimeout(() => {
resolve()
}, 1000) // TODO: a better way to wait page loaded.