monaco-editor

Listening to text selection changes in monaco editor


Is there an event in monaco editor for text selection ? I need to respond to a user selecting part of the code in editor?

Is there a better solution to using timer to get ranges for selection ?

Documents don't seem to mention about it.


Solution

  • You can use onDidChangeCursorPosition or onDidChangeCursorSelection. to listen for such an event.

    var editor = monaco.editor.create(document.getElementById("container"), {
        value: "function hello() {\n\talert('Hello world!');\n}",
        language: "javascript"
    });
    
    editor.onDidChangeCursorPosition((e) => {
        console.log(JSON.stringify(e));
    });
    
    editor.onDidChangeCursorSelection((e) => {
        console.log(JSON.stringify(e));
    });