mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 04:51:31 +08:00 
			
		
		
		
	Merge pull request #452 from TriliumNext/fix-image-copy
Use the electron Clipboard module when using "Copy image to clipboard"
This commit is contained in:
		
						commit
						9c8cf0bc09
					
				| @ -22,15 +22,31 @@ function setupContextMenu($image) { | |||||||
|                     command: "copyImageReferenceToClipboard", |                     command: "copyImageReferenceToClipboard", | ||||||
|                     uiIcon: "bx bx-empty" |                     uiIcon: "bx bx-empty" | ||||||
|                 }, |                 }, | ||||||
|                 {title: "Copy image to clipboard", command: "copyImageToClipboard", uiIcon: "bx bx-empty"}, |                 { title: "Copy image to clipboard", command: "copyImageToClipboard", uiIcon: "bx bx-empty" }, | ||||||
|             ], |             ], | ||||||
|             selectMenuItemHandler: ({command}) => { |             selectMenuItemHandler: async ({ command }) => { | ||||||
|                 if (command === 'copyImageReferenceToClipboard') { |                 if (command === 'copyImageReferenceToClipboard') { | ||||||
|                     imageService.copyImageReferenceToClipboard($image); |                     imageService.copyImageReferenceToClipboard($image); | ||||||
|                 } else if (command === 'copyImageToClipboard') { |                 } else if (command === 'copyImageToClipboard') { | ||||||
|                     const webContents = utils.dynamicRequire('@electron/remote').getCurrentWebContents(); |                     try { | ||||||
|                     utils.dynamicRequire('electron'); |                         const nativeImage = utils.dynamicRequire('electron').nativeImage; | ||||||
|                     webContents.copyImageAt(e.pageX, e.pageY); |                         const clipboard = utils.dynamicRequire('electron').clipboard; | ||||||
|  | 
 | ||||||
|  |                         const response = await fetch( | ||||||
|  |                             $image.attr('src') | ||||||
|  |                         ); | ||||||
|  |                         const blob = await response.blob(); | ||||||
|  | 
 | ||||||
|  |                         clipboard.writeImage( | ||||||
|  |                             nativeImage.createFromBuffer( | ||||||
|  |                                 Buffer.from( | ||||||
|  |                                     await blob.arrayBuffer() | ||||||
|  |                                 ) | ||||||
|  |                             ) | ||||||
|  |                         ); | ||||||
|  |                     } catch (error) { | ||||||
|  |                         console.error('Failed to copy image to clipboard:', error); | ||||||
|  |                     } | ||||||
|                 } else { |                 } else { | ||||||
|                     throw new Error(`Unrecognized command '${command}'`); |                     throw new Error(`Unrecognized command '${command}'`); | ||||||
|                 } |                 } | ||||||
| @ -41,4 +57,4 @@ function setupContextMenu($image) { | |||||||
| 
 | 
 | ||||||
| export default { | export default { | ||||||
|     setupContextMenu |     setupContextMenu | ||||||
| }; | }; | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Elian Doran
						Elian Doran