monaco-editor icon indicating copy to clipboard operation
monaco-editor copied to clipboard

AngularJS integration

Open jack-arms opened this issue 9 years ago • 10 comments

Does anyone have a working example of using Monaco with AngularJS? I got the basic example of Monaco to work with this code:

        require.config({
            paths: { 'vs': 'file:///path_to_project/bower_components/monaco-editor/release/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'
		});
	});

Trying to translate this to an AngularJS directive:

angular
      .module('mymodule')
      .directive('monacoEditor', monacoEditor);

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

        return directive;
    }

However monaco is always undefined.

I have checked that http://localhost:20000/bower_components/monaco-editor/release/min/vs/editor/editor.main.js can definitely be accessed by the browser. Is this an issue of 'http://' vs. 'file:///'? If the second require is failing to load the editor and leaves monaco undefined, how can I see what the error is?

jack-arms avatar Aug 07 '17 21:08 jack-arms

I want to make manaco editor work with angularjs 1.X as well...

SoftTimur avatar Aug 08 '17 10:08 SoftTimur

I have been able to get Monaco to work with angular to some degree by doing the sync load of Monaco rather than the code I posted above. At the beginning of <body> I put:

<script>var require = { paths: { 'vs': '../node_modules/monaco-editor/dev/vs' } };</script>
<script src="../node_modules/monaco-editor/dev/vs/loader.js"></script>
<script src="../node_modules/monaco-editor/dev/vs/editor/editor.main.nls.js"></script>
<script src="../node_modules/monaco-editor/dev/vs/editor/editor.main.js"></script>
<script src="../node_modules/monaco-editor/dev/vs/basic-languages/src/sql.js"></script>

and in <head> I put:

<link rel="stylesheet" data-name="vs/editor/editor.main" href="../node_modules/monaco-editor/dev/vs/editor/editor.main.css" />

And this makes monaco a global variable. I Then I make this a constant for my module:

angular
      .module('mymodule')
      .constant('monaco', monaco)

So that it can be injected into other places in the normal angular way. You can make a directive that uses a link function to create an editor around the element the directive is attached to, so you can write something like:

<div class="monaco-container" monaco-editor={...}></div>

I'm not able to load the languages I need dynamically, so I just put the script tag with the editor stuff for SQL in the body along with the other scripts. Still trying to integrate this process in with gulp as this definitely does not seem like the best way to load Monaco, but it at least works.

jack-arms avatar Aug 09 '17 19:08 jack-arms

Haven't played with Monaco in AngularJS 1.x for two years but if I remember correctly, NG 1.x has its own require so it overrides our amd-loader's require. To make it work smoothly, you can do

  • AMD load Angular 1.x http://marcoslin.github.io/angularAMD/#/home
  • Hold a reference to Monaco's require before loading angularjs. For example, add a script block var monacoRequire = global.require before loading angularjs.

rebornix avatar Aug 09 '17 20:08 rebornix

@rebornix In your comment, do you mean 1 or 2, or 1 and 2?

I wanted to look for a solution other than AMD loader or RequireJS... I will test what @jack-arms proposed...

SoftTimur avatar Aug 09 '17 21:08 SoftTimur

@SoftTimur I'm referring to 1.x only. I haven't ever played with 2 yet so not sure what's going on there.

rebornix avatar Aug 09 '17 21:08 rebornix

Sorry, I was not clear... I asked if you mean 1. AMD load... **and** 2. Hold a reference or 1. AMD load... **or** 2. Hold a reference?

SoftTimur avatar Aug 09 '17 21:08 SoftTimur

@jack-arms Thank you very much for your solution, it works so far... here is a working JSBin.

Additionally, I have at least the syntax highlighting for javascript, html and css without loading their .js. I don't know why you need to load sql.js...

SoftTimur avatar Aug 09 '17 22:08 SoftTimur

I was loading the sql.js file because I was using SQL highlighting, and specifying 'sql' as the language would not automatically load this file. Although I'm not sure if it's supposed to be automatically loaded. I suspected that due to the way I was loading monaco I would have to load these types of things manually, but I will need to experiment more.

jack-arms avatar Aug 11 '17 22:08 jack-arms

@jack-arms @rebornix @joaomoreno @jongalloway Guys, what was proposed previously worked fine, whereas it does not work with ocLazyLoad. Here is my question: https://github.com/Microsoft/monaco-editor/issues/845 and https://stackoverflow.com/questions/50017351/lazy-load-monacoeditor

Do you have any idea?

chengtie avatar Apr 25 '18 13:04 chengtie

We closed this issue because we don't plan to address it in the foreseeable future. If you disagree and feel that this issue is crucial: we are happy to listen and to reconsider.

If you wonder what we are up to, please see our roadmap and issue reporting guidelines.

Thanks for your understanding, and happy coding!

hediet avatar Feb 13 '23 12:02 hediet