我在我的React应用程序中托管了一个摩纳哥编辑器。
到目前为止,我已经让编辑器在编辑器挂载时打开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,您将得到预先填充文本的find控件,所以我相信这是可能的。
任何帮助都将不胜感激。
找到控制室:

发布于 2018-07-11 17:32:08
有一种方法可以做你想做的事,也可以做你已经描述过的事情:
editor.setSelection(new monaco.Range(18, 8, 18, 15));editor.trigger('', 'actions.find');
或
editor.getAction('actions.find').run('');发布于 2018-09-28 09:14:59
你可以在摩纳哥游乐场试一试。
const editor = monaco.editor.create(document.getElementById("container"), {
value: "{\n\t\"dependencies\": {\n\t\t\n\t}\n}\n",
language: "json"
});
const model = editor.getModel();
const range = model.findMatches('d')[0].range;
editor.setSelection(range);
editor.getAction('actions.find').run();首先,从模型中获取要查找的字符串的范围。第二,设定选择。第三,触发选择动作。
https://stackoverflow.com/questions/45629937
复制相似问题