From aea0c3661d20f929edc676ed5484faaf7e022a07 Mon Sep 17 00:00:00 2001 From: Molunerfinn Date: Tue, 18 Sep 2018 18:48:55 +0800 Subject: [PATCH] Added: picgo-core-ipc & fixed dynamic require bug in picgo --- src/main/index.js | 6 +- src/main/utils/picgoCoreIPC.js | 32 +++++++ src/main/utils/uploader.js | 5 +- .../components/SettingView/Plugin.vue | 91 ++++++++++++++++--- 4 files changed, 117 insertions(+), 17 deletions(-) create mode 100644 src/main/utils/picgoCoreIPC.js diff --git a/src/main/index.js b/src/main/index.js index 44a313e..77fa39a 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -7,6 +7,7 @@ import pasteTemplate from './utils/pasteTemplate' import updateChecker from './utils/updateChecker' import pkg from '../../package.json' import picBed from '../datastore/pic-bed' +import picgoCoreIPC from './utils/picgoCoreIPC' /** * Set `__static` path to static files in production * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html @@ -255,7 +256,8 @@ const createSettingWindow = () => { transparent: true, titleBarStyle: 'hidden', webPreferences: { - backgroundThrottling: false + backgroundThrottling: false, + webSecurity: false } } if (process.platform !== 'darwin') { @@ -398,6 +400,8 @@ const updateDefaultPicBed = () => { } } +picgoCoreIPC(app, ipcMain) + ipcMain.on('uploadClipboardFiles', async (evt, file) => { const img = await uploader(file, 'imgFromClipboard', window.webContents) if (img !== false) { diff --git a/src/main/utils/picgoCoreIPC.js b/src/main/utils/picgoCoreIPC.js new file mode 100644 index 0000000..97df425 --- /dev/null +++ b/src/main/utils/picgoCoreIPC.js @@ -0,0 +1,32 @@ +import path from 'path' + +// eslint-disable-next-line +const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require +const PicGo = requireFunc('picgo') + +export default (app, ipcMain) => { + const STORE_PATH = app.getPath('userData') + const CONFIG_PATH = path.join(STORE_PATH, '/data.json') + const picgo = new PicGo(CONFIG_PATH) + + ipcMain.on('getPluginList', event => { + const pluginList = picgo.pluginLoader.getList() + const list = [] + for (let i in pluginList) { + const pluginPath = path.join(STORE_PATH, `/node_modules/${pluginList[i]}`) + const pluginPKG = requireFunc(path.join(pluginPath, 'package.json')) + const obj = { + name: pluginList[i], + author: pluginPKG.author, + description: pluginPKG.description, + logo: path.join(pluginPath, 'logo.png').split(path.sep).join('/'), + config: { + plugin: picgo.pluginLoader.getPlugin(pluginList[i]).config ? picgo.pluginLoader.getPlugin(pluginList[i]).config(picgo) : [] + }, + enabled: picgo.getConfig(`plugins.${pluginList[i]}`) + } + list.push(obj) + } + event.sender.send('pluginList', list) + }) +} diff --git a/src/main/utils/uploader.js b/src/main/utils/uploader.js index 611d734..024179d 100644 --- a/src/main/utils/uploader.js +++ b/src/main/utils/uploader.js @@ -4,10 +4,13 @@ import { BrowserWindow, ipcMain } from 'electron' -import PicGo from 'picgo' import path from 'path' import fecha from 'fecha' +// eslint-disable-next-line +const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require +const PicGo = requireFunc('picgo') + const renameURL = process.env.NODE_ENV === 'development' ? `http://localhost:9080/#rename-page` : `file://${__dirname}/index.html#rename-page` const createRenameWindow = () => { diff --git a/src/renderer/components/SettingView/Plugin.vue b/src/renderer/components/SettingView/Plugin.vue index 0175f0b..b6ffa19 100644 --- a/src/renderer/components/SettingView/Plugin.vue +++ b/src/renderer/components/SettingView/Plugin.vue @@ -10,33 +10,34 @@ size="small" > - - + +
- +
- Uploader-SMMS2 + {{ item.name }}
- saldfjlsajf,ajsldfjasljfk,asmfjsalkfjsakfmasldfjlsajf,ajsfdljsalfjslafdj + {{ item.description }}
- XXXXXXXX + {{ item.author }} - - + + + 重启 + +
- -
- -
-
@@ -45,7 +46,54 @@ export default { name: 'plugin', data () { return { - searchText: '' + searchText: '', + pluginList: [], + menu: null + } + }, + created () { + this.$electron.ipcRenderer.on('pluginList', (evt, list) => { + this.pluginList = list.map(item => { + item.reload = false + return item + }) + }) + this.getPluginList() + document.addEventListener('keydown', (e) => { + if (e.which === 123) { + this.$electron.remote.getCurrentWindow().toggleDevTools() + } + }) + }, + methods: { + buildContextMenu (plugin) { + const _this = this + let menu = [{ + label: '启用插件', + enabled: !plugin.enabled, + click () { + _this.$db.read().set(`plugins.${plugin.name}`, true).write() + plugin.enabled = true + plugin.reload = true + } + }, { + label: '禁用插件', + enabled: plugin.enabled, + click () { + _this.$db.read().set(`plugins.${plugin.name}`, false).write() + plugin.enabled = false + plugin.reload = true + } + }] + this.menu = this.$electron.remote.Menu.buildFromTemplate(menu) + this.menu.popup(this.$electron.remote.getCurrentWindow()) + }, + getPluginList () { + this.$electron.ipcRenderer.send('getPluginList') + }, + reloadApp () { + this.$electron.remote.app.relaunch() + this.$electron.remote.app.exit(0) } } } @@ -70,6 +118,7 @@ export default { user-select text transition all .2s ease-in-out cursor pointer + margin-bottom 10px &:hover background #333 &__logo @@ -78,7 +127,7 @@ export default { float left &__content float left - width calc(100% - 74px) + width calc(100% - 72px) height 64px color #aaa margin-left 8px @@ -100,6 +149,7 @@ export default { font-size 14px height 21px line-height 28px + position relative &__author overflow hidden text-overflow ellipsis @@ -107,4 +157,15 @@ export default { &__config float right font-size 16px + .reload-button + font-size 12px + color #ddd + background #222 + padding 1px 8px + height 18px + line-height 18px + text-align center + position absolute + top 4px + right 20px \ No newline at end of file