mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 13:39:01 +01:00 
			
		
		
		
	Use the electron Clipboard module when using "Copy image to clipboard"
This commit is contained in:
		
							parent
							
								
									9220616bb5
								
							
						
					
					
						commit
						cfc32a14e0
					
				@ -24,13 +24,23 @@ function setupContextMenu($image) {
 | 
				
			|||||||
                },
 | 
					                },
 | 
				
			||||||
                {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 imageUrl = $image.attr('src');
 | 
				
			||||||
                    webContents.copyImageAt(e.pageX, e.pageY);
 | 
					                        const response = await fetch(imageUrl);
 | 
				
			||||||
 | 
					                        const blob = await response.blob();
 | 
				
			||||||
 | 
					                        const arrayBuffer = await blob.arrayBuffer();
 | 
				
			||||||
 | 
					                        const buffer = Buffer.from(arrayBuffer);
 | 
				
			||||||
 | 
					                        const nativeImage = utils.dynamicRequire('electron').nativeImage;
 | 
				
			||||||
 | 
					                        const clipboard = utils.dynamicRequire('electron').clipboard;
 | 
				
			||||||
 | 
					                        const image = nativeImage.createFromBuffer(buffer);
 | 
				
			||||||
 | 
					                        clipboard.writeImage(image);
 | 
				
			||||||
 | 
					                    } 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}'`);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user