【问题标题】:Context-Menu COPY is not working in vscode webview, BUT ctrl+c is working fine上下文菜单复制在 vscode webview 中不起作用,但 ctrl+c 工作正常
【发布时间】:2022-08-05 17:29:25
【问题描述】:

我们正在开发一个带有 webview 的 vscode 扩展。在 webview 中,我们正在执行以下步骤

  1. 选择文本
  2. 右键单击
  3. 出现剪切+复制+粘贴上下文菜单
  4. 点击复制

    • 预期结果:文本应复制到剪贴板
    • 实际结果:文本未被复制。

    笔记: 如果我们只是按下,而不是使用上下文菜单Ctrl+C,文本实际上正在被复制。

    调试结果

    我们进一步调试,发现 Ctrl+C 按键和从上下文菜单中单击 COPY 之间的一个区别editorExtensions.ts文件。

    对于,Ctrl+C,网页浏览实现返回 TRUE

    对于复制,通用域实现返回 TRUE

        public runCommand(accessor: ServicesAccessor, args: any): void | Promise<void> {
            const logService = accessor.get(ILogService);
            logService.trace(`Executing Command \'${this.id}\' which has ${this._implementations.length} bound.`);
            for (const impl of this._implementations) {
                const result = impl.implementation(accessor, args);
                if (result) {
                    logService.trace(`Command \'${this.id}\' was handled by \'${impl.name}\'.`);
                    if (typeof result === \'boolean\') {
                        return;
                    }
                    return result;
                }
            }
            logService.trace(`The Command \'${this.id}\' was not handled by any implementation.`);
        }
    

    所以,我们需要帮助

    • 查找 COPY 上下文菜单不工作的原因以及如何使其工作
    • 如何使 CUT/PASTE 上下文菜单禁用/灰显
  • 很好的分析,但你的问题是什么?我们能帮你什么吗?
  • @MikeLischke,感谢您的回复。我已经更新了这个问题。希望这会回答你的问题。
  • 这报告为VS Code issue #141080。应该易于保存并调用单个工作剪切/复制功能。

标签: visual-studio-code vscode-extensions


【解决方案1】:

Visual Studio Code 1.70 应该根据其发布说明提供完全自定义的上下文菜单,由于this issue,很难从其 web 视图中完全复制。

Webview 上下文菜单

新的 webview 上下文菜单提议使高级webviews 可以轻松自定义当用户在 webview 中右键单击时显示的上下文菜单。这个新的贡献点使用 VS Code 的普通上下文菜单,因此自定义菜单与编辑器的其余部分完全吻合。 Webviews 还可以为 webview 的不同部分显示自定义上下文菜单。

要试用这个新 API,您需要启用 contribWebviewContext 提议。

"enabledApiProposals": [
  "contribWebviewContext"
]

要将新的上下文菜单项添加到您的 web 视图,请在新的 webview/context 部分下的 menus 中添加一个新条目。每个贡献都需要一个命令(这也是标题的来源)和一个when 子句。 when clause 应包含 webview == 'YOUR_WEBVIEW_VIEW_TYPE' 以确保上下文菜单仅适用于您的扩展程序的 web 视图:

"contributes": {
  "menus": {
    "webview/context": [
      {
        "command": "catCoding.yarn",
        "when": "webview == 'catCoding'"
      },
      {
        "command": "catCoding.insertLion",
        "when": "webview == 'catCoding' && webviewSection == 'editor'"
      }
    ]
  },
  "commands": [
    {
      "command": "catCoding.yarn",
      "title": "Yarn ?",
      "category": "Cat Coding"
    },
    {
      "command": "catCoding.insertLion",
      "title": "Insert ?",
      "category": "Cat Coding"
    },
    ...
  ]
}

在 webview 内部,您还可以使用 data-vscode-context data attribute(或在 JavaScript 中使用 dataset.vscodeContext)为 HTML 的特定区域设置上下文。 data-vscode-context 值是一个 JSON 对象,它指定当用户右键单击元素时要设置的上下文。最终的上下文是通过从文档根到被点击的元素来确定的。

以这个 HTML 为例:

<div class="main" data-vscode-context='{"webviewSection": "main", "mouseCount": 4}'>
  <h1>Cat Coding</h1>

  <textarea data-vscode-context='{"webviewSection": "editor", "preventDefaultContextMenuItems": true}'></textarea>
</div>

如果用户右键单击textarea,将设置以下上下文:

  • webviewSection == 'editor' — 这会覆盖父元素中的 webviewSection。
  • mouseCount == 4 — 这是从父元素继承的。
  • preventDefaultContextMenuItems == true — 这是一个特殊的上下文,它隐藏了 VS Code 通常添加到 webview 上下文菜单中的复制和粘贴条目。

如果用户在&lt;textarea&gt; 内单击鼠标右键,他们将看到:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 2010-10-02
    • 2012-08-28
    • 1970-01-01
    • 2021-12-23
    • 2013-06-08
    相关资源
    最近更新 更多