internal-links: Open image link in webapp lightbox.

This will open the image in the webapp lightbox. It shows the same behaviour
that happens when clicking on the image preview.

Improves: #469.
This commit is contained in:
Abhigyan Khaund
2018-04-09 18:16:57 +05:30
committed by Akash Nimare
parent 6e7333eab6
commit 60d693700e
2 changed files with 25 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ const { ipcRenderer } = require('electron');
const SetupSpellChecker = require('./spellchecker');
const ConfigUtil = require(__dirname + '/utils/config-util.js');
const LinkUtil = require(__dirname + '/utils/link-util.js');
// eslint-disable-next-line import/no-unassigned-import
require('./notification');
@@ -55,6 +56,24 @@ document.addEventListener('DOMContentLoaded', () => {
ipcRenderer.send('forward-message', 'reload-viewer');
});
}
// Open image attachment link in the lightbox instead of opening in the default browser
const { $, lightbox } = window;
$('#main_div').on('click', '.message_content p a', function (e) {
const url = $(this).attr('href');
if (LinkUtil.isImage(url)) {
const $img = $(this).parent().siblings('.message_inline_image').find('img');
// prevent the image link from opening in a new page.
e.preventDefault();
// prevent the message compose dialog from happening.
e.stopPropagation();
lightbox.open($img);
}
});
});
// Clean up spellchecker events after you navigate away from this page;

View File

@@ -21,6 +21,12 @@ class LinkUtil {
return (currentDomain === newDomain) && newUrl.includes('/#narrow');
}
isImage(url) {
// test for images extension as well as urls like .png?s=100
const isImageUrl = /\.(bmp|gif|jpg|jpeg|png|webp)\?*.*$/i;
return isImageUrl.test(url);
}
}
module.exports = new LinkUtil();