VS Code 扩展 - 如何将 WebviewPanel 添加到侧边栏?

Wha*_*ion 7 view visual-studio-code vscode-extensions

根据此页面,网络视图可以“在侧边栏或面板区域中呈现”。这些示例展示了如何呈现为编辑器面板......

vscode.window.createWebviewPanel(
    'catCoding', // Identifies the type of the webview. Used internally
    'Cat Coding', // Title of the panel displayed to the user
    vscode.ViewColumn.One, // Editor column to show the new webview panel in.
    {} // Webview options..
);
Run Code Online (Sandbox Code Playgroud)

我正在尝试将网络视图渲染为资源管理器下侧边栏中的附加面板。

我假设对第三个参数进行某种更改vscode.ViewColumn.One

Gam*_*a11 11

看起来createWebviewPanel()实际上适用于编辑器中显示的网络视图。对于侧边栏,您必须使用该页面上链接registerWebviewViewProvider()中所示的内容:webview-view-sample

vscode.window.registerWebviewViewProvider('calicoColors.colorsView', provider));
Run Code Online (Sandbox Code Playgroud)

然后指定通过哪个视图显示它package.json- 该示例使用资源管理器:

{
    "contributes": {
        "views": {
            "explorer": [
                {
                    "type": "webview",
                    "id": "calicoColors.colorsView",
                    "name": "Calico Colors"
                }
            ]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 到目前为止,浏览示例代码和 API 文档,它看起来更像是一个循环定义的迷宫。需要注册一个WebviewViewProvider,WebviewViewProvider又涉及WebviewView,WebviewView又涉及Webview。无法收集其中哪些可以或必须在 JS 上下文中定义,以及哪些可能从 package.json 继承它们的值。 (4认同)

Wha*_*ion 11

@Gamma11,感谢您的回答。绝对有助于解决“定义迷宫”

稍微阐述一下(也许可以使用更严格的 JS(而不是 TS)版本来简化/澄清webview-view-sample ):

1 - 在 package.json 中,您有以下条目将视图定义为位于侧边栏资源管理器中的 Web 视图:

"views": {
    "explorer": [
        {
            "type": "webview",
            "id": "calicoColors.colorsView",
            "name": "Trillion Files"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

2 - 同样在 package.json 中发送到 JS 的激活

"activationEvents": [
    "onView:calicoColors.colorsView"
]
Run Code Online (Sandbox Code Playgroud)

3 - 在 JS 中,事件由 vscode.commands.registerCommand 拾取

function activate(context){
    var thisProvider={
        resolveWebviewView:function(thisWebview, thisWebviewContext, thisToken){
            thisWebviewView.webview.options={enableScripts:true}
            thisWebviewView.webview.html="<!doctype><html>[etc etc]";
        }
    };
    context.subscriptions.push(
        vscode.commands.registerWebviewViewProvider("calicoColors.colorView", thisProvider);
    );
}

function deactivate() { }

module.exports = {
    activate,
    deactivate
}
Run Code Online (Sandbox Code Playgroud)

还有很多属性可以进入 thisProvider,但是这个最少的代码可以在侧边栏中启动并运行面板。