提交 201a9c8e 编写于 作者: M Matt Bierner

Make sure iframe based webview also re-broadcast keyboard events

For #82592
上级 42d9a26e
......@@ -28,6 +28,17 @@ export const enum WebviewMessageChannels {
webviewReady = 'webview-ready',
}
interface IKeydownEvent {
key: string;
keyCode: number;
code: string;
shiftKey: boolean;
altKey: boolean;
ctrlKey: boolean;
metaKey: boolean;
repeat: boolean;
}
interface WebviewContent {
readonly html: string;
readonly options: WebviewContentOptions;
......@@ -111,6 +122,13 @@ export abstract class BaseWebview<T extends HTMLElement> extends Disposable {
this.handleFocusChange(false);
}));
this._register(this.on('did-keydown', (data: KeyboardEvent) => {
// Electron: workaround for https://github.com/electron/electron/issues/14258
// We have to detect keyboard events in the <webview> and dispatch them to our
// keybinding service because these events do not bubble to the parent window anymore.
this.handleKeyDown(data);
}));
this.style();
this._register(webviewThemeDataProvider.onThemeDataChanged(this.style, this));
}
......@@ -243,4 +261,15 @@ export abstract class BaseWebview<T extends HTMLElement> extends Disposable {
this._onDidFocus.fire();
}
}
private handleKeyDown(event: IKeydownEvent) {
// Create a fake KeyboardEvent from the data provided
const emulatedKeyboardEvent = new KeyboardEvent('keydown', event);
// Force override the target
Object.defineProperty(emulatedKeyboardEvent, 'target', {
get: () => this.element,
});
// And re-dispatch
window.dispatchEvent(emulatedKeyboardEvent);
}
}
......@@ -18,6 +18,7 @@ import { WebviewInput } from 'vs/workbench/contrib/webview/browser/webviewEditor
import { KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_VISIBLE, Webview, WebviewEditorOverlay } from 'vs/workbench/contrib/webview/browser/webview';
import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { isWeb } from 'vs/base/common/platform';
export class WebviewEditor extends BaseEditor {
......@@ -98,7 +99,7 @@ export class WebviewEditor extends BaseEditor {
public focus(): void {
super.focus();
if (!this._onFocusWindowHandler.value) {
if (!this._onFocusWindowHandler.value && !isWeb) {
// Make sure we restore focus when switching back to a VS Code window
this._onFocusWindowHandler.value = this._hostService.onDidChangeFocus(focused => {
if (focused && this._editorService.activeControl === this) {
......
......@@ -104,8 +104,9 @@ export class IFrameWebview extends BaseWebview<HTMLIFrameElement> implements Web
}
focus(): void {
console.log('focus');
if (this.element) {
this.element.focus();
this._send('focus');
}
}
......
......@@ -25,16 +25,6 @@ import { WebviewFindDelegate, WebviewFindWidget } from '../browser/webviewFindWi
import { BaseWebview, WebviewMessageChannels } from 'vs/workbench/contrib/webview/browser/baseWebviewElement';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
interface IKeydownEvent {
key: string;
keyCode: number;
code: string;
shiftKey: boolean;
altKey: boolean;
ctrlKey: boolean;
metaKey: boolean;
repeat: boolean;
}
class WebviewTagHandle extends Disposable {
......@@ -183,13 +173,6 @@ class WebviewKeyboardHandler extends Disposable {
this._register(addDisposableListener(this._webviewHandle.webview, 'ipc-message', (event) => {
switch (event.channel) {
case 'did-keydown':
// Electron: workaround for https://github.com/electron/electron/issues/14258
// We have to detect keyboard events in the <webview> and dispatch them to our
// keybinding service because these events do not bubble to the parent window anymore.
this.handleKeydown(event.args[0]);
return;
case 'did-focus':
this.setIgnoreMenuShortcuts(this._ignoreMenuShortcut);
break;
......@@ -214,17 +197,6 @@ class WebviewKeyboardHandler extends Disposable {
contents.setIgnoreMenuShortcuts(value);
}
}
private handleKeydown(event: IKeydownEvent): void {
// Create a fake KeyboardEvent from the data provided
const emulatedKeyboardEvent = new KeyboardEvent('keydown', event);
// Force override the target
Object.defineProperty(emulatedKeyboardEvent, 'target', {
get: () => this._webviewHandle.webview
});
// And re-dispatch
window.dispatchEvent(emulatedKeyboardEvent);
}
}
export class ElectronWebviewBasedWebview extends BaseWebview<WebviewTag> implements Webview, WebviewFindDelegate {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册