0

I'm new in this area. This is what I'm trying to code.

Save editor data on server using post method. Can some one guide me how to achieve that ?

Scripts dynamically populate Monaco editor in . I'm struggling to figure it out how I can post this data to server ?

I'm looking at this code : https://github.com/Microsoft/monaco-editor-samples/tree/master/sample-editor

  • [Please take the tour](https://stackoverflow.com/tour) or at least [read these](https://stackoverflow.com/help/asking) – Rob Nov 13 '17 at 00:35

2 Answers2

1

You could bind editor as form field . Checkout this post here

function save() {
   // get the value of the data
   var value = window.editor.getValue()
   saveValueSomewhere(value);     
}
MPV
  • 337
  • 3
  • 10
1

MPV was on track to get you where you needed to go. Sharing this for posterity.

Put a hidden input into your form and also attach a JavaScript function to your submit button:

<form method="POST">
    <input type="submit" onclick="setupFormPost()" />
    <div id="container" style="width:800px;height:600px;border:1px solid grey"></div>
    <input hidden name="code" id="code" />
</form>

Here is a small snippet of JavaScript that sets up an editor and also the "setupFormPost()" function that will move the value from the Monaco editor into the hidden field that is then easily posted.

<script>
    var editor = monaco.editor.create(document.getElementById('container'), {
        language: 'javascript'
    });

    function setupFormPost() {
        var value = window.editor.getValue()
        $("#code").val(value);
    }
</script>
b.pell
  • 3,873
  • 2
  • 28
  • 39