diff --git a/src/datastore/index.js b/src/datastore/index.js index ddeed76..b480e65 100644 --- a/src/datastore/index.js +++ b/src/datastore/index.js @@ -44,7 +44,7 @@ if (!db.has('settings.shortKey').value()) { // init generate clipboard image files let clipboardFiles = getClipboardFiles() -if (!fs.pathExistsSync(path.join(STORE_PATH, 'windows.ps1'))) { +if (!fs.pathExistsSync(path.join(STORE_PATH, 'windows10.ps1'))) { clipboardFiles.forEach(item => { fs.copyFileSync(item.origin, item.dest) }) @@ -67,7 +67,8 @@ function getClipboardFiles () { let files = [ '/linux.sh', '/mac.applescript', - '/windows.ps1' + '/windows.ps1', + '/windows10.ps1' ] return files.map(item => { diff --git a/src/main/index.js b/src/main/index.js index cb94f20..aa96463 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -22,6 +22,13 @@ import pkg from '../../package.json' import picgoCoreIPC from './utils/picgoCoreIPC' import fixPath from 'fix-path' import { getUploadFiles } from './utils/handleArgv' +import bus from './utils/eventBus' +import { + updateShortKeyFromVersion212 +} from './migrate/shortKeyUpdateHelper' +import { + initShortKeyRegister +} from './utils/shortKeyRegister' if (process.platform === 'darwin') { beforeOpen() } @@ -553,10 +560,13 @@ app.on('ready', () => { } db.read().set('needReload', false).write() updateChecker() - - globalShortcut.register(db.read().get('settings.shortKey.upload').value(), () => { - uploadClipboardFiles() + initEventCenter() + // 不需要阻塞 + process.nextTick(() => { + updateShortKeyFromVersion212(db, db.read().get('settings.shortKey').value()) + initShortKeyRegister(globalShortcut, db.read().get('settings.shortKey').value()) }) + if (process.env.NODE_ENV !== 'development') { let files = getUploadFiles() if (files === null || files.length > 0) { // 如果有文件列表作为参数,说明是命令行启动 @@ -592,12 +602,22 @@ app.on('activate', () => { app.on('will-quit', () => { globalShortcut.unregisterAll() + bus.removeAllListeners() }) app.setLoginItemSettings({ openAtLogin: db.read().get('settings.autoStart').value() || false }) +function initEventCenter () { + const eventList = { + 'picgo:upload': uploadClipboardFiles + } + for (let i in eventList) { + bus.on(i, eventList[i]) + } +} + /** * Auto Updater * diff --git a/src/main/migrate/shortKeyUpdateHelper.js b/src/main/migrate/shortKeyUpdateHelper.js new file mode 100644 index 0000000..96d294b --- /dev/null +++ b/src/main/migrate/shortKeyUpdateHelper.js @@ -0,0 +1,25 @@ +// from v2.1.2 +const updateShortKeyFromVersion212 = (db, shortKeyConfig) => { + let needUpgrade = false + Object.keys(shortKeyConfig).forEach(item => { + if (typeof shortKeyConfig[item] === 'string') { + needUpgrade = true + shortKeyConfig[item] = { + enable: true, + key: shortKeyConfig[item], + name: `picgo:${item}`, + lable: '快捷上传' + } + } + }) + if (needUpgrade) { + db.read().set('settings.shortKey', shortKeyConfig).write() + return shortKeyConfig + } else { + return false + } +} + +export { + updateShortKeyFromVersion212 +} diff --git a/src/main/utils/eventBus.js b/src/main/utils/eventBus.js new file mode 100644 index 0000000..3ebd70a --- /dev/null +++ b/src/main/utils/eventBus.js @@ -0,0 +1,5 @@ +import { EventEmitter } from 'events' + +const bus = new EventEmitter() + +export default bus diff --git a/src/main/utils/shortKeyRegister.js b/src/main/utils/shortKeyRegister.js new file mode 100644 index 0000000..4f3e20c --- /dev/null +++ b/src/main/utils/shortKeyRegister.js @@ -0,0 +1,32 @@ +import bus from '../utils/eventBus' +/** + * + * @param {string} name + */ +const shortKeyHandler = (name) => { + if (name.includes('picgo:')) { + bus.emit(name) + } else if (name.includes('picgo-plugin-')) { + // TODO: 处理插件快捷键 + } +} + +// 初始化阶段的注册 +const initShortKeyRegister = (globalShortcut, shortKeys) => { + 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 { + initShortKeyRegister +} diff --git a/static/windows.ps1 b/static/windows.ps1 index ca1c6bc..480b58f 100644 --- a/static/windows.ps1 +++ b/static/windows.ps1 @@ -1,44 +1,26 @@ -# Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1 + param($imagePath) -# https://github.com/PowerShell/PowerShell/issues/7233 -# fix the output encoding bug -[console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding +# Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1 Add-Type -Assembly PresentationCore -function main { - $img = [Windows.Clipboard]::GetImage() +$img = [Windows.Clipboard]::GetImage() - if ($img -eq $null) { - "no image" - Exit 1 - } - - if (-not $imagePath) { - "no image" - Exit 1 - } - - $fcb = new-object Windows.Media.Imaging.FormatConvertedBitmap($img, [Windows.Media.PixelFormats]::Rgb24, $null, 0) - $stream = [IO.File]::Open($imagePath, "OpenOrCreate") - $encoder = New-Object Windows.Media.Imaging.PngBitmapEncoder - $encoder.Frames.Add([Windows.Media.Imaging.BitmapFrame]::Create($fcb)) | out-null - $encoder.Save($stream) | out-null - $stream.Dispose() | out-null - - $imagePath +if ($img -eq $null) { + "no image" + Exit 1 } -try { - # For WIN10 - $file = Get-Clipboard -Format FileDropList - if ($file -ne $null) { - Convert-Path $file - Exit 1 - } -} catch { - # For WIN7 WIN8 WIN10 - main +if (-not $imagePath) { + "no image" + Exit 1 } -main \ No newline at end of file +$fcb = new-object Windows.Media.Imaging.FormatConvertedBitmap($img, [Windows.Media.PixelFormats]::Rgb24, $null, 0) +$stream = [IO.File]::Open($imagePath, "OpenOrCreate") +$encoder = New-Object Windows.Media.Imaging.PngBitmapEncoder +$encoder.Frames.Add([Windows.Media.Imaging.BitmapFrame]::Create($fcb)) | out-null +$encoder.Save($stream) | out-null +$stream.Dispose() | out-null + +$imagePath diff --git a/static/windows10.ps1 b/static/windows10.ps1 new file mode 100644 index 0000000..ca1c6bc --- /dev/null +++ b/static/windows10.ps1 @@ -0,0 +1,44 @@ +# Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1 +param($imagePath) + +# https://github.com/PowerShell/PowerShell/issues/7233 +# fix the output encoding bug +[console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding + +Add-Type -Assembly PresentationCore +function main { + $img = [Windows.Clipboard]::GetImage() + + if ($img -eq $null) { + "no image" + Exit 1 + } + + if (-not $imagePath) { + "no image" + Exit 1 + } + + $fcb = new-object Windows.Media.Imaging.FormatConvertedBitmap($img, [Windows.Media.PixelFormats]::Rgb24, $null, 0) + $stream = [IO.File]::Open($imagePath, "OpenOrCreate") + $encoder = New-Object Windows.Media.Imaging.PngBitmapEncoder + $encoder.Frames.Add([Windows.Media.Imaging.BitmapFrame]::Create($fcb)) | out-null + $encoder.Save($stream) | out-null + $stream.Dispose() | out-null + + $imagePath +} + +try { + # For WIN10 + $file = Get-Clipboard -Format FileDropList + if ($file -ne $null) { + Convert-Path $file + Exit 1 + } +} catch { + # For WIN7 WIN8 WIN10 + main +} + +main \ No newline at end of file