我在我的React应用程序中托管了一个Monaco编辑器 .
到目前为止,我已经让编辑器在编辑器安装时打开了find控件,但是我需要用一些文本预先填充它 .
目前的一些代码看起来像这样:
...
class CodeEditorMonaco extends Component {
constructor (props) {
super(props)
this.editorDidMount = this.editorDidMount.bind(this)
this.editor = null
}
editorDidMount (editor, monaco) {
editor.focus()
editor.getAction('actions.find').run()
}
render () {
return (
<div className='code-editor'>
<MonacoEditor
width='100%'
height='75vh'
language='json'
editorDidMount={this.editorDidMount}
ref={editor => { this.editor = editor }}
/>
</div>
)
}
}
...
我认为API文档不清楚这是否可行 .
我的第一直觉是 editor.getAction('actions.find').run('text here')
,但这似乎不起作用
当您在编辑器中突出显示一个单词,然后按 CMD+F
时,您会获得预先填充文本的查找控件,因此我相信它可以实现 .
任何帮助,将不胜感激 .
查找控件:
2 回答
有一种方法可以做你想做的事情,而这正是你已经描述过的:
editor.setSelection(new monaco.Range(18, 8, 18, 15));
editor.trigger('', 'actions.find');
要么
editor.getAction('actions.find').run('');
您可以在Monaco游乐场尝试 .
首先,从模型中获取要查找的字符串范围 . 第二,设定选择 . 第三,触发选择动作 .