获得Monaco Editor的价值

时间:2016-06-28 20:47:02

标签: javascript monaco-editor

微软最近开源了他们的monaco编辑器(类似于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 个答案:

答案 0 :(得分:19)

require.config({ paths: { 'vs': 'monaco-editor/min/vs' }});
require(['vs/editor/editor.main'], function() {
    window.editor = monaco.editor.create(document.getElementById('container'),                 {
        value: [
            'function x() {',
            '\tconsole.log("Hello world!");',
            '}'
        ].join('\n'),
        language: 'javascript'
    });
});

function save() {
   // get the value of the data
   var value = window.editor.getValue()
   saveValueSomewhere(value);     
}

答案 1 :(得分:3)

编辑器和模型都支持获取内容:

因此,只要您保留对编辑器或模型的引用,就可以查询内容:

var editor = monaco.editor.create(...);
var text = editor.getValue();

或者在模型的情况下:

var model = monaco.editor.createModel(...);
var text = model.getValue();

如果您有差异编辑器,则无法直接在编辑器上访问该文本,但您可以在各个模型上访问它们(例如通过IStandaloneDiffEditor.getModel()):

var diffEditor = monaco.editor.createDiffEditor(...);
var originalText = diffEditor.getModel().original.getValue();
var modifiedText = diffEditor.getModel().modified.getValue();

或通过不同的编辑(getModifiedEditor()getOriginalEditor()):

var originalText = diffEditor.getModifiedEditor().getValue();
var modifiedText = diffEditor.getOriginalEditor().getValue();

如果您对文本的一部分感兴趣,该模型还支持getValueInRange(),它为您提供指定范围内的文本,由起始列和结束列以及亚麻布分隔,例如:< / p>

var editor = monaco.editor.create(...);
var model = editor.getModel();
var partOfTheText = model.getValueInRange({
  startLineNumber: 1,
  startColumn: 2,

  endLineNumber: 3,
  endColumn: 10,
})

答案 2 :(得分:2)

对我而言,这window.editor.getValue()无效但代码工作正常。

<div id="container" style="width:950px;height:700px;"></div>
<script src="./monaco-editor/dev/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: [
                'print "Hello World!"',
                '# python'
            ].join('\n'),
            language: 'python',
            theme: "vs-dark"
        });

        function saveI() 
        {
            getVal = editor.getValue()
            // get the value of the data
            alert(getVal)
        }
        document.getElementById('container').onclick = saveI;

    });
    // Themes: vs-dark , hc-black
    // language: text/html , javascript
</script>

你可以改变容器&#39;你的&#htmlButton&#39;然后在saveI()函数中使用jQuery ajax保存代码。