From 37a784225e90c9d115367f056957dac88ebcf816 Mon Sep 17 00:00:00 2001 From: Molunerfinn Date: Tue, 30 Jan 2018 15:20:19 +0800 Subject: [PATCH] Fixed: #17 --- .electron-vue/webpack.renderer.config.js | 3 +- package.json | 1 + src/datastore/index.js | 6 +++ src/main/index.js | 68 +++++++++++++++++++----- src/renderer/components/SettingPage.vue | 53 +++++++++++++++++- src/renderer/utils/key-binding.js | 38 +++++++++++++ yarn.lock | 4 ++ 7 files changed, 158 insertions(+), 15 deletions(-) create mode 100644 src/renderer/utils/key-binding.js diff --git a/.electron-vue/webpack.renderer.config.js b/.electron-vue/webpack.renderer.config.js index 94e20bc..9f65dee 100644 --- a/.electron-vue/webpack.renderer.config.js +++ b/.electron-vue/webpack.renderer.config.js @@ -133,7 +133,8 @@ let rendererConfig = { resolve: { alias: { '@': path.join(__dirname, '../src/renderer'), - 'vue$': 'vue/dist/vue.esm.js' + 'vue$': 'vue/dist/vue.esm.js', + 'utils': path.join(__dirname, '../src/renderer/utils') }, extensions: ['.js', '.vue', '.json', '.css', '.node'] }, diff --git a/package.json b/package.json index 278f5f3..d1a67cc 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "element-ui": "^2.0.5", "fs-extra": "^4.0.2", "image-size": "^0.6.1", + "keycode": "^2.1.9", "lodash-id": "^0.14.0", "lowdb": "^1.0.0", "md5": "^2.2.1", diff --git a/src/datastore/index.js b/src/datastore/index.js index 7e3ce8b..d025c20 100644 --- a/src/datastore/index.js +++ b/src/datastore/index.js @@ -29,4 +29,10 @@ if (!db.has('picBed').value()) { }).write() } +if (!db.has('shortKey').value()) { + db.set('shortKey', { + upload: 'CommandOrControl+Shift+P' + }).write() +} + export default db diff --git a/src/main/index.js b/src/main/index.js index 9c68a13..5887f8a 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -21,6 +21,7 @@ let window let settingWindow let tray let menu +let contextMenu const winURL = process.env.NODE_ENV === 'development' ? `http://localhost:9080` : `file://${__dirname}/index.html` @@ -39,7 +40,7 @@ const uploadFailed = () => { function createTray () { const menubarPic = process.platform === 'darwin' ? `${__static}/menubar.png` : `${__static}/menubar-nodarwin.png` tray = new Tray(menubarPic) - const contextMenu = Menu.buildFromTemplate([ + contextMenu = Menu.buildFromTemplate([ { label: '关于', click () { @@ -308,18 +309,26 @@ const uploadClipboardFiles = async () => { imgUrl } } - img = await uploader(uploadImg, 'imgFromClipboard', window.webContents) + img = await uploader(uploadImg, 'imgFromClipboard', settingWindow.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) + 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() } @@ -344,6 +353,10 @@ ipcMain.on('uploadClipboardFiles', async (evt, file) => { } }) +ipcMain.on('uploadClipboardFilesFromUploadPage', () => { + uploadClipboardFiles() +}) + ipcMain.on('uploadChoosedFiles', async (evt, files) => { const imgs = await uploader(files, 'imgFromUploader', settingWindow.webContents) if (imgs !== false) { @@ -367,6 +380,35 @@ ipcMain.on('uploadChoosedFiles', async (evt, files) => { } }) +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('updateDefaultPicBed', (evt) => { + const types = ['weibo', 'qiniu', 'tcyun', 'upyun'] + 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 + } + }) +}) + +const shortKeyHash = { + upload: uploadClipboardFiles +} + const isSecondInstance = app.makeSingleInstance(() => { if (settingWindow) { if (settingWindow.isMinimized()) { @@ -389,7 +431,7 @@ app.on('ready', () => { createTray() updateChecker() - globalShortcut.register('CommandOrControl+Shift+P', () => { + globalShortcut.register(db.read().get('shortKey.upload').value(), () => { uploadClipboardFiles() }) }) diff --git a/src/renderer/components/SettingPage.vue b/src/renderer/components/SettingPage.vue index 7180e08..373cc97 100644 --- a/src/renderer/components/SettingPage.vue +++ b/src/renderer/components/SettingPage.vue @@ -63,11 +63,36 @@ + + + + + + + + 取消 + 确定 + +