Skip to content

Commit

Permalink
BrowserView: Add logic to handle external links.
Browse files Browse the repository at this point in the history
  • Loading branch information
vsvipul committed Jul 27, 2019
1 parent a4d5513 commit 79f412a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
13 changes: 13 additions & 0 deletions app/main/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ export class View extends BrowserView {
}
}
});

this.webContents.addListener('new-window', (e: Event, urlToOpen: string) => {
e.preventDefault();
this.sendAction('handle-link', this.index, urlToOpen);
});
}

zoomIn(): void {
Expand Down Expand Up @@ -149,6 +154,14 @@ export class View extends BrowserView {
return messageCountInTitle ? Number(messageCountInTitle[1]) : 0;
}

downloadUrl(url: string): void {
this.webContents.downloadURL(url);
}

loadUrl(url: string): void {
this.webContents.loadURL(url);
}

sendAction(action: any, ...params: any[]): void {
const win = BrowserWindow.getAllWindows()[0];

Expand Down
6 changes: 6 additions & 0 deletions app/main/viewmanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ class ViewManager {
// So, using a workaround here.
(this.views[this.selectedIndex] as any)[name as keyof View](...params);
});

ipcMain.on('call-specific-view-function', (e: Event, index: number, name: string, ...params: any[]) => {
// Type checking requires spread elements to match up with a rest parameter.
// So, using a workaround here.
(this.views[index] as any)[name as keyof View](...params);
});
}

create(props: ViewProps): void {
Expand Down
13 changes: 4 additions & 9 deletions app/renderer/js/components/handle-external-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ const { shell, app } = remote;

const dingSound = new Audio('../resources/sounds/ding.ogg');

// TODO: TypeScript - Figure out a way to pass correct type here.
function handleExternalLink(this: any, event: any): void {
const { url } = event;
const domainPrefix = DomainUtil.getDomain(this.props.index).url;
function handleExternalLink(index: number, url: string): void {
const domainPrefix = DomainUtil.getDomain(index).url;
const downloadPath = ConfigUtil.getConfigItem('downloadsPath', `${app.getPath('downloads')}`);
const shouldShowInFolder = ConfigUtil.getConfigItem('showDownloadFolder', false);

Expand All @@ -22,8 +20,6 @@ function handleExternalLink(this: any, event: any): void {
} = LinkUtil.isInternal(domainPrefix, url);

if (isWhiteListURL) {
event.preventDefault();

// Code to show pdf in a new BrowserWindow (currently commented out due to bug-upstream)
// Show pdf attachments in a new window
// if (LinkUtil.isPDF(url) && isUploadsURL) {
Expand Down Expand Up @@ -66,16 +62,15 @@ function handleExternalLink(this: any, event: any): void {
ipcRenderer.once('downloadFileFailed', () => {
// Automatic download failed, so show save dialog prompt and download
// through webview
this.$el.downloadURL(url);
ipcRenderer.send('call-specific-view-function', index, 'downloadUrl', url);
ipcRenderer.removeAllListeners('downloadFileCompleted');
});
return;
}

// open internal urls inside the current webview.
this.$el.loadURL(url);
ipcRenderer.send('call-specific-view-function', index, 'loadUrl', url);
} else {
event.preventDefault();
shell.openExternal(url);
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/renderer/js/components/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fs = require('fs');
import ConfigUtil = require('../utils/config-util');
import SystemUtil = require('../utils/system-util');
import BaseComponent = require('../components/base');
import handleExternalLink = require('../components/handle-external-link');
// import handleExternalLink = require('../components/handle-external-link');

const { app, dialog } = remote;

Expand Down Expand Up @@ -64,7 +64,7 @@ class WebView extends BaseComponent {

registerListeners(): void {
this.$el.addEventListener('new-window', event => {
handleExternalLink.call(this, event);
// handleExternalLink.call(this, event);
});

if (shouldSilentWebview) {
Expand Down
5 changes: 5 additions & 0 deletions app/renderer/js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import DNDUtil = require('./utils/dnd-util');
import ReconnectUtil = require('./utils/reconnect-util');
import Logger = require('./utils/logger-util');
import CommonUtil = require('./utils/common-util');
import handleExternalLink = require('./components/handle-external-link');

const logger = new Logger({
file: 'errors.log',
Expand Down Expand Up @@ -802,6 +803,10 @@ class ServerManagerView {
this.badgeCounts[url] = count;
this.updateBadge();
});

ipcRenderer.on('handle-link', (e: Event, index: number, url: string) => {
handleExternalLink(index, url);
});
}
}

Expand Down

0 comments on commit 79f412a

Please sign in to comment.