Put LinkUtil to renderer-side.

This commit is contained in:
Zhongyi Tong
2017-06-16 22:59:27 +08:00
parent 47eec89a9b
commit 7192dc69f6
3 changed files with 32 additions and 18 deletions

View File

@@ -1,16 +0,0 @@
const wurl = require('wurl');
// Check link if it's internal/external
function linkIsInternal(currentUrl, newUrl) {
const currentDomain = wurl('hostname', currentUrl);
const newDomain = wurl('hostname', newUrl);
return currentDomain === newDomain;
}
// We'll be needing this to open images in default browser
const skipImages = '.jpg|.gif|.png|.jpeg|.JPG|.PNG';
module.exports = {
linkIsInternal,
skipImages
};

View File

@@ -2,7 +2,7 @@
const DomainUtil = require(__dirname + '/../utils/domain-util.js');
const SystemUtil = require(__dirname + '/../utils/system-util.js');
const {linkIsInternal, skipImages} = require(__dirname + '/../../../main/link-helper');
const LinkUtil = require(__dirname + '/../utils/link-util.js');
const {app, dialog, shell} = require('electron').remote;
const {ipcRenderer} = require('electron');
@@ -25,6 +25,7 @@ class WebView extends BaseComponent {
this.isActive = isActive;
this.domainUtil = new DomainUtil();
this.systemUtil = new SystemUtil();
this.linkUtil = new LinkUtil();
this.badgeCount = 0;
}
@@ -52,7 +53,7 @@ class WebView extends BaseComponent {
const {url} = event;
const domainPrefix = this.domainUtil.getDomain(this.index).url;
if (linkIsInternal(domainPrefix, url) && url.match(skipImages) === null) {
if (this.linkUtil.isInternal(domainPrefix, url)) {
event.preventDefault();
this.$el.loadURL(url);
} else {

View File

@@ -0,0 +1,29 @@
'use strict';
const wurl = require('wurl');
let instance = null;
class LinkUtil {
constructor() {
if (instance) {
return instance;
} else {
instance = this;
}
return instance;
}
isInternal(currentUrl, newUrl) {
const currentDomain = wurl('hostname', currentUrl);
const newDomain = wurl('hostname', newUrl);
const skipImages = '.jpg|.gif|.png|.jpeg|.JPG|.PNG';
// We'll be needing this to open images in default browser
return (currentDomain === newDomain) || newUrl.match(skipImages);
}
}
module.exports = LinkUtil;