fix(desktop): preserve copy image for large remote media
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import { test } from 'vitest'
|
||||
|
||||
import { imageContextMenuItems } from './image-context-menu'
|
||||
|
||||
function createActions() {
|
||||
const calls = {
|
||||
copyImageAt: [],
|
||||
openImage: [],
|
||||
copyImageAddress: [],
|
||||
saveImage: []
|
||||
}
|
||||
|
||||
return {
|
||||
calls,
|
||||
actions: {
|
||||
copyImageAt: (x, y) => calls.copyImageAt.push([x, y]),
|
||||
openImage: url => calls.openImage.push(url),
|
||||
copyImageAddress: url => calls.copyImageAddress.push(url),
|
||||
saveImage: url => calls.saveImage.push(url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test('keeps Copy Image available when Chromium omits a large image srcURL', () => {
|
||||
const { actions, calls } = createActions()
|
||||
const items = imageContextMenuItems({ mediaType: 'image', srcURL: '', x: 100, y: 120 }, actions)
|
||||
|
||||
assert.deepEqual(
|
||||
items.map(item => item.label),
|
||||
['Copy Image']
|
||||
)
|
||||
|
||||
items[0].click()
|
||||
assert.deepEqual(calls.copyImageAt, [[100, 120]])
|
||||
})
|
||||
|
||||
test('keeps URL-dependent image actions when srcURL is available', () => {
|
||||
const { actions, calls } = createActions()
|
||||
const url = 'https://example.com/image.png'
|
||||
const items = imageContextMenuItems({ mediaType: 'image', srcURL: url, x: 5, y: 8 }, actions)
|
||||
|
||||
assert.deepEqual(
|
||||
items.map(item => item.label),
|
||||
['Open Image', 'Copy Image', 'Copy Image Address', 'Save Image As...']
|
||||
)
|
||||
|
||||
items[0].click()
|
||||
items[1].click()
|
||||
items[2].click()
|
||||
items[3].click()
|
||||
|
||||
assert.deepEqual(calls.openImage, [url])
|
||||
assert.deepEqual(calls.copyImageAt, [[5, 8]])
|
||||
assert.deepEqual(calls.copyImageAddress, [url])
|
||||
assert.deepEqual(calls.saveImage, [url])
|
||||
})
|
||||
|
||||
test('does not add image actions for a non-image target', () => {
|
||||
const { actions } = createActions()
|
||||
|
||||
assert.deepEqual(imageContextMenuItems({ mediaType: 'none', srcURL: '', x: 0, y: 0 }, actions), [])
|
||||
})
|
||||
@@ -0,0 +1,40 @@
|
||||
export function imageContextMenuItems(params, actions) {
|
||||
if (params.mediaType !== 'image') {
|
||||
return []
|
||||
}
|
||||
|
||||
const items = []
|
||||
const srcURL = params.srcURL || ''
|
||||
|
||||
if (srcURL) {
|
||||
items.push({
|
||||
label: 'Open Image',
|
||||
click: () => {
|
||||
if (!srcURL.startsWith('data:')) {
|
||||
actions.openImage(srcURL)
|
||||
}
|
||||
},
|
||||
enabled: !srcURL.startsWith('data:')
|
||||
})
|
||||
}
|
||||
|
||||
items.push({
|
||||
label: 'Copy Image',
|
||||
click: () => actions.copyImageAt(params.x, params.y)
|
||||
})
|
||||
|
||||
if (srcURL) {
|
||||
items.push(
|
||||
{
|
||||
label: 'Copy Image Address',
|
||||
click: () => actions.copyImageAddress(srcURL)
|
||||
},
|
||||
{
|
||||
label: 'Save Image As...',
|
||||
click: () => actions.saveImage(srcURL)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
return items
|
||||
}
|
||||
@@ -17,7 +17,6 @@ import {
|
||||
globalShortcut,
|
||||
ipcMain,
|
||||
Menu,
|
||||
nativeImage,
|
||||
nativeTheme,
|
||||
Notification,
|
||||
powerMonitor,
|
||||
@@ -169,6 +168,7 @@ import { cursorPointInWindow } from './hud-cursor'
|
||||
import { snapHudBounds } from './hud-snap'
|
||||
import { createHudSnapShortcut } from './hud-snap-shortcut'
|
||||
import { buildHudWindowUrl } from './hud-url'
|
||||
import { imageContextMenuItems } from './image-context-menu'
|
||||
import { createLinkTitleWindow, guardLinkTitleSession, readLinkTitleWindowTitle } from './link-title-window'
|
||||
import { ensureMainWindow } from './main-window-lifecycle'
|
||||
import {
|
||||
@@ -5130,17 +5130,6 @@ async function resourceBufferFromUrl(rawUrl) {
|
||||
})
|
||||
}
|
||||
|
||||
async function copyImageFromUrl(rawUrl) {
|
||||
const { buffer } = (await resourceBufferFromUrl(rawUrl)) as any
|
||||
const image = nativeImage.createFromBuffer(buffer)
|
||||
|
||||
if (image.isEmpty()) {
|
||||
throw new Error('Could not read image')
|
||||
}
|
||||
|
||||
clipboard.writeImage(image)
|
||||
}
|
||||
|
||||
async function saveImageFromUrl(rawUrl) {
|
||||
const { buffer, mimeType } = (await resourceBufferFromUrl(rawUrl)) as any
|
||||
const extension = extensionForMimeType(mimeType) || '.png'
|
||||
@@ -5992,39 +5981,19 @@ function installContextMenu(window) {
|
||||
window.webContents.on('context-menu', (_event, params) => {
|
||||
const template = []
|
||||
const hasSelection = Boolean(params.selectionText?.trim())
|
||||
const hasImage = params.mediaType === 'image' && Boolean(params.srcURL)
|
||||
const hasLink = Boolean(params.linkURL)
|
||||
const isEditable = Boolean(params.isEditable)
|
||||
|
||||
if (hasImage) {
|
||||
template.push(
|
||||
{
|
||||
label: 'Open Image',
|
||||
click: () => {
|
||||
if (params.srcURL && !params.srcURL.startsWith('data:')) {
|
||||
openExternalUrl(params.srcURL)
|
||||
}
|
||||
},
|
||||
enabled: !params.srcURL.startsWith('data:')
|
||||
},
|
||||
{
|
||||
label: 'Copy Image',
|
||||
click: () => {
|
||||
void copyImageFromUrl(params.srcURL).catch(error => rememberLog(`Copy image failed: ${error.message}`))
|
||||
}
|
||||
},
|
||||
{
|
||||
label: 'Copy Image Address',
|
||||
click: () => clipboard.writeText(params.srcURL)
|
||||
},
|
||||
{
|
||||
label: 'Save Image As...',
|
||||
click: () => {
|
||||
void saveImageFromUrl(params.srcURL).catch(error => rememberLog(`Save image failed: ${error.message}`))
|
||||
}
|
||||
template.push(
|
||||
...imageContextMenuItems(params, {
|
||||
copyImageAt: (x, y) => window.webContents.copyImageAt(x, y),
|
||||
openImage: openExternalUrl,
|
||||
copyImageAddress: url => clipboard.writeText(url),
|
||||
saveImage: url => {
|
||||
void saveImageFromUrl(url).catch(error => rememberLog(`Save image failed: ${error.message}`))
|
||||
}
|
||||
)
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
if (hasLink) {
|
||||
if (template.length) {
|
||||
|
||||
Reference in New Issue
Block a user