From 60d693700e1e0e09317cacce4441cd25843b2d80 Mon Sep 17 00:00:00 2001 From: Abhigyan Khaund Date: Mon, 9 Apr 2018 18:16:57 +0530 Subject: [PATCH] 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. --- app/renderer/js/preload.js | 19 +++++++++++++++++++ app/renderer/js/utils/link-util.js | 6 ++++++ 2 files changed, 25 insertions(+) diff --git a/app/renderer/js/preload.js b/app/renderer/js/preload.js index 68b333a4..76ddb90c 100644 --- a/app/renderer/js/preload.js +++ b/app/renderer/js/preload.js @@ -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; diff --git a/app/renderer/js/utils/link-util.js b/app/renderer/js/utils/link-util.js index 14d57e5b..a7391e3e 100644 --- a/app/renderer/js/utils/link-util.js +++ b/app/renderer/js/utils/link-util.js @@ -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();