mirror of
https://github.com/microsoft/vscode.git
synced 2026-05-08 17:19:48 +01:00
Moving some electron specific webview js back into electron
This commit is contained in:
@@ -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);
|
||||
}());
|
||||
Reference in New Issue
Block a user