diff --git a/package.json b/package.json index 733194c..fab46f5 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "keycode": "^2.2.0", "lodash-id": "^0.14.0", "lowdb": "^1.0.0", - "picgo": "^1.3.7", + "picgo": "^1.4.2", "vue": "^2.6.10", "vue-gallery": "^2.0.1", "vue-lazyload": "^1.2.6", diff --git a/src/background.ts b/src/background.ts index 0384667..c936959 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1,6 +1,5 @@ 'use strict' -import Uploader from '~/main/utils/uploader' import { app, BrowserWindow, @@ -21,6 +20,8 @@ import { installVueDevtools } from 'vue-cli-plugin-electron-builder/lib' import db from '#/datastore' +import picgo from '~/main/utils/picgo' +import uploader from '~/main/utils/uploader' import beforeOpen from '~/main/utils/beforeOpen' import pasteTemplate from '#/utils/pasteTemplate' import updateChecker from '~/main/utils/updateChecker' @@ -63,14 +64,16 @@ const miniWinURL = isDevelopment fixPath() function createContextMenu () { - const picBeds = getPicBeds(app) + const picBeds = getPicBeds() const submenu = picBeds.map(item => { return { label: item.name, type: 'radio', checked: db.get('picBed.current') === item.type, click () { - db.set('picBed.current', item.type) + picgo.saveConfig({ + 'picBed.current': item.type + }) if (settingWindow) { settingWindow.webContents.send('syncPicBed') } @@ -193,7 +196,7 @@ function createTray () { tray.on('drop-files', async (event: Event, files: string[]) => { const pasteStyle = db.get('settings.pasteStyle') || 'markdown' - const imgs = await new Uploader(files, window!.webContents).upload() + const imgs = await uploader.setWebContents(window!.webContents).upload(files) if (imgs !== false) { for (let i = 0; i < imgs.length; i++) { clipboard.writeText(pasteTemplate(pasteStyle, imgs[i])) @@ -374,7 +377,7 @@ const uploadClipboardFiles = async () => { } else { win = settingWindow || window || createSettingWindow() } - let img = await new Uploader(undefined, win!.webContents).upload() + let img = await uploader.setWebContents(win!.webContents).upload() if (img !== false) { if (img.length > 0) { const pasteStyle = db.get('settings.pasteStyle') || 'markdown' @@ -403,7 +406,7 @@ const uploadClipboardFiles = async () => { const uploadChoosedFiles = async (webContents: WebContents, files: IFileWithPath[]) => { const input = files.map(item => item.path) - const imgs = await new Uploader(input, webContents).upload() + const imgs = await uploader.setWebContents(webContents).upload(input) if (imgs !== false) { const pasteStyle = db.get('settings.pasteStyle') || 'markdown' let pasteText = '' @@ -427,11 +430,11 @@ const uploadChoosedFiles = async (webContents: WebContents, files: IFileWithPath } } -picgoCoreIPC(app, ipcMain) +picgoCoreIPC() // from macOS tray ipcMain.on('uploadClipboardFiles', async () => { - const img = await new Uploader(undefined, window!.webContents).upload() + const img = await uploader.setWebContents(window!.webContents).upload() if (img !== false) { const pasteStyle = db.get('settings.pasteStyle') || 'markdown' clipboard.writeText(pasteTemplate(pasteStyle, img[0])) @@ -536,7 +539,7 @@ ipcMain.on('syncPicBed', () => { }) ipcMain.on('getPicBeds', (evt: IpcMainEvent) => { - const picBeds = getPicBeds(app) + const picBeds = getPicBeds() evt.sender.send('getPicBeds', picBeds) evt.returnValue = picBeds }) diff --git a/src/main.ts b/src/main.ts index 717e183..2d54876 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,6 +6,8 @@ import ElementUI from 'element-ui' import { webFrame } from 'electron' import 'element-ui/lib/theme-chalk/index.css' import VueLazyLoad from 'vue-lazyload' +import axios from 'axios' +import mainMixin from './renderer/utils/mainMixin' webFrame.setVisualZoomLevelLimits(1, 1) webFrame.setLayoutZoomLevelLimits(0, 0) @@ -22,9 +24,11 @@ Vue.prototype.$builtInPicBed = [ 'github' ] Vue.prototype.$db = db +Vue.prototype.$http = axios Vue.use(ElementUI) Vue.use(VueLazyLoad) +Vue.mixin(mainMixin) new Vue({ router, diff --git a/src/main/utils/getPicBeds.ts b/src/main/utils/getPicBeds.ts index 35c2dec..06a17d8 100644 --- a/src/main/utils/getPicBeds.ts +++ b/src/main/utils/getPicBeds.ts @@ -1,14 +1,7 @@ -import path from 'path' import db from '#/datastore' -import { App } from 'electron' -// eslint-disable-next-line -const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require +import picgo from './picgo' -const getPicBeds = (app: App) => { - const PicGo = requireFunc('picgo') - const STORE_PATH = app.getPath('userData') - const CONFIG_PATH = path.join(STORE_PATH, '/data.json') - const picgo = new PicGo(CONFIG_PATH) +const getPicBeds = () => { const picBedTypes = picgo.helper.uploader.getIdList() const picBedFromDB = db.get('picBed.list') || [] const picBeds = picBedTypes.map((item: string) => { diff --git a/src/main/utils/guiApi.ts b/src/main/utils/guiApi.ts index d91f74c..a0fd243 100644 --- a/src/main/utils/guiApi.ts +++ b/src/main/utils/guiApi.ts @@ -7,7 +7,7 @@ import { ipcMain } from 'electron' import db from '#/datastore' -import Uploader from './uploader' +import uploader from './uploader' import pasteTemplate from '#/utils/pasteTemplate' const WEBCONTENTS = Symbol('WEBCONTENTS') @@ -52,7 +52,7 @@ class GuiApi implements IGuiApi { } async upload (input: IUploadOption) { - const imgs = await new Uploader(input, this[WEBCONTENTS]).upload() + const imgs = await uploader.setWebContents(this[WEBCONTENTS]).upload(input) if (imgs !== false) { const pasteStyle = db.get('settings.pasteStyle') || 'markdown' let pasteText = '' diff --git a/src/main/utils/picgo.ts b/src/main/utils/picgo.ts new file mode 100644 index 0000000..6eae582 --- /dev/null +++ b/src/main/utils/picgo.ts @@ -0,0 +1,19 @@ +import PicGoCore from '~/universal/types/picgo' +import { + app +} from 'electron' +import path from 'path' +// eslint-disable-next-line +const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require +const PicGo = requireFunc('picgo') as typeof PicGoCore +const STORE_PATH = app.getPath('userData') + +const CONFIG_PATH = path.join(STORE_PATH, '/data.json') + +const picgo = new PicGo(CONFIG_PATH) +picgo.setConfig({ + debug: true, + PICGO_ENV: 'GUI' +}) + +export default picgo as PicGoCore diff --git a/src/main/utils/picgoCoreIPC.ts b/src/main/utils/picgoCoreIPC.ts index 4d8eeb1..7b36928 100644 --- a/src/main/utils/picgoCoreIPC.ts +++ b/src/main/utils/picgoCoreIPC.ts @@ -1,14 +1,24 @@ import path from 'path' import GuiApi from './guiApi' -import { dialog, shell, IpcMain, IpcMainEvent, App } from 'electron' -import db from '#/datastore' +import { + dialog, + shell, + IpcMain, + IpcMainEvent, + App, + ipcMain, + app +} from 'electron' import PicGoCore from '~/universal/types/picgo' import { IPicGoHelperType } from '#/types/enum' +import shortKeyHandler from './shortKeyHandler' +import picgo from '~/main/utils/picgo' // eslint-disable-next-line const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require -const PicGo = requireFunc('picgo') as typeof PicGoCore -const PluginHandler = requireFunc('picgo/dist/lib/PluginHandler').default +// const PluginHandler = requireFunc('picgo/dist/lib/PluginHandler').default +const STORE_PATH = app.getPath('userData') +// const CONFIG_PATH = path.join(STORE_PATH, '/data.json') type PicGoNotice = { title: string, @@ -48,9 +58,8 @@ const handleConfigWithFunction = (config: any[]) => { return config } -const handleGetPluginList = (ipcMain: IpcMain, STORE_PATH: string, CONFIG_PATH: string) => { +const handleGetPluginList = () => { ipcMain.on('getPluginList', (event: IpcMainEvent) => { - const picgo = new PicGo(CONFIG_PATH) const pluginList = picgo.pluginLoader.getList() const list = [] for (let i in pluginList) { @@ -102,48 +111,49 @@ const handleGetPluginList = (ipcMain: IpcMain, STORE_PATH: string, CONFIG_PATH: }) } -const handlePluginInstall = (ipcMain: IpcMain, CONFIG_PATH: string) => { +const handlePluginInstall = () => { ipcMain.on('installPlugin', async (event: IpcMainEvent, msg: string) => { - const picgo = new PicGo(CONFIG_PATH) - const pluginHandler = new PluginHandler(picgo) - picgo.on('installSuccess', (notice: PicGoNotice) => { + picgo.once('installSuccess', (notice: PicGoNotice) => { event.sender.send('installSuccess', notice.body[0].replace(/picgo-plugin-/, '')) + shortKeyHandler.registerPluginShortKey(notice.body[0]) + picgo.removeAllListeners('installFailed') }) - picgo.on('failed', () => { + picgo.once('installFailed', () => { handleNPMError() + picgo.removeAllListeners('installSuccess') }) - await pluginHandler.uninstall([msg]) - pluginHandler.install([msg]) + await picgo.pluginHandler.install([msg]) picgo.cmd.program.removeAllListeners() }) } -const handlePluginUninstall = (ipcMain: IpcMain, CONFIG_PATH: string) => { +const handlePluginUninstall = () => { ipcMain.on('uninstallPlugin', async (event: IpcMainEvent, msg: string) => { - const picgo = new PicGo(CONFIG_PATH) - const pluginHandler = new PluginHandler(picgo) - picgo.on('uninstallSuccess', (notice: PicGoNotice) => { + picgo.once('uninstallSuccess', (notice: PicGoNotice) => { event.sender.send('uninstallSuccess', notice.body[0].replace(/picgo-plugin-/, '')) + shortKeyHandler.unregisterPluginShortKey(notice.body[0]) + picgo.removeAllListeners('uninstallFailed') }) - picgo.on('failed', () => { + picgo.once('uninstallFailed', () => { handleNPMError() + picgo.removeAllListeners('uninstallSuccess') }) - await pluginHandler.uninstall([msg]) + await picgo.pluginHandler.uninstall([msg]) picgo.cmd.program.removeAllListeners() }) } -const handlePluginUpdate = (ipcMain: IpcMain, CONFIG_PATH: string) => { +const handlePluginUpdate = () => { ipcMain.on('updatePlugin', async (event: IpcMainEvent, msg: string) => { - const picgo = new PicGo(CONFIG_PATH) - const pluginHandler = new PluginHandler(picgo) - picgo.on('updateSuccess', notice => { + picgo.once('updateSuccess', (notice: { body: string[], title: string }) => { event.sender.send('updateSuccess', notice.body[0].replace(/picgo-plugin-/, '')) + picgo.removeAllListeners('updateFailed') }) - picgo.on('failed', () => { + picgo.once('updateFailed', () => { handleNPMError() + picgo.removeAllListeners('updateSuccess') }) - await pluginHandler.update([msg]) + await picgo.pluginHandler.update([msg]) picgo.cmd.program.removeAllListeners() }) } @@ -160,9 +170,8 @@ const handleNPMError = () => { }) } -const handleGetPicBedConfig = (ipcMain: IpcMain, CONFIG_PATH: string) => { +const handleGetPicBedConfig = () => { ipcMain.on('getPicBedConfig', (event: IpcMainEvent, type: string) => { - const picgo = new PicGo(CONFIG_PATH) const name = picgo.helper.uploader.get(type).name || type if (picgo.helper.uploader.get(type).config) { const config = handleConfigWithFunction(picgo.helper.uploader.get(type).config(picgo)) @@ -174,9 +183,8 @@ const handleGetPicBedConfig = (ipcMain: IpcMain, CONFIG_PATH: string) => { }) } -const handlePluginActions = (ipcMain: IpcMain, CONFIG_PATH: string) => { +const handlePluginActions = () => { ipcMain.on('pluginActions', (event: IpcMainEvent, name: string, label: string) => { - const picgo = new PicGo(CONFIG_PATH) const plugin = picgo.pluginLoader.getPlugin(`picgo-plugin-${name}`) const guiApi = new GuiApi(event.sender) if (plugin.guiMenu && plugin.guiMenu(picgo).length > 0) { @@ -190,9 +198,8 @@ const handlePluginActions = (ipcMain: IpcMain, CONFIG_PATH: string) => { }) } -const handleRemoveFiles = (ipcMain: IpcMain, CONFIG_PATH: string) => { +const handleRemoveFiles = () => { ipcMain.on('removeFiles', (event: IpcMainEvent, files: ImgInfo[]) => { - const picgo = new PicGo(CONFIG_PATH) const guiApi = new GuiApi(event.sender) setTimeout(() => { picgo.emit('remove', files, guiApi) @@ -200,26 +207,19 @@ const handleRemoveFiles = (ipcMain: IpcMain, CONFIG_PATH: string) => { }) } -// const handlePluginShortKeyRegister = (plugin) => { -// if (plugin.shortKeys && plugin.shortKeys.length > 0) { -// let shortKeyConfig = db.get('settings.shortKey') -// plugin.shortKeys.forEach(item => { -// if (!shortKeyConfig[item.name]) { -// shortKeyConfig[item.name] = item -// } -// }) -// } -// } - -export default (app: App, ipcMain: IpcMain) => { - const STORE_PATH = app.getPath('userData') - const CONFIG_PATH = path.join(STORE_PATH, '/data.json') - handleGetPluginList(ipcMain, STORE_PATH, CONFIG_PATH) - handlePluginInstall(ipcMain, CONFIG_PATH) - handlePluginUninstall(ipcMain, CONFIG_PATH) - handlePluginUpdate(ipcMain, CONFIG_PATH) - handleGetPicBedConfig(ipcMain, CONFIG_PATH) - handlePluginActions(ipcMain, CONFIG_PATH) - handleRemoveFiles(ipcMain, CONFIG_PATH) - // handlePluginShortKeyRegister({}) +const handlePicGoSaveData = () => { + ipcMain.on('picgoSaveData', (event: IpcMainEvent, data: IObj) => { + picgo.saveConfig(data) + }) +} + +export default () => { + handleGetPluginList() + handlePluginInstall() + handlePluginUninstall() + handlePluginUpdate() + handleGetPicBedConfig() + handlePluginActions() + handleRemoveFiles() + handlePicGoSaveData() } diff --git a/src/main/utils/shortKeyHandler.ts b/src/main/utils/shortKeyHandler.ts index cf1315b..c9d26a4 100644 --- a/src/main/utils/shortKeyHandler.ts +++ b/src/main/utils/shortKeyHandler.ts @@ -10,12 +10,7 @@ import logger from './logger' import GuiApi from './guiApi' import db from '#/datastore' import shortKeyService from './shortKeyService' -// eslint-disable-next-line -const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require - -const PicGo = requireFunc('picgo') as typeof PicGoCore -const STORE_PATH = app.getPath('userData') -const CONFIG_PATH = path.join(STORE_PATH, '/data.json') +import picgo from './picgo' class ShortKeyHandler { private isInModifiedMode: boolean = false @@ -40,7 +35,6 @@ class ShortKeyHandler { }) } private initPluginsShortKey () { - const picgo = new PicGo(CONFIG_PATH) const pluginList = picgo.pluginLoader.getList() for (let item of pluginList) { const plugin = picgo.pluginLoader.getPlugin(item) @@ -75,12 +69,14 @@ class ShortKeyHandler { logger.warn(`${command} do not provide a key to bind`) } if (writeFlag) { - db.set(`settings.shortKey.${command}`, { - enable: true, - name: config.name, - label: config.label, - key: config.key - } as IShortKeyConfig) + picgo.saveConfig({ + [`settings.shortKey.${command}`]: { + enable: true, + name: config.name, + label: config.label, + key: config.key + } + }) } } // enable or disable shortKey @@ -88,13 +84,17 @@ class ShortKeyHandler { const command = `${from}:${item.name}` if (item.enable === false) { globalShortcut.unregister(item.key) - db.set(`settings.shortKey.${command}.enable`, false) + picgo.saveConfig({ + [`settings.shortKey.${command}.enable`]: false + }) return true } else { if (globalShortcut.isRegistered(item.key)) { return false } else { - db.set(`settings.shortKey.${command}.enable`, true) + picgo.saveConfig({ + [`settings.shortKey.${command}.enable`]: true + }) globalShortcut.register(item.key, () => { this.handler(command) }) @@ -107,7 +107,9 @@ class ShortKeyHandler { const command = `${from}:${item.name}` if (globalShortcut.isRegistered(item.key)) return false globalShortcut.unregister(oldKey) - db.set(`settings.shortKey.${command}.key`, item.key) + picgo.saveConfig({ + [`settings.shortKey.${command}.key`]: item.key + }) globalShortcut.register(item.key, () => { this.handler(`${from}:${item.name}`) }) @@ -122,7 +124,6 @@ class ShortKeyHandler { } else if (command.includes('picgo-plugin-')) { const handler = shortKeyService.getShortKeyHandler(command) if (handler) { - const picgo = new PicGo(CONFIG_PATH) // make sure settingWindow is created bus.once('createSettingWindowDone', (cmd: string, settingWindowId: number) => { if (cmd === command) { @@ -138,7 +139,6 @@ class ShortKeyHandler { } } registerPluginShortKey (pluginName: string) { - const picgo = new PicGo(CONFIG_PATH) const plugin = picgo.pluginLoader.getPlugin(pluginName) if (plugin && plugin.commands) { if (typeof plugin.commands !== 'function') { @@ -170,7 +170,7 @@ class ShortKeyHandler { keyList.forEach(item => { globalShortcut.unregister(item.key) shortKeyService.unregisterCommand(item.command) - db.unset('settings.shortKey', item.command) + picgo.unsetConfig('settings.shortKey', item.command) }) } } diff --git a/src/main/utils/updateChecker.ts b/src/main/utils/updateChecker.ts index 998ac5d..eb848cd 100644 --- a/src/main/utils/updateChecker.ts +++ b/src/main/utils/updateChecker.ts @@ -3,13 +3,8 @@ import db from '#/datastore' import axios from 'axios' import pkg from 'root/package.json' const version = pkg.version -let release = 'https://api.github.com/repos/Molunerfinn/PicGo/releases/latest' +let release = 'https://cdn.jsdelivr.net/gh/Molunerfinn/PicGo/package.json' const downloadUrl = 'https://github.com/Molunerfinn/PicGo/releases/latest' -const isDevelopment = process.env.NODE_ENV !== 'production' - -if (isDevelopment) { - release = `${release}?access_token=${process.env.GITHUB_TOKEN}` -} const checkVersion = async () => { let showTip = db.get('settings.showUpdateTip') @@ -25,7 +20,7 @@ const checkVersion = async () => { console.log(err) } if (res.status === 200) { - const latest = res.data.name + const latest = res.data.version const result = compareVersion2Update(version, latest) if (result) { dialog.showMessageBox({ diff --git a/src/main/utils/uploader.ts b/src/main/utils/uploader.ts index 7244959..84835ed 100644 --- a/src/main/utils/uploader.ts +++ b/src/main/utils/uploader.ts @@ -5,37 +5,15 @@ import { ipcMain, WebContents } from 'electron' -import path from 'path' import dayjs from 'dayjs' -import PicGoCore from '~/universal/types/picgo' +import picgo from '~/main/utils/picgo' +import db from '#/datastore' -// eslint-disable-next-line -const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require -const PicGo = requireFunc('picgo') as typeof PicGoCore -const STORE_PATH = app.getPath('userData') -const CONFIG_PATH = path.join(STORE_PATH, '/data.json') const renameURL = process.env.NODE_ENV === 'development' ? `${(process.env.WEBPACK_DEV_SERVER_URL as string)}#rename-page` : `picgo://./index.html#rename-page` -// type BrowserWindowOptions = { -// height: number, -// width: number, -// show: boolean, -// fullscreenable: boolean, -// resizable: boolean, -// vibrancy: string | any, -// webPreferences: { -// nodeIntegration: boolean, -// nodeIntegrationInWorker: boolean, -// backgroundThrottling: boolean -// }, -// backgroundColor?: string -// autoHideMenuBar?: boolean -// transparent?: boolean -// } - -const createRenameWindow = (win: BrowserWindow) => { +const createRenameWindow = (currentWindow: BrowserWindow) => { let options: IBrowserWindowOptions = { height: 175, width: 300, @@ -60,9 +38,9 @@ const createRenameWindow = (win: BrowserWindow) => { const window = new BrowserWindow(options) window.loadURL(renameURL) // check if this window is visible - if (win.isVisible()) { + if (currentWindow && currentWindow.isVisible()) { // bounds: { x: 821, y: 75, width: 800, height: 450 } - const bounds = win.getBounds() + const bounds = currentWindow.getBounds() const positionX = bounds.x + bounds.width / 2 - 150 let positionY // if is the settingWindow @@ -98,27 +76,34 @@ const waitForRename = (window: BrowserWindow, id: number): Promise } class Uploader { - private picgo: PicGoCore - private webContents: WebContents - private img: undefined | string[] - constructor (img: IUploadOption, webContents: WebContents, picgo: PicGoCore | undefined = undefined) { - this.img = img - this.webContents = webContents - this.picgo = picgo || new PicGo(CONFIG_PATH) + private webContents: WebContents | null = null + private currentWindow: BrowserWindow | null = null + constructor () { + this.init() } - upload (): Promise { - const win = BrowserWindow.fromWebContents(this.webContents) - const picgo = this.picgo - picgo.config.debug = true - // for picgo-core - picgo.config.PICGO_ENV = 'GUI' - let input = this.img + init () { + picgo.on('notification', message => { + const notification = new Notification(message) + notification.show() + }) + picgo.on('uploadProgress', progress => { + this.webContents!.send('uploadProgress', progress) + }) + picgo.on('beforeTransform', ctx => { + if (db.get('settings.uploadNotification')) { + const notification = new Notification({ + title: '上传进度', + body: '正在上传' + }) + notification.show() + } + }) picgo.helper.beforeUploadPlugins.register('renameFn', { handle: async ctx => { - const rename = picgo.getConfig('settings.rename') - const autoRename = picgo.getConfig('settings.autoRename') + const rename = db.get('settings.rename') + const autoRename = db.get('settings.autoRename') await Promise.all(ctx.output.map(async (item, index) => { let name: undefined | string | null let fileName: string | undefined @@ -128,7 +113,7 @@ class Uploader { fileName = item.fileName } if (rename) { - const window = createRenameWindow(win) + const window = createRenameWindow(this.currentWindow!) await waitForShow(window.webContents) window.webContents.send('rename', fileName, window.webContents.id) name = await waitForRename(window, window.webContents.id) @@ -137,46 +122,40 @@ class Uploader { })) } }) + } - picgo.on('beforeTransform', ctx => { - if (ctx.getConfig('settings.uploadNotification')) { - const notification = new Notification({ - title: '上传进度', - body: '正在上传' - }) - notification.show() - } - }) + setWebContents (webContents: WebContents) { + this.webContents = webContents + return this + } - picgo.upload(input) + upload (img?: IUploadOption): Promise { + this.currentWindow = BrowserWindow.fromWebContents(this.webContents!) - picgo.on('notification', message => { - const notification = new Notification(message) - notification.show() - }) - - picgo.on('uploadProgress', progress => { - this.webContents.send('uploadProgress', progress) - }) + picgo.upload(img) return new Promise((resolve) => { - picgo.on('finished', ctx => { + picgo.once('finished', ctx => { if (ctx.output.every((item: ImgInfo) => item.imgUrl)) { resolve(ctx.output) } else { resolve(false) } + picgo.removeAllListeners('failed') }) - picgo.on('failed', ctx => { - const notification = new Notification({ - title: '上传失败', - body: '请检查配置和上传的文件是否符合要求' - }) - notification.show() + picgo.once('failed', ctx => { + setTimeout(() => { + const notification = new Notification({ + title: '上传失败', + body: '请检查配置和上传的文件是否符合要求' + }) + notification.show() + }, 500) + picgo.removeAllListeners('finished') resolve(false) }) }) } } -export default Uploader +export default new Uploader() diff --git a/src/renderer/components/ChoosePicBed.vue b/src/renderer/components/ChoosePicBed.vue index 51115ec..72d1fb0 100644 --- a/src/renderer/components/ChoosePicBed.vue +++ b/src/renderer/components/ChoosePicBed.vue @@ -27,7 +27,9 @@ export default { }, methods: { choosePicBed (val) { - this.$db.set('picBed.current', this.type) + this.letPicGoSaveData({ + 'picBed.current': this.type + }) this.$emit('update:choosed', this.type) } } diff --git a/src/renderer/pages/MiniPage.vue b/src/renderer/pages/MiniPage.vue index 50bf7ce..1de798c 100644 --- a/src/renderer/pages/MiniPage.vue +++ b/src/renderer/pages/MiniPage.vue @@ -143,7 +143,9 @@ export default class extends Vue { type: 'radio', checked: this.$db.get('picBed.current') === item.type, click () { - _this.$db.set('picBed.current', item.type) + _this.letPicGoSaveData({ + 'picBed.current': item.type + }) ipcRenderer.send('syncPicBed') } } diff --git a/src/renderer/pages/PicGoSetting.vue b/src/renderer/pages/PicGoSetting.vue index dd1f443..53a4c57 100644 --- a/src/renderer/pages/PicGoSetting.vue +++ b/src/renderer/pages/PicGoSetting.vue @@ -371,7 +371,9 @@ export default class extends Vue { } confirmProxy () { this.proxyVisible = false - db.set('picBed.proxy', this.proxy) + this.letPicGoSaveData({ + 'picBed.proxy': this.proxy + }) const successNotification = new Notification('设置代理', { body: '设置成功' }) @@ -391,7 +393,9 @@ export default class extends Vue { } return item }) - db.set('picBed.list', list) + this.letPicGoSaveData({ + 'picBed.list': list + }) ipcRenderer.send('getPicBeds') } handleAutoStartChange (val: boolean) { @@ -399,10 +403,14 @@ export default class extends Vue { ipcRenderer.send('autoStart', val) } handleRename (val: boolean) { - db.set('settings.rename', val) + this.letPicGoSaveData({ + 'settings.rename': val + }) } handleAutoRename (val: boolean) { - db.set('settings.autoRename', val) + this.letPicGoSaveData({ + 'settings.autoRename': val + }) } compareVersion2Update (current: string, latest: string) { const currentVersion = current.split('.').map(item => parseInt(item)) @@ -447,7 +455,9 @@ export default class extends Vue { if (this.form.logLevel.length === 0) { return this.$message.error('请选择日志记录等级') } - db.set('settings.logLevel', this.form.logLevel) + this.letPicGoSaveData({ + 'settings.logLevel': this.form.logLevel + }) const successNotification = new Notification('设置日志', { body: '设置成功' }) diff --git a/src/renderer/pages/Plugin.vue b/src/renderer/pages/Plugin.vue index c79d7af..23c7e7c 100644 --- a/src/renderer/pages/Plugin.vue +++ b/src/renderer/pages/Plugin.vue @@ -199,7 +199,9 @@ export default class extends Vue { label: '启用插件', enabled: !plugin.enabled, click () { - _this.$db.set(`picgoPlugins.picgo-plugin-${plugin.name}`, true) + _this.letPicGoSaveData({ + [`picgoPlugins.picgo-plugin-${plugin.name}`]: true + }) plugin.enabled = true _this.getPicBeds() } @@ -207,7 +209,9 @@ export default class extends Vue { label: '禁用插件', enabled: plugin.enabled, click () { - _this.$db.set(`picgoPlugins.picgo-plugin-${plugin.name}`, false) + _this.letPicGoSaveData({ + [`picgoPlugins.picgo-plugin-${plugin.name}`]: false + }) plugin.enabled = false _this.getPicBeds() if (plugin.config.transformer.name) { @@ -334,9 +338,13 @@ export default class extends Vue { toggleTransformer (transformer: string) { let currentTransformer = this.$db.get('picBed.transformer') || 'path' if (currentTransformer === transformer) { - this.$db.set('picBed.transformer', 'path') + this.letPicGoSaveData({ + 'picBed.transformer': 'path' + }) } else { - this.$db.set('picBed.transformer', transformer) + this.letPicGoSaveData({ + 'picBed.transformer': transformer + }) } } async handleConfirmConfig () { @@ -345,13 +353,19 @@ export default class extends Vue { if (result !== false) { switch (this.currentType) { case 'plugin': - this.$db.set(`picgo-plugin-${this.configName}`, result) + this.letPicGoSaveData({ + [`picgo-plugin-${this.configName}`]: result + }) break case 'uploader': - this.$db.set(`picBed.${this.configName}`, result) + this.letPicGoSaveData({ + [`picBed.${this.configName}`]: result + }) break case 'transformer': - this.$db.set(`transformer.${this.configName}`, result) + this.letPicGoSaveData({ + [`transformer.${this.configName}`]: result + }) break } const successNotification = new Notification('设置结果', { @@ -368,9 +382,13 @@ export default class extends Vue { // this.$http.get(`https://api.npms.io/v2/search?q=${val}`) this.$http.get(`https://registry.npmjs.com/-/v1/search?text=${val}`) .then((res: INPMSearchResult) => { - this.pluginList = res.data.objects.map((item: INPMSearchResultObject) => { - return this.handleSearchResult(item) - }) + this.pluginList = res.data.objects + .filter((item:INPMSearchResultObject) => { + return item.package.name.includes('picgo-plugin-') + }) + .map((item: INPMSearchResultObject) => { + return this.handleSearchResult(item) + }) this.loading = false }) .catch(err => { @@ -404,13 +422,17 @@ export default class extends Vue { if (item === 'uploader') { const current = this.$db.get('picBed.current') if (current === name) { - this.$db.set('picBed.current', 'smms') + this.letPicGoSaveData({ + 'picBed.current': 'smms' + }) } } if (item === 'transformer') { const current = this.$db.get('picBed.transformer') if (current === name) { - this.$db.set('picBed.transformer', 'path') + this.letPicGoSaveData({ + 'picBed.transformer': 'path' + }) } } } @@ -422,6 +444,9 @@ export default class extends Vue { goAwesomeList () { remote.shell.openExternal('https://github.com/PicGo/Awesome-PicGo') } + letPicGoSaveData (data: IObj) { + ipcRenderer.send('picgoSaveData', data) + } beforeDestroy () { ipcRenderer.removeAllListeners('pluginList') ipcRenderer.removeAllListeners('installSuccess') diff --git a/src/renderer/pages/Upload.vue b/src/renderer/pages/Upload.vue index 5511043..994ac87 100644 --- a/src/renderer/pages/Upload.vue +++ b/src/renderer/pages/Upload.vue @@ -166,7 +166,9 @@ export default class extends Vue { type: 'radio', checked: this.$db.get('picBed.current') === item.type, click () { - _this.$db.set('picBed.current', item.type) + _this.letPicGoSaveData({ + 'picBed.current': item.type + }) ipcRenderer.send('syncPicBed') } } diff --git a/src/renderer/pages/picbeds/AliYun.vue b/src/renderer/pages/picbeds/AliYun.vue index 6597b46..5d666be 100644 --- a/src/renderer/pages/picbeds/AliYun.vue +++ b/src/renderer/pages/picbeds/AliYun.vue @@ -90,7 +90,9 @@ export default class extends Vue { // @ts-ignore this.$refs.aliyun.validate((valid) => { if (valid) { - this.$db.set('picBed.aliyun', this.form) + this.letPicGoSaveData({ + 'picBed.aliyun': this.form + }) const successNotification = new window.Notification('设置结果', { body: '设置成功' }) diff --git a/src/renderer/pages/picbeds/GitHub.vue b/src/renderer/pages/picbeds/GitHub.vue index 723a6a3..6b272ae 100644 --- a/src/renderer/pages/picbeds/GitHub.vue +++ b/src/renderer/pages/picbeds/GitHub.vue @@ -81,7 +81,9 @@ export default class extends Vue { // @ts-ignore this.$refs.github.validate((valid) => { if (valid) { - this.$db.set('picBed.github', this.form) + this.letPicGoSaveData({ + 'picBed.github': this.form + }) const successNotification = new Notification('设置结果', { body: '设置成功' }) diff --git a/src/renderer/pages/picbeds/Imgur.vue b/src/renderer/pages/picbeds/Imgur.vue index 7a4db8a..fda0e9d 100644 --- a/src/renderer/pages/picbeds/Imgur.vue +++ b/src/renderer/pages/picbeds/Imgur.vue @@ -58,7 +58,9 @@ export default class extends Vue { // @ts-ignore this.$refs.imgur.validate((valid) => { if (valid) { - this.$db.set('picBed.imgur', this.form) + this.letPicGoSaveData({ + 'picBed.imgur': this.form + }) const successNotification = new Notification('设置结果', { body: '设置成功' }) diff --git a/src/renderer/pages/picbeds/Others.vue b/src/renderer/pages/picbeds/Others.vue index ad2ae40..1fb3983 100644 --- a/src/renderer/pages/picbeds/Others.vue +++ b/src/renderer/pages/picbeds/Others.vue @@ -56,7 +56,9 @@ export default class extends Vue { // @ts-ignore const result = await this.$refs.configForm.validate() if (result !== false) { - this.$db.set(`picBed.${this.type}`, result) + this.letPicGoSaveData({ + [`picBed.${this.type}`]: result + }) const successNotification = new Notification('设置结果', { body: '设置成功' }) @@ -66,7 +68,9 @@ export default class extends Vue { } } setDefaultPicBed (type: string) { - this.$db.set('picBed.current', type) + this.letPicGoSaveData({ + 'picBed.current': type + }) // @ts-ignore 来自mixin的数据 this.defaultPicBed = type const successNotification = new Notification('设置默认图床', { diff --git a/src/renderer/pages/picbeds/Qiniu.vue b/src/renderer/pages/picbeds/Qiniu.vue index 821a8d6..af87385 100644 --- a/src/renderer/pages/picbeds/Qiniu.vue +++ b/src/renderer/pages/picbeds/Qiniu.vue @@ -98,7 +98,9 @@ export default class extends Vue { // @ts-ignore this.$refs.qiniu.validate((valid) => { if (valid) { - this.$db.set('picBed.qiniu', this.form) + this.letPicGoSaveData({ + 'picBed.qiniu': this.form + }) const successNotification = new Notification('设置结果', { body: '设置成功' }) diff --git a/src/renderer/pages/picbeds/SMMS.vue b/src/renderer/pages/picbeds/SMMS.vue index 7a2e40e..b12c318 100644 --- a/src/renderer/pages/picbeds/SMMS.vue +++ b/src/renderer/pages/picbeds/SMMS.vue @@ -24,7 +24,9 @@ import mixin from '@/utils/ConfirmButtonMixin' }) export default class extends Vue { confirm () { - this.$db.set('picBed.smms', true) + this.letPicGoSaveData({ + 'picBed.smms': true + }) // @ts-ignore 来自mixin this.setDefaultPicBed('smms') const successNotification = new window.Notification('设置结果', { diff --git a/src/renderer/pages/picbeds/TcYun.vue b/src/renderer/pages/picbeds/TcYun.vue index 794775e..823cdf2 100644 --- a/src/renderer/pages/picbeds/TcYun.vue +++ b/src/renderer/pages/picbeds/TcYun.vue @@ -114,7 +114,9 @@ export default class extends Vue { // @ts-ignore this.$refs.tcyun.validate((valid) => { if (valid) { - this.$db.set('picBed.tcyun', this.form) + this.letPicGoSaveData({ + 'picBed.tcyun': this.form + }) const successNotification = new window.Notification('设置结果', { body: '设置成功' }) diff --git a/src/renderer/pages/picbeds/UpYun.vue b/src/renderer/pages/picbeds/UpYun.vue index c381ebc..80e12b6 100644 --- a/src/renderer/pages/picbeds/UpYun.vue +++ b/src/renderer/pages/picbeds/UpYun.vue @@ -89,7 +89,9 @@ export default class extends Vue { // @ts-ignore this.$refs.tcyun.validate((valid) => { if (valid) { - this.$db.set('picBed.upyun', this.form) + this.letPicGoSaveData({ + 'picBed.upyun': this.form + }) const successNotification = new Notification('设置结果', { body: '设置成功' }) diff --git a/src/renderer/pages/picbeds/Weibo.vue b/src/renderer/pages/picbeds/Weibo.vue index 98acb99..1fa98ed 100644 --- a/src/renderer/pages/picbeds/Weibo.vue +++ b/src/renderer/pages/picbeds/Weibo.vue @@ -91,12 +91,14 @@ export default { confirm (formName) { this.$refs[formName].validate((valid) => { if (valid) { - this.$db.set('picBed.weibo', { - username: this.form.username, - password: this.form.password, - quality: this.quality, - cookie: this.form.cookie, - chooseCookie: this.chooseCookie + this.letPicGoSaveData({ + 'picBed.weibo': { + username: this.form.username, + password: this.form.password, + quality: this.quality, + cookie: this.form.cookie, + chooseCookie: this.chooseCookie + } }) const successNotification = new window.Notification('设置结果', { body: '设置成功' diff --git a/src/renderer/utils/ConfirmButtonMixin.ts b/src/renderer/utils/ConfirmButtonMixin.ts index 7f846fb..98e1fc6 100644 --- a/src/renderer/utils/ConfirmButtonMixin.ts +++ b/src/renderer/utils/ConfirmButtonMixin.ts @@ -1,9 +1,12 @@ import { Component, Vue } from 'vue-property-decorator' +import { ipcRenderer } from 'electron' @Component export default class extends Vue { defaultPicBed = this.$db.get('picBed.current') setDefaultPicBed (type: string) { - this.$db.set('picBed.current', type) + this.letPicGoSaveData({ + 'picBed.current': type + }) this.defaultPicBed = type const successNotification = new Notification('设置默认图床', { body: '设置成功' diff --git a/src/renderer/utils/mainMixin.ts b/src/renderer/utils/mainMixin.ts new file mode 100644 index 0000000..20b20a3 --- /dev/null +++ b/src/renderer/utils/mainMixin.ts @@ -0,0 +1,8 @@ +import { Component, Vue } from 'vue-property-decorator' +import { ipcRenderer } from 'electron' +@Component +export default class extends Vue { + letPicGoSaveData (data: IObj) { + ipcRenderer.send('picgoSaveData', data) + } +} diff --git a/src/universal/datastore/index.ts b/src/universal/datastore/index.ts index 596e722..b529627 100644 --- a/src/universal/datastore/index.ts +++ b/src/universal/datastore/index.ts @@ -59,7 +59,7 @@ class DB { return this.read().get(key).insert(value).write() } unset (key: string, value: any): boolean { - return this.read().get(key).unset(value).write() + return this.read().get(key).unset(value).value() } } diff --git a/src/universal/types/extra-vue.d.ts b/src/universal/types/extra-vue.d.ts index f7133cc..082156c 100644 --- a/src/universal/types/extra-vue.d.ts +++ b/src/universal/types/extra-vue.d.ts @@ -8,5 +8,6 @@ declare module 'vue/types/vue' { $db: typeof db $http: typeof axios $builtInPicBed: string[] + letPicGoSaveData(data: IObj): void } } diff --git a/src/universal/types/types.d.ts b/src/universal/types/types.d.ts index 823d393..d37a7d9 100644 --- a/src/universal/types/types.d.ts +++ b/src/universal/types/types.d.ts @@ -162,7 +162,7 @@ interface IShowInputBoxOption { type IShowFileExplorerOption = IObj -type IUploadOption = undefined | string[] +type IUploadOption = string[] interface IShowNotificationOption { title: string diff --git a/yarn.lock b/yarn.lock index 81494b4..a955526 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2870,12 +2870,15 @@ commander@~2.8.1: dependencies: graceful-readlink ">= 1.0.0" -comment-json@^1.1.3: - version "1.1.3" - resolved "https://registry.yarnpkg.com/comment-json/-/comment-json-1.1.3.tgz#6986c3330fee0c4c9e00c2398cd61afa5d8f239e" - integrity sha1-aYbDMw/uDEyeAMI5jNYa+l2PI54= +comment-json@^2.3.1: + version "2.3.1" + resolved "https://registry.npm.taobao.org/comment-json/download/comment-json-2.3.1.tgz#40f24f573ffece9b9a87806244c330d614a32604" + integrity sha1-QPJPVz/+zpuah4BiRMMw1hSjJgQ= dependencies: - json-parser "^1.0.0" + core-util-is "^1.0.2" + esprima "^4.0.1" + has-own-prop "^2.0.0" + repeat-string "^1.6.1" commitizen@^4.0.3: version "4.0.3" @@ -3262,7 +3265,7 @@ core-js@^3.3.2, core-js@^3.4.3: resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.6.0.tgz#2b854e451de1967d1e29896025cdc13a2518d9ea" integrity sha512-AHPTNKzyB+YwgDWoSOCaid9PUSEF6781vsfiK8qUz62zRR448/XgK2NtCbpiUGizbep8Lrpt0Du19PpGGZvw3Q== -core-util-is@1.0.2, core-util-is@~1.0.0: +core-util-is@1.0.2, core-util-is@^1.0.2, core-util-is@~1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac= @@ -4534,12 +4537,7 @@ espree@^5.0.1: acorn-jsx "^5.0.0" eslint-visitor-keys "^1.0.0" -esprima@^2.7.0: - version "2.7.3" - resolved "https://registry.yarnpkg.com/esprima/-/esprima-2.7.3.tgz#96e3b70d5779f6ad49cd032673d1c312767ba581" - integrity sha1-luO3DVd59q1JzQMmc9HDEnZ7pYE= - -esprima@^4.0.0: +esprima@^4.0.0, esprima@^4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71" integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A== @@ -5628,6 +5626,11 @@ has-flag@^4.0.0: resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b" integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ== +has-own-prop@^2.0.0: + version "2.0.0" + resolved "https://registry.npm.taobao.org/has-own-prop/download/has-own-prop-2.0.0.tgz#f0f95d58f65804f5d218db32563bb85b8e0417af" + integrity sha1-8PldWPZYBPXSGNsyVju4W44EF68= + has-symbol-support-x@^1.4.1: version "1.4.2" resolved "https://registry.yarnpkg.com/has-symbol-support-x/-/has-symbol-support-x-1.4.2.tgz#1409f98bc00247da45da67cee0a36f282ff26455" @@ -6661,13 +6664,6 @@ json-parse-better-errors@^1.0.1, json-parse-better-errors@^1.0.2: resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9" integrity sha512-mrqyZKfX5EhL7hvqcV6WG1yYjnjeuYDzDhhcAAUrq8Po85NBQBJP+ZDUT75qZQ98IkUoBqdkExkukOU7Ts2wrw== -json-parser@^1.0.0: - version "1.1.5" - resolved "https://registry.yarnpkg.com/json-parser/-/json-parser-1.1.5.tgz#e62ec5261d1a6a5fc20e812a320740c6d9005677" - integrity sha1-5i7FJh0aal/CDoEqMgdAxtkAVnc= - dependencies: - esprima "^2.7.0" - json-schema-traverse@^0.4.1: version "0.4.1" resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz#69f6a87d9513ab8bb8fe63bdb0979c448e684660" @@ -8280,14 +8276,14 @@ performance-now@^2.1.0: resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b" integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns= -picgo@^1.3.7: - version "1.3.7" - resolved "https://registry.yarnpkg.com/picgo/-/picgo-1.3.7.tgz#b152ec8235062a5638243c20afbafe87bf295c47" - integrity sha512-iJamLDhBy+Io/ePS6G+WT7ncXdmATrDFRFP4RvypqkuIdvPBULhukWyIEbB5hiXaQbYNBK6xx2trxp86YuvyeQ== +picgo@^1.4.2: + version "1.4.2" + resolved "https://registry.npm.taobao.org/picgo/download/picgo-1.4.2.tgz#3a9ca1fb3c7c51c0b94036eac95a842bb022a5dc" + integrity sha1-Opyh+zx8UcC5QDbqyVqEK7Aipdw= dependencies: chalk "^2.4.1" commander "^2.17.0" - comment-json "^1.1.3" + comment-json "^2.3.1" cross-spawn "^6.0.5" dayjs "^1.7.4" download-git-repo "^1.1.0"