简体   繁体   English

您如何在 Monaco 编辑器中处理来自 IViewZone 的输入事件?

[英]How do you process input events from an IViewZone in the Monaco Editor?

If you try this Monaco playground:如果你试试这个摩纳哥游乐场:

https://microsoft.github.io/monaco-editor/playground.html#interacting-with-the-editor-listening-to-mouse-events https://microsoft.github.io/monaco-editor/playground.html#interacting-with-the-editor-listening-to-mouse-events

and add this on line 36 to set the HTML of the light green IViewZone that is inserted:并将其添加到第 36 行以设置插入的浅绿色IViewZone的 HTML:

domNode.innerHTML = '<a href="https://www.microsoft.com/">Microsoft</a>';

You will see that the link is NOT clickable.您将看到该链接不可点击。 This is a simplified repro of the more general problem of being unable to get input events on your DOM node.这是无法在 DOM 节点上获取输入事件的更普遍问题的简化再现。 For example, you can call addEventListener() for mousemove .例如,您可以为mousemove调用addEventListener() click , etc. on domNode , but your handler will never get called even though you can see it is present in Chrome DevTools.domNodeclick等,但即使您可以看到它存在于 Chrome DevTools 中,您的处理程序也永远不会被调用。

Note that VS Code itself has very rich implementations of IViewZone , such as the "peek" view for Show References.请注意,VS Code 本身具有非常丰富的IViewZone实现,例如 Show References 的“peek”视图。 That view even supports scrolling!该视图甚至支持滚动! Therefore, it seems possible to get these input events, but it it is not immediately obvious from the documentation.因此,获取这些输入事件似乎是可能的,但从文档中并不是很明显。

It might be possible to use the editor's own onMouseDown() methods (as shown at the bottom of the playground) and then delegate to the appropriate target, though that does not appear to be what VS Code is doing.可以使用编辑器自己的onMouseDown()方法(如操场底部所示),然后委托给适当的目标,尽管这似乎不是 VS Code 正在做的事情。

The reason that the link in the view zone is not clickable is the whole view zone is rendered under the view lines (source code), which means the whole view zone is not clickable.视图区中的链接不可点击的原因是整个视图区都在视图线(源代码)下呈现,这意味着整个视图区不可点击。 To render an element which users can interact with, a contentWidget should be used.要呈现用户可以与之交互的元素,应使用contentWidget

The peek view in VS Code is a view zone (which creates empty whitespace between view lines) and a content widget above it, which you can interact with. VS Code 中的 peek 视图是一个视图区域(它在视图行之间创建空白)和它上面的内容小部件,您可以与之交互。 They are placed at the same position so users won't see the view zone behind the content widget.它们被放置在相同的位置,因此用户不会看到内容小部件后面的视图区域。

In monaco playground , change line 52 to this.domNode.innerHTML = '<a target="_blank" href="https://www.microsoft.com/">Microsoft</a>';monaco playground 中,将第 52 行更改为this.domNode.innerHTML = '<a target="_blank" href="https://www.microsoft.com/">Microsoft</a>'; and run the sample again, you can click the link and a new tab will be opened.并再次运行示例,您可以单击该链接并打开一个新选项卡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM