Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I get the Text of the Model #37

Closed
KyleGobel opened this issue Jun 28, 2016 · 4 comments
Closed

How do I get the Text of the Model #37

KyleGobel opened this issue Jun 28, 2016 · 4 comments

Comments

@KyleGobel
Copy link

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.

@ptyork
Copy link

ptyork commented Jun 29, 2016

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()

@KyleGobel
Copy link
Author

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.

@stoplion
Copy link

Is that the best way to get the array of lines?

@BennyHinrichs
Copy link

@stoplion No, that would be model.getLinesContent()

@vscodebot vscodebot bot locked and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants