mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-24 18:49:00 +01:00
Adding settings to control which schemes simple browser is enabled for
Also adds a setting to enable/disable the focus lock indicator
This commit is contained in:
42
extensions/simple-browser/src/dispose.ts
Normal file
42
extensions/simple-browser/src/dispose.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
export function disposeAll(disposables: vscode.Disposable[]) {
|
||||
while (disposables.length) {
|
||||
const item = disposables.pop();
|
||||
if (item) {
|
||||
item.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class Disposable {
|
||||
private _isDisposed = false;
|
||||
|
||||
protected _disposables: vscode.Disposable[] = [];
|
||||
|
||||
public dispose(): any {
|
||||
if (this._isDisposed) {
|
||||
return;
|
||||
}
|
||||
this._isDisposed = true;
|
||||
disposeAll(this._disposables);
|
||||
}
|
||||
|
||||
protected _register<T extends vscode.Disposable>(value: T): T {
|
||||
if (this._isDisposed) {
|
||||
value.dispose();
|
||||
} else {
|
||||
this._disposables.push(value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
protected get isDisposed() {
|
||||
return this._isDisposed;
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,14 @@
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { URL } from 'url';
|
||||
import * as vscode from 'vscode';
|
||||
import { SimpleBrowserManager } from './simpleBrowserManager';
|
||||
import * as nls from 'vscode-nls';
|
||||
import { SimpleBrowserManager } from './simpleBrowserManager';
|
||||
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
const openCommand = 'simpleBrowser.open';
|
||||
const openApiCommand = 'simpleBrowser.api.open';
|
||||
const showCommand = 'simpleBrowser.show';
|
||||
|
||||
export function activate(context: vscode.ExtensionContext) {
|
||||
@@ -30,7 +31,7 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
}
|
||||
}));
|
||||
|
||||
context.subscriptions.push(vscode.commands.registerCommand(openCommand, (url: vscode.Uri) => {
|
||||
context.subscriptions.push(vscode.commands.registerCommand(openApiCommand, (url: vscode.Uri) => {
|
||||
manager.show(url.toString());
|
||||
}));
|
||||
|
||||
@@ -41,9 +42,22 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const enabledHosts = configuration.get<string[]>('opener.enabledHosts', [
|
||||
'localhost',
|
||||
'127.0.0.1'
|
||||
]);
|
||||
try {
|
||||
const url = new URL(uri.toString());
|
||||
if (!enabledHosts.includes(url.hostname)) {
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
title: localize('openTitle', "Open in simple browser"),
|
||||
command: openCommand,
|
||||
command: openApiCommand,
|
||||
arguments: [uri]
|
||||
};
|
||||
}
|
||||
|
||||
@@ -5,31 +5,34 @@
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import * as nls from 'vscode-nls';
|
||||
import { Disposable } from './dispose';
|
||||
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
export class SimpleBrowserView {
|
||||
export class SimpleBrowserView extends Disposable {
|
||||
|
||||
public static readonly viewType = 'simpleBrowser.view';
|
||||
private static readonly title = localize('view.title', "Simple Browser");
|
||||
|
||||
private readonly _webviewPanel: vscode.WebviewPanel;
|
||||
|
||||
private readonly _onDidDispose = new vscode.EventEmitter<void>();
|
||||
private readonly _onDidDispose = this._register(new vscode.EventEmitter<void>());
|
||||
public readonly onDispose = this._onDidDispose.event;
|
||||
|
||||
constructor(
|
||||
private readonly extensionUri: vscode.Uri,
|
||||
url: string,
|
||||
) {
|
||||
this._webviewPanel = vscode.window.createWebviewPanel(SimpleBrowserView.viewType, SimpleBrowserView.title, {
|
||||
super();
|
||||
|
||||
this._webviewPanel = this._register(vscode.window.createWebviewPanel(SimpleBrowserView.viewType, SimpleBrowserView.title, {
|
||||
viewColumn: vscode.ViewColumn.Active,
|
||||
}, {
|
||||
enableScripts: true,
|
||||
retainContextWhenHidden: true,
|
||||
});
|
||||
}));
|
||||
|
||||
this._webviewPanel.webview.onDidReceiveMessage(e => {
|
||||
this._register(this._webviewPanel.webview.onDidReceiveMessage(e => {
|
||||
switch (e.type) {
|
||||
case 'openExternal':
|
||||
try {
|
||||
@@ -40,18 +43,28 @@ export class SimpleBrowserView {
|
||||
}
|
||||
break;
|
||||
}
|
||||
});
|
||||
}));
|
||||
|
||||
this._webviewPanel.onDidDispose(() => {
|
||||
this._register(this._webviewPanel.onDidDispose(() => {
|
||||
this.dispose();
|
||||
});
|
||||
}));
|
||||
|
||||
this._register(vscode.workspace.onDidChangeConfiguration(e => {
|
||||
if (e.affectsConfiguration('simpleBrowser.focusLockIndicator.enabled')) {
|
||||
const configuration = vscode.workspace.getConfiguration('simpleBrowser');
|
||||
this._webviewPanel.webview.postMessage({
|
||||
type: 'didChangeFocusLockIndicatorEnabled',
|
||||
focusLockEnabled: configuration.get<boolean>('focusLockIndicator.enabled', true)
|
||||
});
|
||||
}
|
||||
}));
|
||||
|
||||
this.show(url);
|
||||
}
|
||||
|
||||
public dispose() {
|
||||
this._onDidDispose.fire();
|
||||
this._webviewPanel.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
public show(url: string) {
|
||||
@@ -60,6 +73,8 @@ export class SimpleBrowserView {
|
||||
}
|
||||
|
||||
private getHtml(url: string) {
|
||||
const configuration = vscode.workspace.getConfiguration('simpleBrowser');
|
||||
|
||||
const nonce = new Date().getTime() + '' + new Date().getMilliseconds();
|
||||
|
||||
const mainJs = this.extensionResourceUrl('media', 'index.js');
|
||||
@@ -82,6 +97,7 @@ export class SimpleBrowserView {
|
||||
|
||||
<meta id="simple-browser-settings" data-settings="${escapeAttribute(JSON.stringify({
|
||||
url: url,
|
||||
focusLockEnabled: configuration.get<boolean>('focusLockIndicator.enabled', true)
|
||||
}))}">
|
||||
|
||||
<link rel="stylesheet" type="text/css" href="${mainCss}">
|
||||
|
||||
Reference in New Issue
Block a user