Nishant
Nishant

Reputation: 5089

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.

Upvotes: 8

Views: 4158

Answers (1)

rcjsuen
rcjsuen

Reputation: 938

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

Upvotes: 16

Related Questions