提交 89aa8bf1 编写于 作者: M Matt Bierner

Moving some electron specific webview js back into electron

上级 c9c29f3e
......@@ -3,6 +3,17 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// @ts-check
/**
* @typedef {{
* postMessage: (channel: string, data?: any) => void,
* onMessage: (channel: string, handler: any) => void,
* focusIframeOnCreate?: boolean,
* ready?: Promise<void>,
* onIframeLoaded: (iframe: HTMLIFrameElement) => void
* }} WebviewHost
*/
(function () {
'use strict';
......@@ -134,23 +145,13 @@
}
/**
* @typedef {{
* postMessage: (channel: string, data?: any) => void,
* onMessage: (channel: string, handler: any) => void,
* focusIframeOnCreate?: boolean,
* ready?: Promise<void>
* }} HostCommunications
*/
/**
* @param {HostCommunications} host
* @param {WebviewHost} host
*/
function createWebviewManager(host) {
// state
let firstLoad = true;
let loadTimeout;
let pendingMessages = [];
let isInDevelopmentMode = false;
const initData = {
initialScrollProgress: undefined
......@@ -442,44 +443,10 @@
}
});
if (!FAKE_LOAD) {
newFrame.contentWindow.onbeforeunload = () => {
if (isInDevelopmentMode) { // Allow reloads while developing a webview
host.postMessage('do-reload');
return false;
}
// Block navigation when not in development mode
console.log('prevented webview navigation');
return false;
};
}
// Bubble out link clicks
newFrame.contentWindow.addEventListener('click', handleInnerClick);
// Electron 4 eats mouseup events from inside webviews
// https://github.com/microsoft/vscode/issues/75090
// Try to fix this by rebroadcasting mouse moves and mouseups so that we can
// emulate these on the main window
if (!FAKE_LOAD) {
let isMouseDown = false;
newFrame.contentWindow.addEventListener('mousedown', () => {
isMouseDown = true;
});
const tryDispatchSyntheticMouseEvent = (e) => {
if (!isMouseDown) {
host.postMessage('synthetic-mouse-event', { type: e.type, screenX: e.screenX, screenY: e.screenY, clientX: e.clientX, clientY: e.clientY });
}
};
newFrame.contentWindow.addEventListener('mouseup', e => {
tryDispatchSyntheticMouseEvent(e);
isMouseDown = false;
});
newFrame.contentWindow.addEventListener('mousemove', tryDispatchSyntheticMouseEvent);
}
host.onIframeLoaded(newFrame);
}
if (!FAKE_LOAD) {
......@@ -511,9 +478,6 @@
initData.initialScrollProgress = progress;
});
host.onMessage('devtools-opened', () => {
isInDevelopmentMode = true;
});
trackFocus({
onFocus: () => host.postMessage('did-focus'),
......
......@@ -28,14 +28,54 @@
// @ts-ignore
const ipcRenderer = require('electron').ipcRenderer;
require('../../browser/pre/main')({
let isInDevelopmentMode = false;
/**
* @type {import('../../browser/pre/main').WebviewHost}
*/
const host = {
postMessage: (channel, data) => {
ipcRenderer.sendToHost(channel, data);
},
onMessage: (channel, handler) => {
ipcRenderer.on(channel, handler);
},
focusIframeOnCreate: true
focusIframeOnCreate: true,
onIframeLoaded: (newFrame) => {
newFrame.contentWindow.onbeforeunload = () => {
if (isInDevelopmentMode) { // Allow reloads while developing a webview
host.postMessage('do-reload');
return false;
}
// Block navigation when not in development mode
console.log('prevented webview navigation');
return false;
};
// Electron 4 eats mouseup events from inside webviews
// https://github.com/microsoft/vscode/issues/75090
// Try to fix this by rebroadcasting mouse moves and mouseups so that we can
// emulate these on the main window
let isMouseDown = false;
newFrame.contentWindow.addEventListener('mousedown', () => {
isMouseDown = true;
});
const tryDispatchSyntheticMouseEvent = (e) => {
if (!isMouseDown) {
host.postMessage('synthetic-mouse-event', { type: e.type, screenX: e.screenX, screenY: e.screenY, clientX: e.clientX, clientY: e.clientY });
}
};
newFrame.contentWindow.addEventListener('mouseup', e => {
tryDispatchSyntheticMouseEvent(e);
isMouseDown = false;
});
newFrame.contentWindow.addEventListener('mousemove', tryDispatchSyntheticMouseEvent);
}
};
host.onMessage('devtools-opened', () => {
isInDevelopmentMode = true;
});
document.addEventListener('DOMContentLoaded', () => {
......@@ -46,4 +86,6 @@
ipcRenderer.sendToHost(message.data.command, message.data.data);
};
});
require('../../browser/pre/main')(host);
}());
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册