feat: 实现撤销重做功能并优化剪贴板处理
添加撤销/重做历史记录功能,支持Ctrl+Z/Y/Shift+Z快捷键 优化剪贴板处理逻辑,支持GIF动画和多文件复制 更新帮助文档和图标资源,完善打包配置
This commit is contained in:
517
main.js
517
main.js
@@ -185,39 +185,162 @@ app.whenReady().then(() => {
|
||||
});
|
||||
|
||||
function handlePaste() {
|
||||
console.log('handlePaste')
|
||||
console.log('========== handlePaste ==========')
|
||||
|
||||
let payload = {}
|
||||
///strimg = JSON.stringify(img)
|
||||
var formats = clipboard.availableFormats();
|
||||
console.log('Available clipboard formats:', formats)
|
||||
|
||||
// 尝试读取所有可能的格式
|
||||
console.log('Trying FileNameW...')
|
||||
var rawFilePath = clipboard.read('FileNameW');
|
||||
|
||||
if (rawFilePath) {
|
||||
var filePath = rawFilePath.replace(new RegExp(String.fromCharCode(0), 'g'), '');
|
||||
payload.type = 'filePath'
|
||||
payload.filePath = filePath
|
||||
console.log(filePath)
|
||||
|
||||
} else if (formats.indexOf('image/png') > -1) {
|
||||
img = clipboard.readImage();
|
||||
payload.type = 'dataURL'
|
||||
payload.dataURL = img.toDataURL().replace('png', 'gif')
|
||||
} else if (formats.indexOf('text/plain') > -1) {
|
||||
//potential link
|
||||
var potentialUrl = clipboard.readText()
|
||||
console.log("potentialUrl", potentialUrl)
|
||||
if (validateUrl(potentialUrl)) {
|
||||
payload.type = 'filePath'
|
||||
payload.filePath = potentialUrl
|
||||
} else {
|
||||
// paste as text element
|
||||
payload.type = 'text'
|
||||
payload.text = clipboard.readText()
|
||||
console.log('FileNameW result:', rawFilePath ? 'found' : 'not found')
|
||||
|
||||
// 尝试读取 HTML 格式(可能包含文件路径)
|
||||
if (!rawFilePath && formats.indexOf('text/html') > -1) {
|
||||
try {
|
||||
const html = clipboard.readHTML();
|
||||
console.log('HTML content:', html.substring(0, 200));
|
||||
// 尝试从 HTML 中提取文件路径
|
||||
const srcMatch = html.match(/src=["']file:\/\/\/([^"']+)["']/i);
|
||||
if (srcMatch) {
|
||||
rawFilePath = srcMatch[1].replace(/\//g, '\\');
|
||||
console.log('Extracted file path from HTML:', rawFilePath);
|
||||
}
|
||||
} catch (err) {
|
||||
console.log('Failed to read HTML:', err.message);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(formats)
|
||||
//console.log(payload)
|
||||
if (rawFilePath) {
|
||||
var filePath = rawFilePath.replace(new RegExp(String.fromCharCode(0), 'g'), '');
|
||||
|
||||
// 检查是否是图片文件,如果是则转换为 dataURL
|
||||
const imageExtensions = ['.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp'];
|
||||
const isImage = imageExtensions.some(ext => filePath.toLowerCase().endsWith(ext));
|
||||
|
||||
if (isImage && fs.existsSync(filePath)) {
|
||||
try {
|
||||
// 读取文件并转换为 base64
|
||||
const imageBuffer = fs.readFileSync(filePath);
|
||||
const base64 = imageBuffer.toString('base64');
|
||||
|
||||
// 根据文件扩展名确定 MIME 类型
|
||||
let mimeType = 'image/png';
|
||||
if (filePath.toLowerCase().endsWith('.jpg') || filePath.toLowerCase().endsWith('.jpeg')) {
|
||||
mimeType = 'image/jpeg';
|
||||
} else if (filePath.toLowerCase().endsWith('.gif')) {
|
||||
mimeType = 'image/gif';
|
||||
} else if (filePath.toLowerCase().endsWith('.bmp')) {
|
||||
mimeType = 'image/bmp';
|
||||
} else if (filePath.toLowerCase().endsWith('.webp')) {
|
||||
mimeType = 'image/webp';
|
||||
}
|
||||
|
||||
payload.type = 'dataURL';
|
||||
payload.dataURL = `data:${mimeType};base64,${base64}`;
|
||||
console.log('Converted file to dataURL:', filePath, mimeType);
|
||||
} catch (err) {
|
||||
console.error('Error converting file to dataURL:', err);
|
||||
payload.type = 'filePath';
|
||||
payload.filePath = filePath;
|
||||
}
|
||||
} else {
|
||||
payload.type = 'filePath';
|
||||
payload.filePath = filePath;
|
||||
}
|
||||
console.log(filePath)
|
||||
|
||||
} else {
|
||||
// 尝试读取原始图片数据(支持 GIF 动画)
|
||||
let imageBuffer = null;
|
||||
let mimeType = null;
|
||||
|
||||
console.log('No file path found, trying to read image data...')
|
||||
|
||||
// 尝试所有可能的图片格式
|
||||
const imageFormats = [
|
||||
'image/gif',
|
||||
'image/png',
|
||||
'image/jpeg',
|
||||
'image/jpg',
|
||||
'image/bmp',
|
||||
'image/webp',
|
||||
'CF_DIB', // Windows DIB format
|
||||
'CF_DIBV5' // Windows DIB v5 format
|
||||
];
|
||||
|
||||
for (const format of imageFormats) {
|
||||
if (formats.indexOf(format) > -1) {
|
||||
try {
|
||||
console.log(`Trying to read format: ${format}`)
|
||||
const buffer = clipboard.readBuffer(format);
|
||||
if (buffer && buffer.length > 0) {
|
||||
imageBuffer = buffer;
|
||||
|
||||
// 检测实际的图片类型(通过文件头)
|
||||
const header = buffer.slice(0, 6).toString('hex');
|
||||
console.log(`Buffer header: ${header}, size: ${buffer.length}`)
|
||||
|
||||
if (header.startsWith('474946')) {
|
||||
mimeType = 'image/gif';
|
||||
console.log('Detected GIF by header');
|
||||
} else if (header.startsWith('89504e')) {
|
||||
mimeType = 'image/png';
|
||||
console.log('Detected PNG by header');
|
||||
} else if (header.startsWith('ffd8ff')) {
|
||||
mimeType = 'image/jpeg';
|
||||
console.log('Detected JPEG by header');
|
||||
} else {
|
||||
// 使用格式名称作为后备
|
||||
mimeType = format.startsWith('image/') ? format : 'image/png';
|
||||
console.log('Using format name as mime type:', mimeType);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(`Failed to read ${format}:`, err.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (imageBuffer && imageBuffer.length > 0) {
|
||||
// 成功读取到原始图片数据
|
||||
const base64 = imageBuffer.toString('base64');
|
||||
payload.type = 'dataURL';
|
||||
payload.dataURL = `data:${mimeType};base64,${base64}`;
|
||||
console.log('✓ Successfully converted clipboard image to dataURL');
|
||||
console.log(' Type:', mimeType);
|
||||
console.log(' Size:', imageBuffer.length, 'bytes');
|
||||
console.log(' Base64 length:', base64.length);
|
||||
} else if (formats.indexOf('image/png') > -1 || formats.some(f => f.startsWith('image/'))) {
|
||||
// 降级方案:使用 readImage(会丢失 GIF 动画)
|
||||
console.log('⚠ Using fallback readImage (will lose GIF animation)');
|
||||
img = clipboard.readImage();
|
||||
payload.type = 'dataURL';
|
||||
payload.dataURL = img.toDataURL();
|
||||
console.log(' Fallback image size:', img.getSize());
|
||||
} else if (formats.indexOf('text/plain') > -1) {
|
||||
// 文本内容
|
||||
var potentialUrl = clipboard.readText();
|
||||
console.log("Checking if text is URL:", potentialUrl);
|
||||
if (validateUrl(potentialUrl)) {
|
||||
payload.type = 'filePath';
|
||||
payload.filePath = potentialUrl;
|
||||
console.log('✓ Detected URL');
|
||||
} else {
|
||||
// paste as text element
|
||||
payload.type = 'text';
|
||||
payload.text = clipboard.readText();
|
||||
console.log('✓ Pasting as text');
|
||||
}
|
||||
} else {
|
||||
console.log('✗ No supported format found in clipboard');
|
||||
}
|
||||
}
|
||||
|
||||
console.log('========== Final payload type:', payload.type, '==========')
|
||||
mainWin.webContents.send('clipboard', JSON.stringify(payload)) // send to web page
|
||||
}
|
||||
function addToRecent(filePath) {
|
||||
@@ -464,6 +587,36 @@ app.whenReady().then(() => {
|
||||
ipcMain.on('show-help', (event) => {
|
||||
createHelpWindow(mainWin)
|
||||
})
|
||||
ipcMain.on('convert-file-to-dataurl', (event, filePath) => {
|
||||
try {
|
||||
console.log('Converting file to dataURL:', filePath)
|
||||
|
||||
// 读取文件
|
||||
const imageBuffer = fs.readFileSync(filePath)
|
||||
const base64 = imageBuffer.toString('base64')
|
||||
|
||||
// 根据文件扩展名确定 MIME 类型
|
||||
let mimeType = 'image/png'
|
||||
const lowerPath = filePath.toLowerCase()
|
||||
if (lowerPath.endsWith('.jpg') || lowerPath.endsWith('.jpeg')) {
|
||||
mimeType = 'image/jpeg'
|
||||
} else if (lowerPath.endsWith('.gif')) {
|
||||
mimeType = 'image/gif'
|
||||
} else if (lowerPath.endsWith('.bmp')) {
|
||||
mimeType = 'image/bmp'
|
||||
} else if (lowerPath.endsWith('.webp')) {
|
||||
mimeType = 'image/webp'
|
||||
}
|
||||
|
||||
const dataURL = `data:${mimeType};base64,${base64}`
|
||||
console.log('Converted to dataURL, type:', mimeType)
|
||||
|
||||
// 发送回渲染进程
|
||||
event.sender.send('file-converted-to-dataurl', dataURL)
|
||||
} catch (err) {
|
||||
console.error('Error converting file to dataURL:', err)
|
||||
}
|
||||
})
|
||||
ipcMain.on('copy-text', (event, text) => {
|
||||
clipboard.writeText(text)
|
||||
console.log('Copied text to clipboard:', text)
|
||||
@@ -473,7 +626,40 @@ app.whenReady().then(() => {
|
||||
console.log('Received', imageDataURLs.length, 'images to copy')
|
||||
|
||||
if (imageDataURLs.length === 1) {
|
||||
// 只有一张图片,直接复制
|
||||
// 只有一张图片,使用 copy-image 的逻辑(支持 GIF)
|
||||
console.log('Single image, using copy-image handler')
|
||||
// 触发 copy-image 事件
|
||||
mainWin.webContents.send('trigger-copy-single-image')
|
||||
// 直接调用 copy-image 的逻辑
|
||||
const imagePath = imageDataURLs[0]
|
||||
|
||||
// 检测是否是 GIF
|
||||
if (imagePath.startsWith('data:')) {
|
||||
const base64Data = imagePath.split(',')[1]
|
||||
const buffer = Buffer.from(base64Data, 'base64')
|
||||
const header = buffer.slice(0, 6).toString('hex')
|
||||
|
||||
if (header.startsWith('474946')) {
|
||||
// 是 GIF,保存为临时文件
|
||||
console.log('Detected GIF in multiple selection')
|
||||
const tempDir = path.join(app.getPath('temp'), 'RefVroid-clipboard')
|
||||
if (!fs.existsSync(tempDir)) {
|
||||
fs.mkdirSync(tempDir, { recursive: true })
|
||||
}
|
||||
|
||||
const tempFilePath = path.join(tempDir, `animated_${Date.now()}.gif`)
|
||||
fs.writeFileSync(tempFilePath, buffer)
|
||||
|
||||
clipboard.clear()
|
||||
const filePathBuffer = Buffer.from(tempFilePath + '\0', 'ucs2')
|
||||
clipboard.writeBuffer('FileNameW', filePathBuffer)
|
||||
|
||||
console.log('✓ Copied GIF from multiple selection')
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 不是 GIF,使用普通方法
|
||||
const img = nativeImage.createFromDataURL(imageDataURLs[0])
|
||||
if (!img.isEmpty()) {
|
||||
clipboard.clear()
|
||||
@@ -505,14 +691,37 @@ app.whenReady().then(() => {
|
||||
|
||||
// 保存所有图片为临时文件
|
||||
const filePaths = []
|
||||
const timestamp = Date.now()
|
||||
|
||||
imageDataURLs.forEach((dataURL, index) => {
|
||||
try {
|
||||
const img = nativeImage.createFromDataURL(dataURL)
|
||||
if (!img.isEmpty()) {
|
||||
const filePath = path.join(tempDir, `image_${Date.now()}_${index + 1}.png`)
|
||||
fs.writeFileSync(filePath, img.toPNG())
|
||||
// 检测图片类型
|
||||
const base64Data = dataURL.split(',')[1]
|
||||
const buffer = Buffer.from(base64Data, 'base64')
|
||||
const header = buffer.slice(0, 6).toString('hex')
|
||||
|
||||
let extension = 'png'
|
||||
let fileBuffer = null
|
||||
|
||||
if (header.startsWith('474946')) {
|
||||
// GIF - 保存原始数据
|
||||
extension = 'gif'
|
||||
fileBuffer = buffer
|
||||
console.log(' Image', index + 1, 'is GIF, preserving animation')
|
||||
} else {
|
||||
// 其他格式 - 转换为 PNG
|
||||
const img = nativeImage.createFromDataURL(dataURL)
|
||||
if (!img.isEmpty()) {
|
||||
fileBuffer = img.toPNG()
|
||||
console.log(' Image', index + 1, 'converted to PNG')
|
||||
}
|
||||
}
|
||||
|
||||
if (fileBuffer) {
|
||||
const filePath = path.join(tempDir, `image_${timestamp}_${index + 1}.${extension}`)
|
||||
fs.writeFileSync(filePath, fileBuffer)
|
||||
filePaths.push(filePath)
|
||||
console.log('Saved temp image:', filePath)
|
||||
console.log(' Saved:', filePath)
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error saving image', index, ':', err)
|
||||
@@ -524,27 +733,58 @@ app.whenReady().then(() => {
|
||||
return
|
||||
}
|
||||
|
||||
console.log('Copying', filePaths.length, 'files:', filePaths)
|
||||
console.log('Copying', filePaths.length, 'files')
|
||||
|
||||
// 清空剪贴板
|
||||
clipboard.clear()
|
||||
|
||||
// 方法1: 使用 HTML 格式(QQ 支持)
|
||||
const htmlImages = filePaths.map(fp =>
|
||||
`<img src="file:///${fp.replace(/\\/g, '/')}" />`
|
||||
).join('')
|
||||
const html = `<html><body>${htmlImages}</body></html>`
|
||||
// 方法1: 使用 Windows 文件格式(FileNameW)
|
||||
// 这是 Windows 文件复制的标准格式,大多数应用都支持
|
||||
try {
|
||||
// 构建文件列表(每个路径以 null 结尾,最后再加一个 null)
|
||||
let fileListBuffer = Buffer.alloc(0)
|
||||
filePaths.forEach(fp => {
|
||||
const pathBuffer = Buffer.from(fp + '\0', 'ucs2')
|
||||
fileListBuffer = Buffer.concat([fileListBuffer, pathBuffer])
|
||||
})
|
||||
// 添加最后的 null 终止符
|
||||
fileListBuffer = Buffer.concat([fileListBuffer, Buffer.from('\0', 'ucs2')])
|
||||
|
||||
clipboard.writeBuffer('FileNameW', fileListBuffer)
|
||||
console.log(' Wrote FileNameW format')
|
||||
} catch (err) {
|
||||
console.error('Failed to write FileNameW:', err)
|
||||
}
|
||||
|
||||
// 方法2: 使用文件路径格式
|
||||
const fileList = filePaths.join('\n')
|
||||
// 方法2: 使用 HTML 格式(某些应用支持)
|
||||
try {
|
||||
const htmlImages = filePaths.map(fp =>
|
||||
`<img src="file:///${fp.replace(/\\/g, '/')}" />`
|
||||
).join('')
|
||||
const html = `<html><body>${htmlImages}</body></html>`
|
||||
|
||||
clipboard.writeHTML(html)
|
||||
console.log(' Wrote HTML format')
|
||||
} catch (err) {
|
||||
console.error('Failed to write HTML:', err)
|
||||
}
|
||||
|
||||
// 同时写入多种格式
|
||||
clipboard.write({
|
||||
text: fileList,
|
||||
html: html
|
||||
})
|
||||
// 方法3: 如果只有一张图片,同时写入图片数据(提高兼容性)
|
||||
if (filePaths.length === 1) {
|
||||
try {
|
||||
const buffer = fs.readFileSync(filePaths[0])
|
||||
const img = nativeImage.createFromBuffer(buffer)
|
||||
if (!img.isEmpty()) {
|
||||
clipboard.writeImage(img)
|
||||
console.log(' Also wrote image data for single image')
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to write image data:', err)
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Copied', filePaths.length, 'images in multiple formats')
|
||||
console.log('✓ Copied', filePaths.length, 'file(s) in multiple formats')
|
||||
console.log(' Note: Some apps (like Feishu) may show file paths instead of images')
|
||||
|
||||
} catch (err) {
|
||||
console.error('Error copying multiple images:', err)
|
||||
@@ -552,20 +792,121 @@ app.whenReady().then(() => {
|
||||
})
|
||||
ipcMain.on('copy-image', (event, imagePath) => {
|
||||
try {
|
||||
let img = null
|
||||
console.log('========== copy-image ==========')
|
||||
console.log('Image path/dataURL:', imagePath.substring(0, 100))
|
||||
|
||||
// 如果是 dataURL,直接写入
|
||||
// 如果是 dataURL,检测真实的图片类型
|
||||
if (imagePath.startsWith('data:')) {
|
||||
img = nativeImage.createFromDataURL(imagePath)
|
||||
console.log('Processing dataURL image, size:', img.getSize())
|
||||
|
||||
// 确保图片不为空
|
||||
if (img.isEmpty()) {
|
||||
console.error('Created image from dataURL is empty')
|
||||
clipboard.writeText('图片复制失败')
|
||||
try {
|
||||
// 从 dataURL 提取 base64 数据
|
||||
const base64Data = imagePath.split(',')[1]
|
||||
const buffer = Buffer.from(base64Data, 'base64')
|
||||
|
||||
// 检测文件头来确定真实类型
|
||||
const header = buffer.slice(0, 6).toString('hex')
|
||||
console.log('Image buffer header:', header, 'size:', buffer.length, 'bytes')
|
||||
|
||||
let actualMimeType = null
|
||||
let isGif = false
|
||||
|
||||
if (header.startsWith('474946')) {
|
||||
// GIF 文件头: 47 49 46 = "GIF"
|
||||
actualMimeType = 'image/gif'
|
||||
isGif = true
|
||||
console.log('✓ Detected GIF by file header')
|
||||
} else if (header.startsWith('89504e')) {
|
||||
// PNG 文件头: 89 50 4E 47
|
||||
actualMimeType = 'image/png'
|
||||
console.log('✓ Detected PNG by file header')
|
||||
} else if (header.startsWith('ffd8ff')) {
|
||||
// JPEG 文件头: FF D8 FF
|
||||
actualMimeType = 'image/jpeg'
|
||||
console.log('✓ Detected JPEG by file header')
|
||||
} else {
|
||||
console.log('⚠ Unknown image format, using declared MIME type')
|
||||
actualMimeType = imagePath.split(';')[0].split(':')[1]
|
||||
}
|
||||
|
||||
if (isGif) {
|
||||
// GIF - 保存为临时文件并复制文件
|
||||
console.log('Copying GIF with animation...')
|
||||
|
||||
try {
|
||||
// 创建临时文件
|
||||
const tempDir = path.join(app.getPath('temp'), 'RefVroid-clipboard')
|
||||
if (!fs.existsSync(tempDir)) {
|
||||
fs.mkdirSync(tempDir, { recursive: true })
|
||||
}
|
||||
|
||||
// 清理旧的临时文件
|
||||
try {
|
||||
const files = fs.readdirSync(tempDir)
|
||||
const now = Date.now()
|
||||
files.forEach(file => {
|
||||
const filePath = path.join(tempDir, file)
|
||||
const stats = fs.statSync(filePath)
|
||||
// 删除超过1小时的文件
|
||||
if (now - stats.mtimeMs > 3600000) {
|
||||
fs.unlinkSync(filePath)
|
||||
}
|
||||
})
|
||||
} catch (err) {
|
||||
// 忽略清理错误
|
||||
}
|
||||
|
||||
const tempFilePath = path.join(tempDir, `animated_${Date.now()}.gif`)
|
||||
fs.writeFileSync(tempFilePath, buffer)
|
||||
console.log(' Created temp GIF file:', tempFilePath)
|
||||
|
||||
// 使用 Electron 的 clipboard.writeBuffer 写入文件路径
|
||||
// 这是 Windows 文件复制的标准格式
|
||||
clipboard.clear()
|
||||
|
||||
// 写入文件路径(CF_HDROP 格式)
|
||||
const filePathBuffer = Buffer.from(tempFilePath + '\0', 'ucs2')
|
||||
clipboard.writeBuffer('FileNameW', filePathBuffer)
|
||||
|
||||
console.log('✓ Successfully copied GIF file to clipboard')
|
||||
console.log(' Temp file:', tempFilePath)
|
||||
console.log(' You can now paste it to QQ or other apps')
|
||||
return
|
||||
} catch (err) {
|
||||
console.error('Failed to copy GIF with temp file:', err)
|
||||
|
||||
// 降级方案:只写入静态图片
|
||||
clipboard.clear()
|
||||
const img = nativeImage.createFromBuffer(buffer)
|
||||
if (!img.isEmpty()) {
|
||||
clipboard.writeImage(img)
|
||||
console.log('⚠ Copied as static image (animation lost)')
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 其他格式 - 使用 nativeImage
|
||||
console.log('Copying', actualMimeType, 'image...')
|
||||
const correctedDataURL = `data:${actualMimeType};base64,${base64Data}`
|
||||
const img = nativeImage.createFromDataURL(correctedDataURL)
|
||||
|
||||
if (!img.isEmpty()) {
|
||||
clipboard.clear()
|
||||
clipboard.writeImage(img)
|
||||
console.log('✓ Successfully copied', actualMimeType, 'to clipboard')
|
||||
console.log(' Image size:', img.getSize())
|
||||
return
|
||||
} else {
|
||||
console.error('✗ Created image is empty')
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to process dataURL:', err)
|
||||
clipboard.writeText('图片复制失败: ' + err.message)
|
||||
return
|
||||
}
|
||||
} else if (imagePath.startsWith('http://') || imagePath.startsWith('https://')) {
|
||||
}
|
||||
|
||||
// 处理网络图片
|
||||
if (imagePath.startsWith('http://') || imagePath.startsWith('https://')) {
|
||||
// 网络图片暂时只能复制 URL
|
||||
clipboard.writeText(imagePath)
|
||||
console.log('Copied image URL to clipboard:', imagePath)
|
||||
@@ -591,23 +932,57 @@ app.whenReady().then(() => {
|
||||
return
|
||||
}
|
||||
|
||||
img = nativeImage.createFromPath(filePath)
|
||||
// 检查是否是 GIF 文件
|
||||
const isGifFile = filePath.toLowerCase().endsWith('.gif')
|
||||
|
||||
if (isGifFile) {
|
||||
// GIF 文件 - 直接复制文件路径
|
||||
try {
|
||||
console.log('Detected GIF file, copying file path')
|
||||
const buffer = fs.readFileSync(filePath)
|
||||
console.log('GIF buffer size:', buffer.length, 'bytes')
|
||||
|
||||
clipboard.clear()
|
||||
|
||||
// 写入文件路径(Windows 格式)
|
||||
clipboard.write({
|
||||
text: filePath,
|
||||
bookmark: filePath
|
||||
})
|
||||
|
||||
// 同时写入 GIF buffer
|
||||
clipboard.writeBuffer('image/gif', buffer)
|
||||
|
||||
// 写入静态图片作为降级
|
||||
const img = nativeImage.createFromBuffer(buffer)
|
||||
if (!img.isEmpty()) {
|
||||
clipboard.writeImage(img)
|
||||
console.log(' Also wrote static image as fallback')
|
||||
}
|
||||
|
||||
console.log('✓ Successfully copied GIF file')
|
||||
return
|
||||
} catch (err) {
|
||||
console.error('Failed to copy GIF file:', err)
|
||||
// 降级到普通方法
|
||||
}
|
||||
}
|
||||
|
||||
// 其他格式的文件
|
||||
const img = nativeImage.createFromPath(filePath)
|
||||
console.log('Image loaded, size:', img.getSize())
|
||||
}
|
||||
|
||||
if (img && !img.isEmpty()) {
|
||||
// 清空剪贴板
|
||||
clipboard.clear()
|
||||
|
||||
// 写入图片到剪贴板
|
||||
clipboard.writeImage(img)
|
||||
|
||||
console.log('Successfully copied image to clipboard')
|
||||
console.log('Image size:', img.getSize())
|
||||
console.log('Image aspect ratio:', img.getAspectRatio())
|
||||
} else {
|
||||
console.log('Image is empty, copying error message')
|
||||
clipboard.writeText('图片为空,无法复制')
|
||||
if (!img.isEmpty()) {
|
||||
clipboard.clear()
|
||||
clipboard.writeImage(img)
|
||||
console.log('✓ Successfully copied image to clipboard')
|
||||
console.log(' Image size:', img.getSize())
|
||||
return
|
||||
} else {
|
||||
console.error('✗ Image is empty')
|
||||
clipboard.writeText('图片为空')
|
||||
return
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error copying image:', err)
|
||||
|
||||
Reference in New Issue
Block a user