微软最近开源了他们的摩纳哥编辑器(类似于ace / codemirror) .
https://github.com/Microsoft/monaco-editor
我已经在浏览器中运行并运行了,但仍然无法弄清楚如何获取编辑器的当前文本,就像我想要保存它一样 .
例:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
</head>
<body>
<div id="container" style="width:800px;height:600px;border:1px solid grey"></div>
<script src="monaco-editor/min/vs/loader.js"></script>
<script>
require.config({ paths: { 'vs': 'monaco-editor/min/vs' }});
require(['vs/editor/editor.main'], function() {
var editor = monaco.editor.create(document.getElementById('container'), {
value: [
'function x() {',
'\tconsole.log("Hello world!");',
'}'
].join('\n'),
language: 'javascript'
});
});
function save() {
// how do I get the value/code inside the editor?
var value = "";
saveValueSomewhere(value);
}
</script>
</body>
</html>
3 回答
对我来说这
window.editor.getValue()
没有用,但是在代码工作之下 .您可以将'container'更改为'htmlButton',然后在
saveI()
函数中使用jQuery ajax保存代码 .编辑器和模型都支持获取内容:
monaco.editor.IModel.getValue()
monaco.editor.IStandaloneCodeEditor.getValue()
因此,只要您保留对编辑器或模型的引用,就可以查询内容:
或者在模型的情况下:
如果您有差异编辑器,则无法直接在 editor 上访问文本,但您可以在各个模型上访问它们(即通过IStandaloneDiffEditor.getModel()):
或者通过不同的编辑器(getModifiedEditor()和getOriginalEditor()):
如果您对文本的一部分感兴趣,该模型还支持getValueInRange(),它为您提供指定范围内的文本,由起始列和结束列以及亚麻布分隔,例如: