Monaco-editor: Default folding at specified level on editor start

Created on 3 Feb 2017  路  1Comment  路  Source: microsoft/monaco-editor

I want to achive the following:

After creating a editor with following code :

   var editor = monaco.editor.create(container,
                    {
                        value: '...',
                        language: 'java'
                    });

I want to fold the code at level 1. How can I achive this in javascript ?

This is how it looks at startup at the moment:
grafik

But it should look like this:

grafik

Most helpful comment

You could try using (run here):

var jsCode = [
    '"use strict";',
    'function Person(age) {',
    '   if (age) {',
    '       this.age = age;',
    '   }',
    '   if (age) {',
    '       this.age = age;',
    '   }',
    '}',
    'Person.prototype.getAge = function () {',
    '   return this.age;',
    '};'
].join('\n');

var editor = monaco.editor.create(document.getElementById("container"), {
    value: jsCode,
    language: "javascript",
    folding: true
});

editor.getAction('editor.foldLevel2').run();

screen shot 2017-06-15 at 11 58 59

>All comments

You could try using (run here):

var jsCode = [
    '"use strict";',
    'function Person(age) {',
    '   if (age) {',
    '       this.age = age;',
    '   }',
    '   if (age) {',
    '       this.age = age;',
    '   }',
    '}',
    'Person.prototype.getAge = function () {',
    '   return this.age;',
    '};'
].join('\n');

var editor = monaco.editor.create(document.getElementById("container"), {
    value: jsCode,
    language: "javascript",
    folding: true
});

editor.getAction('editor.foldLevel2').run();

screen shot 2017-06-15 at 11 58 59

Was this page helpful?
0 / 5 - 0 ratings