mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-02-02 11:08:13 -05:00
✨ Feature: add upload-clipboard-image from electron' clipboard
ISSUES CLOSED: #822
This commit is contained in:
parent
6bcd019b31
commit
27628da458
@ -10,9 +10,19 @@ import db, { GalleryDB } from '~/main/apis/core/datastore'
|
|||||||
import { handleCopyUrl } from '~/main/utils/common'
|
import { handleCopyUrl } from '~/main/utils/common'
|
||||||
import { handleUrlEncode } from '#/utils/common'
|
import { handleUrlEncode } from '#/utils/common'
|
||||||
import { T } from '#/i18n/index'
|
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 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 !== false) {
|
||||||
if (img.length > 0) {
|
if (img.length > 0) {
|
||||||
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
|
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
|
||||||
|
@ -2,7 +2,8 @@ import {
|
|||||||
Notification,
|
Notification,
|
||||||
BrowserWindow,
|
BrowserWindow,
|
||||||
ipcMain,
|
ipcMain,
|
||||||
WebContents
|
WebContents,
|
||||||
|
clipboard
|
||||||
} from 'electron'
|
} from 'electron'
|
||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
import picgo from '@core/picgo'
|
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 { RENAME_FILE_NAME, TALKING_DATA_EVENT } from '~/universal/events/constants'
|
||||||
import logger from '@core/picgo/logger'
|
import logger from '@core/picgo/logger'
|
||||||
import { T } from '~/universal/i18n'
|
import { T } from '~/universal/i18n'
|
||||||
|
import fse from 'fs-extra'
|
||||||
|
import path from 'path'
|
||||||
|
|
||||||
const waitForShow = (webcontent: WebContents) => {
|
const waitForShow = (webcontent: WebContents) => {
|
||||||
return new Promise<void>((resolve) => {
|
return new Promise<void>((resolve) => {
|
||||||
@ -109,6 +112,32 @@ class Uploader {
|
|||||||
return this
|
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> {
|
async upload (img?: IUploadOption): Promise<ImgInfo[]|false> {
|
||||||
try {
|
try {
|
||||||
const startTime = Date.now()
|
const startTime = Date.now()
|
||||||
|
@ -128,6 +128,16 @@
|
|||||||
@change="handleAutoCopyUrl"
|
@change="handleAutoCopyUrl"
|
||||||
></el-switch>
|
></el-switch>
|
||||||
</el-form-item>
|
</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
|
<el-form-item
|
||||||
:label="$T('CHOOSE_SHOWED_PICBED')"
|
:label="$T('CHOOSE_SHOWED_PICBED')"
|
||||||
>
|
>
|
||||||
@ -371,7 +381,8 @@ export default class extends Vue {
|
|||||||
miniWindowOntop: false,
|
miniWindowOntop: false,
|
||||||
logLevel: ['all'],
|
logLevel: ['all'],
|
||||||
autoCopyUrl: true,
|
autoCopyUrl: true,
|
||||||
checkBetaUpdate: true
|
checkBetaUpdate: true,
|
||||||
|
useBuiltinClipboard: false
|
||||||
}
|
}
|
||||||
|
|
||||||
picBed: IPicBedType[] = []
|
picBed: IPicBedType[] = []
|
||||||
@ -446,6 +457,7 @@ export default class extends Vue {
|
|||||||
this.form.logLevel = this.initLogLevel(settings.logLevel || [])
|
this.form.logLevel = this.initLogLevel(settings.logLevel || [])
|
||||||
this.form.autoCopyUrl = settings.autoCopy === undefined ? true : settings.autoCopy
|
this.form.autoCopyUrl = settings.autoCopy === undefined ? true : settings.autoCopy
|
||||||
this.form.checkBetaUpdate = settings.checkBetaUpdate === undefined ? true : settings.checkBetaUpdate
|
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.customLink.value = settings.customLink || '$url'
|
||||||
this.shortKey.upload = settings.shortKey.upload
|
this.shortKey.upload = settings.shortKey.upload
|
||||||
@ -539,6 +551,10 @@ export default class extends Vue {
|
|||||||
this.saveConfig('settings.checkBetaUpdate', val)
|
this.saveConfig('settings.checkBetaUpdate', val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
useBuiltinClipboardChange (val: boolean) {
|
||||||
|
this.saveConfig('settings.useBuiltinClipboard', val)
|
||||||
|
}
|
||||||
|
|
||||||
handleShowPicBedListChange (val: string[]) {
|
handleShowPicBedListChange (val: string[]) {
|
||||||
const list = this.picBed.map(item => {
|
const list = this.picBed.map(item => {
|
||||||
if (!val.includes(item.name)) {
|
if (!val.includes(item.name)) {
|
||||||
|
@ -103,6 +103,7 @@ export const ZH_CN = {
|
|||||||
SETTINGS_DEFAULT_PICBED: '设置默认图床',
|
SETTINGS_DEFAULT_PICBED: '设置默认图床',
|
||||||
SETTINGS_SET_DEFAULT_PICBED: '设为默认图床',
|
SETTINGS_SET_DEFAULT_PICBED: '设为默认图床',
|
||||||
SETTINGS_NOT_CONFIG_OPTIONS: '暂无配置项',
|
SETTINGS_NOT_CONFIG_OPTIONS: '暂无配置项',
|
||||||
|
SETTINGS_USE_BUILTIN_CLIPBOARD_UPLOAD: '使用内置剪贴板上传',
|
||||||
|
|
||||||
// shortcut page
|
// shortcut page
|
||||||
SHORTCUT_NAME: '快捷键名称',
|
SHORTCUT_NAME: '快捷键名称',
|
||||||
|
1
src/universal/types/view.d.ts
vendored
1
src/universal/types/view.d.ts
vendored
@ -9,6 +9,7 @@ interface ISettingForm {
|
|||||||
logLevel: string[]
|
logLevel: string[]
|
||||||
autoCopyUrl: boolean
|
autoCopyUrl: boolean
|
||||||
checkBetaUpdate: boolean
|
checkBetaUpdate: boolean
|
||||||
|
useBuiltinClipboard: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IShortKeyMap {
|
interface IShortKeyMap {
|
||||||
|
Loading…
Reference in New Issue
Block a user