Probably a dumb question but I'm having an awful hard time with it.
I loaded up the easy sample, and all I want to do is get the text out that the user has entered.
This is proving hard for me to find
<!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'
});
});
</script>
</body>
</html>
If I add a function
function saveFile() {
var value = */ somehow get the text output that is in the editor */;
someMethodThatSaves(value);
}
How would I go about doing that?
The only method I've found is something like
monaco.editor.getModels()[0]._lines which would give me the line objects array and in turn I could get at _text of each line. I'm assuming this is not the way it was meant to be done.
Any pointers appreciated.
I created a Stack Overflow question asking the same thing with the monaco-editor tag, but I thought i'd probably have better luck asking here since this is so new.
editor.getValue()
Though do note that the scope of the editor variable above will not work. So declare var editor before the require and assign it in the require callback function.
Alternatively: monaco.editor.getModels()[0].getValue()
I see...
I was trying things off the monaco.editor object, it makes sense that'd it be on the editor instance that was created.
Thanks for clearing that up.
Is that the best way to get the array of lines?
@stoplion No, that would be model.getLinesContent()
Most helpful comment
editor.getValue()
Though do note that the scope of the editor variable above will not work. So declare var editor before the require and assign it in the require callback function.
Alternatively: monaco.editor.getModels()[0].getValue()