microsoft / microsoft/monaco-editor

Is there an initialized or ready event?

Open
#115 33 comments 45 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

editor-api feature-request
Dominant language
JavaScript
Stars
46.8k
Forks
4.1k
Avg merge
17h 58m
Merged PRs (30d)
1

Description

Hello,

I'm curious if there's an initialized or ready event or something I can listen to when instantiating an editor?

Would be useful in the below case for example:

<script>
    require.config({ paths: { "vs": "node_modules/monaco-editor/min/vs" } });
    require(["vs/editor/editor.main"], function() {
        monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
            validate: false,
            allowComments: false
        });

        var editor = monaco.editor.create(document.getElementById("container"), {
            value: '{ "prop": "Hello world" }',
            language: "json",
            theme: "vs-dark",
            contextmenu: false,
            quickSuggestions: false,
            automaticLayout: true
        });

        editor.getAction("editor.action.format").run();
    });
</script>

Here the line editor.getAction("editor.action.format").run(); doesn't have any effect. The JSON doesn't get formatted and I assume it's because the editor is not fully initialized at that point.

I know I can solve it with setTimeout, but I was wondering if there's any other option.

Thanks in advance, keep up the good work :)

UPDATE:

Adding the below solves it, not the most beautiful solution though :)

var didScrollChangeDisposable = editor.onDidScrollChange(function(event) {
    didScrollChangeDisposable.dispose();
    editor.getAction("editor.action.format").run();
});

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the AMD require callback and the call to monaco.editor.create shown in the issue. Reproduce whether editor.getAction("editor.action.format").run() works immediately, compare it with the onDidScrollChange workaround, and inspect the editor initialization lifecycle. Done means the initialization state or supported event is documented or exposed clearly enough to trigger formatting reliably.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.