Mailspring/internal_packages/message-list/lib/inline-download-prompts.es6
Ben Gotow aa7ef91b0b fix(files): When download mode is “manual” prompt about inline attachments
Summary:
When you have your "Download attachments for new mail" setting set
to "manually", inline images always appear broken with no explanation.

This patch listens for the image load to fail and displays a button which
queues the fetchFile task on click. This seemed like the best approach because
it doesn't slow down the loading of the message with more fstats / lookups.
(Seeing if the file has already been downloaded is an async operation)

Test Plan: No specs atm

Reviewers: evan, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3313
2016-10-03 11:22:44 -07:00

42 lines
1.3 KiB
JavaScript

import {Actions, Utils} from 'nylas-exports';
function _runOnImageNode(node) {
if (node.src && node.dataset.nylasFile) {
node.addEventListener('error', () => {
const file = JSON.parse(atob(node.dataset.nylasFile), Utils.registeredObjectReviver);
const initialDisplay = node.style.display;
const downloadButton = document.createElement('a');
downloadButton.classList.add('inline-download-prompt')
downloadButton.textContent = "Click to download inline image";
downloadButton.addEventListener('click', () => {
Actions.fetchFile(file);
node.parentNode.removeChild(downloadButton);
node.addEventListener('load', () => {
node.style.display = initialDisplay;
});
});
node.style.display = 'none';
node.parentNode.insertBefore(downloadButton, node);
});
}
}
export function encodedAttributeForFile(file) {
return btoa(JSON.stringify(file, Utils.registeredObjectReplacer));
}
export function addInlineDownloadPrompts(doc) {
const imgTagWalker = document.createTreeWalker(doc.body, NodeFilter.SHOW_ELEMENT, {
acceptNode: (node) => {
if (node.nodeName === 'IMG') {
return NodeFilter.FILTER_ACCEPT;
}
return NodeFilter.FILTER_SKIP;
},
});
while (imgTagWalker.nextNode()) {
_runOnImageNode(imgTagWalker.currentNode);
}
}