Mailspring/internal_packages/message-list/lib/autoscale-images.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

54 lines
1.5 KiB
JavaScript

function _getDimension(node, dim) {
const raw = node.style[dim] || node[dim];
if (!raw) {
return [null, ''];
}
const valueRegexp = /(\d*)(.*)/;
const match = valueRegexp.exec(raw);
if (!match) {
return [null, ''];
}
const value = match[1];
const units = match[2] || 'px';
return [value / 1, units];
}
function _runOnImageNode(node) {
const [width, widthUnits] = _getDimension(node, 'width');
const [height, heightUnits] = _getDimension(node, 'height');
if (node.style.maxWidth || node.style.maxHeight) {
return;
}
// VW is like %, but always basd on the iframe width, regardless of whether
// a container is position: relative.
// https://web-design-weekly.com/2014/11/18/viewport-units-vw-vh-vmin-vmax/
if (width && height && (widthUnits === heightUnits)) {
node.style.maxWidth = '100vw';
node.style.maxHeight = `${100 * height / width}vw`;
} else if (!height) {
node.style.maxWidth = '100vw';
} else {
// If your image has a width and height in different units, or a height and
// no width, we don't want to screw with it because it would change the
// aspect ratio.
}
}
export function autoscaleImages(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);
}
}