在monaco编辑器中聆听文本选择更改

Nis*_*ant 3 monaco-editor

摩纳哥编辑中是否有一个文本选择活动?我需要回应用户在编辑器中选择部分代码?

是否有更好的解决方案使用计时器来获取选择范围?

文件似乎没有提及它.

rcj*_*uen 7

你可以使用onDidChangeCursorPositiononDidChangeCursorSelection.听这样的事件.

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));
});
Run Code Online (Sandbox Code Playgroud)