簡體   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