From c530d31dd55e16be1fef22615d74d0817b13b5aa Mon Sep 17 00:00:00 2001 From: Jarrad Whitaker Date: Wed, 29 Jul 2020 10:53:39 +1000 Subject: [PATCH] check all clipboard contents for Files, not just the first. This was implemented for the "Copy Image" function of browsers, which on Firefox and Chrome on Linux was tested to give item[0] as "text/html" and item[1] as a file. This effectively worked in rich text mode anyway as it is able to paste text/html snippets, but was unable to work in plain text mode. --- .../composer-editor/composer-editor.tsx | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/app/src/components/composer-editor/composer-editor.tsx b/app/src/components/composer-editor/composer-editor.tsx index 8ea087e06..c8790be19 100644 --- a/app/src/components/composer-editor/composer-editor.tsx +++ b/app/src/components/composer-editor/composer-editor.tsx @@ -277,33 +277,34 @@ export function handleFilePasted(event: ClipboardEvent, onFileReceived: (path: s if (event.clipboardData.items.length === 0) { return false; } - const item = event.clipboardData.items[0]; + for (const i in event.clipboardData.items) { + const item = event.clipboardData.items[i]; + // If the pasteboard has a file on it, stream it to a temporary + // file and fire our `onFilePaste` event. + if (item.kind === 'file') { + const temp = require('temp'); + const blob = item.getAsFile(); + const ext = + { + 'image/png': '.png', + 'image/jpg': '.jpg', + 'image/tiff': '.tiff', + }[item.type] || ''; - // If the pasteboard has a file on it, stream it to a temporary - // file and fire our `onFilePaste` event. - if (item.kind === 'file') { - const temp = require('temp'); - const blob = item.getAsFile(); - const ext = - { - 'image/png': '.png', - 'image/jpg': '.jpg', - 'image/tiff': '.tiff', - }[item.type] || ''; - - const reader = new FileReader(); - reader.addEventListener('loadend', () => { - const buffer = Buffer.from(new Uint8Array(reader.result as any)); - const tmpFolder = temp.path('-mailspring-attachment'); - const tmpPath = path.join(tmpFolder, `Pasted File${ext}`); - fs.mkdir(tmpFolder, () => { - fs.writeFile(tmpPath, buffer, () => { - onFileReceived(tmpPath); + const reader = new FileReader(); + reader.addEventListener('loadend', () => { + const buffer = Buffer.from(new Uint8Array(reader.result as any)); + const tmpFolder = temp.path('-mailspring-attachment'); + const tmpPath = path.join(tmpFolder, `Pasted File${ext}`); + fs.mkdir(tmpFolder, () => { + fs.writeFile(tmpPath, buffer, () => { + onFileReceived(tmpPath); + }); }); }); - }); - reader.readAsArrayBuffer(blob); - return true; + reader.readAsArrayBuffer(blob); + return true; + } } const macCopiedFile = decodeURI(ElectronClipboard.read('public.file-url').replace('file://', ''));