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

Is there an initialized or ready event?

Open dezsiszabi opened this issue 8 years ago • 31 comments

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();
});

dezsiszabi avatar Aug 19 '16 21:08 dezsiszabi

Perhaps we could expose the language registries (https://github.com/Microsoft/vscode/blob/66c13c81fd8d844fa2cba38e03bd719e285a1c7e/src/vs/editor/common/modes.ts#L1026), they fire events when language providers are attached.

alexdima avatar Aug 22 '16 08:08 alexdima

Hi,

first, many thanks to @dezsiszabi for pointing out one (dirty) solution. It helped me with my approach to automatically trigger a AngularJS function after the monaco editor is fully loaded and displaying some code. Works fine.

I think there should be some "ready" events when the editor is up and running. Or are they already here and I'm missing something out?

fdeitelhoff avatar Aug 26 '16 18:08 fdeitelhoff

I'd like to up-vote on this one.. I ended up sing the onDidScrollChange too with a checker on if (!(editor.getModel() as any)._tokenizationSupport) return; (and then a this.didInit check)... awful code - but that works.. But before that one is hit, I also do a require(["vs/editor/common/modes/languageConfigurationRegistry"], (mod: any) => { this._disposables.push(mod.LanguageConfigurationRegistry.onDidChange(() => { ... to modify loaded language :-S

The reason above is that, when language HTML has been async loaded, I do a extend of that (e.g. showing written css colors as colors in html styles and css blocks etc.) so it end up being an unknown/random race to what has been loaded and initialised.

Thanks

AndersMad avatar Aug 29 '17 08:08 AndersMad

I have the same struggle too. Thanks for show me a solution but I like to bump this requested feature. onReady event would be nice.

jesusvimlet avatar Mar 21 '18 01:03 jesusvimlet

OnReady or onLoaded Event would be better.

DevidCIC avatar May 14 '18 14:05 DevidCIC

@dezsiszabi hello~ why I can not use the action 'editor.action.format'.The action is not exist . image image

CoderPoet avatar May 23 '18 15:05 CoderPoet

@dezsiszabi this is supported actions image

CoderPoet avatar May 23 '18 15:05 CoderPoet

@dezsiszabi hello~ why I can not use the action 'editor.action.format'.The action is not exist . image image

maybe 'editor.action.formatDocument' is work

tsy77 avatar Oct 28 '18 08:10 tsy77

Perhaps we could expose the language registries (https://github.com/Microsoft/vscode/blob/66c13c81fd8d844fa2cba38e03bd719e285a1c7e/src/vs/editor/common/modes.ts#L1026), they fire events when language providers are attached.

has this event been added? It's really needed because onDidScrollChange will be fired before language providers are attached.

tsy77 avatar Oct 28 '18 10:10 tsy77

@dezsiszabi this is supported actions image

How can I print the event Lists?

sunzhengxin92 avatar Jan 09 '19 11:01 sunzhengxin92

@sunzhengxin92 window.codeEditor._actions

0xAnon101 avatar Dec 06 '19 07:12 0xAnon101

onDidScrollChange not working in the newest version

wangdingwei avatar Jun 03 '21 05:06 wangdingwei

Is there any progress?

lb1129 avatar Oct 07 '21 02:10 lb1129

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();
});

it's not work when codestring is short

lb1129 avatar Oct 07 '21 07:10 lb1129

use onDidCreateEditor

  monaco.editor.onDidCreateEditor(function(event) {
    console.log('editor crated');
  });

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

sekedus avatar Jul 05 '22 01:07 sekedus

if someone needs to really check, if model has been loaded, one may use mutation observer. You should only look for childList of the container, where you loaded monaco editor to keep costs of mutation observing to minimum;

let observer = new MutationObserver((mutations, o) => {

            if (Array.from(mutations).some(mutation => {
                return Array.from(mutation.addedNodes).some(node => node.classList.contains('monaco-editor'));
            })) 
           {
                console.log('editor loaded');
               
            }
        }).observe(container, { childList: true, })

ankurpiparsania avatar Jul 06 '22 23:07 ankurpiparsania

I am trying to add a keydown event listener to the editor, and when using the above 2, the event listener is gone when the editor is fully loaded.

6 years later, has an initialized or ready event supported?

magicds avatar Sep 05 '22 03:09 magicds

6 years later, has an initialized or ready event supported?

still waiting

zSp33dy avatar Sep 21 '22 07:09 zSp33dy

try this hook

Instance.onDidLayoutChange((event:any)=>{
      // console.log('onDidLayoutChange')
      if(MEInstance){
        MEInstance.trigger(
          "anyString",
          "editor.action.formatDocument",
          null
        ); // format code
      }
 })

enmotion avatar Oct 25 '22 08:10 enmotion

@alexdima I want to help push this feature forward. Any pointers as to where to start contributing?

farskid avatar Nov 20 '22 20:11 farskid

I have this monaco editor which gets mounted and unmounted by the user. this is how I could do this:

function formatDocument() {
  editor.getAction('editor.action.formatDocument').run();
}

/// on first initialization
editor.onDidChangeModelLanguageConfiguration(formatDocument);

/// on every initialization
editor.onDidLayoutChange(formatDocument);

mhsattarian avatar Jan 18 '23 19:01 mhsattarian

@dezsiszabi hello~ why I can not use the action 'editor.action.format'.The action is not exist . image image

const handleEditorDidMount = (editor: any,monaco:any) => { setTimeout( () => editor.getAction('editor.action.formatDocument').run(), 100, ); };

I've used it like this. It worked for json.

Ankit1996-tech avatar Feb 07 '23 10:02 Ankit1996-tech

6 years later, has an initialized or ready event supported?

still waiting

Now it's seven years.

imguolao avatar Mar 17 '23 01:03 imguolao

My 2 cents: use retry with exponential backoff, something like this:

export async function promiseRetry<T>(
  promise: () => Promise<T>,
  initial_retry_count: number,
  retry_delay: number,
): Promise<T> {
  async function __promiseRetry(promise: () => Promise<T>, remaining_retry_count: number): Promise<T> {
    try {
      const res = await promise();
      return res;
    } catch (e) {
      if (remaining_retry_count <= 0) {
        throw e;
      }
      setTimeout(() => {
        __promiseRetry(promise, remaining_retry_count - 1);
      }, retry_delay ** (1 + 0.1 * (initial_retry_count - remaining_retry_count + 1)));
    }
  }

  return __promiseRetry(promise, initial_retry_count);
}

Then, when you want to get a worker ref (or anything else that requires it to be loaded):

// So.. there is no way to check when the model is initalized apparently!
// https://github.com/microsoft/monaco-editor/issues/115

// If we accidentally call the `getTypeScriptWorker()` function to early, it throws.
//  Just use retry with exponential backoff to get it!
await promiseRetry(
  async () => {
    const getWorker = await monaco.languages.typescript.getTypeScriptWorker();
    const tsWorker = await getWorker();
    dispatch('fullyLoaded', { model: editor.getModel(), worker: tsWorker });
  },
  5,
  10,
);

Cobular avatar May 15 '23 22:05 Cobular