Search code examples
javascripteventsmonaco-editor

Trying to add standard DOM event listener to monaco editor


I'm trying to add focus / blur standard DOM events to monaco-editor.

editor.addEventListener("blur", function(){
    <do something>
});

I received the following answer:

Uncaught TypeError: editor.addEventListener is not a function

Also tried jquery

$(editor).on("blur", function(){
    <do something>
});

No errors this time, but nothing happens. I mean, the event didn't fire.

I've also tried to attach the listeners to editor container div, but same results.

any ideas?


Solution

  • In Monaco Editor

    To listen for focus event, you can use

    editor.onDidFocusEditorWidget(()=>{
         console.log("Focus event triggerd !")
    })
    

    and For Blur event, you can use

    editor.onDidBlurEditorWidget(()=>{
         console.log("Blur event triggerd !")
    })