javascript - VScode 扩展向 showInputBox 添加取消事件

标签 javascript typescript visual-studio-code vscode-extensions

我有一个输入框,要求用户在我的扩展程序中输入他们的用户名。我希望当用户单击 esc 或取消扩展时停止运行。

这是我到目前为止尝试过的,但没有运气(似乎 onCancellationRequested 期待的是一个事件而不是一个方法..

await window.showInputBox(
{prompt: "UserName: ",
    placeHolder: "UserName"}, 
{isCancellationRequested: true, onCancellationRequested: (cancelled)=> {throw new Error('cancelled')}})

最佳答案

这不是 CancellationToken 的用途。它的作用与您想要的几乎相反,它可用于在用户执行任何输入之前以编程方式取消输入弹出窗口。

如果想知道用户是否取消了输入框,需要检查showInputBox() docs中提到的Thenable的返回值:

The returned value will be undefined if the input box was canceled (e.g. pressing ESC). [...]

vscode.window.showInputBox({prompt: 'UserName', placeHolder: 'UserName'}).then(value => {
    if (value === undefined) {
        throw new Error('cancelled');
    }
    // handle valid values
});

关于javascript - VScode 扩展向 showInputBox 添加取消事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57054786/

相关文章:

javascript - 使用 knockout.js 在对象数组中按未知索引显示对象

JavaScript 和 jQuery : accessing class member variables from event callback

javascript - 存储表行的 ID

powershell - VSCode - 在单独的窗口中打开终端

javascript - 如何使用 jQuery 更改元素的文本值?

javascript - *ngIf 密码验证不起作用 Angular 5

angular - 在使用 Angular 在 Firebase 中上传之前调整大小和压缩图像质量

javascript - 如何消除javascript中 "Leave Site"的警报?

visual-studio-code - 如何在 Visual Studio 代码的同一行中显示错误或建议

javascript - VS代码: [js] This constructor function may be converted to a class declaration