最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Monaco Editor: Update Cursor position on text inserted - Stack Overflow

matteradmin3PV0评论

I'm adding some text to the Monaco editor using a button outside it (i.e. "hello world") and then I'm trying to set the cursor position to the next line.

I tried using the "setPosition({column:x, lineNumber:y})" function from the editor, but it doesn't work.

This is how I'm implementing it:

insertInPosition(textToInsert:string, cursorPosition:any){
    this.editorInstance.setPosition(cursorPosition);
    var allInstructions = this.instructionSet.split("\n")
    allInstructions.splice(cursorPosition.lineNumber - 1, 0, textToInsert);
    allInstructions.splice(cursorPosition.lineNumber, 1);
    allInstructions = allInstructions.join("\n");
    this.editorInstance.setPosition(cursorPosition);
}

I expect to see the cursor in the line and column defined by cursorPosition, but I actually see that the cursor points to line 1 and column 1 (At the top of the editor).

I also tried to use the same api editor.setPosition() inside the onDidChangeModelContent() method, but it doesn't works. And when I print in console the editor.getPosition() I receive the correct positions.

Any idea on what could be wrong?

I'm adding some text to the Monaco editor using a button outside it (i.e. "hello world") and then I'm trying to set the cursor position to the next line.

I tried using the "setPosition({column:x, lineNumber:y})" function from the editor, but it doesn't work.

This is how I'm implementing it:

insertInPosition(textToInsert:string, cursorPosition:any){
    this.editorInstance.setPosition(cursorPosition);
    var allInstructions = this.instructionSet.split("\n")
    allInstructions.splice(cursorPosition.lineNumber - 1, 0, textToInsert);
    allInstructions.splice(cursorPosition.lineNumber, 1);
    allInstructions = allInstructions.join("\n");
    this.editorInstance.setPosition(cursorPosition);
}

I expect to see the cursor in the line and column defined by cursorPosition, but I actually see that the cursor points to line 1 and column 1 (At the top of the editor).

I also tried to use the same api editor.setPosition() inside the onDidChangeModelContent() method, but it doesn't works. And when I print in console the editor.getPosition() I receive the correct positions.

Any idea on what could be wrong?

Share asked Apr 30, 2019 at 20:10 Oscar_sgcOscar_sgc 3501 gold badge8 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7
this.editor?.trigger('keyboard', 'type', {text: 'value'});
this.editor?.focus();
const position: any  = this.editor?.getPosition();
this.editor?.setPosition(position);

Some monaco editor events (including onDidChangeModelContent()), change cursor position after executing. I took this information from here.

To prevent it from happening, you can do something like this:

var overridenPosition = null;

editor.onDidChangeModelContent(e => {
    if (/* your condition here */) {
        // your logic here
        overridenPosition = { lineNumber: 4, column: 2 }; // put your value here
    }
});

editor.onDidChangeCursorPosition(e => {
    if (overridenPosition != null) {
        editor.setPosition(overridenPosition);
        overridenPosition = null;
    }
});
Post a comment

comment list (0)

  1. No comments so far