PicList/src/main/index.js

544 lines
14 KiB
JavaScript
Raw Normal View History

2017-11-27 19:21:12 -05:00
'use strict'
2017-11-29 10:13:35 -05:00
import uploader from './utils/uploader.js'
import { app, BrowserWindow, Tray, Menu, Notification, clipboard, ipcMain, globalShortcut, dialog } from 'electron'
2017-11-28 10:56:15 -05:00
import db from '../datastore'
import pasteTemplate from './utils/pasteTemplate'
2017-12-23 03:38:19 -05:00
import updateChecker from './utils/updateChecker'
import pkg from '../../package.json'
2018-06-05 08:29:16 -04:00
import picBed from '../datastore/pic-bed'
2017-11-27 19:21:12 -05:00
/**
* Set `__static` path to static files in production
* https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
*/
if (process.env.NODE_ENV !== 'development') {
global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}
2018-01-12 04:24:07 -05:00
if (process.env.DEBUG_ENV === 'debug') {
global.__static = require('path').join(__dirname, '../../static').replace(/\\/g, '\\\\')
}
2017-11-27 19:21:12 -05:00
let window
let settingWindow
2018-07-05 12:43:53 -04:00
let miniWindow
2017-11-27 19:21:12 -05:00
let tray
2017-12-15 04:37:38 -05:00
let menu
2018-01-30 02:20:19 -05:00
let contextMenu
2017-11-27 19:21:12 -05:00
const winURL = process.env.NODE_ENV === 'development'
? `http://localhost:9080`
: `file://${__dirname}/index.html`
const settingWinURL = process.env.NODE_ENV === 'development'
? `http://localhost:9080/#setting/upload`
: `file://${__dirname}/index.html#setting/upload`
2018-07-05 12:43:53 -04:00
const miniWinURL = process.env.NODE_ENV === 'development'
? `http://localhost:9080/#mini-page`
: `file://${__dirname}/index.html#mini-page`
2017-11-27 19:21:12 -05:00
2017-12-22 09:30:16 -05:00
const uploadFailed = () => {
const notification = new Notification({
title: '上传失败',
body: '请检查你的图床配置!'
})
notification.show()
}
2017-11-27 19:21:12 -05:00
function createTray () {
2018-01-10 03:51:09 -05:00
const menubarPic = process.platform === 'darwin' ? `${__static}/menubar.png` : `${__static}/menubar-nodarwin.png`
tray = new Tray(menubarPic)
2018-06-05 08:29:16 -04:00
const submenu = picBed.map(item => {
return {
label: item.name,
type: 'radio',
checked: db.read().get('picBed.current').value() === item.type,
click () {
db.read().set('picBed.current', item.type).write()
}
}
})
2018-01-30 02:20:19 -05:00
contextMenu = Menu.buildFromTemplate([
{
label: '关于',
click () {
dialog.showMessageBox({
title: 'PicGo',
message: 'PicGo',
detail: `Version: ${pkg.version}\nAuthor: Molunerfinn\nGithub: https://github.com/Molunerfinn/PicGo`
})
}
},
2017-11-27 19:21:12 -05:00
{
2017-11-28 10:56:15 -05:00
label: '打开详细窗口',
2017-11-27 19:21:12 -05:00
click () {
if (settingWindow === null) {
createSettingWindow()
2017-12-11 08:52:14 -05:00
settingWindow.show()
2017-11-27 19:21:12 -05:00
} else {
settingWindow.show()
settingWindow.focus()
}
if (miniWindow) {
miniWindow.hide()
}
2017-11-27 19:21:12 -05:00
}
2017-11-28 10:56:15 -05:00
},
{
label: '选择默认图床',
type: 'submenu',
2018-06-05 08:29:16 -04:00
submenu
2017-12-11 08:52:14 -05:00
},
2018-01-10 04:12:24 -05:00
{
label: '打开更新助手',
type: 'checkbox',
checked: db.get('picBed.showUpdateTip').value(),
click () {
const value = db.read().get('picBed.showUpdateTip').value()
db.read().set('picBed.showUpdateTip', !value).write()
}
},
2017-12-11 08:52:14 -05:00
{
role: 'quit',
label: '退出'
2017-11-27 19:21:12 -05:00
}
])
tray.on('right-click', () => {
if (window) {
window.hide()
}
2017-11-27 19:21:12 -05:00
tray.popUpContextMenu(contextMenu)
})
tray.on('click', () => {
2018-01-10 03:51:09 -05:00
if (process.platform === 'darwin') {
let img = clipboard.readImage()
let obj = []
if (!img.isEmpty()) {
// 从剪贴板来的图片默认转为png
const imgUrl = 'data:image/png;base64,' + Buffer.from(img.toPNG(), 'binary').toString('base64')
obj.push({
width: img.getSize().width,
height: img.getSize().height,
imgUrl
})
}
toggleWindow()
setTimeout(() => {
window.webContents.send('clipboardFiles', obj)
}, 0)
} else {
if (window) {
window.hide()
}
2018-01-10 03:51:09 -05:00
if (settingWindow === null) {
createSettingWindow()
settingWindow.show()
} else {
settingWindow.show()
settingWindow.focus()
}
2017-11-27 19:21:12 -05:00
}
})
tray.on('drag-enter', () => {
tray.setImage(`${__static}/upload.png`)
})
tray.on('drag-end', () => {
2017-12-11 08:52:14 -05:00
tray.setImage(`${__static}/menubar.png`)
2017-11-27 19:21:12 -05:00
})
tray.on('drop-files', async (event, files) => {
2017-11-29 10:13:35 -05:00
const pasteStyle = db.read().get('picBed.pasteStyle').value() || 'markdown'
const imgs = await uploader(files, 'imgFromPath', window.webContents)
2017-12-22 09:30:16 -05:00
if (imgs !== false) {
for (let i in imgs) {
clipboard.writeText(pasteTemplate(pasteStyle, imgs[i].imgUrl))
const notification = new Notification({
title: '上传成功',
body: imgs[i].imgUrl,
icon: files[i]
})
setTimeout(() => {
notification.show()
}, i * 100)
}
window.webContents.send('dragFiles', imgs)
} else {
uploadFailed()
2017-11-27 19:21:12 -05:00
}
})
2017-12-08 02:52:41 -05:00
// toggleWindow()
2017-11-27 19:21:12 -05:00
}
const createWindow = () => {
if (process.platform !== 'darwin' && process.platform !== 'win32') {
return
}
2017-11-27 19:21:12 -05:00
window = new BrowserWindow({
height: 350,
width: 196, // 196
show: false,
frame: false,
fullscreenable: false,
resizable: false,
transparent: true,
vibrancy: 'ultra-dark',
webPreferences: {
backgroundThrottling: false
}
})
window.loadURL(winURL)
window.on('closed', () => {
window = null
})
window.on('blur', () => {
window.hide()
})
}
2018-07-05 12:43:53 -04:00
const createMiniWidow = () => {
2018-07-06 11:54:36 -04:00
if (miniWindow) {
return false
}
2018-07-05 12:43:53 -04:00
miniWindow = new BrowserWindow({
height: 64,
2018-07-06 11:54:36 -04:00
width: 64,
show: false,
2018-07-05 12:43:53 -04:00
frame: false,
fullscreenable: false,
resizable: false,
transparent: true,
2018-07-06 12:01:06 -04:00
icon: `${__static}/logo.png`,
2018-07-05 12:43:53 -04:00
webPreferences: {
backgroundThrottling: false
}
})
miniWindow.loadURL(miniWinURL)
miniWindow.on('closed', () => {
miniWindow = null
})
}
2017-11-27 19:21:12 -05:00
const createSettingWindow = () => {
2018-01-10 03:51:09 -05:00
const options = {
2017-11-27 19:21:12 -05:00
height: 450,
width: 800,
2017-12-08 02:52:41 -05:00
show: false,
2017-11-27 19:21:12 -05:00
frame: true,
center: true,
fullscreenable: false,
resizable: false,
2018-01-10 03:51:09 -05:00
title: 'PicGo',
2017-11-27 19:21:12 -05:00
vibrancy: 'ultra-dark',
transparent: true,
titleBarStyle: 'hidden',
webPreferences: {
backgroundThrottling: false
}
2018-01-10 03:51:09 -05:00
}
if (process.platform !== 'darwin') {
2018-01-10 03:51:09 -05:00
options.show = true
options.frame = false
options.backgroundColor = '#3f3c37'
options.transparent = false
2018-07-05 12:43:53 -04:00
options.icon = `${__static}/logo.png`
2018-01-10 03:51:09 -05:00
}
settingWindow = new BrowserWindow(options)
2017-11-27 19:21:12 -05:00
settingWindow.loadURL(settingWinURL)
settingWindow.on('closed', () => {
settingWindow = null
})
createMenu()
2018-07-05 12:43:53 -04:00
createMiniWidow()
2017-11-27 19:21:12 -05:00
}
2017-12-15 04:37:38 -05:00
const createMenu = () => {
if (process.env.NODE_ENV !== 'development') {
const template = [{
label: 'Edit',
submenu: [
{ label: 'Undo', accelerator: 'CmdOrCtrl+Z', selector: 'undo:' },
{ label: 'Redo', accelerator: 'Shift+CmdOrCtrl+Z', selector: 'redo:' },
{ type: 'separator' },
{ label: 'Cut', accelerator: 'CmdOrCtrl+X', selector: 'cut:' },
{ label: 'Copy', accelerator: 'CmdOrCtrl+C', selector: 'copy:' },
{ label: 'Paste', accelerator: 'CmdOrCtrl+V', selector: 'paste:' },
{ label: 'Select All', accelerator: 'CmdOrCtrl+A', selector: 'selectAll:' },
{
label: 'Quit',
accelerator: 'CmdOrCtrl+Q',
click () {
app.quit()
}
2017-12-15 04:37:38 -05:00
}
]
}]
menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
}
2017-12-15 04:37:38 -05:00
}
2017-11-27 19:21:12 -05:00
const getWindowPosition = () => {
const windowBounds = window.getBounds()
const trayBounds = tray.getBounds()
const x = Math.round(trayBounds.x + (trayBounds.width / 2) - (windowBounds.width / 2))
const y = Math.round(trayBounds.y + trayBounds.height - 10)
return {
x,
y
}
}
const toggleWindow = () => {
if (window.isVisible()) {
window.hide()
} else {
showWindow()
}
}
const showWindow = () => {
const position = getWindowPosition()
window.setPosition(position.x, position.y, false)
2017-12-07 01:33:14 -05:00
window.webContents.send('updateFiles')
2017-11-27 19:21:12 -05:00
window.show()
window.focus()
}
const uploadClipboardFiles = async () => {
let img = clipboard.readImage()
let uploadImg = null
if (!img.isEmpty()) {
// 从剪贴板来的图片默认转为png
const imgUrl = 'data:image/png;base64,' + Buffer.from(img.toPNG(), 'binary').toString('base64')
uploadImg = {
width: img.getSize().width,
height: img.getSize().height,
imgUrl
}
}
let win
if (miniWindow.isVisible) {
win = miniWindow
} else {
win = settingWindow || window
}
img = await uploader(uploadImg, 'imgFromClipboard', win.webContents)
if (img !== false) {
2018-01-30 02:20:19 -05:00
if (img.length > 0) {
const pasteStyle = db.read().get('picBed.pasteStyle').value() || 'markdown'
clipboard.writeText(pasteTemplate(pasteStyle, img[0].imgUrl))
const notification = new Notification({
title: '上传成功',
body: img[0].imgUrl,
icon: img[0].imgUrl
})
notification.show()
window.webContents.send('clipboardFiles', [])
window.webContents.send('uploadFiles', img)
} else {
const notification = new Notification({
title: '上传不成功',
body: '你剪贴板最新的一条记录不是图片哦'
})
notification.show()
}
} else {
uploadFailed()
}
}
2017-11-27 19:21:12 -05:00
ipcMain.on('uploadClipboardFiles', async (evt, file) => {
2017-11-29 10:13:35 -05:00
const img = await uploader(file, 'imgFromClipboard', window.webContents)
2017-12-22 09:30:16 -05:00
if (img !== false) {
const pasteStyle = db.read().get('picBed.pasteStyle').value() || 'markdown'
clipboard.writeText(pasteTemplate(pasteStyle, img[0].imgUrl))
const notification = new Notification({
title: '上传成功',
body: img[0].imgUrl,
// icon: file[0]
icon: img[0].imgUrl
2017-12-22 09:30:16 -05:00
})
notification.show()
window.webContents.send('clipboardFiles', [])
window.webContents.send('uploadFiles', img)
} else {
uploadFailed()
}
2017-11-28 10:56:15 -05:00
})
2018-01-30 02:20:19 -05:00
ipcMain.on('uploadClipboardFilesFromUploadPage', () => {
uploadClipboardFiles()
})
2017-11-28 10:56:15 -05:00
ipcMain.on('uploadChoosedFiles', async (evt, files) => {
2018-07-05 12:43:53 -04:00
const imgs = await uploader(files, 'imgFromUploader', evt.sender)
2017-12-22 09:30:16 -05:00
if (imgs !== false) {
const pasteStyle = db.read().get('picBed.pasteStyle').value() || 'markdown'
let pasteText = ''
for (let i in imgs) {
pasteText += pasteTemplate(pasteStyle, imgs[i].imgUrl) + '\r\n'
const notification = new Notification({
title: '上传成功',
body: imgs[i].imgUrl,
icon: files[i].path
})
setTimeout(() => {
notification.show()
}, i * 100)
}
clipboard.writeText(pasteText)
window.webContents.send('uploadFiles', imgs)
} else {
uploadFailed()
2017-11-28 10:56:15 -05:00
}
2017-11-27 19:21:12 -05:00
})
2018-01-30 02:20:19 -05:00
ipcMain.on('updateShortKey', (evt, oldKey) => {
globalShortcut.unregisterAll()
for (let key in oldKey) {
globalShortcut.register(db.read().get('shortKey').value()[key], () => {
return shortKeyHash[key]()
})
}
const notification = new Notification({
title: '操作成功',
body: '你的快捷键已经修改成功'
})
notification.show()
})
ipcMain.on('updateCustomLink', (evt, oldLink) => {
const notification = new Notification({
title: '操作成功',
body: '你的自定义链接格式已经修改成功'
})
notification.show()
})
ipcMain.on('updateDefaultPicBed', (evt) => {
2018-06-26 05:12:21 -04:00
if (process.platform === 'darwin' || process.platform === 'win32') {
const types = picBed.map(item => item.type)
let submenuItem = contextMenu.items[2].submenu.items
submenuItem.forEach((item, index) => {
const result = db.read().get('picBed.current').value() === types[index]
if (result) {
item.click() // It's a bug which can not set checked status
}
})
} else {
return false
}
2018-01-30 02:20:19 -05:00
})
2018-05-02 07:34:24 -04:00
ipcMain.on('autoStart', (evt, val) => {
app.setLoginItemSettings({
openAtLogin: val
})
})
2018-07-06 11:54:36 -04:00
ipcMain.on('openSettingWindow', (evt) => {
if (!settingWindow) {
createSettingWindow()
} else {
settingWindow.show()
}
miniWindow.hide()
})
ipcMain.on('openMiniWindow', (evt) => {
if (!miniWindow) {
createMiniWidow()
}
miniWindow.show()
miniWindow.focus()
2018-07-06 11:54:36 -04:00
settingWindow.hide()
})
2018-01-30 02:20:19 -05:00
const shortKeyHash = {
upload: uploadClipboardFiles
}
const isSecondInstance = app.makeSingleInstance(() => {
if (settingWindow) {
if (settingWindow.isMinimized()) {
settingWindow.restore()
}
settingWindow.focus()
}
})
if (isSecondInstance) {
app.quit()
}
2018-01-10 03:51:09 -05:00
if (process.platform === 'win32') {
app.setAppUserModelId(pkg.build.appId)
}
if (process.env.XDG_CURRENT_DESKTOP && process.env.XDG_CURRENT_DESKTOP.includes('Unity')) {
process.env.XDG_CURRENT_DESKTOP = 'Unity'
}
2017-11-27 19:21:12 -05:00
app.on('ready', () => {
createWindow()
createSettingWindow()
if (process.platform === 'darwin' || process.platform === 'win32') {
createTray()
}
2017-12-23 03:38:19 -05:00
updateChecker()
2018-01-30 02:20:19 -05:00
globalShortcut.register(db.read().get('shortKey.upload').value(), () => {
uploadClipboardFiles()
})
2017-11-27 19:21:12 -05:00
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (window === null) {
2017-11-27 19:21:12 -05:00
createWindow()
}
if (settingWindow === null) {
createSettingWindow()
}
2017-11-27 19:21:12 -05:00
})
app.on('will-quit', () => {
globalShortcut.unregisterAll()
})
2018-05-02 05:17:55 -04:00
app.setLoginItemSettings({
openAtLogin: db.read().get('picBed.autoStart').value() || false
})
2017-11-27 19:21:12 -05:00
/**
* Auto Updater
*
* Uncomment the following code below and install `electron-updater` to
* support auto updating. Code Signing with a valid certificate is required.
* https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
*/
// import { autoUpdater } from 'electron-updater'
// autoUpdater.on('update-downloaded', () => {
// autoUpdater.quitAndInstall()
// })
// app.on('ready', () => {
// if (process.env.NODE_ENV === 'production') {
// autoUpdater.checkForUpdates()
// }
// })