简体   繁体   English

monaco-editor:如何以“actions.find”的形式订阅/收听本机命令/动作

[英]monaco-editor: how to subscribe / listen native commands / actions as 'actions.find'

For a long time I've been trying to find a way, example or information about how I can subscribe or listen native commands/actions as actions.find .很长一段时间以来,我一直在尝试寻找一种方法、示例或信息,以了解如何将本机命令/操作作为actions.find订阅或收听。

My case is - to create some side-effect when user use "Find Matches" functionality in editor's workbench (called by keyboard shortcut or context-menu), at finally I want to get as argument 'searching 'substring' in my effect.我的情况是 - 当用户在编辑器的工作台中使用“查找匹配”功能(通过键盘快捷键或上下文菜单调用)时创建一些副作用,最后我想在我的效果中作为参数“搜索“子字符串”。

Hopefully the community can help me or place some similar case resolving sample.希望社区可以帮助我或放置一些类似的案例解决示例。

After debugging and studying the source code of monaco-editor, I found, as it seems to me, the correct solution for using the API (IMHO) for my case.在调试和研究 monaco-editor 的源代码之后,我发现在我看来,使用 API(恕我直言)的正确解决方案。

We can use API of FindController , that is one of permanently contributions in each instance, we can use there public method onFindReplaceStateChange我们可以使用FindController的 API ,这是每个实例中的永久贡献之一,我们可以使用公共方法onFindReplaceStateChange

// to get relevant FindController from editor instance we can use
var findController = editor.getContribution('editor.contrib.findController')

// to get FindReplaceState from FindController
var findSearchState = editor.getContribution('editor.contrib.findController').getState();

// to subscribe to 'find matches' event bus
editor.getContribution('editor.contrib.findController')
   .getState().onFindReplaceStateChange(
       (...rest) => console.log('FindReplaceStateChange: ', ...rest)
   );

// to force & silent set current search substring
editor.getContribution('editor.contrib.findController').setSearchString('foo-bar');

// to programmatically call search with options, 
// not 'editor.getAction('actions.find').run()' (that not have arguments) 
editor.getContribution('editor.contrib.findController').start({
    forceRevealReplace: false,
    seedSearchStringFromSelection: false,
    seedSearchStringFromGlobalClipboard: false,
    shouldFocus: false,
    shouldAnimate: false,
    updateSearchScope: false
});

I have prepared an example with explanations for solving my case (synchronization of search between several editors) on a CodeSandbox -我准备了一个示例,解释了在 CodeSandbox 上解决我的案例(多个编辑器之间的搜索同步)-
Monaco Editor [ Multiple Instances search sync case ] Monaco Editor [多实例搜索同步案例]

screencast 'how it works' (GIF)截屏视频“它是如何工作的”(GIF)

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

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