mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-06-01 13:59:50 +08:00
@@ -16,7 +16,7 @@ import db, { GalleryDB } from '@core/datastore'
|
||||
import picgo from '@core/picgo'
|
||||
|
||||
import uploader from 'apis/app/uploader'
|
||||
import { uploadClipboardFiles } from 'apis/app/uploader/apis'
|
||||
import { handleSecondaryUpload, uploadClipboardFiles } from 'apis/app/uploader/apis'
|
||||
import windowManager from 'apis/app/window/windowManager'
|
||||
|
||||
import { buildPicBedListMenu } from '~/events/remotes/menu'
|
||||
@@ -309,6 +309,7 @@ export function createTray(tooltip: string) {
|
||||
const pasteStyle = db.get(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN
|
||||
const rawInput = cloneDeep(files)
|
||||
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)!
|
||||
await handleSecondaryUpload(trayWindow.webContents, files, 'tray')
|
||||
const imgs = await uploader.setWebContents(trayWindow.webContents).upload(files)
|
||||
const deleteLocalFile = db.get(configPaths.settings.deleteLocalFile) || false
|
||||
if (imgs !== false) {
|
||||
|
||||
@@ -14,6 +14,7 @@ import pasteTemplate from '~/utils/pasteTemplate'
|
||||
|
||||
import { IPasteStyle, IWindowList } from '#/types/enum'
|
||||
import { configPaths } from '#/utils/configPaths'
|
||||
import { changeCurrentUploader } from '~/utils/handleUploaderConfig'
|
||||
|
||||
const handleClipboardUploading = async (): Promise<false | ImgInfo[]> => {
|
||||
const useBuiltinClipboard =
|
||||
@@ -28,6 +29,7 @@ const handleClipboardUploading = async (): Promise<false | ImgInfo[]> => {
|
||||
}
|
||||
|
||||
export const uploadClipboardFiles = async (): Promise<IStringKeyMap> => {
|
||||
await handleSecondaryUpload(undefined, undefined, 'clipboard')
|
||||
const img = await handleClipboardUploading()
|
||||
if (img !== false) {
|
||||
if (img.length > 0) {
|
||||
@@ -84,6 +86,7 @@ export const uploadChoosedFiles = async (
|
||||
): Promise<IStringKeyMap[]> => {
|
||||
const input = files.map(item => item.path)
|
||||
const rawInput = cloneDeep(input)
|
||||
await handleSecondaryUpload(webContents, input)
|
||||
const imgs = await uploader.setWebContents(webContents).upload(input)
|
||||
const result = []
|
||||
if (imgs !== false) {
|
||||
@@ -132,3 +135,65 @@ export const uploadChoosedFiles = async (
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
export const handleSecondaryUpload = async (
|
||||
webContents?: WebContents,
|
||||
input?: string[],
|
||||
uploadType: 'clipboard' | 'file' | 'tray' = 'file'
|
||||
): Promise<void> => {
|
||||
const enableSecondUploader = db.get(configPaths.settings.enableSecondUploader) || false
|
||||
let currentPicBedType = ''
|
||||
let currentPicBedConfig = {} as IStringKeyMap
|
||||
let currentPicBedConfigId = ''
|
||||
let needRestore = false
|
||||
if (enableSecondUploader) {
|
||||
const secondUploader = db.get(configPaths.picBed.secondUploader)
|
||||
const secondUploaderConfig = db.get(configPaths.picBed.secondUploaderConfig)
|
||||
const secondUploaderId = db.get(configPaths.picBed.secondUploaderId)
|
||||
const currentPicBed = db.get('picBed') || ({} as IStringKeyMap)
|
||||
currentPicBedType = currentPicBed.uploader || currentPicBed.current || 'smms'
|
||||
currentPicBedConfig = currentPicBed[currentPicBedType] || ({} as IStringKeyMap)
|
||||
currentPicBedConfigId = currentPicBedConfig._id
|
||||
if (
|
||||
secondUploader === currentPicBedType &&
|
||||
secondUploaderConfig._configName === currentPicBedConfig._configName &&
|
||||
secondUploaderId === currentPicBedConfigId
|
||||
) {
|
||||
picgo.log.info('second uploader is the same as current uploader')
|
||||
} else {
|
||||
needRestore = true
|
||||
let secondImgs: ImgInfo[] | false = false
|
||||
changeCurrentUploader(secondUploader, secondUploaderConfig, secondUploaderId)
|
||||
if (uploadType === 'clipboard') {
|
||||
secondImgs = await handleClipboardUploading()
|
||||
} else {
|
||||
secondImgs = await uploader.setWebContents(webContents!).upload(input)
|
||||
}
|
||||
if (secondImgs !== false) {
|
||||
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
|
||||
if (uploadType === 'clipboard') {
|
||||
if (secondImgs.length > 0) {
|
||||
await GalleryDB.getInstance().insert(secondImgs[0])
|
||||
trayWindow?.webContents?.send('clipboardFiles', [])
|
||||
trayWindow?.webContents?.send('uploadFiles', secondImgs)
|
||||
}
|
||||
} else {
|
||||
for (let i = 0; i < secondImgs.length; i++) {
|
||||
await GalleryDB.getInstance().insert(secondImgs[i])
|
||||
}
|
||||
if (uploadType === 'tray') {
|
||||
trayWindow?.webContents?.send('dragFiles', secondImgs)
|
||||
} else {
|
||||
trayWindow?.webContents?.send('uploadFiles', secondImgs)
|
||||
}
|
||||
}
|
||||
if (windowManager.has(IWindowList.SETTING_WINDOW) && uploadType !== 'tray') {
|
||||
windowManager.get(IWindowList.SETTING_WINDOW)!.webContents?.send('updateGallery')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (needRestore) {
|
||||
changeCurrentUploader(currentPicBedType, currentPicBedConfig, currentPicBedConfigId)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import pasteTemplate from '~/utils/pasteTemplate'
|
||||
import { SHOW_INPUT_BOX } from '#/events/constants'
|
||||
import { IPasteStyle } from '#/types/enum'
|
||||
import { configPaths } from '#/utils/configPaths'
|
||||
import { handleSecondaryUpload } from '../app/uploader/apis'
|
||||
|
||||
// Cross-process support may be required in the future
|
||||
class GuiApi implements IGuiApi {
|
||||
@@ -78,6 +79,7 @@ class GuiApi implements IGuiApi {
|
||||
this.windowId = await getWindowId()
|
||||
const webContents = this.getWebcontentsByWindowId(this.windowId)
|
||||
const rawInput = cloneDeep(input)
|
||||
await handleSecondaryUpload(webContents!, input)
|
||||
const imgs = await uploader.setWebContents(webContents!).upload(input)
|
||||
if (imgs !== false) {
|
||||
const pasteStyle = db.get(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN
|
||||
|
||||
@@ -13,7 +13,7 @@ import { T } from '~/i18n'
|
||||
import clipboardPoll from '~/utils/clipboardPoll'
|
||||
import { setTrayToolTip } from '~/utils/common'
|
||||
import getPicBeds from '~/utils/getPicBeds'
|
||||
import { changeCurrentUploader } from '~/utils/handleUploaderConfig'
|
||||
import { changeCurrentUploader, changeSecondUploader } from '~/utils/handleUploaderConfig'
|
||||
|
||||
import {
|
||||
PICGO_CONFIG_PLUGIN,
|
||||
@@ -139,6 +139,59 @@ const buildMainPageMenu = (win: BrowserWindow) => {
|
||||
return Menu.buildFromTemplate(template)
|
||||
}
|
||||
|
||||
const buildSecondPicBedMenu = () => {
|
||||
const picBeds = getPicBeds()
|
||||
const secondUploader = picgo.getConfig(configPaths.picBed.secondUploader)
|
||||
const defaultSecondUploaderId = picgo.getConfig(configPaths.picBed.secondUploaderId)
|
||||
const currentPicBedName = picBeds.find(item => item.type === secondUploader)?.name
|
||||
const picBedConfigList = picgo.getConfig<IUploaderConfig>('uploader')
|
||||
const currentPicBedMenuItem = [
|
||||
{
|
||||
label: `${T('CURRENT_SECOND_PICBED')} - ${currentPicBedName || 'None'}`,
|
||||
enabled: false
|
||||
},
|
||||
{
|
||||
type: 'separator'
|
||||
}
|
||||
]
|
||||
let submenu = picBeds
|
||||
.filter(item => item.visible)
|
||||
.map(item => {
|
||||
const configList = picBedConfigList?.[item.type]?.configList
|
||||
const hasSubmenu = !!configList
|
||||
return {
|
||||
label: item.name,
|
||||
type: !hasSubmenu ? 'checkbox' : undefined,
|
||||
checked: !hasSubmenu ? secondUploader === item.type : undefined,
|
||||
submenu: hasSubmenu
|
||||
? configList.map(config => {
|
||||
return {
|
||||
label: config._configName || 'Default',
|
||||
// if only one config, use checkbox, or radio will checked as default
|
||||
// see: https://github.com/electron/electron/issues/21292
|
||||
type: 'checkbox',
|
||||
checked: config._id === defaultSecondUploaderId && item.type === secondUploader,
|
||||
click: function () {
|
||||
changeSecondUploader(item.type, config, config._id)
|
||||
}
|
||||
}
|
||||
})
|
||||
: undefined,
|
||||
click: !hasSubmenu
|
||||
? function () {
|
||||
picgo.saveConfig({
|
||||
[configPaths.picBed.secondUploader]: item.type
|
||||
})
|
||||
}
|
||||
: undefined
|
||||
}
|
||||
})
|
||||
// @ts-expect-error submenu type
|
||||
submenu = currentPicBedMenuItem.concat(submenu)
|
||||
// @ts-expect-error submenu type
|
||||
return Menu.buildFromTemplate(submenu)
|
||||
}
|
||||
|
||||
const buildPicBedListMenu = () => {
|
||||
const picBeds = getPicBeds()
|
||||
const currentPicBed = picgo.getConfig(configPaths.picBed.uploader)
|
||||
@@ -339,4 +392,4 @@ const buildPluginPageMenu = (plugin: IPicGoPlugin) => {
|
||||
return Menu.buildFromTemplate(menu)
|
||||
}
|
||||
|
||||
export { buildMiniPageMenu, buildMainPageMenu, buildPicBedListMenu, buildPluginPageMenu }
|
||||
export { buildMiniPageMenu, buildMainPageMenu, buildPicBedListMenu, buildPluginPageMenu, buildSecondPicBedMenu }
|
||||
|
||||
@@ -15,17 +15,12 @@ import { manageRouter } from '~/events/rpc/routes/manage'
|
||||
import { IRPCActionType, IRPCType } from '#/types/enum'
|
||||
import { RPC_ACTIONS, RPC_ACTIONS_INVOKE } from '#/events/constants'
|
||||
|
||||
const isDevelopment = process.env.NODE_ENV !== 'production'
|
||||
|
||||
class RPCServer implements IRPCServer {
|
||||
private routes: IRPCRoutes = new Map()
|
||||
private routesWithResponse: IRPCRoutes = new Map()
|
||||
|
||||
private rpcEventHandler = async (event: IpcMainEvent, action: IRPCActionType, args: any[]) => {
|
||||
try {
|
||||
if (isDevelopment) {
|
||||
console.log(`action: ${action} args: ${JSON.stringify(args)}`)
|
||||
}
|
||||
const route = this.routes.get(action)
|
||||
await route?.handler?.(event, args)
|
||||
} catch (e: any) {
|
||||
@@ -35,9 +30,6 @@ class RPCServer implements IRPCServer {
|
||||
|
||||
private rpcEventHandlerWithResponse = async (event: IpcMainInvokeEvent, action: IRPCActionType, args: any[]) => {
|
||||
try {
|
||||
if (isDevelopment) {
|
||||
console.log(`action: ${action} args: ${JSON.stringify(args)}`)
|
||||
}
|
||||
const route = this.routesWithResponse.get(action)
|
||||
return await route?.handler?.(event, args)
|
||||
} catch (e: any) {
|
||||
|
||||
@@ -2,7 +2,13 @@ import { app, BrowserWindow } from 'electron'
|
||||
|
||||
import windowManager from 'apis/app/window/windowManager'
|
||||
|
||||
import { buildMainPageMenu, buildMiniPageMenu, buildPicBedListMenu, buildPluginPageMenu } from '~/events/remotes/menu'
|
||||
import {
|
||||
buildMainPageMenu,
|
||||
buildMiniPageMenu,
|
||||
buildPicBedListMenu,
|
||||
buildPluginPageMenu,
|
||||
buildSecondPicBedMenu
|
||||
} from '~/events/remotes/menu'
|
||||
import { openMiniWindow } from '~/utils/windowHelper'
|
||||
|
||||
import { IRPCActionType, IWindowList } from '#/types/enum'
|
||||
@@ -83,6 +89,16 @@ export default [
|
||||
})
|
||||
}
|
||||
},
|
||||
{
|
||||
action: IRPCActionType.SHOW_SECOND_UPLOADER_MENU,
|
||||
handler: async () => {
|
||||
const window = windowManager.get(IWindowList.SETTING_WINDOW)!
|
||||
const menu = buildSecondPicBedMenu()
|
||||
menu.popup({
|
||||
window
|
||||
})
|
||||
}
|
||||
},
|
||||
{
|
||||
action: IRPCActionType.SHOW_PLUGIN_PAGE_MENU,
|
||||
handler: async (_: IIPCEvent, args: [plugin: IPicGoPlugin]) => {
|
||||
|
||||
@@ -56,6 +56,25 @@ export const getPicBedConfig = (type: string) => {
|
||||
}
|
||||
}
|
||||
|
||||
export const changeSecondUploader = (type: string, config?: IStringKeyMap, id?: string) => {
|
||||
if (!type) {
|
||||
return
|
||||
}
|
||||
if (id) {
|
||||
picgo.saveConfig({
|
||||
[configPaths.picBed.secondUploaderId]: id
|
||||
})
|
||||
}
|
||||
if (config) {
|
||||
picgo.saveConfig({
|
||||
[configPaths.picBed.secondUploaderConfig]: config
|
||||
})
|
||||
}
|
||||
picgo.saveConfig({
|
||||
[configPaths.picBed.secondUploader]: type
|
||||
})
|
||||
}
|
||||
|
||||
export const changeCurrentUploader = (type: string, config?: IStringKeyMap, id?: string) => {
|
||||
if (!type) {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user