From a8a0b9b4246de1dbff385ca2c8130ea0d659dcec Mon Sep 17 00:00:00 2001 From: Molunerfinn Date: Sat, 23 Dec 2017 19:46:20 +0800 Subject: [PATCH] Added: globalshotcut for clipboard upload --- src/main/index.js | 43 +++++++++++++++++++++++-- src/main/utils/img2base64.js | 16 +++++---- src/renderer/components/SettingPage.vue | 4 ++- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/src/main/index.js b/src/main/index.js index 327672f..27ba5fc 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -1,7 +1,7 @@ 'use strict' import uploader from './utils/uploader.js' -import { app, BrowserWindow, Tray, Menu, Notification, clipboard, ipcMain } from 'electron' +import { app, BrowserWindow, Tray, Menu, Notification, clipboard, ipcMain, globalShortcut } from 'electron' import db from '../datastore' import pasteTemplate from './utils/pasteTemplate' import updateChecker from './utils/updateChecker' @@ -252,6 +252,35 @@ const showWindow = () => { 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 + } + } + img = await uploader(uploadImg, 'imgFromClipboard', window.webContents) + 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: img[0].imgUrl + }) + notification.show() + window.webContents.send('clipboardFiles', []) + window.webContents.send('uploadFiles', img) + } else { + uploadFailed() + } +} + ipcMain.on('uploadClipboardFiles', async (evt, file) => { const img = await uploader(file, 'imgFromClipboard', window.webContents) if (img !== false) { @@ -260,7 +289,8 @@ ipcMain.on('uploadClipboardFiles', async (evt, file) => { const notification = new Notification({ title: '上传成功', body: img[0].imgUrl, - icon: file[0] + // icon: file[0] + icon: img[0].imgUrl }) notification.show() window.webContents.send('clipboardFiles', []) @@ -298,6 +328,11 @@ app.on('ready', () => { createTray() createMenu() updateChecker() + + globalShortcut.register('CommandOrControl+Shift+P', () => { + console.log(1) + uploadClipboardFiles() + }) }) app.on('window-all-closed', () => { @@ -314,6 +349,10 @@ app.on('activate', () => { } }) +app.on('will-quit', () => { + globalShortcut.unregisterAll() +}) + /** * Auto Updater * diff --git a/src/main/utils/img2base64.js b/src/main/utils/img2base64.js index b59717d..fbd93a2 100644 --- a/src/main/utils/img2base64.js +++ b/src/main/utils/img2base64.js @@ -21,13 +21,15 @@ const imgFromPath = async (imgPath) => { const imgFromClipboard = (file) => { let result = [] - const today = new Date().toLocaleString().replace(/[ ]+/g, '-') - result.push({ - base64Image: file.imgUrl.replace(/^data\S+,/, ''), - fileName: `${today}.png`, - width: file.width, - height: file.height - }) + if (file !== null) { + const today = new Date().toLocaleString().replace(/[ ]+/g, '-').replace(/\/+/g, '-') + result.push({ + base64Image: file.imgUrl.replace(/^data\S+,/, ''), + fileName: `${today}.png`, + width: file.width, + height: file.height + }) + } return result } diff --git a/src/renderer/components/SettingPage.vue b/src/renderer/components/SettingPage.vue index 8c5baab..03ab6b4 100644 --- a/src/renderer/components/SettingPage.vue +++ b/src/renderer/components/SettingPage.vue @@ -1,7 +1,7 @@