繁体   English   中英

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

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

很长一段时间以来,我一直在尝试寻找一种方法、示例或信息,以了解如何将本机命令/操作作为actions.find订阅或收听。

我的情况是 - 当用户在编辑器的工作台中使用“查找匹配”功能(通过键盘快捷键或上下文菜单调用)时创建一些副作用,最后我想在我的效果中作为参数“搜索“子字符串”。

希望社区可以帮助我或放置一些类似的案例解决示例。

在调试和研究 monaco-editor 的源代码之后,我发现在我看来,使用 API(恕我直言)的正确解决方案。

我们可以使用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
});

我准备了一个示例,解释了在 CodeSandbox 上解决我的案例(多个编辑器之间的搜索同步)-
Monaco Editor [多实例搜索同步案例]

截屏视频“它是如何工作的”(GIF)

暂无
暂无

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

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