mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-02-02 11:08:13 -05:00
🚧 WIP: shortcut system
This commit is contained in:
parent
fe9e19a84d
commit
f21940ef23
@ -44,7 +44,7 @@ if (!db.has('settings.shortKey').value()) {
|
|||||||
|
|
||||||
// init generate clipboard image files
|
// init generate clipboard image files
|
||||||
let clipboardFiles = getClipboardFiles()
|
let clipboardFiles = getClipboardFiles()
|
||||||
if (!fs.pathExistsSync(path.join(STORE_PATH, 'windows.ps1'))) {
|
if (!fs.pathExistsSync(path.join(STORE_PATH, 'windows10.ps1'))) {
|
||||||
clipboardFiles.forEach(item => {
|
clipboardFiles.forEach(item => {
|
||||||
fs.copyFileSync(item.origin, item.dest)
|
fs.copyFileSync(item.origin, item.dest)
|
||||||
})
|
})
|
||||||
@ -67,7 +67,8 @@ function getClipboardFiles () {
|
|||||||
let files = [
|
let files = [
|
||||||
'/linux.sh',
|
'/linux.sh',
|
||||||
'/mac.applescript',
|
'/mac.applescript',
|
||||||
'/windows.ps1'
|
'/windows.ps1',
|
||||||
|
'/windows10.ps1'
|
||||||
]
|
]
|
||||||
|
|
||||||
return files.map(item => {
|
return files.map(item => {
|
||||||
|
@ -22,6 +22,13 @@ import pkg from '../../package.json'
|
|||||||
import picgoCoreIPC from './utils/picgoCoreIPC'
|
import picgoCoreIPC from './utils/picgoCoreIPC'
|
||||||
import fixPath from 'fix-path'
|
import fixPath from 'fix-path'
|
||||||
import { getUploadFiles } from './utils/handleArgv'
|
import { getUploadFiles } from './utils/handleArgv'
|
||||||
|
import bus from './utils/eventBus'
|
||||||
|
import {
|
||||||
|
updateShortKeyFromVersion212
|
||||||
|
} from './migrate/shortKeyUpdateHelper'
|
||||||
|
import {
|
||||||
|
initShortKeyRegister
|
||||||
|
} from './utils/shortKeyRegister'
|
||||||
if (process.platform === 'darwin') {
|
if (process.platform === 'darwin') {
|
||||||
beforeOpen()
|
beforeOpen()
|
||||||
}
|
}
|
||||||
@ -553,10 +560,13 @@ app.on('ready', () => {
|
|||||||
}
|
}
|
||||||
db.read().set('needReload', false).write()
|
db.read().set('needReload', false).write()
|
||||||
updateChecker()
|
updateChecker()
|
||||||
|
initEventCenter()
|
||||||
globalShortcut.register(db.read().get('settings.shortKey.upload').value(), () => {
|
// 不需要阻塞
|
||||||
uploadClipboardFiles()
|
process.nextTick(() => {
|
||||||
|
updateShortKeyFromVersion212(db, db.read().get('settings.shortKey').value())
|
||||||
|
initShortKeyRegister(globalShortcut, db.read().get('settings.shortKey').value())
|
||||||
})
|
})
|
||||||
|
|
||||||
if (process.env.NODE_ENV !== 'development') {
|
if (process.env.NODE_ENV !== 'development') {
|
||||||
let files = getUploadFiles()
|
let files = getUploadFiles()
|
||||||
if (files === null || files.length > 0) { // 如果有文件列表作为参数,说明是命令行启动
|
if (files === null || files.length > 0) { // 如果有文件列表作为参数,说明是命令行启动
|
||||||
@ -592,12 +602,22 @@ app.on('activate', () => {
|
|||||||
|
|
||||||
app.on('will-quit', () => {
|
app.on('will-quit', () => {
|
||||||
globalShortcut.unregisterAll()
|
globalShortcut.unregisterAll()
|
||||||
|
bus.removeAllListeners()
|
||||||
})
|
})
|
||||||
|
|
||||||
app.setLoginItemSettings({
|
app.setLoginItemSettings({
|
||||||
openAtLogin: db.read().get('settings.autoStart').value() || false
|
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
|
* Auto Updater
|
||||||
*
|
*
|
||||||
|
25
src/main/migrate/shortKeyUpdateHelper.js
Normal file
25
src/main/migrate/shortKeyUpdateHelper.js
Normal file
@ -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
|
||||||
|
}
|
5
src/main/utils/eventBus.js
Normal file
5
src/main/utils/eventBus.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import { EventEmitter } from 'events'
|
||||||
|
|
||||||
|
const bus = new EventEmitter()
|
||||||
|
|
||||||
|
export default bus
|
32
src/main/utils/shortKeyRegister.js
Normal file
32
src/main/utils/shortKeyRegister.js
Normal file
@ -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
|
||||||
|
}
|
@ -1,44 +1,26 @@
|
|||||||
# Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1
|
|
||||||
param($imagePath)
|
param($imagePath)
|
||||||
|
|
||||||
# https://github.com/PowerShell/PowerShell/issues/7233
|
# Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1
|
||||||
# fix the output encoding bug
|
|
||||||
[console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
|
|
||||||
|
|
||||||
Add-Type -Assembly PresentationCore
|
Add-Type -Assembly PresentationCore
|
||||||
function main {
|
$img = [Windows.Clipboard]::GetImage()
|
||||||
$img = [Windows.Clipboard]::GetImage()
|
|
||||||
|
|
||||||
if ($img -eq $null) {
|
if ($img -eq $null) {
|
||||||
"no image"
|
"no image"
|
||||||
Exit 1
|
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 {
|
if (-not $imagePath) {
|
||||||
# For WIN10
|
"no image"
|
||||||
$file = Get-Clipboard -Format FileDropList
|
Exit 1
|
||||||
if ($file -ne $null) {
|
|
||||||
Convert-Path $file
|
|
||||||
Exit 1
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
# For WIN7 WIN8 WIN10
|
|
||||||
main
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main
|
$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
|
||||||
|
44
static/windows10.ps1
Normal file
44
static/windows10.ps1
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user