1

I am learning Monaco editor now and I see this piece of code here:

<!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:1pxsolid 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>

In the snippet, an editor created in an anonymous function because monaco need to be required before that.

My confusion is that how can I declare an editor out of that anonymous function? This demo code definitely constrains the scope of editor.

Zanecola
  • 1,394
  • 3
  • 15
  • 27
  • This post may be useful https://stackoverflow.com/questions/16521471/relation-between-commonjs-amd-and-requirejs – Stretch0 Jun 27 '17 at 02:37
  • 1
    The file `vs/editor/editor.main` seems to contain the code that defines `monaco`. What do you find confusing about that? If you wonder what exactly `require` does, see http://requirejs.org/ (the [monaco docs](https://github.com/Microsoft/monaco-editor#faq) say they are using AMD modules). – Felix Kling Jun 27 '17 at 02:39
  • @FelixKling I cannot find require.js in the monaco package – Zanecola Jun 27 '17 at 03:43
  • "*My confusion is that how can I declare an editor out of that anonymous function?*" - you cannot. And you don't (shouldn't) need to. – Bergi Jun 27 '17 at 03:44
  • @Bergi well, should I put all the logics on editor in that callback function? I can see some demo code using `monaco.editor.create` out of the function like this: https://microsoft.github.io/monaco-editor/playground.html , while I don't know where they used that 'require' function – Zanecola Jun 27 '17 at 03:48
  • @Zanecat Yes, you should. I don't know what that playground contains, but it doesn't look like a complete script. – Bergi Jun 27 '17 at 09:22
  • @Bergi Alright...... That makes sense, I will try to understand. – Zanecola Jun 27 '17 at 10:44

0 Answers0