Feature: add upload-clipboard-image from electron' clipboard

ISSUES CLOSED: #822
This commit is contained in:
PiEgg 2022-02-20 12:35:18 +08:00
parent 6bcd019b31
commit 27628da458
5 changed files with 61 additions and 4 deletions

View File

@ -10,9 +10,19 @@ import db, { GalleryDB } from '~/main/apis/core/datastore'
import { handleCopyUrl } from '~/main/utils/common'
import { handleUrlEncode } from '#/utils/common'
import { T } from '#/i18n/index'
export const uploadClipboardFiles = async (): Promise<string> => {
// import dayjs from 'dayjs'
const handleClipboardUploading = async (): Promise<false | ImgInfo[]> => {
const useBuiltinClipboard = !!db.get('settings.useBuiltinClipboard')
const win = windowManager.getAvailableWindow()
const img = await uploader.setWebContents(win!.webContents).upload()
if (useBuiltinClipboard) {
return await uploader.setWebContents(win!.webContents).uploadWithBuildInClipboard()
}
return await uploader.setWebContents(win!.webContents).upload()
}
export const uploadClipboardFiles = async (): Promise<string> => {
const img = await handleClipboardUploading()
if (img !== false) {
if (img.length > 0) {
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)

View File

@ -2,7 +2,8 @@ import {
Notification,
BrowserWindow,
ipcMain,
WebContents
WebContents,
clipboard
} from 'electron'
import dayjs from 'dayjs'
import picgo from '@core/picgo'
@ -15,6 +16,8 @@ import { showNotification, calcDurationRange } from '~/main/utils/common'
import { RENAME_FILE_NAME, TALKING_DATA_EVENT } from '~/universal/events/constants'
import logger from '@core/picgo/logger'
import { T } from '~/universal/i18n'
import fse from 'fs-extra'
import path from 'path'
const waitForShow = (webcontent: WebContents) => {
return new Promise<void>((resolve) => {
@ -109,6 +112,32 @@ class Uploader {
return this
}
/**
* use electron's clipboard image to upload
*/
async uploadWithBuildInClipboard (): Promise<ImgInfo[]|false> {
let filePath = ''
try {
const nativeImage = clipboard.readImage()
if (nativeImage.isEmpty()) {
return false
}
const buffer = nativeImage.toPNG()
const baseDir = picgo.baseDir
const fileName = `${dayjs().format('YYYYMMDDHHmmSSS')}.png`
filePath = path.join(baseDir, fileName)
await fse.writeFile(filePath, buffer)
return await this.upload([filePath])
} catch (e: any) {
logger.error(e)
return false
} finally {
if (filePath) {
fse.unlink(filePath)
}
}
}
async upload (img?: IUploadOption): Promise<ImgInfo[]|false> {
try {
const startTime = Date.now()

View File

@ -128,6 +128,16 @@
@change="handleAutoCopyUrl"
></el-switch>
</el-form-item>
<el-form-item
:label="$T('SETTINGS_USE_BUILTIN_CLIPBOARD_UPLOAD')"
>
<el-switch
v-model="form.useBuiltinClipboard"
:active-text="$T('SETTINGS_OPEN')"
:inactive-text="$T('SETTINGS_CLOSE')"
@change="useBuiltinClipboardChange"
></el-switch>
</el-form-item>
<el-form-item
:label="$T('CHOOSE_SHOWED_PICBED')"
>
@ -371,7 +381,8 @@ export default class extends Vue {
miniWindowOntop: false,
logLevel: ['all'],
autoCopyUrl: true,
checkBetaUpdate: true
checkBetaUpdate: true,
useBuiltinClipboard: false
}
picBed: IPicBedType[] = []
@ -446,6 +457,7 @@ export default class extends Vue {
this.form.logLevel = this.initLogLevel(settings.logLevel || [])
this.form.autoCopyUrl = settings.autoCopy === undefined ? true : settings.autoCopy
this.form.checkBetaUpdate = settings.checkBetaUpdate === undefined ? true : settings.checkBetaUpdate
this.form.useBuiltinClipboard = settings.useBuiltinClipboard === undefined ? false : settings.useBuiltinClipboard
this.customLink.value = settings.customLink || '$url'
this.shortKey.upload = settings.shortKey.upload
@ -539,6 +551,10 @@ export default class extends Vue {
this.saveConfig('settings.checkBetaUpdate', val)
}
useBuiltinClipboardChange (val: boolean) {
this.saveConfig('settings.useBuiltinClipboard', val)
}
handleShowPicBedListChange (val: string[]) {
const list = this.picBed.map(item => {
if (!val.includes(item.name)) {

View File

@ -103,6 +103,7 @@ export const ZH_CN = {
SETTINGS_DEFAULT_PICBED: '设置默认图床',
SETTINGS_SET_DEFAULT_PICBED: '设为默认图床',
SETTINGS_NOT_CONFIG_OPTIONS: '暂无配置项',
SETTINGS_USE_BUILTIN_CLIPBOARD_UPLOAD: '使用内置剪贴板上传',
// shortcut page
SHORTCUT_NAME: '快捷键名称',

View File

@ -9,6 +9,7 @@ interface ISettingForm {
logLevel: string[]
autoCopyUrl: boolean
checkBetaUpdate: boolean
useBuiltinClipboard: boolean
}
interface IShortKeyMap {