owe*_*col 3 javascript reactjs visual-studio-monaco monaco-editor
我在我的 React 应用程序中托管了一个摩纳哥编辑器。
到目前为止,我已经让编辑器在编辑器安装后打开查找控件,但我需要用一些文本预先填充它。
目前的代码如下所示:
...
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>
)
}
}
...
Run Code Online (Sandbox Code Playgroud)
我认为 API 文档并不清楚这是否可行。
我的第一直觉是这样做,
editor.getAction('actions.find').run('text here')
但这似乎行不通
当您在编辑器本身中突出显示一个单词,然后按下时,CMD+F您将获得预先填充文本的查找控件,因此我相信这是可以实现的。
任何帮助,将不胜感激。
您可以在摩纳哥游乐场尝试一下。
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();
Run Code Online (Sandbox Code Playgroud)
首先,从模型中获取要查找的字符串范围。二是设置选择。第三,触发选择动作。
| 归档时间: |
|
| 查看次数: |
1828 次 |
| 最近记录: |