From 20e38f4eb961a2d0ae294207a35f4c51d2ef4f6b Mon Sep 17 00:00:00 2001 From: PiEgg Date: Thu, 25 Aug 2022 20:04:50 +0800 Subject: [PATCH] :bug: Fix: macos clipboard image can't show on tray page ISSUES CLOSED: #961 --- src/main/apis/app/system/index.ts | 38 ++++++++++++++++++++------ src/main/apis/app/window/windowList.ts | 3 +- src/main/utils/common.ts | 17 ++++++++++++ src/renderer/pages/TrayPage.vue | 1 - 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/main/apis/app/system/index.ts b/src/main/apis/app/system/index.ts index 5f0c754db..eb3557431 100644 --- a/src/main/apis/app/system/index.ts +++ b/src/main/apis/app/system/index.ts @@ -1,3 +1,4 @@ +import fs from 'fs-extra' import { app, Menu, @@ -15,7 +16,7 @@ import { IWindowList } from '#/types/enum' import picgo from '@core/picgo' import pasteTemplate from '~/main/utils/pasteTemplate' import pkg from 'root/package.json' -import { handleCopyUrl } from '~/main/utils/common' +import { ensureFilePath, handleCopyUrl } from '~/main/utils/common' import { privacyManager } from '~/main/utils/privacyManager' // import { T } from '#/i18n' import { T } from '~/main/i18n' @@ -173,18 +174,37 @@ export function createTray () { tray.on('click', (event, bounds) => { if (process.platform === 'darwin') { toggleWindow(bounds) - setTimeout(() => { + setTimeout(async () => { const img = clipboard.readImage() const obj: ImgInfo[] = [] if (!img.isEmpty()) { // 从剪贴板来的图片默认转为png - // @ts-ignore - const imgUrl = 'data:image/png;base64,' + Buffer.from(img.toPNG(), 'binary').toString('base64') - obj.push({ - width: img.getSize().width, - height: img.getSize().height, - imgUrl - }) + // https://github.com/electron/electron/issues/9035 + const imgPath = clipboard.read('public.file-url') + if (imgPath) { + const decodePath = ensureFilePath(imgPath) + if (decodePath === imgPath) { + obj.push({ + imgUrl: imgPath + }) + } else { + if (decodePath !== '') { + // 带有中文的路径,无法直接被img.src所使用,会被转义 + const base64 = await fs.readFile(decodePath.replace('file://', ''), { encoding: 'base64' }) + obj.push({ + imgUrl: `data:image/png;base64,${base64}` + }) + } + } + } else { + const imgUrl = img.toDataURL() + // console.log(imgUrl) + obj.push({ + width: img.getSize().width, + height: img.getSize().height, + imgUrl + }) + } } windowManager.get(IWindowList.TRAY_WINDOW)!.webContents.send('clipboardFiles', obj) }, 0) diff --git a/src/main/apis/app/window/windowList.ts b/src/main/apis/app/window/windowList.ts index a3a03655a..e36d2c5b9 100644 --- a/src/main/apis/app/window/windowList.ts +++ b/src/main/apis/app/window/windowList.ts @@ -41,7 +41,8 @@ windowList.set(IWindowList.TRAY_WINDOW, { nodeIntegration: !!process.env.ELECTRON_NODE_INTEGRATION, contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION, nodeIntegrationInWorker: true, - backgroundThrottling: false + backgroundThrottling: false, + webSecurity: false } } }, diff --git a/src/main/utils/common.ts b/src/main/utils/common.ts index f99873364..b3c9865bb 100644 --- a/src/main/utils/common.ts +++ b/src/main/utils/common.ts @@ -1,3 +1,4 @@ +import fs from 'fs-extra' import db from '~/main/apis/core/datastore' import { clipboard, Notification, dialog } from 'electron' @@ -69,3 +70,19 @@ export const calcDurationRange = (duration: number) => { // max range return 100000 } + +/** + * macOS public.file-url will get encoded file path, + * so we need to decode it + */ +export const ensureFilePath = (filePath: string, prefix = 'file://'): string => { + filePath = filePath.replace(prefix, '') + if (fs.existsSync(filePath)) { + return `${prefix}${filePath}` + } + filePath = decodeURIComponent(filePath) + if (fs.existsSync(filePath)) { + return `${prefix}${filePath}` + } + return '' +} diff --git a/src/renderer/pages/TrayPage.vue b/src/renderer/pages/TrayPage.vue index c1bbec996..493379d65 100644 --- a/src/renderer/pages/TrayPage.vue +++ b/src/renderer/pages/TrayPage.vue @@ -107,7 +107,6 @@ export default class extends Vue { }) ipcRenderer.on('uploadFiles', async () => { this.files = (await this.$$db.get({ orderBy: 'desc', limit: 5 })).data - console.log(this.files) this.uploadFlag = false }) ipcRenderer.on('updateFiles', () => {