diff --git a/src/main/index.js b/src/main/index.js index 8d22545d..0641c61a 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -151,10 +151,11 @@ function createTray () { tray.on('drop-files', async (event, files) => { const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown' - const imgs = await new Uploader(files, 'imgFromPath', window.webContents).upload() + const imgs = await new Uploader(files, window.webContents).upload() if (imgs !== false) { for (let i in imgs) { - clipboard.writeText(pasteTemplate(pasteStyle, imgs[i].imgUrl)) + const url = imgs[i].url || imgs[i].imgUrl + clipboard.writeText(pasteTemplate(pasteStyle, url)) const notification = new Notification({ title: '上传成功', body: imgs[i].imgUrl, @@ -163,10 +164,8 @@ function createTray () { setTimeout(() => { notification.show() }, i * 100) + db.read().get('uploaded').insert(imgs[i]).write() } - imgs.forEach(item => { - db.read().get('uploaded').insert(item).write() - }) window.webContents.send('dragFiles', imgs) } }) @@ -332,16 +331,15 @@ const uploadClipboardFiles = async () => { if (img !== false) { if (img.length > 0) { const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown' - clipboard.writeText(pasteTemplate(pasteStyle, img[0].imgUrl)) + const url = img[0].url || img[0].imgUrl + clipboard.writeText(pasteTemplate(pasteStyle, url)) const notification = new Notification({ title: '上传成功', body: img[0].imgUrl, icon: img[0].imgUrl }) notification.show() - img.forEach(item => { - db.read().get('uploaded').insert(item).write() - }) + db.read().get('uploaded').insert(img[0]).write() window.webContents.send('clipboardFiles', []) window.webContents.send('uploadFiles', img) if (settingWindow) { @@ -361,10 +359,11 @@ picgoCoreIPC(app, ipcMain) // from macOS tray ipcMain.on('uploadClipboardFiles', async (evt, file) => { - const img = await new Uploader(file, 'imgFromClipboard', window.webContents).upload() + const img = await new Uploader(file, window.webContents).upload() if (img !== false) { const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown' - clipboard.writeText(pasteTemplate(pasteStyle, img[0].imgUrl)) + const url = img[0].url || img[0].imgUrl + clipboard.writeText(pasteTemplate(pasteStyle, url)) const notification = new Notification({ title: '上传成功', body: img[0].imgUrl, @@ -372,9 +371,7 @@ ipcMain.on('uploadClipboardFiles', async (evt, file) => { icon: img[0].imgUrl }) notification.show() - img.forEach(item => { - db.read().get('uploaded').insert(item).write() - }) + db.read().get('uploaded').insert(img[0]).write() window.webContents.send('clipboardFiles', []) window.webContents.send('uploadFiles') if (settingWindow) { @@ -389,12 +386,13 @@ ipcMain.on('uploadClipboardFilesFromUploadPage', () => { ipcMain.on('uploadChoosedFiles', async (evt, files) => { const input = files.map(item => item.path) - const imgs = await new Uploader(input, 'imgFromUploader', evt.sender).upload() + const imgs = await new Uploader(input, evt.sender).upload() if (imgs !== false) { const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown' let pasteText = '' for (let i in imgs) { - pasteText += pasteTemplate(pasteStyle, imgs[i].imgUrl) + '\r\n' + const url = imgs[i].url || imgs[i].imgUrl + pasteText += pasteTemplate(pasteStyle, url) + '\r\n' const notification = new Notification({ title: '上传成功', body: imgs[i].imgUrl, @@ -403,11 +401,9 @@ ipcMain.on('uploadChoosedFiles', async (evt, files) => { setTimeout(() => { notification.show() }, i * 100) + db.read().get('uploaded').insert(imgs[i]).write() } clipboard.writeText(pasteText) - imgs.forEach(item => { - db.read().get('uploaded').insert(item).write() - }) window.webContents.send('uploadFiles', imgs) if (settingWindow) { settingWindow.webContents.send('updateGallery') diff --git a/src/main/utils/guiApi.js b/src/main/utils/guiApi.js new file mode 100644 index 00000000..1aeaf116 --- /dev/null +++ b/src/main/utils/guiApi.js @@ -0,0 +1,85 @@ +import { + dialog, + BrowserWindow, + clipboard, + Notification +} from 'electron' +import db from '../../datastore' +import Uploader from './uploader' +import pasteTemplate from './pasteTemplate' +const WEBCONTENTS = Symbol('WEBCONTENTS') +const IPCMAIN = Symbol('IPCMAIN') + +class GuiApi { + constructor (ipcMain, webcontents) { + this[WEBCONTENTS] = webcontents + this[IPCMAIN] = ipcMain + } + + /** + * for plugin showInputBox + * @param {object} options + * return type is string or '' + */ + showInputBox (options) { + if (options === undefined) { + options = { + title: '', + placeholder: '' + } + } + this[WEBCONTENTS].send('showInputBox', options) + return new Promise((resolve, reject) => { + this[IPCMAIN].once('showInputBox', (event, value) => { + resolve(value) + }) + }) + } + + /** + * for plugin show file explorer + * @param {object} options + */ + showFileExplorer (options) { + if (options === undefined) { + options = {} + } + return new Promise((resolve, reject) => { + dialog.showOpenDialog(BrowserWindow.fromWebContents(this[WEBCONTENTS]), options, filename => { + resolve(filename) + }) + }) + } + + /** + * for plugin to upload file + * @param {array} input + */ + async upload (input) { + const imgs = await new Uploader(input, this[WEBCONTENTS]).upload() + if (imgs !== false) { + const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown' + let pasteText = '' + for (let i in imgs) { + const url = imgs[i].url || imgs[i].imgUrl + pasteText += pasteTemplate(pasteStyle, url) + '\r\n' + const notification = new Notification({ + title: '上传成功', + body: imgs[i].imgUrl, + icon: imgs[i].imgUrl + }) + setTimeout(() => { + notification.show() + }, i * 100) + db.read().get('uploaded').insert(imgs[i]).write() + } + clipboard.writeText(pasteText) + this[WEBCONTENTS].send('uploadFiles', imgs) + this[WEBCONTENTS].send('updateGallery') + return imgs + } + return [] + } +} + +export default GuiApi diff --git a/src/main/utils/picgoCoreIPC.js b/src/main/utils/picgoCoreIPC.js index 6d2ac9d5..43275f60 100644 --- a/src/main/utils/picgoCoreIPC.js +++ b/src/main/utils/picgoCoreIPC.js @@ -1,4 +1,5 @@ import path from 'path' +import GuiApi from './guiApi' // eslint-disable-next-line const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require @@ -50,6 +51,7 @@ const handleGetPluginList = (ipcMain, STORE_PATH, CONFIG_PATH) => { description: pluginPKG.description, logo: 'file://' + path.join(pluginPath, 'logo.png').split(path.sep).join('/'), version: pluginPKG.version, + gui: pluginPKG.gui || false, config: { plugin: { name: pluginList[i].replace(/picgo-plugin-/, ''), @@ -66,6 +68,7 @@ const handleGetPluginList = (ipcMain, STORE_PATH, CONFIG_PATH) => { }, enabled: picgo.getConfig(`plugins.${pluginList[i]}`), homepage: pluginPKG.homepage ? pluginPKG.homepage : '', + guiActions: typeof plugin.guiActions === 'function', ing: false } list.push(obj) @@ -125,6 +128,17 @@ const handleGetPicBedConfig = (ipcMain, CONFIG_PATH) => { }) } +const handlePluginActions = (ipcMain, CONFIG_PATH) => { + ipcMain.on('pluginActions', (event, name) => { + const picgo = new PicGo(CONFIG_PATH) + const plugin = picgo.pluginLoader.getPlugin(`picgo-plugin-${name}`) + const guiApi = new GuiApi(ipcMain, event.sender) + if (plugin.guiActions && typeof plugin.guiActions === 'function') { + plugin.guiActions(picgo, guiApi) + } + }) +} + export default (app, ipcMain) => { const STORE_PATH = app.getPath('userData') const CONFIG_PATH = path.join(STORE_PATH, '/data.json') @@ -133,4 +147,5 @@ export default (app, ipcMain) => { handlePluginUninstall(ipcMain, CONFIG_PATH) handlePluginUpdate(ipcMain, CONFIG_PATH) handleGetPicBedConfig(ipcMain, CONFIG_PATH) + handlePluginActions(ipcMain, CONFIG_PATH) } diff --git a/src/main/utils/uploader.js b/src/main/utils/uploader.js index 5dbd3f7f..e6224d50 100644 --- a/src/main/utils/uploader.js +++ b/src/main/utils/uploader.js @@ -75,9 +75,8 @@ const waitForRename = (window, id) => { } class Uploader { - constructor (img, type, webContents) { + constructor (img, webContents) { this.img = img - this.type = type this.webContents = webContents } @@ -142,7 +141,11 @@ class Uploader { } }) picgo.on('failed', ctx => { - console.log(ctx) + const notification = new Notification({ + title: '上传失败', + body: '请检查配置和上传的文件是否符合要求' + }) + notification.show() resolve(false) }) }) diff --git a/src/renderer/pages/Gallery.vue b/src/renderer/pages/Gallery.vue index 4bed9584..b3dcdda3 100644 --- a/src/renderer/pages/Gallery.vue +++ b/src/renderer/pages/Gallery.vue @@ -79,7 +79,7 @@ >