Add webContents listeners to modals (#2247)
* Add webContents listeners to modals * Fix tests
This commit is contained in:
@@ -15,6 +15,14 @@ jest.mock('electron', () => ({
|
|||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
jest.mock('common/config', () => ({
|
||||||
|
teams: [],
|
||||||
|
}));
|
||||||
|
|
||||||
|
jest.mock('main/views/webContentEvents', () => ({
|
||||||
|
addWebContentsEventListeners: jest.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
jest.mock('./modalView', () => ({
|
jest.mock('./modalView', () => ({
|
||||||
ModalView: jest.fn(),
|
ModalView: jest.fn(),
|
||||||
}));
|
}));
|
||||||
|
@@ -19,8 +19,10 @@ import {
|
|||||||
GET_MODAL_UNCLOSEABLE,
|
GET_MODAL_UNCLOSEABLE,
|
||||||
RESIZE_MODAL,
|
RESIZE_MODAL,
|
||||||
} from 'common/communication';
|
} from 'common/communication';
|
||||||
|
import Config from 'common/config';
|
||||||
|
|
||||||
import {getAdjustedWindowBoundaries} from 'main/utils';
|
import {getAdjustedWindowBoundaries} from 'main/utils';
|
||||||
|
import WebContentsEventManager from 'main/views/webContentEvents';
|
||||||
import WindowManager from 'main/windows/windowManager';
|
import WindowManager from 'main/windows/windowManager';
|
||||||
|
|
||||||
import {ModalView} from './modalView';
|
import {ModalView} from './modalView';
|
||||||
@@ -87,6 +89,7 @@ export class ModalManager {
|
|||||||
if (index === 0) {
|
if (index === 0) {
|
||||||
WindowManager.sendToRenderer(MODAL_OPEN);
|
WindowManager.sendToRenderer(MODAL_OPEN);
|
||||||
modal.show(undefined, Boolean(withDevTools));
|
modal.show(undefined, Boolean(withDevTools));
|
||||||
|
WebContentsEventManager.addWebContentsEventListeners(modal.view.webContents, () => Config.teams.concat());
|
||||||
} else {
|
} else {
|
||||||
WindowManager.sendToRenderer(MODAL_CLOSE);
|
WindowManager.sendToRenderer(MODAL_CLOSE);
|
||||||
modal.hide();
|
modal.hide();
|
||||||
|
@@ -277,7 +277,7 @@ export class ViewManager {
|
|||||||
log.error(`Couldn't find a view with the name ${viewName}`);
|
log.error(`Couldn't find a view with the name ${viewName}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
WebContentsEventManager.addWebContentsEventListeners(view, this.getServers);
|
WebContentsEventManager.addMattermostViewEventListeners(view, this.getServers);
|
||||||
}
|
}
|
||||||
|
|
||||||
finishLoading = (server: string) => {
|
finishLoading = (server: string) => {
|
||||||
|
@@ -32,6 +32,10 @@ jest.mock('../windows/windowManager', () => ({
|
|||||||
showMainWindow: jest.fn(),
|
showMainWindow: jest.fn(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
jest.mock('common/config', () => ({
|
||||||
|
spellcheck: true,
|
||||||
|
}));
|
||||||
|
|
||||||
jest.mock('common/utils/url', () => ({
|
jest.mock('common/utils/url', () => ({
|
||||||
parseURL: (url) => {
|
parseURL: (url) => {
|
||||||
try {
|
try {
|
||||||
|
@@ -6,6 +6,7 @@ import log from 'electron-log';
|
|||||||
|
|
||||||
import {TeamWithTabs} from 'types/config';
|
import {TeamWithTabs} from 'types/config';
|
||||||
|
|
||||||
|
import Config from 'common/config';
|
||||||
import urlUtils from 'common/utils/url';
|
import urlUtils from 'common/utils/url';
|
||||||
|
|
||||||
import ContextMenu from 'main/contextMenu';
|
import ContextMenu from 'main/contextMenu';
|
||||||
@@ -221,9 +222,31 @@ export class WebContentsEventManager {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
addWebContentsEventListeners = (mmview: MattermostView, getServersFunction: () => TeamWithTabs[]) => {
|
addMattermostViewEventListeners = (mmview: MattermostView, getServersFunction: () => TeamWithTabs[]) => {
|
||||||
const contents = mmview.view.webContents;
|
this.addWebContentsEventListeners(
|
||||||
|
mmview.view.webContents,
|
||||||
|
getServersFunction,
|
||||||
|
(contents: WebContents) => {
|
||||||
|
contents.on('page-title-updated', mmview.handleTitleUpdate);
|
||||||
|
contents.on('page-favicon-updated', mmview.handleFaviconUpdate);
|
||||||
|
contents.on('update-target-url', mmview.handleUpdateTarget);
|
||||||
|
contents.on('did-navigate', mmview.handleDidNavigate);
|
||||||
|
},
|
||||||
|
(contents: WebContents) => {
|
||||||
|
contents.removeListener('page-title-updated', mmview.handleTitleUpdate);
|
||||||
|
contents.removeListener('page-favicon-updated', mmview.handleFaviconUpdate);
|
||||||
|
contents.removeListener('update-target-url', mmview.handleUpdateTarget);
|
||||||
|
contents.removeListener('did-navigate', mmview.handleDidNavigate);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
addWebContentsEventListeners = (
|
||||||
|
contents: WebContents,
|
||||||
|
getServersFunction: () => TeamWithTabs[],
|
||||||
|
addListeners?: (contents: WebContents) => void,
|
||||||
|
removeListeners?: (contents: WebContents) => void,
|
||||||
|
) => {
|
||||||
// initialize custom login tracking
|
// initialize custom login tracking
|
||||||
this.customLogins[contents.id] = {
|
this.customLogins[contents.id] = {
|
||||||
inProgress: false,
|
inProgress: false,
|
||||||
@@ -244,34 +267,28 @@ export class WebContentsEventManager {
|
|||||||
const didStartNavigation = this.generateDidStartNavigation(getServersFunction);
|
const didStartNavigation = this.generateDidStartNavigation(getServersFunction);
|
||||||
contents.on('did-start-navigation', didStartNavigation as (e: Event, u: string) => void);
|
contents.on('did-start-navigation', didStartNavigation as (e: Event, u: string) => void);
|
||||||
|
|
||||||
const spellcheck = mmview.options.webPreferences?.spellcheck;
|
const spellcheck = Config.useSpellChecker;
|
||||||
const newWindow = this.generateNewWindowListener(getServersFunction, spellcheck);
|
const newWindow = this.generateNewWindowListener(getServersFunction, spellcheck);
|
||||||
contents.setWindowOpenHandler(newWindow);
|
contents.setWindowOpenHandler(newWindow);
|
||||||
|
|
||||||
contents.on('page-title-updated', mmview.handleTitleUpdate);
|
addListeners?.(contents);
|
||||||
contents.on('page-favicon-updated', mmview.handleFaviconUpdate);
|
|
||||||
contents.on('update-target-url', mmview.handleUpdateTarget);
|
|
||||||
contents.on('did-navigate', mmview.handleDidNavigate);
|
|
||||||
|
|
||||||
const removeListeners = () => {
|
const removeWebContentsListeners = () => {
|
||||||
try {
|
try {
|
||||||
contents.removeListener('will-navigate', willNavigate as (e: Event, u: string) => void);
|
contents.removeListener('will-navigate', willNavigate as (e: Event, u: string) => void);
|
||||||
contents.removeListener('did-start-navigation', didStartNavigation as (e: Event, u: string) => void);
|
contents.removeListener('did-start-navigation', didStartNavigation as (e: Event, u: string) => void);
|
||||||
contents.removeListener('page-title-updated', mmview.handleTitleUpdate);
|
removeListeners?.(contents);
|
||||||
contents.removeListener('page-favicon-updated', mmview.handleFaviconUpdate);
|
|
||||||
contents.removeListener('update-target-url', mmview.handleUpdateTarget);
|
|
||||||
contents.removeListener('did-navigate', mmview.handleDidNavigate);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log.error(`Error while trying to detach listeners, this might be ok if the process crashed: ${e}`);
|
log.error(`Error while trying to detach listeners, this might be ok if the process crashed: ${e}`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.listeners[contents.id] = removeListeners;
|
this.listeners[contents.id] = removeWebContentsListeners;
|
||||||
contents.once('render-process-gone', (event, details) => {
|
contents.once('render-process-gone', (event, details) => {
|
||||||
if (details.reason !== 'clean-exit') {
|
if (details.reason !== 'clean-exit') {
|
||||||
log.error('Renderer process for a webcontent is no longer available:', details.reason);
|
log.error('Renderer process for a webcontent is no longer available:', details.reason);
|
||||||
}
|
}
|
||||||
removeListeners();
|
removeWebContentsListeners();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user