从 vscode 扩展中的虚拟文档发送命令 [英] sending a command from a virtual document in a vscode extension

查看:24
本文介绍了从 vscode 扩展中的虚拟文档发送命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 Visual Studio Code 扩展,在那里我制作了一个虚拟文档

I am making a Visual Studio Code extension, where I make a virtual document

let provider = new TextDocumentContentProvider();
let registration = vscode.workspace.registerTextDocumentContentProvider('nucleus-preview', provider);

我注册了一个命令:

vscode.commands.registerCommand('extension.sendMessage', (message) => {
    console.log('the message is ', message)
});

在虚拟文档中,我想使用 JavaScript 将消息发送回扩展程序.

In the virtual document I want to send a message back to the extension using JavaScript.

如果我在虚拟文档中有这样的链接:

If I have a link in the virtual document like such:

<a href="command:extension.sayHi?message=hi">say Hi</a>

它调用命令但消息未定义.这就是我所得到的.

It calls the command but the message is undefined. That's as far as I got.

我不想使用链接调用它,我想使用 TypeScript 从虚拟文档中的 Polymer Element (v2) 方法发送消息.

I don't want to call it using a link, I want to send the message using TypeScript from a method of a Polymer Element (v2) in the virtual doc.

推荐答案

命令参数需要作为编码的 json 数组而不是参数传递:

The command arguments needs to be passed as an encoded json array instead of parameters:

command:extension.sayHi?%5B%22hi%22%5D

尝试使用辅助函数,例如:

Try using a helper function like:

const createCommandUri = (name, ...args) =>
    `command:${name}?${encodeURIComponent(JSON.stringify(args))}`

<小时>

我们没有以编程方式将命令发送回编辑器的官方 API,但您可以使用 内置降价扩展的方法:

window.parent.postMessage({
    command: "did-click-link",
    data: createCommandUri('extension.sendMessage', 'hi')
}, "file://")

不是很好,但很管用

这篇关于从 vscode 扩展中的虚拟文档发送命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆