🚧 WIP: almost finish shortKey system

This commit is contained in:
Molunerfinn
2019-12-22 17:22:32 +08:00
parent b8ec879e23
commit fec4360043
17 changed files with 384 additions and 341 deletions

View File

@@ -3,68 +3,56 @@ import {
BrowserWindow,
clipboard,
Notification,
IpcMain,
WebContents
WebContents,
ipcMain
} from 'electron'
import db from '#/datastore'
import Uploader from './uploader'
import pasteTemplate from '#/utils/pasteTemplate'
import PicGoCore from '~/universal/types/picgo'
const WEBCONTENTS = Symbol('WEBCONTENTS')
const IPCMAIN = Symbol('IPCMAIN')
const PICGO = Symbol('PICGO')
class GuiApi {
class GuiApi implements IGuiApi {
private [WEBCONTENTS]: WebContents
private [IPCMAIN]: IpcMain
private [PICGO]: PicGoCore
constructor (ipcMain: IpcMain, webcontents: WebContents, picgo: PicGoCore) {
constructor (webcontents: WebContents) {
this[WEBCONTENTS] = webcontents
this[IPCMAIN] = ipcMain
this[PICGO] = picgo
}
/**
* for plugin showInputBox
* @param {object} options
* return type is string or ''
*/
showInputBox (options: IShowInputBoxOption) {
if (options === undefined) {
options = {
title: '',
placeholder: ''
}
private async showSettingWindow () {
const settingWindow = BrowserWindow.fromWebContents(this[WEBCONTENTS])
if (settingWindow.isVisible()) {
return true
}
this[WEBCONTENTS].send('showInputBox', options)
settingWindow.show()
return new Promise((resolve, reject) => {
this[IPCMAIN].once('showInputBox', (event: Event, value: string) => {
setTimeout(() => {
resolve()
}, 1000) // TODO: a better way to wait page loaded.
})
}
async showInputBox (options: IShowInputBoxOption = {
title: '',
placeholder: ''
}) {
await this.showSettingWindow()
this[WEBCONTENTS].send('showInputBox', options)
return new Promise<string>((resolve, reject) => {
ipcMain.once('showInputBox', (event: Event, value: string) => {
resolve(value)
})
})
}
/**
* for plugin show file explorer
* @param {object} options
*/
showFileExplorer (options: {}) {
if (options === undefined) {
options = {}
}
return new Promise((resolve, reject) => {
showFileExplorer (options: IShowFileExplorerOption = {}) {
return new Promise<string>((resolve, reject) => {
dialog.showOpenDialog(BrowserWindow.fromWebContents(this[WEBCONTENTS]), options, (filename: string) => {
resolve(filename)
})
})
}
/**
* for plugin to upload file
* @param {array} input
*/
async upload (input: []) {
const imgs = await new Uploader(input, this[WEBCONTENTS], this[PICGO]).upload()
async upload (input: IUploadOption) {
const imgs = await new Uploader(input, this[WEBCONTENTS]).upload()
if (imgs !== false) {
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
let pasteText = ''
@@ -88,11 +76,7 @@ class GuiApi {
return []
}
/**
* For notification
* @param {Object} options
*/
showNotification (options = {
showNotification (options: IShowNotificationOption = {
title: '',
body: ''
}) {
@@ -103,17 +87,13 @@ class GuiApi {
notification.show()
}
/**
*
* @param {Object} options
*/
showMessageBox (options = {
showMessageBox (options: IShowMessageBoxOption = {
title: '',
message: '',
type: 'info',
buttons: ['Yes', 'No']
}) {
return new Promise((resolve, reject) => {
return new Promise<IShowMessageBoxResult>((resolve, reject) => {
dialog.showMessageBox(
BrowserWindow.fromWebContents(this[WEBCONTENTS]),
options

View File

@@ -15,7 +15,7 @@ class Logger {
warn: IChalkType.warn,
error: IChalkType.error
}
protected handleLog (type: ILogType, msg: string | Error): string | Error | undefined {
protected handleLog (type: ILogType, msg: ILoggerType): ILoggerType {
// if configPath is invalid then this.ctx.config === undefined
// if not then check config.silent
const log = chalk[this.level[type]](`[PicGo ${type.toUpperCase()}]:`)
@@ -26,7 +26,7 @@ class Logger {
return msg
}
protected handleWriteLog (type: string, msg: string | Error): void {
protected handleWriteLog (type: string, msg: ILoggerType): void {
try {
const logLevel = db.get('settings.logLevel')
const logPath = db.get('settings.logPath') || path.join(baseDir, './picgo.log')
@@ -56,19 +56,19 @@ class Logger {
}
}
success (msg: string | Error): string | Error | undefined {
success (msg: ILoggerType): ILoggerType {
return this.handleLog('success', msg)
}
info (msg: string | Error): string | Error | undefined {
info (msg: ILoggerType): ILoggerType {
return this.handleLog('info', msg)
}
error (msg: string | Error): string | Error | undefined {
error (msg: ILoggerType): ILoggerType {
return this.handleLog('error', msg)
}
warn (msg: string | Error): string | Error | undefined {
warn (msg: ILoggerType): ILoggerType {
return this.handleLog('warn', msg)
}
}

View File

@@ -178,7 +178,7 @@ const handlePluginActions = (ipcMain: IpcMain, CONFIG_PATH: string) => {
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(ipcMain, event.sender, picgo)
const guiApi = new GuiApi(event.sender)
if (plugin.guiMenu && plugin.guiMenu(picgo).length > 0) {
const menu: GuiMenuItem[] = plugin.guiMenu(picgo)
menu.forEach(item => {
@@ -193,7 +193,7 @@ const handlePluginActions = (ipcMain: IpcMain, CONFIG_PATH: string) => {
const handleRemoveFiles = (ipcMain: IpcMain, CONFIG_PATH: string) => {
ipcMain.on('removeFiles', (event: IpcMainEvent, files: ImgInfo[]) => {
const picgo = new PicGo(CONFIG_PATH)
const guiApi = new GuiApi(ipcMain, event.sender, picgo)
const guiApi = new GuiApi(event.sender)
setTimeout(() => {
picgo.emit('remove', files, guiApi)
}, 500)

View File

@@ -1,60 +1,178 @@
import bus from './eventBus'
import PicGoCore from '~/universal/types/picgo'
import path from 'path'
import {
GlobalShortcut
app,
globalShortcut,
BrowserWindow
} from 'electron'
let isInModifiedMode = false // 修改快捷键模式
bus.on('toggleShortKeyModifiedMode', flag => {
isInModifiedMode = flag
})
/**
*
* @param {string} name
*/
const shortKeyHandler = (name: string) => {
if (isInModifiedMode) {
return
}
if (name.includes('picgo:')) {
bus.emit(name)
} else if (name.includes('picgo-plugin-')) {
// TODO: 处理插件快捷键
}
}
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 shortKeyUpdater = (globalShortcut: GlobalShortcut, item: IShortKeyConfig, oldKey: string) => {
// 如果提供了旧key则解绑
if (oldKey) {
globalShortcut.unregister(oldKey)
const PicGo = requireFunc('picgo') as typeof PicGoCore
const STORE_PATH = app.getPath('userData')
const CONFIG_PATH = path.join(STORE_PATH, '/data.json')
class ShortKeyHandler {
private isInModifiedMode: boolean = false
constructor () {
bus.on('toggleShortKeyModifiedMode', flag => {
this.isInModifiedMode = flag
})
}
if (item.enable === false) {
globalShortcut.unregister(item.key)
} else {
init () {
this.initBuiltInShortKey()
this.initPluginsShortKey()
}
private initBuiltInShortKey () {
const commands = db.get('settings.shortKey') as IShortKeyConfigs
Object.keys(commands)
.filter(item => item.includes('picgo:'))
.map(command => {
const config = commands[command]
globalShortcut.register(config.key, () => {
this.handler(command)
})
})
}
private initPluginsShortKey () {
const picgo = new PicGo(CONFIG_PATH)
const pluginList = picgo.pluginLoader.getList()
for (let item of pluginList) {
const plugin = picgo.pluginLoader.getPlugin(item)
// if a plugin has commands
if (plugin && plugin.commands) {
if (typeof plugin.commands !== 'function') {
logger.warn(`${item}'s commands is not a function`)
continue
}
const commands = plugin.commands(picgo) as IPluginShortKeyConfig[]
for (let cmd of commands) {
const command = `${item}:${cmd.name}`
if (db.has(`settings.shortKey[${command}]`)) {
const commandConfig = db.get(`settings.shortKey.${command}`) as IShortKeyConfig
this.registerShortKey(commandConfig, command, cmd.handle, false)
} else {
this.registerShortKey(cmd, command, cmd.handle, true)
}
}
} else {
continue
}
}
}
private registerShortKey (config: IShortKeyConfig | IPluginShortKeyConfig, command: string, handler: IShortKeyHandler, writeFlag: boolean) {
shortKeyService.registerCommand(command, handler)
if (config.key) {
globalShortcut.register(config.key, () => {
this.handler(command)
})
} else {
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)
}
}
// enable or disable shortKey
bindOrUnbindShortKey (item: IShortKeyConfig, from: string): boolean {
const command = `${from}:${item.name}`
if (item.enable === false) {
globalShortcut.unregister(item.key)
db.set(`settings.shortKey.${command}.enable`, false)
return true
} else {
if (globalShortcut.isRegistered(item.key)) {
return false
} else {
db.set(`settings.shortKey.${command}.enable`, true)
globalShortcut.register(item.key, () => {
this.handler(command)
})
return true
}
}
}
// update shortKey bindings
updateShortKey (item: IShortKeyConfig, oldKey: string, from: string): boolean {
const command = `${from}:${item.name}`
if (globalShortcut.isRegistered(item.key)) return false
globalShortcut.unregister(oldKey)
db.set(`settings.shortKey.${command}.key`, item.key)
globalShortcut.register(item.key, () => {
shortKeyHandler(item.name)
this.handler(`${from}:${item.name}`)
})
return true
}
private async handler (command: string) {
if (this.isInModifiedMode) {
return
}
if (command.includes('picgo:')) {
bus.emit(command)
} 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) {
const webContents = BrowserWindow.fromId(settingWindowId).webContents
const guiApi = new GuiApi(webContents)
return handler(picgo, guiApi)
}
})
bus.emit('createSettingWindow', command)
}
} else {
logger.warn(`can not find command: ${command}`)
}
}
registerPluginShortKey (pluginName: string) {
const picgo = new PicGo(CONFIG_PATH)
const plugin = picgo.pluginLoader.getPlugin(pluginName)
if (plugin && plugin.commands) {
if (typeof plugin.commands !== 'function') {
logger.warn(`${pluginName}'s commands is not a function`)
return
}
const commands = plugin.commands(picgo) as IPluginShortKeyConfig[]
for (let cmd of commands) {
const command = `${pluginName}:${cmd.name}`
if (db.has(`settings.shortKey[${command}]`)) {
const commandConfig = db.get(`settings.shortKey[${command}]`) as IShortKeyConfig
this.registerShortKey(commandConfig, command, cmd.handle, false)
} else {
this.registerShortKey(cmd, command, cmd.handle, true)
}
}
}
}
unregisterPluginShortKey (pluginName: string) {
const commands = db.get('settings.shortKey') as IShortKeyConfigs
const keyList = Object.keys(commands)
.filter(command => command.includes(pluginName))
.map(command => {
return {
command,
key: commands[command].key
}
}) as IKeyCommandType[]
keyList.forEach(item => {
globalShortcut.unregister(item.key)
shortKeyService.unregisterCommand(item.command)
db.unset('settings.shortKey', item.command)
})
}
}
// 初始化阶段的注册
const initShortKeyRegister = (globalShortcut: GlobalShortcut, shortKeys: IShortKeyConfig[]) => {
let errorList = []
for (let i in shortKeys) {
try {
if (shortKeys[i].enable) {
globalShortcut.register(shortKeys[i].key, () => {
shortKeyHandler(shortKeys[i].name)
})
}
} catch (e) {
errorList.push(shortKeys[i])
}
}
}
export {
shortKeyUpdater,
initShortKeyRegister
}
export default new ShortKeyHandler()

View File

@@ -0,0 +1,21 @@
import logger from './logger'
class ShortKeyService {
private commandList: Map<string, IShortKeyHandler> = new Map()
registerCommand (command: string, handler: IShortKeyHandler) {
this.commandList.set(command, handler)
}
unregisterCommand (command: string) {
this.commandList.delete(command)
}
getShortKeyHandler (command: string): IShortKeyHandler | null {
const handler = this.commandList.get(command)
if (handler) return handler
logger.warn(`cannot find command: ${command}`)
return null
}
getCommandList () {
return [...this.commandList.keys()]
}
}
export default new ShortKeyService()

View File

@@ -88,7 +88,7 @@ const waitForRename = (window: BrowserWindow, id: number): Promise<string|null>
return new Promise((resolve, reject) => {
ipcMain.once(`rename${id}`, (evt: Event, newName: string) => {
resolve(newName)
window.hide()
window.close()
})
window.on('close', () => {
resolve(null)
@@ -101,7 +101,7 @@ class Uploader {
private picgo: PicGoCore
private webContents: WebContents
private img: undefined | string[]
constructor (img: undefined | string[], webContents: WebContents, picgo: PicGoCore | undefined = undefined) {
constructor (img: IUploadOption, webContents: WebContents, picgo: PicGoCore | undefined = undefined) {
this.img = img
this.webContents = webContents
this.picgo = picgo || new PicGo(CONFIG_PATH)