聆听摩纳哥编辑器中的文本选择变化

Listening to text selection changes in monaco editor

摩纳哥编辑器中是否有用于文本选择的事件? 我需要响应用户在编辑器中选择部分代码吗?

使用定时器获取选择范围有没有更好的解决方案?

文献中似乎没有提及。

您可以使用 onDidChangeCursorPosition or onDidChangeCursorSelection。监听这样的事件。

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