diff --git a/src/main/index.js b/src/main/index.js index 481efae..ce87202 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -1,6 +1,6 @@ 'use strict' -import uploader from './utils/uploader.js' +import Uploader from './utils/uploader.js' import { app, BrowserWindow, Tray, Menu, Notification, clipboard, ipcMain, globalShortcut, dialog } from 'electron' import db from '../datastore' import pasteTemplate from './utils/pasteTemplate' @@ -146,7 +146,7 @@ function createTray () { tray.on('drop-files', async (event, files) => { const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown' - const imgs = await uploader(files, 'imgFromPath', window.webContents) + const imgs = await new Uploader(files, 'imgFromPath', window.webContents).upload() if (imgs !== false) { for (let i in imgs) { clipboard.writeText(pasteTemplate(pasteStyle, imgs[i].imgUrl)) @@ -323,7 +323,7 @@ const uploadClipboardFiles = async () => { } else { win = settingWindow || window } - let img = await uploader(undefined, 'imgFromClipboard', win.webContents) + let img = await new Uploader(undefined, 'imgFromClipboard', win.webContents).upload() if (img !== false) { if (img.length > 0) { const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown' @@ -371,7 +371,7 @@ const updateDefaultPicBed = () => { picgoCoreIPC(app, ipcMain) ipcMain.on('uploadClipboardFiles', async (evt, file) => { - const img = await uploader(file, 'imgFromClipboard', window.webContents) + const img = await new Uploader(file, 'imgFromClipboard', window.webContents).upload() if (img !== false) { const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown' clipboard.writeText(pasteTemplate(pasteStyle, img[0].imgUrl)) @@ -399,7 +399,7 @@ ipcMain.on('uploadClipboardFilesFromUploadPage', () => { ipcMain.on('uploadChoosedFiles', async (evt, files) => { const input = files.map(item => item.path) - const imgs = await uploader(input, 'imgFromUploader', evt.sender) + const imgs = await new Uploader(input, 'imgFromUploader', evt.sender).upload() if (imgs !== false) { const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown' let pasteText = '' diff --git a/src/main/utils/picgoCoreIPC.js b/src/main/utils/picgoCoreIPC.js index 64555f4..b81ab46 100644 --- a/src/main/utils/picgoCoreIPC.js +++ b/src/main/utils/picgoCoreIPC.js @@ -37,6 +37,7 @@ const handleGetPluginList = (ipcMain, STORE_PATH, CONFIG_PATH) => { ipcMain.on('getPluginList', event => { const picgo = new PicGo(CONFIG_PATH) const pluginList = picgo.pluginLoader.getList() + // console.log(pluginList.length) const list = [] for (let i in pluginList) { const plugin = picgo.pluginLoader.getPlugin(pluginList[i]) @@ -65,7 +66,8 @@ const handleGetPluginList = (ipcMain, STORE_PATH, CONFIG_PATH) => { } }, enabled: picgo.getConfig(`plugins.${pluginList[i]}`), - homepage: pluginPKG.homepage ? pluginPKG.homepage : '' + homepage: pluginPKG.homepage ? pluginPKG.homepage : '', + ing: false } list.push(obj) } @@ -91,7 +93,7 @@ const handlePluginUninstall = (ipcMain, STORE_PATH, CONFIG_PATH) => { const picgo = new PicGo(CONFIG_PATH) const pluginHandler = new PluginHandler(picgo) picgo.on('uninstallSuccess', notice => { - event.sender.send('installSuccess', notice.body[0].replace(/picgo-plugin-/, '')) + event.sender.send('uninstallSuccess', notice.body[0].replace(/picgo-plugin-/, '')) }) pluginHandler.uninstall([`picgo-plugin-${msg}`]) picgo.cmd.program.removeAllListeners() diff --git a/src/main/utils/uploader.js b/src/main/utils/uploader.js index a529f9a..42e99a5 100644 --- a/src/main/utils/uploader.js +++ b/src/main/utils/uploader.js @@ -74,69 +74,78 @@ const waitForRename = (window, id) => { }) } -const uploader = (img, type, webContents) => { - const win = BrowserWindow.fromWebContents(webContents) - const picgo = new PicGo(CONFIG_PATH) - picgo.config.debug = true - let input = img +class Uploader { + constructor (img, type, webContents) { + this.img = img + this.type = type + this.webContents = webContents + } - picgo.helper.beforeUploadPlugins.register('renameFn', { - handle: async ctx => { - const rename = picgo.getConfig('settings.rename') - const autoRename = picgo.getConfig('settings.autoRename') - await Promise.all(ctx.output.map(async (item, index) => { - let name - let fileName - if (autoRename) { - fileName = dayjs().add(index, 'second').format('YYYYMMDDHHmmss') + item.extname - } else { - fileName = item.fileName - } - if (rename) { - const window = createRenameWindow(win) - await waitForShow(window.webContents) - window.webContents.send('rename', fileName, window.webContents.id) - name = await waitForRename(window, window.webContents.id) - } - item.fileName = name || fileName - })) - } - }) + upload () { + const win = BrowserWindow.fromWebContents(this.webContents) + const picgo = new PicGo(CONFIG_PATH) + console.log(picgo.pluginLoader.getList()) + picgo.config.debug = true + let input = this.img - picgo.on('beforeTransform', ctx => { - if (ctx.getConfig('settings.uploadNotification')) { - const notification = new Notification({ - title: '上传进度', - body: '正在上传' - }) - notification.show() - } - }) - - picgo.upload(input) - - picgo.on('notification', message => { - const notification = new Notification(message) - notification.show() - }) - - picgo.on('uploadProgress', progress => { - webContents.send('uploadProgress', progress) - }) - - return new Promise((resolve) => { - picgo.on('finished', ctx => { - if (ctx.output.every(item => item.imgUrl)) { - resolve(ctx.output) - } else { - resolve(false) + picgo.helper.beforeUploadPlugins.register('renameFn', { + handle: async ctx => { + const rename = picgo.getConfig('settings.rename') + const autoRename = picgo.getConfig('settings.autoRename') + await Promise.all(ctx.output.map(async (item, index) => { + let name + let fileName + if (autoRename) { + fileName = dayjs().add(index, 'second').format('YYYYMMDDHHmmss') + item.extname + } else { + fileName = item.fileName + } + if (rename) { + const window = createRenameWindow(win) + await waitForShow(window.webContents) + window.webContents.send('rename', fileName, window.webContents.id) + name = await waitForRename(window, window.webContents.id) + } + item.fileName = name || fileName + })) } }) - picgo.on('failed', ctx => { - console.log(ctx) - resolve(false) + + picgo.on('beforeTransform', ctx => { + if (ctx.getConfig('settings.uploadNotification')) { + const notification = new Notification({ + title: '上传进度', + body: '正在上传' + }) + notification.show() + } }) - }) + + picgo.upload(input) + + picgo.on('notification', message => { + const notification = new Notification(message) + notification.show() + }) + + picgo.on('uploadProgress', progress => { + this.webContents.send('uploadProgress', progress) + }) + + return new Promise((resolve) => { + picgo.on('finished', ctx => { + if (ctx.output.every(item => item.imgUrl)) { + resolve(ctx.output) + } else { + resolve(false) + } + }) + picgo.on('failed', ctx => { + console.log(ctx) + resolve(false) + }) + }) + } } -export default uploader +export default Uploader diff --git a/src/renderer/pages/Plugin.vue b/src/renderer/pages/Plugin.vue index d6752f7..af9846a 100644 --- a/src/renderer/pages/Plugin.vue +++ b/src/renderer/pages/Plugin.vue @@ -18,7 +18,10 @@ -
+
{{ item.name }} {{ ' ' + item.version }}
@@ -32,25 +35,33 @@
@@ -81,7 +92,6 @@