Commit 234b273c authored by JC Brand's avatar JC Brand

message-view: Add support for rendering Imgur URLs

parent b0a9782d
...@@ -275,9 +275,11 @@ converse.plugins.add('converse-message-view', { ...@@ -275,9 +275,11 @@ converse.plugins.add('converse-message-view', {
const msg_content = msg.querySelector('.chat-msg__text'); const msg_content = msg.querySelector('.chat-msg__text');
if (text && text !== url) { if (text && text !== url) {
msg_content.innerHTML = await this.transformBodyText(text); msg_content.innerHTML = await this.transformBodyText(text);
if (_converse.show_images_inline) {
await u.renderImageURLs(_converse, msg_content); await u.renderImageURLs(_converse, msg_content);
} }
} }
}
if (this.model.get('type') !== 'headline') { if (this.model.get('type') !== 'headline') {
this.renderAvatar(msg); this.renderAvatar(msg);
} }
......
...@@ -182,6 +182,18 @@ u.applyDragResistance = function (value, default_value) { ...@@ -182,6 +182,18 @@ u.applyDragResistance = function (value, default_value) {
}; };
function renderImage (img_url, link_url, el, callback) {
if (u.isImageURL(img_url)) {
return isImage(img_url)
.then(() => sizzle(`a[href="${link_url}"]`, el).forEach(a => (a.outerHTML = tpl_image({'url': img_url}))))
.then(callback)
.catch(callback);
} else {
return callback();
}
}
/** /**
* Returns a Promise which resolves once all images have been loaded. * Returns a Promise which resolves once all images have been loaded.
* @method u#renderImageURLs * @method u#renderImageURLs
...@@ -197,20 +209,11 @@ u.renderImageURLs = function (_converse, el) { ...@@ -197,20 +209,11 @@ u.renderImageURLs = function (_converse, el) {
return Promise.all( return Promise.all(
list.map(url => list.map(url =>
new Promise((resolve) => { new Promise((resolve) => {
if (u.isImageURL(url)) { if (url.startsWith('https://imgur.com') && !u.isImageURL(url)) {
return isImage(url).then(img => { const imgur_url = url + '.png';
const i = new Image(); renderImage(imgur_url, url, el, resolve);
i.src = img.src;
i.addEventListener('load', resolve);
// We also resolve (instead of reject) for non-images,
// otherwise the Promise.all resolves prematurely.
i.addEventListener('error', resolve);
const { __ } = _converse;
sizzle(`a[href="${url}"]`, el)
.forEach(a => (a.outerHTML = tpl_image({url, 'label_download': __('Download')})));
}).catch(resolve)
} else { } else {
return resolve(); renderImage(url, url, el, resolve);
} }
}) })
) )
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment