From 7e33530a8d7cb30fb9fef2c48cba97e5bb88d73c Mon Sep 17 00:00:00 2001 From: Vijay Aravamudhan Date: Sat, 27 May 2023 21:51:11 +0530 Subject: Add new context menu item to download images (#935) --- src/webview/contextMenuBuilder.ts | 45 ++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 15 deletions(-) (limited to 'src/webview/contextMenuBuilder.ts') diff --git a/src/webview/contextMenuBuilder.ts b/src/webview/contextMenuBuilder.ts index 2e64977c1..6d904eee3 100644 --- a/src/webview/contextMenuBuilder.ts +++ b/src/webview/contextMenuBuilder.ts @@ -633,27 +633,42 @@ export class ContextMenuBuilder { menu.append(copyImageUrl); // TODO: This doesn't seem to work on linux, so, limiting to Mac for now - if (isMac && menuInfo.srcURL.startsWith('blob:')) { - const downloadImage = new MenuItem({ - label: this.stringTable.downloadImage(), - click: () => { - const urlWithoutBlob = menuInfo.srcURL.slice(5); - this.convertImageToBase64(menuInfo.srcURL, (dataURL: any) => { - const url = new window.URL(urlWithoutBlob); + if (isMac) { + const clickHandler = menuInfo.srcURL.startsWith('blob:') + ? () => { + const urlWithoutBlob = menuInfo.srcURL.slice(5); + this.convertImageToBase64(menuInfo.srcURL, (dataURL: any) => { + const url = new window.URL(urlWithoutBlob); + const fileName = url.pathname.slice(1); + ipcRenderer.send('download-file', { + content: dataURL, + fileOptions: { + name: `${fileName}.png`, + }, + }); + }); + this._sendNotificationOnClipboardEvent( + menuInfo.clipboardNotifications, + () => `Image downloaded: ${urlWithoutBlob}`, + ); + } + : () => { + const url = new window.URL(menuInfo.srcURL); const fileName = url.pathname.slice(1); ipcRenderer.send('download-file', { - content: dataURL, + url: menuInfo.srcURL, fileOptions: { name: fileName, - mime: 'image/png', }, }); - }); - this._sendNotificationOnClipboardEvent( - menuInfo.clipboardNotifications, - () => `Image downloaded: ${urlWithoutBlob}`, - ); - }, + this._sendNotificationOnClipboardEvent( + menuInfo.clipboardNotifications, + () => `Image downloaded: ${menuInfo.srcURL}`, + ); + }; + const downloadImage = new MenuItem({ + label: this.stringTable.downloadImage(), + click: clickHandler, }); menu.append(downloadImage); -- cgit v1.2.3-70-g09d2